@jscutlery/playwright-ct-angular 0.8.0 → 0.9.0

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 +1 -1
  2. package/register-source.mjs +38 -34
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jscutlery/playwright-ct-angular",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "dependencies": {
5
5
  "@babel/core": "7.23.9",
6
6
  "@playwright/experimental-ct-core": "1.47.1",
@@ -1,7 +1,13 @@
1
1
  import { Component, reflectComponentType } from '@angular/core';
2
- import { ComponentFixtureAutoDetect, getTestBed, TestBed, TestComponentRenderer } from '@angular/core/testing';
3
- import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
4
- import { Subscription } from 'rxjs';
2
+ import {
3
+ getTestBed,
4
+ TestBed,
5
+ TestComponentRenderer,
6
+ } from '@angular/core/testing';
7
+ import {
8
+ BrowserDynamicTestingModule,
9
+ platformBrowserDynamicTesting,
10
+ } from '@angular/platform-browser-dynamic/testing';
5
11
 
6
12
  /**
7
13
  * @typedef {{type: string} & import('./index').MountTemplateOptions} TemplateInfo
@@ -10,10 +16,20 @@ import { Subscription } from 'rxjs';
10
16
 
11
17
  getTestBed().initTestEnvironment(
12
18
  BrowserDynamicTestingModule,
13
- platformBrowserDynamicTesting()
19
+ platformBrowserDynamicTesting(),
14
20
  );
15
21
 
16
22
  window.playwrightMount = async (component, rootElement, hooksConfig) => {
23
+ TestBed.configureTestingModule({
24
+ providers: [
25
+ {
26
+ provide: TestComponentRenderer,
27
+ useValue: new PlaywrightTestComponentRenderer(rootElement),
28
+ },
29
+ ...(component.providers ?? []),
30
+ ],
31
+ });
32
+
17
33
  for (const hook of window.__pw_hooks_before_mount || [])
18
34
  await hook({ hooksConfig, TestBed });
19
35
 
@@ -27,11 +43,12 @@ window.playwrightMount = async (component, rootElement, hooksConfig) => {
27
43
 
28
44
  window.playwrightUnmount = async (rootElement) => {
29
45
  const fixture = __pwFixtureRegistry.get(rootElement.id);
30
- if (!fixture)
31
- throw new Error('Component was not mounted');
46
+ if (!fixture) throw new Error('Component was not mounted');
32
47
 
33
48
  /* Unsubscribe from all outputs. */
34
- for (const subscription of Object.values(__pwOutputSubscriptionRegistry.get(fixture) ?? {}))
49
+ for (const subscription of Object.values(
50
+ __pwOutputSubscriptionRegistry.get(fixture) ?? {},
51
+ ))
35
52
  subscription?.unsubscribe();
36
53
  __pwOutputSubscriptionRegistry.delete(fixture);
37
54
 
@@ -44,8 +61,7 @@ window.playwrightUnmount = async (rootElement) => {
44
61
  */
45
62
  window.playwrightUpdate = async (rootElement, component) => {
46
63
  const fixture = __pwFixtureRegistry.get(rootElement.id);
47
- if (!fixture)
48
- throw new Error('Component was not mounted');
64
+ if (!fixture) throw new Error('Component was not mounted');
49
65
 
50
66
  __pwUpdateProps(fixture, component);
51
67
  __pwUpdateEvents(fixture, component.on);
@@ -68,37 +84,28 @@ async function __pwRenderComponent(component, rootElement) {
68
84
  let componentClass;
69
85
 
70
86
  if (__pwIsTemplate(component)) {
71
- const templateInfo = /** @type {TemplateInfo} */(component);
87
+ const templateInfo = /** @type {TemplateInfo} */ (component);
72
88
  componentClass = Component({
73
89
  standalone: true,
74
90
  selector: 'pw-template-component',
75
91
  imports: templateInfo.imports,
76
- template: templateInfo.type
77
- })(class {
78
- });
92
+ template: templateInfo.type,
93
+ })(class {});
79
94
  } else {
80
- componentClass = /** @type {import('@angular/core').Type<unknown>} */(component.type);
95
+ componentClass = /** @type {import('@angular/core').Type<unknown>} */ (
96
+ component.type
97
+ );
81
98
  }
82
99
 
83
100
  const componentMetadata = reflectComponentType(componentClass);
84
101
  if (!componentMetadata?.isStandalone)
85
102
  throw new Error('Only standalone components are supported');
86
103
 
87
- TestBed.configureTestingModule({
88
- imports: [componentClass],
89
- providers: [
90
- { provide: ComponentFixtureAutoDetect, useValue: true },
91
- {
92
- provide: TestComponentRenderer,
93
- useValue: new PlaywrightTestComponentRenderer(rootElement)
94
- },
95
- ...component.providers ?? []
96
- ]
97
- });
98
-
99
104
  const fixture = TestBed.createComponent(componentClass);
100
105
  __pwUpdateProps(fixture, component);
101
106
  __pwUpdateEvents(fixture, component.on);
107
+ /* TODO: switch back to ComponentFixtureAutoDetect once this is fixed https://github.com/angular/angular/issues/57856 */
108
+ fixture.autoDetectChanges();
102
109
 
103
110
  await fixture.whenStable();
104
111
 
@@ -110,8 +117,7 @@ async function __pwRenderComponent(component, rootElement) {
110
117
  * @param {ComponentInfo} componentInfo
111
118
  */
112
119
  function __pwUpdateProps(fixture, componentInfo) {
113
- if (!componentInfo.props)
114
- return;
120
+ if (!componentInfo.props) return;
115
121
 
116
122
  if (__pwIsTemplate(componentInfo)) {
117
123
  Object.assign(fixture.componentInstance, componentInfo.props);
@@ -133,9 +139,9 @@ function __pwUpdateEvents(fixture, events = {}) {
133
139
  outputSubscriptionRecord[name]?.unsubscribe();
134
140
 
135
141
  /* Store new subscription. */
136
- outputSubscriptionRecord[name] = fixture.componentInstance[
137
- name
138
- ].subscribe((/** @type {unknown} */ event) => listener(event));
142
+ outputSubscriptionRecord[name] = fixture.componentInstance[name].subscribe(
143
+ (/** @type {unknown} */ event) => listener(event),
144
+ );
139
145
  }
140
146
 
141
147
  /* Update output subscription registry. */
@@ -150,7 +156,6 @@ function __pwIsTemplate(component) {
150
156
  }
151
157
 
152
158
  class PlaywrightTestComponentRenderer extends TestComponentRenderer {
153
-
154
159
  constructor(rootElement) {
155
160
  super();
156
161
  this._children = [];
@@ -164,8 +169,7 @@ class PlaywrightTestComponentRenderer extends TestComponentRenderer {
164
169
  }
165
170
 
166
171
  removeAllRootElements() {
167
- for (const child of this._children)
168
- this._rootElement.removeChild(child);
172
+ for (const child of this._children) this._rootElement.removeChild(child);
169
173
  this._children = [];
170
174
  }
171
175
  }