@playwright/experimental-ct-react 1.41.0 → 1.41.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/registerSource.mjs +12 -105
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@playwright/experimental-ct-react",
3
- "version": "1.41.0",
3
+ "version": "1.41.1",
4
4
  "description": "Playwright Component Testing for React",
5
5
  "repository": {
6
6
  "type": "git",
@@ -29,7 +29,7 @@
29
29
  }
30
30
  },
31
31
  "dependencies": {
32
- "@playwright/experimental-ct-core": "1.41.0",
32
+ "@playwright/experimental-ct-core": "1.41.1",
33
33
  "@vitejs/plugin-react": "^4.0.0"
34
34
  },
35
35
  "bin": {
@@ -17,130 +17,38 @@
17
17
  // @ts-check
18
18
  // This file is injected into the registry as text, no dependencies are allowed.
19
19
 
20
- import * as __pwReact from 'react';
20
+ import __pwReact from 'react';
21
21
  import { createRoot as __pwCreateRoot } from 'react-dom/client';
22
-
23
- /** @typedef {import('../playwright-ct-core/types/component').JsxComponentChild} JsxComponentChild */
24
22
  /** @typedef {import('../playwright-ct-core/types/component').JsxComponent} JsxComponent */
25
- /** @typedef {import('react').FunctionComponent} FrameworkComponent */
26
23
 
27
- /** @type {Map<string, () => Promise<FrameworkComponent>>} */
28
- const __pwLoaderRegistry = new Map();
29
- /** @type {Map<string, FrameworkComponent>} */
30
- const __pwRegistry = new Map();
31
24
  /** @type {Map<Element, import('react-dom/client').Root>} */
32
25
  const __pwRootRegistry = new Map();
33
26
 
34
- /**
35
- * @param {Record<string, () => Promise<FrameworkComponent>>} components
36
- */
37
- export function pwRegister(components) {
38
- for (const [name, value] of Object.entries(components))
39
- __pwLoaderRegistry.set(name, value);
40
- }
41
-
42
27
  /**
43
28
  * @param {any} component
44
29
  * @returns {component is JsxComponent}
45
30
  */
46
- function isComponent(component) {
47
- return component.__pw_component_marker === true && component.kind === 'jsx';
31
+ function isJsxComponent(component) {
32
+ return typeof component === 'object' && component && component.__pw_type === 'jsx';
48
33
  }
49
34
 
50
35
  /**
51
- * @param {JsxComponent | JsxComponentChild} component
36
+ * @param {any} value
52
37
  */
53
- async function __pwResolveComponent(component) {
54
- if (!isComponent(component))
55
- return;
56
-
57
- let componentFactory = __pwLoaderRegistry.get(component.type);
58
- if (!componentFactory) {
59
- // Lookup by shorthand.
60
- for (const [name, value] of __pwLoaderRegistry) {
61
- if (component.type.endsWith(`_${name}`)) {
62
- componentFactory = value;
63
- break;
64
- }
38
+ function __pwRender(value) {
39
+ return window.__pwTransformObject(value, v => {
40
+ if (isJsxComponent(v)) {
41
+ const component = v;
42
+ const props = component.props ? __pwRender(component.props) : {};
43
+ return { result: __pwReact.createElement(/** @type { any } */ (component.type), { ...props, children: undefined }, props.children) };
65
44
  }
66
- }
67
-
68
- if (!componentFactory && component.type[0].toUpperCase() === component.type[0])
69
- throw new Error(`Unregistered component: ${component.type}. Following components are registered: ${[...__pwRegistry.keys()]}`);
70
-
71
- if (componentFactory)
72
- __pwRegistry.set(component.type, await componentFactory());
73
-
74
- if (component.children?.length)
75
- await Promise.all(component.children.map(child => __pwResolveComponent(child)));
76
-
77
- if (component.props)
78
- await __resolveProps(component.props);
79
- }
80
-
81
- /**
82
- * @param {Record<string, any>} props
83
- */
84
- async function __resolveProps(props) {
85
- for (const prop of Object.values(props)) {
86
- if (Array.isArray(prop))
87
- await Promise.all(prop.map(child => __pwResolveComponent(child)));
88
- else if (isComponent(prop))
89
- await __pwResolveComponent(prop);
90
- else if (typeof prop === 'object' && prop !== null)
91
- await __resolveProps(prop);
92
- }
93
- }
94
-
95
- /**
96
- * @param {JsxComponentChild} child
97
- */
98
- function __renderChild(child) {
99
- if (Array.isArray(child))
100
- return child.map(grandChild => __renderChild(grandChild));
101
- if (isComponent(child))
102
- return __pwRender(child);
103
- return child;
104
- }
105
-
106
- /**
107
- * @param {Record<string, any>} props
108
- */
109
- function __renderProps(props) {
110
- const newProps = {};
111
- for (const [key, prop] of Object.entries(props)) {
112
- if (Array.isArray(prop))
113
- newProps[key] = prop.map(child => __renderChild(child));
114
- else if (isComponent(prop))
115
- newProps[key] = __renderChild(prop);
116
- else if (typeof prop === 'object' && prop !== null)
117
- newProps[key] = __renderProps(prop);
118
- else
119
- newProps[key] = prop;
120
- }
121
- return newProps;
122
- }
123
-
124
- /**
125
- * @param {JsxComponent} component
126
- */
127
- function __pwRender(component) {
128
- const componentFunc = __pwRegistry.get(component.type);
129
- const props = __renderProps(component.props || {});
130
- const children = component.children?.map(child => __renderChild(child)).filter(child => {
131
- if (typeof child === 'string')
132
- return !!child.trim();
133
- return true;
134
45
  });
135
- const reactChildren = Array.isArray(children) && children.length === 1 ? children[0] : children;
136
- return __pwReact.createElement(componentFunc || component.type, props, reactChildren);
137
46
  }
138
47
 
139
48
  window.playwrightMount = async (component, rootElement, hooksConfig) => {
140
- if (component.kind !== 'jsx')
49
+ if (!isJsxComponent(component))
141
50
  throw new Error('Object mount notation is not supported');
142
51
 
143
- await __pwResolveComponent(component);
144
52
  let App = () => __pwRender(component);
145
53
  for (const hook of window.__pw_hooks_before_mount || []) {
146
54
  const wrapper = await hook({ App, hooksConfig });
@@ -171,10 +79,9 @@ window.playwrightUnmount = async rootElement => {
171
79
  };
172
80
 
173
81
  window.playwrightUpdate = async (rootElement, component) => {
174
- if (component.kind !== 'jsx')
82
+ if (!isJsxComponent(component))
175
83
  throw new Error('Object mount notation is not supported');
176
84
 
177
- await __pwResolveComponent(component);
178
85
  const root = __pwRootRegistry.get(rootElement);
179
86
  if (root === undefined)
180
87
  throw new Error('Component was not mounted');