diff --git a/packages/playwright-ct-core/src/mount.ts b/packages/playwright-ct-core/src/mount.ts index 7220916a4b..e743a6aaa1 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 5bd5d7e017..ed5c616bfa 100644 --- a/packages/playwright-ct-core/types/component.d.ts +++ b/packages/playwright-ct-core/types/component.d.ts @@ -14,11 +14,6 @@ * 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 type JsxComponent = { __pw_type: 'jsx', type: any, @@ -47,10 +42,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-react/hooks.d.ts b/packages/playwright-ct-react/hooks.d.ts index 0fa7cf3e1a..1093f1a3a3 100644 --- a/packages/playwright-ct-react/hooks.d.ts +++ b/packages/playwright-ct-react/hooks.d.ts @@ -14,11 +14,9 @@ * limitations under the License. */ -import type { JsonObject } from '@playwright/experimental-ct-core/types/component'; - -export declare function beforeMount( +export declare function beforeMount( callback: (params: { hooksConfig?: HooksConfig; App: () => JSX.Element }) => Promise ): void; -export declare function afterMount( +export declare function afterMount( callback: (params: { hooksConfig?: HooksConfig }) => Promise ): void; diff --git a/packages/playwright-ct-react/index.d.ts b/packages/playwright-ct-react/index.d.ts index 7cfcf04ba0..c086d4bec5 100644 --- a/packages/playwright-ct-react/index.d.ts +++ b/packages/playwright-ct-react/index.d.ts @@ -15,10 +15,9 @@ */ import type { Locator } from 'playwright/test'; -import type { JsonObject } from '@playwright/experimental-ct-core/types/component'; import type { TestType } from '@playwright/experimental-ct-core'; -export interface MountOptions { +export interface MountOptions { hooksConfig?: HooksConfig; } @@ -28,7 +27,7 @@ export interface MountResult extends Locator { } export const test: TestType<{ - mount( + mount( component: JSX.Element, options?: MountOptions ): Promise; diff --git a/packages/playwright-ct-react17/hooks.d.ts b/packages/playwright-ct-react17/hooks.d.ts index 0fa7cf3e1a..1093f1a3a3 100644 --- a/packages/playwright-ct-react17/hooks.d.ts +++ b/packages/playwright-ct-react17/hooks.d.ts @@ -14,11 +14,9 @@ * limitations under the License. */ -import type { JsonObject } from '@playwright/experimental-ct-core/types/component'; - -export declare function beforeMount( +export declare function beforeMount( callback: (params: { hooksConfig?: HooksConfig; App: () => JSX.Element }) => Promise ): void; -export declare function afterMount( +export declare function afterMount( callback: (params: { hooksConfig?: HooksConfig }) => Promise ): void; diff --git a/packages/playwright-ct-react17/index.d.ts b/packages/playwright-ct-react17/index.d.ts index 7cfcf04ba0..c086d4bec5 100644 --- a/packages/playwright-ct-react17/index.d.ts +++ b/packages/playwright-ct-react17/index.d.ts @@ -15,10 +15,9 @@ */ import type { Locator } from 'playwright/test'; -import type { JsonObject } from '@playwright/experimental-ct-core/types/component'; import type { TestType } from '@playwright/experimental-ct-core'; -export interface MountOptions { +export interface MountOptions { hooksConfig?: HooksConfig; } @@ -28,7 +27,7 @@ export interface MountResult extends Locator { } export const test: TestType<{ - mount( + mount( component: JSX.Element, options?: MountOptions ): Promise; diff --git a/packages/playwright-ct-solid/hooks.d.ts b/packages/playwright-ct-solid/hooks.d.ts index 38d483ec6d..097c8cf11f 100644 --- a/packages/playwright-ct-solid/hooks.d.ts +++ b/packages/playwright-ct-solid/hooks.d.ts @@ -14,12 +14,11 @@ * limitations under the License. */ -import { JSXElement } from "solid-js"; -import type { JsonObject } from '@playwright/experimental-ct-core/types/component'; +import { JSXElement } from 'solid-js'; -export declare function beforeMount( +export declare function beforeMount( callback: (params: { hooksConfig?: HooksConfig, App: () => JSXElement }) => Promise ): void; -export declare function afterMount( +export declare function afterMount( callback: (params: { hooksConfig?: HooksConfig }) => Promise ): void; diff --git a/packages/playwright-ct-solid/index.d.ts b/packages/playwright-ct-solid/index.d.ts index 7cfcf04ba0..c086d4bec5 100644 --- a/packages/playwright-ct-solid/index.d.ts +++ b/packages/playwright-ct-solid/index.d.ts @@ -15,10 +15,9 @@ */ import type { Locator } from 'playwright/test'; -import type { JsonObject } from '@playwright/experimental-ct-core/types/component'; import type { TestType } from '@playwright/experimental-ct-core'; -export interface MountOptions { +export interface MountOptions { hooksConfig?: HooksConfig; } @@ -28,7 +27,7 @@ export interface MountResult extends Locator { } export const test: TestType<{ - mount( + mount( component: JSX.Element, options?: MountOptions ): Promise; diff --git a/packages/playwright-ct-svelte/hooks.d.ts b/packages/playwright-ct-svelte/hooks.d.ts index 03f801c338..dfa037c7de 100644 --- a/packages/playwright-ct-svelte/hooks.d.ts +++ b/packages/playwright-ct-svelte/hooks.d.ts @@ -15,15 +15,14 @@ */ import type { ComponentConstructorOptions, SvelteComponent } from 'svelte'; -import type { JsonObject } from '@playwright/experimental-ct-core/types/component'; -export declare function beforeMount( +export declare function beforeMount( callback: (params: { hooksConfig?: HooksConfig, App: new (options: Partial) => SvelteComponent }) => Promise ): void; -export declare function afterMount( +export declare function afterMount( callback: (params: { hooksConfig?: HooksConfig; svelteComponent: SvelteComponent; diff --git a/packages/playwright-ct-svelte/index.d.ts b/packages/playwright-ct-svelte/index.d.ts index bd1ddd5b36..eb6d464032 100644 --- a/packages/playwright-ct-svelte/index.d.ts +++ b/packages/playwright-ct-svelte/index.d.ts @@ -15,7 +15,6 @@ */ import type { Locator } from 'playwright/test'; -import type { JsonObject } from '@playwright/experimental-ct-core/types/component'; import type { SvelteComponent, ComponentProps } from 'svelte/types/runtime'; import type { TestType } from '@playwright/experimental-ct-core'; @@ -23,7 +22,7 @@ type ComponentSlot = string | string[]; type ComponentSlots = Record & { default?: ComponentSlot }; type ComponentEvents = Record; -export interface MountOptions { +export interface MountOptions { props?: ComponentProps; slots?: ComponentSlots; on?: ComponentEvents; @@ -39,7 +38,7 @@ export interface MountResult extends Locator } export const test: TestType<{ - mount( + mount( component: new (...args: any[]) => Component, options?: MountOptions ): Promise>; diff --git a/packages/playwright-ct-vue/hooks.d.ts b/packages/playwright-ct-vue/hooks.d.ts index 2a18496007..69b91cc2cb 100644 --- a/packages/playwright-ct-vue/hooks.d.ts +++ b/packages/playwright-ct-vue/hooks.d.ts @@ -15,12 +15,11 @@ */ import type { App, ComponentPublicInstance } from 'vue'; -import type { JsonObject } from '@playwright/experimental-ct-core/types/component'; -export declare function beforeMount( +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 308e47c18c..f755a6bcf3 100644 --- a/packages/playwright-ct-vue/index.d.ts +++ b/packages/playwright-ct-vue/index.d.ts @@ -15,7 +15,6 @@ */ import type { Locator } from 'playwright/test'; -import type { JsonObject } from '@playwright/experimental-ct-core/types/component'; import type { TestType } from '@playwright/experimental-ct-core'; type ComponentSlot = string | string[]; @@ -29,14 +28,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,11 +54,11 @@ export interface MountResultJsx extends Locator { } export const test: TestType<{ - mount( + mount( component: JSX.Element, options: MountOptionsJsx ): Promise; - mount( + mount( component: Component, options?: MountOptions ): Promise>; diff --git a/packages/playwright-ct-vue2/hooks.d.ts b/packages/playwright-ct-vue2/hooks.d.ts index 4c3a391117..5009f44348 100644 --- a/packages/playwright-ct-vue2/hooks.d.ts +++ b/packages/playwright-ct-vue2/hooks.d.ts @@ -14,17 +14,16 @@ * limitations under the License. */ -import { ComponentOptions } from 'vue'; -import { CombinedVueInstance, Vue, VueConstructor } from 'vue/types/vue'; -import type { JsonObject } from '@playwright/experimental-ct-core/types/component'; +import type { ComponentOptions } from 'vue'; +import type { CombinedVueInstance, Vue, VueConstructor } from 'vue/types/vue'; -export declare function beforeMount( - callback: (params: { - hooksConfig?: HooksConfig, - Vue: VueConstructor, +export declare function beforeMount( + callback: (params: { + hooksConfig?: HooksConfig, + Vue: VueConstructor, }) => Promise & Record> ): void; -export declare function afterMount( +export declare function afterMount( callback: (params: { hooksConfig?: HooksConfig; instance: CombinedVueInstance< diff --git a/packages/playwright-ct-vue2/index.d.ts b/packages/playwright-ct-vue2/index.d.ts index f76285fb8f..b4fd75e395 100644 --- a/packages/playwright-ct-vue2/index.d.ts +++ b/packages/playwright-ct-vue2/index.d.ts @@ -15,7 +15,6 @@ */ import type { Locator } from 'playwright/test'; -import type { JsonObject } from '@playwright/experimental-ct-core/types/component'; import type { TestType } from '@playwright/experimental-ct-core'; type Slot = string | string[]; @@ -29,14 +28,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,11 +54,11 @@ export interface MountResultJsx extends Locator { } export const test: TestType<{ - mount( + mount( component: JSX.Element, options?: MountOptionsJsx ): Promise; - mount( + mount( component: Component, options?: MountOptions ): Promise>; diff --git a/tests/components/ct-vue-vite/playwright/index.ts b/tests/components/ct-vue-vite/playwright/index.ts index 977ee33845..f23ab5a90a 100644 --- a/tests/components/ct-vue-vite/playwright/index.ts +++ b/tests/components/ct-vue-vite/playwright/index.ts @@ -4,14 +4,17 @@ import Button from '../src/components/Button.vue'; import '../src/assets/index.css'; export type HooksConfig = { - route?: string; routing?: boolean; + components?: Record; } beforeMount(async ({ app, hooksConfig }) => { - if (hooksConfig?.routing) + if (hooksConfig?.routing) app.use(router as any); // TODO: remove any and fix the various installed conflicting Vue versions - app.component('Button', Button); + + for (const [name, component] of Object.entries(hooksConfig?.components || {})) + app.component(name, component); + console.log(`Before mount: ${JSON.stringify(hooksConfig)}, app: ${!!app}`); }); diff --git a/tests/components/ct-vue-vite/tests/slots/slots.spec.js b/tests/components/ct-vue-vite/tests/slots/slots.spec.js index 2af38036b5..a33c9dac92 100644 --- a/tests/components/ct-vue-vite/tests/slots/slots.spec.js +++ b/tests/components/ct-vue-vite/tests/slots/slots.spec.js @@ -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, { @@ -16,6 +17,9 @@ test('render a component as slot', async ({ mount }) => { slots: { default: '