feat(ct): resolve hooksConfig import refs

This commit is contained in:
sand4rt 2024-04-21 22:12:55 +02:00
parent 7ad255301f
commit 301896241c
8 changed files with 34 additions and 29 deletions

View file

@ -102,6 +102,7 @@ async function innerMount(page: Page, componentRef: JsxComponent | ImportRef, op
const selector = await page.evaluate(async ({ component, hooksConfig }) => { const selector = await page.evaluate(async ({ component, hooksConfig }) => {
component = await window.__pwUnwrapObject(component); component = await window.__pwUnwrapObject(component);
hooksConfig = await window.__pwUnwrapObject(hooksConfig);
let rootElement = document.getElementById('root'); let rootElement = document.getElementById('root');
if (!rootElement) { if (!rootElement) {
rootElement = document.createElement('div'); rootElement = document.createElement('div');

View file

@ -14,10 +14,9 @@
* limitations under the License. * limitations under the License.
*/ */
type JsonPrimitive = string | number | boolean | null; export interface RegisterHooksConfig {
type JsonValue = JsonPrimitive | JsonObject | JsonArray;
type JsonArray = JsonValue[]; };
export type JsonObject = { [Key in string]?: JsonValue };
export type JsxComponent = { export type JsxComponent = {
__pw_type: 'jsx', __pw_type: 'jsx',
@ -47,10 +46,10 @@ declare global {
playwrightMount(component: Component, rootElement: Element, hooksConfig?: any): Promise<void>; playwrightMount(component: Component, rootElement: Element, hooksConfig?: any): Promise<void>;
playwrightUnmount(rootElement: Element): Promise<void>; playwrightUnmount(rootElement: Element): Promise<void>;
playwrightUpdate(rootElement: Element, component: Component): Promise<void>; playwrightUpdate(rootElement: Element, component: Component): Promise<void>;
__pw_hooks_before_mount?: (<HooksConfig extends JsonObject = JsonObject>( __pw_hooks_before_mount?: (<HooksConfig extends RegisterHooksConfig = RegisterHooksConfig>(
params: { hooksConfig?: HooksConfig; [key: string]: any } params: { hooksConfig?: HooksConfig; [key: string]: any }
) => Promise<any>)[]; ) => Promise<any>)[];
__pw_hooks_after_mount?: (<HooksConfig extends JsonObject = JsonObject>( __pw_hooks_after_mount?: (<HooksConfig extends RegisterHooksConfig = RegisterHooksConfig>(
params: { hooksConfig?: HooksConfig; [key: string]: any } params: { hooksConfig?: HooksConfig; [key: string]: any }
) => Promise<void>)[]; ) => Promise<void>)[];
// Can't start with __pw due to core reuse bindings logic for __pw*. // Can't start with __pw due to core reuse bindings logic for __pw*.

View file

@ -15,12 +15,13 @@
*/ */
import type { App, ComponentPublicInstance } from 'vue'; import type { App, ComponentPublicInstance } from 'vue';
import type { JsonObject } from '@playwright/experimental-ct-core/types/component';
export declare function beforeMount<HooksConfig extends JsonObject>( export interface RegisterHooksConfig {}
export declare function beforeMount<HooksConfig = RegisterHooksConfig>(
callback: (params: { app: App; hooksConfig?: HooksConfig }) => Promise<void> callback: (params: { app: App; hooksConfig?: HooksConfig }) => Promise<void>
): void; ): void;
export declare function afterMount<HooksConfig extends JsonObject>( export declare function afterMount<HooksConfig = RegisterHooksConfig>(
callback: (params: { callback: (params: {
app: App; app: App;
hooksConfig?: HooksConfig; hooksConfig?: HooksConfig;

View file

@ -15,7 +15,7 @@
*/ */
import type { Locator } from 'playwright/test'; 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'; import type { TestType } from '@playwright/experimental-ct-core';
type ComponentSlot = string | string[]; type ComponentSlot = string | string[];
@ -29,14 +29,14 @@ type ComponentProps<T> =
T extends (props: infer P, ...args: any) => any ? P : T extends (props: infer P, ...args: any) => any ? P :
{}; {};
export interface MountOptions<HooksConfig extends JsonObject, Component> { export interface MountOptions<Component, HooksConfig extends RegisterHooksConfig> {
props?: ComponentProps<Component>; props?: ComponentProps<Component>;
slots?: ComponentSlots; slots?: ComponentSlots;
on?: ComponentEvents; on?: ComponentEvents;
hooksConfig?: HooksConfig; hooksConfig?: HooksConfig;
} }
export interface MountOptionsJsx<HooksConfig extends JsonObject> { export interface MountOptionsJsx<HooksConfig extends RegisterHooksConfig> {
hooksConfig?: HooksConfig; hooksConfig?: HooksConfig;
} }
@ -55,13 +55,13 @@ export interface MountResultJsx extends Locator {
} }
export const test: TestType<{ export const test: TestType<{
mount<HooksConfig extends JsonObject>( mount<HooksConfig extends RegisterHooksConfig>(
component: JSX.Element, component: JSX.Element,
options: MountOptionsJsx<HooksConfig> options: MountOptionsJsx<HooksConfig>
): Promise<MountResultJsx>; ): Promise<MountResultJsx>;
mount<HooksConfig extends JsonObject, Component = unknown>( mount<Component = unknown, HooksConfig extends RegisterHooksConfig>(
component: Component, component: Component,
options?: MountOptions<HooksConfig, Component> options?: MountOptions<Component, HooksConfig>
): Promise<MountResult<Component>>; ): Promise<MountResult<Component>>;
}>; }>;

View file

@ -1,20 +1,22 @@
import { beforeMount, afterMount } from '@playwright/experimental-ct-vue/hooks'; import { beforeMount, afterMount } from '@playwright/experimental-ct-vue/hooks';
import { router } from '../src/router'; import { router } from '../src/router';
import Button from '../src/components/Button.vue';
import '../src/assets/index.css'; import '../src/assets/index.css';
export type HooksConfig = { declare module '@playwright/experimental-ct-vue/hooks' {
route?: string; interface RegisterHooksConfig {
routing?: boolean; routing?: boolean;
components?: Record<string, any>;
}
} }
beforeMount<HooksConfig>(async ({ app, hooksConfig }) => { 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.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<HooksConfig>(async ({ instance }) => { afterMount(async ({ instance }) => {
console.log(`After mount el: ${instance.$el.constructor.name}`); console.log(`After mount el: ${instance.$el.constructor.name}`);
}); });

View file

@ -1,6 +1,7 @@
import { test, expect } from '@playwright/experimental-ct-vue'; import { test, expect } from '@playwright/experimental-ct-vue';
import DefaultSlot from '@/components/DefaultSlot.vue'; import DefaultSlot from '@/components/DefaultSlot.vue';
import NamedSlots from '@/components/NamedSlots.vue'; import NamedSlots from '@/components/NamedSlots.vue';
import Button from '@/components/Button.vue';
test('render a default slot', async ({ mount }) => { test('render a default slot', async ({ mount }) => {
const component = await mount(DefaultSlot, { const component = await mount(DefaultSlot, {
@ -14,8 +15,11 @@ test('render a default slot', async ({ mount }) => {
test('render a component as slot', async ({ mount }) => { test('render a component as slot', async ({ mount }) => {
const component = await mount(DefaultSlot, { const component = await mount(DefaultSlot, {
slots: { slots: {
default: '<Button title="Submit" />', // component is registered globally in /playwright/index.ts default: '<Button title="Submit" />',
}, },
hooksConfig: {
components: { Button }
}
}); });
await expect(component).toContainText('Submit'); await expect(component).toContainText('Submit');
}); });

View file

@ -1,9 +1,8 @@
import { test, expect } from '@playwright/experimental-ct-vue'; import { test, expect } from '@playwright/experimental-ct-vue';
import type { HooksConfig } from '../../playwright';
import App from '@/App.vue'; import App from '@/App.vue';
test('navigate to a page by clicking a link', async ({ page, mount }) => { test('navigate to a page by clicking a link', async ({ page, mount }) => {
const component = await mount<HooksConfig>(App, { const component = await mount(App, {
hooksConfig: { routing: true }, hooksConfig: { routing: true },
}); });
await expect(component.getByRole('main')).toHaveText('Login'); await expect(component.getByRole('main')).toHaveText('Login');

View file

@ -1,9 +1,8 @@
import { test, expect } from '@playwright/experimental-ct-vue'; import { test, expect } from '@playwright/experimental-ct-vue';
import type { HooksConfig } from '../../playwright';
import App from '@/App.vue'; import App from '@/App.vue';
test('navigate to a page by clicking a link', async ({ page, mount }) => { test('navigate to a page by clicking a link', async ({ page, mount }) => {
const component = await mount<HooksConfig>(<App />, { const component = await mount(<App />, {
hooksConfig: { routing: true }, hooksConfig: { routing: true },
}); });
await expect(component.getByRole('main')).toHaveText('Login'); await expect(component.getByRole('main')).toHaveText('Login');