Skip to content

Commit

Permalink
updated the code to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
shairez committed Feb 1, 2024
1 parent 522a4f3 commit 8a2a76c
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 98 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"private": false,
"scripts": {
"build": "nx build playwright-ct-qwik",
"commit": "git-cz",
"format:fix": "pretty-quick --staged",
"prepare": "husky install"
Expand Down
1 change: 1 addition & 0 deletions packages/playwright-ct-qwik/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"targets": {
"build-mount": {
"executor": "@nx/js:tsc",
"inputs": ["{projectRoot}/src/**/*"],
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/packages/playwright-ct-qwik",
Expand Down
7 changes: 5 additions & 2 deletions packages/playwright-ct-qwik/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
* limitations under the License.
*/

const { program } = require('@playwright/experimental-ct-core/lib/program');
program.parse(process.argv);
// import { program } from '@playwright/experimental-ct-core/lib/program';

// program.parse(process.argv);

module.exports = require('@playwright/experimental-ct-core/cli');
28 changes: 0 additions & 28 deletions packages/playwright-ct-qwik/src/hooks.d.ts

This file was deleted.

23 changes: 17 additions & 6 deletions packages/playwright-ct-qwik/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Copyright (c) Microsoft Corporation.
*
Expand All @@ -14,16 +15,26 @@
* limitations under the License.
*/

const __pw_hooks_before_mount = [];
const __pw_hooks_after_mount = [];
import type { JSXOutput } from '@builder.io/qwik';
import type { JsonObject } from '@playwright/experimental-ct-core/types/component';

const __pw_hooks_before_mount: any[] = [];
const __pw_hooks_after_mount: any[] = [];

window.__pw_hooks_before_mount = __pw_hooks_before_mount;
window.__pw_hooks_after_mount = __pw_hooks_after_mount;

export const beforeMount = (callback) => {
export function beforeMount<HooksConfig extends JsonObject>(
callback: (params: {
hooksConfig?: HooksConfig;
App: () => JSXOutput;
}) => Promise<void | JSXOutput>,
): void {
__pw_hooks_before_mount.push(callback);
};
}

export const afterMount = (callback) => {
export function afterMount<HooksConfig extends JsonObject>(
callback: (params: { hooksConfig?: HooksConfig }) => Promise<void>,
): void {
__pw_hooks_after_mount.push(callback);
};
}
45 changes: 11 additions & 34 deletions packages/playwright-ct-qwik/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,4 @@
/* eslint-disable @typescript-eslint/no-var-requires */
// /* eslint-disable @typescript-eslint/no-var-requires */
// import {
// test as baseTest,
// devices,
// expect,
// defineConfig as originalDefineConfig,
// } from '@playwright/test';
// import path from 'path';

// const { fixtures } = require('@playwright/experimental-ct-core/lib/mount');

// process.env['NODE_ENV'] = 'test';

// function plugin() {
// const {
// createPlugin,
// } = require('@playwright/experimental-ct-core/lib/vitePlugin');
// return createPlugin(path.join(__dirname, 'registerSource.mjs'), () => {
// // import('').then((m) => m.default)
// });
// }

// const test = baseTest.extend(fixtures);
// const defineConfig = (config: any) =>
// originalDefineConfig({ ...config, _plugins: [plugin] });

// export { test, expect, devices, defineConfig };

// ---------- NEW STUFF ---------

/**
* Copyright (c) Microsoft Corporation.
Expand All @@ -45,17 +16,23 @@
* limitations under the License.
*/

const { test, expect, devices, defineConfig: originalDefineConfig } = require('@playwright/experimental-ct-core');
const {
test,
expect,
devices,
defineConfig: originalDefineConfig,
} = require('@playwright/experimental-ct-core');
import path from 'path';

const plugin = async () => {
// Only fetch upon request to avoid resolution in workers.
const { createPlugin } = require('@playwright/experimental-ct-core/plugin');
return createPlugin(
path.join(__dirname, 'register-source.mjs'),
() => import('@builder.io/qwik/optimizer').then(plugin => plugin.qwikVite()));
return createPlugin(path.join(__dirname, 'register-source.mjs'), () =>
import('@builder.io/qwik/optimizer').then((plugin) => plugin.qwikVite()),
);
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const defineConfig = (config: any, ...configs: any[]) => originalDefineConfig({ ...config, _plugins: [plugin] }, ...configs);
const defineConfig = (config: any, ...configs: any[]) =>
originalDefineConfig({ ...config, _plugins: [plugin] }, ...configs);

module.exports = { test, expect, devices, defineConfig };
15 changes: 10 additions & 5 deletions packages/playwright-ct-qwik/src/register-source.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Copyright (c) Microsoft Corporation.
*
Expand All @@ -19,6 +20,7 @@

import { JSXOutput, render } from '@builder.io/qwik';
import { QWIK_LOADER } from '@builder.io/qwik/loader';
import { Component } from '@playwright/experimental-ct-core/types/component';
/**
* @param {any} component
* @returns {component is JsxComponent}
Expand All @@ -39,14 +41,14 @@ addQwikLoader();
const __pwUnmountKey = Symbol('unmountKey');

window.playwrightMount = async (
component: JSXOutput,
component: Component,
rootElement: HTMLElement,
hooksConfig: unknown,
) => {
// if (!isJsxComponent(component))
// throw new Error('Object mount notation is not supported');

let componentToRender = component;
let componentToRender = component as JSXOutput;
for (const hook of window['__pw_hooks_before_mount'] || []) {
const wrapper = await hook({ component, hooksConfig });
if (wrapper) {
Expand All @@ -56,20 +58,23 @@ window.playwrightMount = async (

const { cleanup } = await render(rootElement, componentToRender);

rootElement[__pwUnmountKey] = cleanup;
(rootElement as any)[__pwUnmountKey] = cleanup;

for (const hook of window['__pw_hooks_after_mount'] || [])
await hook({ hooksConfig });
};

window.playwrightUnmount = async (rootElement) => {
const cleanup = rootElement[__pwcleanupKey];
const cleanup = (rootElement as any)[__pwUnmountKey];
if (!cleanup) throw new Error('Component was not mounted');

cleanup();
};

window.playwrightUpdate = async (rootElement, component) => {
window.playwrightUpdate = async (
rootElement: HTMLElement,
component: Component,
) => {
// if (!isJsxComponent(component))
// throw new Error('Object mount notation is not supported');

Expand Down
22 changes: 0 additions & 22 deletions packages/playwright-ct-qwik/src/register.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/playwright-ct-qwik/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"types": ["node"],
"importHelpers": false
},
"include": ["src/index.ts"],
"include": ["src/*.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
}

0 comments on commit 8a2a76c

Please sign in to comment.