Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
chore: bump playwright to 1.41.1 (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
sand4rt authored Jan 31, 2024
1 parent 1beebe6 commit c0f6668
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 97 deletions.
2 changes: 1 addition & 1 deletion ct-angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@angular-devkit/build-angular": "^15.1.0",
"@angular/cli": "~15.1.0",
"@angular/compiler-cli": "^15.1.0",
"@playwright/test": "1.41.0",
"@playwright/test": "1.41.1",
"@sand4rt/experimental-ct-angular": "workspace:*",
"typescript": "~4.9.4"
}
Expand Down
2 changes: 1 addition & 1 deletion ct-angular/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default defineConfig({
testDir: 'tests',
forbidOnly: !!process.env['CI'],
retries: process.env['CI'] ? 2 : 0,
reporter: 'html',
reporter: process.env['CI'] ? 'html' : 'line',
use: {
trace: 'on-first-retry',
ctViteConfig: {
Expand Down
13 changes: 7 additions & 6 deletions playwright-ct-angular/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sand4rt/experimental-ct-angular",
"version": "1.41.0",
"version": "1.41.1",
"description": "Playwright Component Testing for Angular",
"homepage": "https://playwright.dev",
"repository": {
Expand Down Expand Up @@ -40,9 +40,9 @@
}
},
"dependencies": {
"@analogjs/vite-plugin-angular": "0.2.28",
"@analogjs/vite-plugin-angular": "0.2.30",
"@angular-devkit/build-angular": "^16.1.0",
"@playwright/experimental-ct-core": "1.41.0"
"@playwright/experimental-ct-core": "1.41.1"
},
"devDependencies": {
"@angular/animations": "^16.1.7",
Expand All @@ -53,21 +53,22 @@
"@angular/platform-browser": "^16.1.7",
"@angular/platform-browser-dynamic": "^16.1.7",
"@angular/router": "^16.1.7",
"@playwright/test": "1.41.0",
"@playwright/test": "1.41.1",
"rxjs": "~7.8.1",
"tslib": "^2.5.0",
"typescript": "^5.0.4",
"zone.js": "~0.13.1"
},
"peerDependencies": {
"@playwright/test": ">=1.41.0",
"@playwright/test": ">=1.41.1",
"typescript": ">=4.9.3",
"@angular/common": ">=15.1.0 || >=16.0.0",
"@angular/platform-browser": ">=15.1.0 || >=16.0.0",
"@angular/router": ">=15.1.0 || >=16.0.0",
"@angular/core": ">=15.1.0 || >=16.0.0"
},
"bin": {
"playwright": "./cli.js"
"playwright": "./cli.js",
"pw-angular": "cli.js"
}
}
80 changes: 13 additions & 67 deletions playwright-ct-angular/registerSource.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ import { Router } from '@angular/router';
/** @typedef {import('@playwright/experimental-ct-core/types/component').ObjectComponent} ObjectComponent */
/** @typedef {import('@angular/core').Type} FrameworkComponent */

/** @type {Map<string, () => Promise<FrameworkComponent>>} */
const __pwLoaderRegistry = new Map();
/** @type {Map<string, FrameworkComponent>} */
const __pwRegistry = new Map();
/** @type {Map<string, import('@angular/core/testing').ComponentFixture>} */
const __pwFixtureRegistry = new Map();

Expand All @@ -40,50 +36,6 @@ getTestBed().initTestEnvironment(
platformBrowserDynamicTesting(),
);

/**
* @param {{[key: string]: () => Promise<FrameworkComponent>}} components
*/
export function pwRegister(components) {
for (const [name, value] of Object.entries(components))
__pwLoaderRegistry.set(name, value);
}

/**
* @param {Component} component
* @returns {component is JsxComponent | ObjectComponent}
*/
function isComponent(component) {
return !(typeof component !== 'object' || Array.isArray(component));
}

/**
* @param {Component} component
*/
async function __pwResolveComponent(component) {
if (!isComponent(component))
return

let componentFactory = __pwLoaderRegistry.get(component.type);
if (!componentFactory) {
// Lookup by shorthand.
for (const [name, value] of __pwLoaderRegistry) {
if (component.type.endsWith(`_${name}`)) {
componentFactory = value;
break;
}
}
}

if (!componentFactory && component.type[0].toUpperCase() === component.type[0])
throw new Error(`Unregistered component: ${component.type}. Following components are registered: ${[...__pwRegistry.keys()]}`);

if(componentFactory)
__pwRegistry.set(component.type, await componentFactory())

if ('children' in component)
await Promise.all(component.children.map(child => __pwResolveComponent(child)))
}

/**
* @param {import('@angular/core/testing').ComponentFixture} fixture
*/
Expand Down Expand Up @@ -154,17 +106,10 @@ function __pwCreateSlot(value) {
}

/**
* @param {Component} component
* @param {ObjectComponent} component
*/
async function __pwRenderComponent(component) {
const Component = __pwRegistry.get(component.type);
if (!Component)
throw new Error(`Unregistered component: ${component.type}. Following components are registered: ${[...__pwRegistry.keys()]}`);

if (component.kind !== 'object')
throw new Error('JSX mount notation is not supported');

const componentMetadata = reflectComponentType(Component);
const componentMetadata = reflectComponentType(component.type);
if (!componentMetadata?.isStandalone)
throw new Error('Only standalone components are supported');

Expand All @@ -174,30 +119,32 @@ async function __pwRenderComponent(component) {
})(class {});

TestBed.configureTestingModule({
imports: [Component],
imports: [component.type],
declarations: [WrapperComponent]
});

await TestBed.compileComponents();

__pwUpdateSlots(WrapperComponent, component.options?.slots, componentMetadata.selector);
__pwUpdateSlots(WrapperComponent, component.slots, componentMetadata.selector);

// TODO: only inject when router is provided
TestBed.inject(Router).initialNavigation();

const fixture = TestBed.createComponent(WrapperComponent);
fixture.nativeElement.id = 'root';

__pwUpdateProps(fixture, component.options?.props);
__pwUpdateEvents(fixture, component.options?.on);
__pwUpdateProps(fixture, component.props);
__pwUpdateEvents(fixture, component.on);

fixture.autoDetectChanges();

return fixture;
}

window.playwrightMount = async (component, rootElement, hooksConfig) => {
await __pwResolveComponent(component);
if (component.__pw_type === 'jsx')
throw new Error('JSX mount notation is not supported');

for (const hook of window.__pw_hooks_before_mount || [])
await hook({ hooksConfig, TestBed });

Expand All @@ -219,19 +166,18 @@ window.playwrightUnmount = async rootElement => {
};

window.playwrightUpdate = async (rootElement, component) => {
await __pwResolveComponent(component);
if (component.kind === 'jsx')
if (component.__pw_type === 'jsx')
throw new Error('JSX mount notation is not supported');

if (component.options?.slots)
if (component.slots)
throw new Error('Update slots is not supported yet');

const fixture = __pwFixtureRegistry.get(rootElement.id);
if (!fixture)
throw new Error('Component was not mounted');

__pwUpdateProps(fixture, component.options?.props);
__pwUpdateEvents(fixture, component.options?.on);
__pwUpdateProps(fixture, component.props);
__pwUpdateEvents(fixture, component.on);

fixture.detectChanges();
};
Loading

0 comments on commit c0f6668

Please sign in to comment.