-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
mount.ts
126 lines (108 loc) · 5.19 KB
/
mount.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { Fixtures, Locator, Page, BrowserContextOptions, PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, PlaywrightWorkerOptions, BrowserContext } from 'playwright/test';
import type { Component, JsxComponent, MountOptions, ObjectComponentOptions } from '../types/component';
import type { ContextReuseMode, FullConfigInternal } from '../../playwright/src/common/config';
import type { ImportRef } from './injected/importRegistry';
import { wrapObject } from './injected/serializers';
let boundCallbacksForMount: Function[] = [];
interface MountResult extends Locator {
unmount(locator: Locator): Promise<void>;
update(options: Omit<MountOptions, 'hooksConfig'> | string | JsxComponent): Promise<void>;
}
type TestFixtures = PlaywrightTestArgs & PlaywrightTestOptions & {
mount: (component: any, options: any) => Promise<MountResult>;
};
type WorkerFixtures = PlaywrightWorkerArgs & PlaywrightWorkerOptions & { _ctWorker: { context: BrowserContext | undefined, hash: string } };
type BaseTestFixtures = {
_contextFactory: (options?: BrowserContextOptions) => Promise<BrowserContext>,
_optionContextReuseMode: ContextReuseMode
};
export const fixtures: Fixtures<TestFixtures, WorkerFixtures, BaseTestFixtures> = {
_optionContextReuseMode: 'when-possible',
serviceWorkers: 'block',
_ctWorker: [{ context: undefined, hash: '' }, { scope: 'worker' }],
page: async ({ page }, use, info) => {
if (!((info as any)._configInternal as FullConfigInternal).defineConfigWasUsed)
throw new Error('Component testing requires the use of the defineConfig() in your playwright-ct.config.{ts,js}: https://aka.ms/playwright/ct-define-config');
await (page as any)._wrapApiCall(async () => {
await page.exposeFunction('__ctDispatchFunction', (ordinal: number, args: any[]) => {
boundCallbacksForMount[ordinal](...args);
});
await page.goto(process.env.PLAYWRIGHT_TEST_BASE_URL!);
}, true);
await use(page);
},
mount: async ({ page }, use) => {
await use(async (componentRef: JsxComponent | ImportRef, options?: ObjectComponentOptions & MountOptions) => {
const selector = await (page as any)._wrapApiCall(async () => {
return await innerMount(page, componentRef, options);
}, true);
const locator = page.locator(selector);
return Object.assign(locator, {
unmount: async () => {
await locator.evaluate(async () => {
const rootElement = document.getElementById('root')!;
await window.playwrightUnmount(rootElement);
});
},
update: async (options: JsxComponent | ObjectComponentOptions) => {
if (isJsxComponent(options))
return await innerUpdate(page, options);
await innerUpdate(page, componentRef, options);
}
});
});
boundCallbacksForMount = [];
},
};
function isJsxComponent(component: any): component is JsxComponent {
return typeof component === 'object' && component && component.__pw_type === 'jsx';
}
async function innerUpdate(page: Page, componentRef: JsxComponent | ImportRef, options: ObjectComponentOptions = {}): Promise<void> {
const component = wrapObject(createComponent(componentRef, options), boundCallbacksForMount);
await page.evaluate(async ({ component }) => {
component = await window.__pwUnwrapObject(component);
const rootElement = document.getElementById('root')!;
return await window.playwrightUpdate(rootElement, component);
}, { component });
}
async function innerMount(page: Page, componentRef: JsxComponent | ImportRef, options: ObjectComponentOptions & MountOptions = {}): Promise<string> {
const component = wrapObject(createComponent(componentRef, options), boundCallbacksForMount);
// WebKit does not wait for deferred scripts.
await page.waitForFunction(() => !!window.playwrightMount);
const selector = await page.evaluate(async ({ component, hooksConfig }) => {
component = await window.__pwUnwrapObject(component);
let rootElement = document.getElementById('root');
if (!rootElement) {
rootElement = document.createElement('div');
rootElement.id = 'root';
document.body.appendChild(rootElement);
}
await window.playwrightMount(component, rootElement, hooksConfig);
return '#root >> internal:control=component';
}, { component, hooksConfig: options.hooksConfig });
return selector;
}
function createComponent(component: JsxComponent | ImportRef, options: ObjectComponentOptions = {}): Component {
if (component.__pw_type === 'jsx')
return component;
return {
__pw_type: 'object-component',
type: component,
...options,
};
}