From 301896241c6fcb66520f64bc483bd1f9006e0380 Mon Sep 17 00:00:00 2001 From: sand4rt Date: Sun, 21 Apr 2024 22:12:55 +0200 Subject: [PATCH 1/2] feat(ct): resolve hooksConfig import refs --- packages/playwright-ct-core/src/mount.ts | 1 + .../playwright-ct-core/types/component.d.ts | 11 +++++----- packages/playwright-ct-vue/hooks.d.ts | 7 ++++--- packages/playwright-ct-vue/index.d.ts | 12 +++++------ .../ct-vue-vite/playwright/index.ts | 20 ++++++++++--------- .../ct-vue-vite/tests/slots/slots.spec.ts | 6 +++++- .../tests/vue-router/vue-router.spec.ts | 3 +-- .../tests/vue-router/vue-router.spec.tsx | 3 +-- 8 files changed, 34 insertions(+), 29 deletions(-) diff --git a/packages/playwright-ct-core/src/mount.ts b/packages/playwright-ct-core/src/mount.ts index 7220916a4b8a9..e743a6aaa1924 100644 --- a/packages/playwright-ct-core/src/mount.ts +++ b/packages/playwright-ct-core/src/mount.ts @@ -102,6 +102,7 @@ async function innerMount(page: Page, componentRef: JsxComponent | ImportRef, op const selector = await page.evaluate(async ({ component, hooksConfig }) => { component = await window.__pwUnwrapObject(component); + hooksConfig = await window.__pwUnwrapObject(hooksConfig); let rootElement = document.getElementById('root'); if (!rootElement) { rootElement = document.createElement('div'); diff --git a/packages/playwright-ct-core/types/component.d.ts b/packages/playwright-ct-core/types/component.d.ts index 5bd5d7e017fb7..fc8d0eef58394 100644 --- a/packages/playwright-ct-core/types/component.d.ts +++ b/packages/playwright-ct-core/types/component.d.ts @@ -14,10 +14,9 @@ * limitations under the License. */ -type JsonPrimitive = string | number | boolean | null; -type JsonValue = JsonPrimitive | JsonObject | JsonArray; -type JsonArray = JsonValue[]; -export type JsonObject = { [Key in string]?: JsonValue }; +export interface RegisterHooksConfig { + +}; export type JsxComponent = { __pw_type: 'jsx', @@ -47,10 +46,10 @@ declare global { playwrightMount(component: Component, rootElement: Element, hooksConfig?: any): Promise; playwrightUnmount(rootElement: Element): Promise; playwrightUpdate(rootElement: Element, component: Component): Promise; - __pw_hooks_before_mount?: (( + __pw_hooks_before_mount?: (( params: { hooksConfig?: HooksConfig; [key: string]: any } ) => Promise)[]; - __pw_hooks_after_mount?: (( + __pw_hooks_after_mount?: (( params: { hooksConfig?: HooksConfig; [key: string]: any } ) => Promise)[]; // Can't start with __pw due to core reuse bindings logic for __pw*. diff --git a/packages/playwright-ct-vue/hooks.d.ts b/packages/playwright-ct-vue/hooks.d.ts index 2a18496007e4d..a2848df1bf4de 100644 --- a/packages/playwright-ct-vue/hooks.d.ts +++ b/packages/playwright-ct-vue/hooks.d.ts @@ -15,12 +15,13 @@ */ import type { App, ComponentPublicInstance } from 'vue'; -import type { JsonObject } from '@playwright/experimental-ct-core/types/component'; -export declare function beforeMount( +export interface RegisterHooksConfig {} + +export declare function beforeMount( callback: (params: { app: App; hooksConfig?: HooksConfig }) => Promise ): void; -export declare function afterMount( +export declare function afterMount( callback: (params: { app: App; hooksConfig?: HooksConfig; diff --git a/packages/playwright-ct-vue/index.d.ts b/packages/playwright-ct-vue/index.d.ts index 308e47c18cad7..0afd1dfed2618 100644 --- a/packages/playwright-ct-vue/index.d.ts +++ b/packages/playwright-ct-vue/index.d.ts @@ -15,7 +15,7 @@ */ import type { Locator } from 'playwright/test'; -import type { JsonObject } from '@playwright/experimental-ct-core/types/component'; +import type { RegisterHooksConfig } from './hooks'; import type { TestType } from '@playwright/experimental-ct-core'; type ComponentSlot = string | string[]; @@ -29,14 +29,14 @@ type ComponentProps = T extends (props: infer P, ...args: any) => any ? P : {}; -export interface MountOptions { +export interface MountOptions { props?: ComponentProps; slots?: ComponentSlots; on?: ComponentEvents; hooksConfig?: HooksConfig; } -export interface MountOptionsJsx { +export interface MountOptionsJsx { hooksConfig?: HooksConfig; } @@ -55,13 +55,13 @@ export interface MountResultJsx extends Locator { } export const test: TestType<{ - mount( + mount( component: JSX.Element, options: MountOptionsJsx ): Promise; - mount( + mount( component: Component, - options?: MountOptions + options?: MountOptions ): Promise>; }>; diff --git a/tests/components/ct-vue-vite/playwright/index.ts b/tests/components/ct-vue-vite/playwright/index.ts index 977ee338459e1..ae20ce194523d 100644 --- a/tests/components/ct-vue-vite/playwright/index.ts +++ b/tests/components/ct-vue-vite/playwright/index.ts @@ -1,20 +1,22 @@ import { beforeMount, afterMount } from '@playwright/experimental-ct-vue/hooks'; import { router } from '../src/router'; -import Button from '../src/components/Button.vue'; import '../src/assets/index.css'; -export type HooksConfig = { - route?: string; - routing?: boolean; +declare module '@playwright/experimental-ct-vue/hooks' { + interface RegisterHooksConfig { + routing?: boolean; + components?: Record; + } } -beforeMount(async ({ app, hooksConfig }) => { - if (hooksConfig?.routing) +beforeMount(async ({ app, hooksConfig }) => { + if (hooksConfig?.routing) app.use(router as any); // TODO: remove any and fix the various installed conflicting Vue versions - app.component('Button', Button); - console.log(`Before mount: ${JSON.stringify(hooksConfig)}, app: ${!!app}`); + + for (const [name, component] of Object.entries(hooksConfig?.components || {})) + app.component(name, component); }); -afterMount(async ({ instance }) => { +afterMount(async ({ instance }) => { console.log(`After mount el: ${instance.$el.constructor.name}`); }); diff --git a/tests/components/ct-vue-vite/tests/slots/slots.spec.ts b/tests/components/ct-vue-vite/tests/slots/slots.spec.ts index 2af38036b593d..c7165df895701 100644 --- a/tests/components/ct-vue-vite/tests/slots/slots.spec.ts +++ b/tests/components/ct-vue-vite/tests/slots/slots.spec.ts @@ -1,6 +1,7 @@ import { test, expect } from '@playwright/experimental-ct-vue'; import DefaultSlot from '@/components/DefaultSlot.vue'; import NamedSlots from '@/components/NamedSlots.vue'; +import Button from '@/components/Button.vue'; test('render a default slot', async ({ mount }) => { const component = await mount(DefaultSlot, { @@ -14,8 +15,11 @@ test('render a default slot', async ({ mount }) => { test('render a component as slot', async ({ mount }) => { const component = await mount(DefaultSlot, { slots: { - default: '