@jscutlery/playwright-ct-angular 0.6.3 → 0.6.5

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/register-source.mjs +37 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jscutlery/playwright-ct-angular",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
4
4
  "dependencies": {
5
5
  "@babel/core": "7.23.9",
6
6
  "@playwright/experimental-ct-core": "1.43.1",
@@ -37,4 +37,4 @@
37
37
  },
38
38
  "module": "./index.esm.js",
39
39
  "main": "./index.cjs.js"
40
- }
40
+ }
@@ -1,10 +1,9 @@
1
1
  import '@angular/compiler';
2
2
  import 'zone.js';
3
-
4
- import { getTestBed, TestBed } from '@angular/core/testing';
3
+ import { Component, reflectComponentType } from '@angular/core';
4
+ import { ComponentFixtureAutoDetect, getTestBed, TestBed, TestComponentRenderer } from '@angular/core/testing';
5
5
  import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
6
6
  import { Subscription } from 'rxjs';
7
- import { Component, reflectComponentType } from '@angular/core';
8
7
 
9
8
  /**
10
9
  * @typedef {{type: string} & import('./index').MountTemplateOptions} TemplateInfo
@@ -20,10 +19,7 @@ window.playwrightMount = async (component, rootElement, hooksConfig) => {
20
19
  for (const hook of window.__pw_hooks_before_mount || [])
21
20
  await hook({ hooksConfig, TestBed });
22
21
 
23
- const fixture = await __pwRenderComponent(component);
24
-
25
- fixture.autoDetectChanges();
26
- await fixture.whenStable();
22
+ const fixture = await __pwRenderComponent(component, rootElement);
27
23
 
28
24
  for (const hook of window.__pw_hooks_after_mount || [])
29
25
  await hook({ hooksConfig });
@@ -56,7 +52,6 @@ window.playwrightUpdate = async (rootElement, component) => {
56
52
  __pwUpdateProps(fixture, component);
57
53
  __pwUpdateEvents(fixture, component.on);
58
54
 
59
- fixture.autoDetectChanges();
60
55
  await fixture.whenStable();
61
56
  };
62
57
 
@@ -68,8 +63,9 @@ const __pwFixtureRegistry = new Map();
68
63
 
69
64
  /**
70
65
  * @param {ComponentInfo} component
66
+ * @param {Element} rootElement
71
67
  */
72
- async function __pwRenderComponent(component) {
68
+ async function __pwRenderComponent(component, rootElement) {
73
69
  /** @type {import('@angular/core').Type<unknown>} */
74
70
  let componentClass;
75
71
 
@@ -92,15 +88,22 @@ async function __pwRenderComponent(component) {
92
88
 
93
89
  TestBed.configureTestingModule({
94
90
  imports: [componentClass],
95
- providers: component.providers
91
+ providers: [
92
+ { provide: ComponentFixtureAutoDetect, useValue: true },
93
+ {
94
+ provide: TestComponentRenderer,
95
+ useValue: new PlaywrightTestComponentRenderer(rootElement)
96
+ },
97
+ ...component.providers ?? []
98
+ ]
96
99
  });
97
100
 
98
101
  const fixture = TestBed.createComponent(componentClass);
99
- fixture.nativeElement.id = 'root';
100
-
101
102
  __pwUpdateProps(fixture, component);
102
103
  __pwUpdateEvents(fixture, component.on);
103
104
 
105
+ await fixture.whenStable();
106
+
104
107
  return fixture;
105
108
  }
106
109
 
@@ -114,6 +117,7 @@ function __pwUpdateProps(fixture, componentInfo) {
114
117
 
115
118
  if (__pwIsTemplate(componentInfo)) {
116
119
  Object.assign(fixture.componentInstance, componentInfo.props);
120
+ fixture.detectChanges();
117
121
  } else {
118
122
  for (const [name, value] of Object.entries(componentInfo.props))
119
123
  fixture.componentRef.setInput(name, value);
@@ -146,3 +150,24 @@ function __pwUpdateEvents(fixture, events = {}) {
146
150
  function __pwIsTemplate(component) {
147
151
  return typeof component.type === 'string';
148
152
  }
153
+
154
+ class PlaywrightTestComponentRenderer extends TestComponentRenderer {
155
+
156
+ constructor(rootElement) {
157
+ super();
158
+ this._children = [];
159
+ this._rootElement = rootElement;
160
+ }
161
+
162
+ insertRootElement(testRootElementId) {
163
+ const testRootElement = document.createElement('div');
164
+ testRootElement.id = testRootElementId;
165
+ this._rootElement.appendChild(testRootElement);
166
+ }
167
+
168
+ removeAllRootElements() {
169
+ for (const child of this._children)
170
+ this._rootElement.removeChild(child);
171
+ this._children = [];
172
+ }
173
+ }