@jscutlery/playwright-ct-angular 0.1.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.
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@jscutlery/playwright-ct-angular",
3
+ "version": "0.1.0",
4
+ "dependencies": {
5
+ "jest-mock": "^28.1.3",
6
+ "rxjs": "^7.5.7",
7
+ "vite": "^3.2.1"
8
+ },
9
+ "peerDependencies": {
10
+ "@angular/core": "*",
11
+ "@angular/platform-browser": "*",
12
+ "@angular/platform-browser-dynamic": "*",
13
+ "@playwright/test": "^1.28.0",
14
+ "tslib": "*"
15
+ },
16
+ "module": "./index.js",
17
+ "main": "./index.cjs",
18
+ "type": "module",
19
+ "types": "./index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./index.d.ts",
23
+ "import": "./index.js",
24
+ "require": "./index.cjs"
25
+ }
26
+ }
27
+ }
@@ -0,0 +1,62 @@
1
+ import '@angular/compiler';
2
+ import 'zone.js';
3
+ import { getTestBed, TestBed } from '@angular/core/testing';
4
+ import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
5
+ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
6
+ import { Subscription } from 'rxjs';
7
+
8
+ const _componentRegistryMap = new Map();
9
+
10
+ getTestBed().initTestEnvironment(
11
+ BrowserDynamicTestingModule,
12
+ platformBrowserDynamicTesting()
13
+ );
14
+
15
+ /**
16
+ * This is the function used by Playwright CT to map each component to its symbol.
17
+ * The key here is a slug generated by Playwright CT based on the component's module path + component's name.
18
+ */
19
+ function register(components) {
20
+ for (const [name, value] of Object.entries(components)) {
21
+ _componentRegistryMap.set(name, value);
22
+ }
23
+ }
24
+
25
+ /**
26
+ * Container subscription holding all outputs subscription
27
+ * in order to unsubscribe when component is unmounted.
28
+ */
29
+ let subscription;
30
+
31
+ globalThis.playwrightMount = async (component, rootElement, hooksConfig) => {
32
+ const cmpType = _componentRegistryMap.get(component.type);
33
+
34
+ subscription = new Subscription();
35
+
36
+ TestBed.configureTestingModule({
37
+ imports: [BrowserAnimationsModule]
38
+ });
39
+ const fixture = TestBed.createComponent(cmpType);
40
+
41
+ /* Set inputs. */
42
+ for (const [name, value] of Object.entries(component.options.inputs ?? {})) {
43
+ fixture.componentInstance[name] = value;
44
+ }
45
+
46
+ /* Subscribe to outputs. */
47
+ for (const [name, callback] of Object.entries(component.options.outputs ?? {})) {
48
+ subscription.add(fixture.componentInstance[name].subscribe(callback));
49
+ }
50
+
51
+ fixture.autoDetectChanges();
52
+ };
53
+
54
+ globalThis.playwrightUnmount = async (rootElement) => {
55
+ subscription.unsubscribe();
56
+ subscription = null;
57
+ getTestBed().resetTestingModule();
58
+ };
59
+
60
+ globalThis.playwrightUpdate = async (rootElement, component) => {
61
+ rootElement.innerHTML = component;
62
+ };