Skip to content

Commit

Permalink
feat(ct): resolve hooksConfig import refs
Browse files Browse the repository at this point in the history
  • Loading branch information
sand4rt committed Apr 21, 2024
1 parent 7ad2553 commit 9a15f00
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 24 deletions.
1 change: 1 addition & 0 deletions packages/playwright-ct-core/src/mount.ts
Expand Up @@ -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');
Expand Down
11 changes: 5 additions & 6 deletions packages/playwright-ct-core/types/component.d.ts
Expand Up @@ -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',
Expand Down Expand Up @@ -47,10 +46,10 @@ declare global {
playwrightMount(component: Component, rootElement: Element, hooksConfig?: any): Promise<void>;
playwrightUnmount(rootElement: Element): 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 }
) => Promise<any>)[];
__pw_hooks_after_mount?: (<HooksConfig extends JsonObject = JsonObject>(
__pw_hooks_after_mount?: (<HooksConfig extends RegisterHooksConfig = RegisterHooksConfig>(
params: { hooksConfig?: HooksConfig; [key: string]: any }
) => Promise<void>)[];
// Can't start with __pw due to core reuse bindings logic for __pw*.
Expand Down
7 changes: 4 additions & 3 deletions packages/playwright-ct-vue/hooks.d.ts
Expand Up @@ -15,12 +15,13 @@
*/

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>
): void;
export declare function afterMount<HooksConfig extends JsonObject>(
export declare function afterMount<HooksConfig = RegisterHooksConfig>(
callback: (params: {
app: App;
hooksConfig?: HooksConfig;
Expand Down
12 changes: 6 additions & 6 deletions packages/playwright-ct-vue/index.d.ts
Expand Up @@ -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[];
Expand All @@ -29,14 +29,14 @@ type ComponentProps<T> =
T extends (props: infer P, ...args: any) => any ? P :
{};

export interface MountOptions<HooksConfig extends JsonObject, Component> {
export interface MountOptions<HooksConfig extends RegisterHooksConfig = RegisterHooksConfig> {
props?: ComponentProps<Component>;
slots?: ComponentSlots;
on?: ComponentEvents;
hooksConfig?: HooksConfig;
}

export interface MountOptionsJsx<HooksConfig extends JsonObject> {
export interface MountOptionsJsx<HooksConfig = RegisterHooksConfig> {
hooksConfig?: HooksConfig;
}

Expand All @@ -55,13 +55,13 @@ export interface MountResultJsx extends Locator {
}

export const test: TestType<{
mount<HooksConfig extends JsonObject>(
mount<HooksConfig extends RegisterHooksConfig = RegisterHooksConfig>(
component: JSX.Element,
options: MountOptionsJsx<HooksConfig>
): Promise<MountResultJsx>;
mount<HooksConfig extends JsonObject, Component = unknown>(
mount<HooksConfig extends RegisterHooksConfig = RegisterHooksConfig, Component = unknown>(
component: Component,
options?: MountOptions<HooksConfig, Component>
options?: MountOptions<HooksConfig>
): Promise<MountResult<Component>>;
}>;

Expand Down
21 changes: 13 additions & 8 deletions tests/components/ct-vue-vite/playwright/index.ts
@@ -1,20 +1,25 @@
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 {
route?: string;
routing?: boolean;
components?: Record<string, any>;
}
}

beforeMount<HooksConfig>(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<HooksConfig>(async ({ instance }) => {
afterMount(async ({ instance }) => {
console.log(`After mount el: ${instance.$el.constructor.name}`);
});
6 changes: 5 additions & 1 deletion 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, {
Expand All @@ -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: '<Button title="Submit" />', // component is registered globally in /playwright/index.ts
default: '<Button title="Submit" />',
},
hooksConfig: {
components: { Button }
}
});
await expect(component).toContainText('Submit');
});
Expand Down

0 comments on commit 9a15f00

Please sign in to comment.