2022-05-04 07:25:50 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-06-05 00:07:06 +03:00
|
|
|
// @ts-check
|
|
|
|
|
2022-05-04 07:25:50 +03:00
|
|
|
// This file is injected into the registry as text, no dependencies are allowed.
|
|
|
|
|
2022-06-05 00:07:06 +03:00
|
|
|
/** @typedef {import('../playwright-test/types/component').Component} Component */
|
|
|
|
/** @typedef {any} FrameworkComponent */
|
|
|
|
|
|
|
|
/** @type {Map<string, FrameworkComponent>} */
|
2022-05-04 07:25:50 +03:00
|
|
|
const registry = new Map();
|
|
|
|
|
2022-06-05 00:07:06 +03:00
|
|
|
/**
|
|
|
|
* @param {{[key: string]: FrameworkComponent}} components
|
|
|
|
*/
|
2022-05-05 02:01:48 +03:00
|
|
|
export function register(components) {
|
2022-05-04 07:25:50 +03:00
|
|
|
for (const [name, value] of Object.entries(components))
|
|
|
|
registry.set(name, value);
|
|
|
|
}
|
|
|
|
|
2022-07-12 19:37:33 +03:00
|
|
|
window.playwrightMount = async (component, rootElement, hooksConfig) => {
|
2022-05-04 07:25:50 +03:00
|
|
|
let componentCtor = registry.get(component.type);
|
|
|
|
if (!componentCtor) {
|
|
|
|
// Lookup by shorthand.
|
|
|
|
for (const [name, value] of registry) {
|
|
|
|
if (component.type.endsWith(`_${name}_svelte`)) {
|
|
|
|
componentCtor = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!componentCtor)
|
|
|
|
throw new Error(`Unregistered component: ${component.type}. Following components are registered: ${[...registry.keys()]}`);
|
|
|
|
|
2022-06-05 00:07:06 +03:00
|
|
|
if (component.kind !== 'object')
|
|
|
|
throw new Error('JSX mount notation is not supported');
|
|
|
|
|
2022-07-12 19:37:33 +03:00
|
|
|
|
|
|
|
for (const hook of /** @type {any} */(window).__pw_hooks_before_mount || [])
|
|
|
|
await hook({ hooksConfig });
|
|
|
|
|
2022-05-04 07:25:50 +03:00
|
|
|
const wrapper = new componentCtor({
|
2022-06-05 00:07:06 +03:00
|
|
|
target: rootElement,
|
2022-05-04 07:25:50 +03:00
|
|
|
props: component.options?.props,
|
|
|
|
});
|
2022-07-12 19:37:33 +03:00
|
|
|
|
|
|
|
for (const hook of /** @type {any} */(window).__pw_hooks_after_mount || [])
|
|
|
|
await hook({ hooksConfig });
|
|
|
|
|
2022-05-04 07:25:50 +03:00
|
|
|
for (const [key, listener] of Object.entries(component.options?.on || {}))
|
|
|
|
wrapper.$on(key, event => listener(event.detail));
|
|
|
|
};
|