2022-06-02 17:37:43 -07: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
// This file is injected into the registry as text, no dependencies are allowed.
|
|
|
|
|
|
|
|
import Vue from 'vue';
|
|
|
|
|
|
|
|
/** @typedef {import('../playwright-test/types/component').Component} Component */
|
2022-06-04 14:07:06 -07:00
|
|
|
/** @typedef {import('vue').Component} FrameworkComponent */
|
2022-06-02 17:37:43 -07:00
|
|
|
|
2022-06-04 14:07:06 -07:00
|
|
|
/** @type {Map<string, FrameworkComponent>} */
|
2022-06-02 17:37:43 -07:00
|
|
|
const registry = new Map();
|
|
|
|
|
|
|
|
/**
|
2022-06-04 14:07:06 -07:00
|
|
|
* @param {{[key: string]: FrameworkComponent}} components
|
2022-06-02 17:37:43 -07:00
|
|
|
*/
|
|
|
|
export function register(components) {
|
|
|
|
for (const [name, value] of Object.entries(components))
|
|
|
|
registry.set(name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-06-04 14:07:06 -07:00
|
|
|
* @param {Component | string} child
|
|
|
|
* @param {import('vue').CreateElement} h
|
2022-06-02 17:37:43 -07:00
|
|
|
*/
|
2022-10-10 21:49:52 +02:00
|
|
|
function createChild(child, h) {
|
|
|
|
return typeof child === 'string' ? child : createWrapper(child, h);
|
2022-06-02 17:37:43 -07:00
|
|
|
}
|
|
|
|
|
2022-10-18 22:02:53 +02:00
|
|
|
/**
|
|
|
|
* Exists to support fallthrough attributes:
|
|
|
|
* https://vuejs.org/guide/components/attrs.html#fallthrough-attributes
|
|
|
|
* @param {any} Component
|
|
|
|
* @param {string} key
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
function componentHasKeyInProps(Component, key) {
|
|
|
|
if (Array.isArray(Component.props))
|
|
|
|
return Component.props.includes(key);
|
|
|
|
|
|
|
|
return Object.entries(Component.props).flat().includes(key);
|
|
|
|
}
|
|
|
|
|
2022-06-02 17:37:43 -07:00
|
|
|
/**
|
2022-06-04 14:07:06 -07:00
|
|
|
* @param {Component} component
|
|
|
|
* @param {import('vue').CreateElement} h
|
2022-06-02 17:37:43 -07:00
|
|
|
*/
|
2022-10-10 21:49:52 +02:00
|
|
|
function createComponent(component, h) {
|
2022-06-02 17:37:43 -07:00
|
|
|
/**
|
|
|
|
* @type {import('vue').Component | string | undefined}
|
|
|
|
*/
|
|
|
|
let componentFunc = registry.get(component.type);
|
|
|
|
if (!componentFunc) {
|
|
|
|
// Lookup by shorthand.
|
|
|
|
for (const [name, value] of registry) {
|
|
|
|
if (component.type.endsWith(`_${name}_vue`)) {
|
|
|
|
componentFunc = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!componentFunc && component.type[0].toUpperCase() === component.type[0])
|
|
|
|
throw new Error(`Unregistered component: ${component.type}. Following components are registered: ${[...registry.keys()]}`);
|
|
|
|
|
|
|
|
componentFunc = componentFunc || component.type;
|
|
|
|
|
|
|
|
const isVueComponent = componentFunc !== component.type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {(import('vue').VNode | string)[]}
|
|
|
|
*/
|
|
|
|
const children = [];
|
|
|
|
|
|
|
|
/** @type {import('vue').VNodeData} */
|
|
|
|
const nodeData = {};
|
|
|
|
nodeData.attrs = {};
|
|
|
|
nodeData.props = {};
|
|
|
|
nodeData.scopedSlots = {};
|
|
|
|
nodeData.on = {};
|
|
|
|
|
|
|
|
if (component.kind === 'jsx') {
|
|
|
|
for (const child of component.children || []) {
|
|
|
|
if (typeof child !== 'string' && child.type === 'template' && child.kind === 'jsx') {
|
|
|
|
const slotProperty = Object.keys(child.props).find(k => k.startsWith('v-slot:'));
|
|
|
|
const slot = slotProperty ? slotProperty.substring('v-slot:'.length) : 'default';
|
2022-10-10 21:49:52 +02:00
|
|
|
nodeData.scopedSlots[slot] = () => child.children.map(c => createChild(c, h));
|
2022-06-02 17:37:43 -07:00
|
|
|
} else {
|
2022-10-10 21:49:52 +02:00
|
|
|
children.push(createChild(child, h));
|
2022-06-02 17:37:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const [key, value] of Object.entries(component.props)) {
|
|
|
|
if (key.startsWith('v-on:')) {
|
|
|
|
const event = key.substring('v-on:'.length);
|
|
|
|
nodeData.on[event] = value;
|
|
|
|
} else {
|
2022-10-18 22:02:53 +02:00
|
|
|
if (isVueComponent && componentHasKeyInProps(componentFunc, key))
|
2022-06-02 17:37:43 -07:00
|
|
|
nodeData.props[key] = value;
|
|
|
|
else
|
|
|
|
nodeData.attrs[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (component.kind === 'object') {
|
|
|
|
// Vue test util syntax.
|
|
|
|
const options = component.options || {};
|
|
|
|
for (const [key, value] of Object.entries(options.slots || {})) {
|
2022-10-10 21:49:52 +02:00
|
|
|
const list = (Array.isArray(value) ? value : [value]).map(v => createChild(v, h));
|
2022-06-02 17:37:43 -07:00
|
|
|
if (key === 'default')
|
|
|
|
children.push(...list);
|
|
|
|
else
|
|
|
|
nodeData.scopedSlots[key] = () => list;
|
|
|
|
}
|
|
|
|
nodeData.props = options.props || {};
|
|
|
|
for (const [key, value] of Object.entries(options.on || {}))
|
|
|
|
nodeData.on[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @type {(string|import('vue').VNode)[] | undefined} */
|
|
|
|
let lastArg;
|
|
|
|
if (Object.entries(nodeData.scopedSlots).length) {
|
|
|
|
if (children.length)
|
|
|
|
nodeData.scopedSlots.default = () => children;
|
|
|
|
} else if (children.length) {
|
|
|
|
lastArg = children;
|
|
|
|
}
|
|
|
|
|
2022-10-10 21:49:52 +02:00
|
|
|
return { Component: componentFunc, nodeData, slots: lastArg };
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Component} component
|
|
|
|
* @param {import('vue').CreateElement} h
|
|
|
|
* @returns {import('vue').VNode}
|
|
|
|
*/
|
|
|
|
function createWrapper(component, h) {
|
|
|
|
const { Component, nodeData, slots } = createComponent(component, h);
|
|
|
|
const wrapper = h(Component, nodeData, slots);
|
2022-06-02 17:37:43 -07:00
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
2022-08-04 19:43:43 +02:00
|
|
|
const instanceKey = Symbol('instanceKey');
|
2022-10-10 21:49:52 +02:00
|
|
|
const wrapperKey = Symbol('wrapperKey');
|
2022-08-04 19:43:43 +02:00
|
|
|
|
2022-07-12 08:37:33 -08:00
|
|
|
window.playwrightMount = async (component, rootElement, hooksConfig) => {
|
2022-12-20 00:33:50 +01:00
|
|
|
let options = {};
|
2022-07-12 08:37:33 -08:00
|
|
|
for (const hook of /** @type {any} */(window).__pw_hooks_before_mount || [])
|
2022-12-20 00:33:50 +01:00
|
|
|
options = await hook({ hooksConfig, Vue });
|
2022-07-12 08:37:33 -08:00
|
|
|
|
|
|
|
const instance = new Vue({
|
2022-12-20 00:33:50 +01:00
|
|
|
...options,
|
2022-10-10 21:49:52 +02:00
|
|
|
render: h => {
|
|
|
|
const wrapper = createWrapper(component, h);
|
|
|
|
/** @type {any} */ (rootElement)[wrapperKey] = wrapper;
|
|
|
|
return wrapper;
|
|
|
|
},
|
2022-06-02 17:37:43 -07:00
|
|
|
}).$mount();
|
2022-07-12 08:37:33 -08:00
|
|
|
rootElement.appendChild(instance.$el);
|
2022-08-04 19:43:43 +02:00
|
|
|
/** @type {any} */ (rootElement)[instanceKey] = instance;
|
2022-07-12 08:37:33 -08:00
|
|
|
|
|
|
|
for (const hook of /** @type {any} */(window).__pw_hooks_after_mount || [])
|
|
|
|
await hook({ hooksConfig, instance });
|
2022-06-02 17:37:43 -07:00
|
|
|
};
|
2022-07-27 15:12:36 -07:00
|
|
|
|
2022-08-04 19:43:43 +02:00
|
|
|
window.playwrightUnmount = async rootElement => {
|
|
|
|
const component = /** @type {any} */(rootElement)[instanceKey];
|
2022-07-27 15:12:36 -07:00
|
|
|
if (!component)
|
|
|
|
throw new Error('Component was not mounted');
|
|
|
|
component.$destroy();
|
2022-08-04 19:43:43 +02:00
|
|
|
component.$el.remove();
|
2022-07-27 15:12:36 -07:00
|
|
|
};
|
2022-08-23 20:37:55 +02:00
|
|
|
|
2022-10-11 04:56:33 +02:00
|
|
|
window.playwrightUpdate = async (element, options) => {
|
2022-10-10 21:49:52 +02:00
|
|
|
const wrapper = /** @type {any} */(element)[wrapperKey];
|
|
|
|
if (!wrapper)
|
2022-08-23 20:37:55 +02:00
|
|
|
throw new Error('Component was not mounted');
|
|
|
|
|
2022-10-10 21:49:52 +02:00
|
|
|
const component = wrapper.componentInstance;
|
|
|
|
const { nodeData, slots } = createComponent(options, component.$createElement);
|
|
|
|
|
|
|
|
for (const [name, value] of Object.entries(nodeData.on || {})) {
|
|
|
|
component.$on(name, value);
|
|
|
|
component.$listeners[name] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.assign(component.$scopedSlots, nodeData.scopedSlots);
|
|
|
|
component.$slots.default = slots;
|
|
|
|
|
|
|
|
for (const [key, value] of Object.entries(nodeData.props || {}))
|
|
|
|
component[key] = value;
|
|
|
|
|
|
|
|
if (!Object.keys(nodeData.props || {}).length)
|
|
|
|
component.$forceUpdate();
|
2022-08-23 20:37:55 +02:00
|
|
|
};
|