feat(ct): react declaration merging hooks config

This commit is contained in:
sand4rt 2024-05-28 22:16:21 +02:00
parent e047c478a4
commit cd57031c19
7 changed files with 24 additions and 19 deletions

View file

@ -14,9 +14,11 @@
* limitations under the License. * limitations under the License.
*/ */
export declare function beforeMount<HooksConfig>( export interface RegisterHooksConfig {}
export declare function beforeMount<HooksConfig = RegisterHooksConfig>(
callback: (params: { hooksConfig?: HooksConfig; App: () => JSX.Element }) => Promise<void | JSX.Element> callback: (params: { hooksConfig?: HooksConfig; App: () => JSX.Element }) => Promise<void | JSX.Element>
): void; ): void;
export declare function afterMount<HooksConfig>( export declare function afterMount<HooksConfig = RegisterHooksConfig>(
callback: (params: { hooksConfig?: HooksConfig }) => Promise<void> callback: (params: { hooksConfig?: HooksConfig }) => Promise<void>
): void; ): void;

View file

@ -16,8 +16,9 @@
import type { Locator } from 'playwright/test'; import type { Locator } from 'playwright/test';
import type { TestType } from '@playwright/experimental-ct-core'; import type { TestType } from '@playwright/experimental-ct-core';
import type { RegisterHooksConfig } from './hooks';
export interface MountOptions<HooksConfig> { export interface MountOptions<HooksConfig extends RegisterHooksConfig> {
hooksConfig?: HooksConfig; hooksConfig?: HooksConfig;
} }
@ -27,11 +28,11 @@ export interface MountResult extends Locator {
} }
export const test: TestType<{ export const test: TestType<{
mount<HooksConfig>( mount<HooksConfig extends RegisterHooksConfig = RegisterHooksConfig>(
component: JSX.Element, component: JSX.Element,
options?: MountOptions<HooksConfig> options?: MountOptions<HooksConfig>
): Promise<MountResult>; ): Promise<MountResult>;
}>; }>;
export { defineConfig, PlaywrightTestConfig } from '@playwright/experimental-ct-core'; export { defineConfig, type PlaywrightTestConfig } from '@playwright/experimental-ct-core';
export { expect, devices } from 'playwright/test'; export { expect, devices } from 'playwright/test';

View file

@ -14,9 +14,11 @@
* limitations under the License. * limitations under the License.
*/ */
export declare function beforeMount<HooksConfig>( export interface RegisterHooksConfig {}
export declare function beforeMount<HooksConfig = RegisterHooksConfig>(
callback: (params: { hooksConfig?: HooksConfig; App: () => JSX.Element }) => Promise<void | JSX.Element> callback: (params: { hooksConfig?: HooksConfig; App: () => JSX.Element }) => Promise<void | JSX.Element>
): void; ): void;
export declare function afterMount<HooksConfig>( export declare function afterMount<HooksConfig = RegisterHooksConfig>(
callback: (params: { hooksConfig?: HooksConfig }) => Promise<void> callback: (params: { hooksConfig?: HooksConfig }) => Promise<void>
): void; ): void;

View file

@ -16,8 +16,9 @@
import type { Locator } from 'playwright/test'; import type { Locator } from 'playwright/test';
import type { TestType } from '@playwright/experimental-ct-core'; import type { TestType } from '@playwright/experimental-ct-core';
import type { RegisterHooksConfig } from './hooks';
export interface MountOptions<HooksConfig> { export interface MountOptions<HooksConfig extends RegisterHooksConfig> {
hooksConfig?: HooksConfig; hooksConfig?: HooksConfig;
} }
@ -27,11 +28,11 @@ export interface MountResult extends Locator {
} }
export const test: TestType<{ export const test: TestType<{
mount<HooksConfig>( mount<HooksConfig extends RegisterHooksConfig = RegisterHooksConfig>(
component: JSX.Element, component: JSX.Element,
options?: MountOptions<HooksConfig> options?: MountOptions<HooksConfig>
): Promise<MountResult>; ): Promise<MountResult>;
}>; }>;
export { defineConfig, PlaywrightTestConfig } from '@playwright/experimental-ct-core'; export { defineConfig, type PlaywrightTestConfig } from '@playwright/experimental-ct-core';
export { expect, devices } from 'playwright/test'; export { expect, devices } from 'playwright/test';

View file

@ -2,18 +2,19 @@ import { beforeMount, afterMount } from '@playwright/experimental-ct-react/hooks
import { BrowserRouter } from 'react-router-dom'; import { BrowserRouter } from 'react-router-dom';
import '../src/assets/index.css'; import '../src/assets/index.css';
export type HooksConfig = { declare module '@playwright/experimental-ct-react/hooks' {
route?: string; interface RegisterHooksConfig {
routing?: boolean; routing?: boolean;
}
} }
beforeMount<HooksConfig>(async ({ hooksConfig, App }) => { beforeMount(async ({ hooksConfig, App }) => {
console.log(`Before mount: ${JSON.stringify(hooksConfig)}`); console.log(`Before mount: ${JSON.stringify(hooksConfig)}`);
if (hooksConfig?.routing) if (hooksConfig?.routing)
return <BrowserRouter><App /></BrowserRouter>; return <BrowserRouter><App /></BrowserRouter>;
}); });
afterMount<HooksConfig>(async () => { afterMount(async () => {
console.log(`After mount`); console.log(`After mount`);
}); });

View file

@ -1,9 +1,8 @@
import { test, expect } from '@playwright/experimental-ct-react'; import { test, expect } from '@playwright/experimental-ct-react';
import App from '@/App'; import App from '@/App';
import type { HooksConfig } from '../playwright';
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');
@ -14,7 +13,7 @@ test('navigate to a page by clicking a link', async ({ page, mount }) => {
}); });
test('update should not reset mount hooks', async ({ page, mount }) => { test('update should not reset mount hooks', async ({ page, mount }) => {
const component = await mount<HooksConfig>(<App title='before'/>, { const component = await mount(<App title='before'/>, {
hooksConfig: { routing: true }, hooksConfig: { routing: true },
}); });
await expect(component.getByRole('heading')).toHaveText('before'); await expect(component.getByRole('heading')).toHaveText('before');

View file

@ -21,6 +21,5 @@
"*": ["_"], "*": ["_"],
} }
}, },
"include": ["src", "tests"],
"references": [{ "path": "./tsconfig.node.json" }] "references": [{ "path": "./tsconfig.node.json" }]
} }