Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/playwright-ct-vue2/hooks.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@
* limitations under the License.
*/

import { CombinedVueInstance, Vue } from 'vue/types/vue';

import { ComponentOptions } from 'vue';
import { CombinedVueInstance, Vue, VueConstructor } from 'vue/types/vue';

type JsonPrimitive = string | number | boolean | null;
type JsonValue = JsonPrimitive | JsonObject | JsonArray;
type JsonArray = JsonValue[];
type JsonObject = { [Key in string]?: JsonValue };

export declare function beforeMount<HooksConfig extends JsonObject>(
callback: (params: { hooksConfig: HooksConfig }) => Promise<void>
callback: (params: {
hooksConfig: HooksConfig,
Vue: VueConstructor<Vue>,
}) => Promise<void | ComponentOptions<Vue> & Record<string, unknown>>
): void;
export declare function afterMount<HooksConfig extends JsonObject>(
callback: (params: {
Expand Down
15 changes: 6 additions & 9 deletions packages/playwright-ct-vue2/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,14 @@ export interface MountOptions<
props?: Props;
slots?: Record<string, Slot> & { default?: Slot };
on?: Record<string, Function>;
hooksConfig?: JsonObject;
hooksConfig?: HooksConfig;
}

interface MountResult<
HooksConfig extends JsonObject,
Props extends Record<string, unknown>
> extends Locator {
unmount(): Promise<void>;
update(
options: Omit<MountOptions<HooksConfig, Props>, 'hooksConfig'>
): Promise<void>;
update(options: Omit<MountOptions<never, Props>, 'hooksConfig'>): Promise<void>;
}

interface MountResultJsx extends Locator {
Expand All @@ -70,15 +67,15 @@ export interface ComponentFixtures {
mount(component: JSX.Element): Promise<MountResultJsx>;
mount<HooksConfig extends JsonObject>(
component: any,
options?: MountOptions<HooksConfig, any>
): Promise<MountResult<HooksConfig, any>>;
options?: MountOptions<HooksConfig, Record<string, unknown>>
): Promise<MountResult<Record<string, unknown>>>;
mount<
HooksConfig extends JsonObject,
Props extends Record<string, unknown> = Record<string, unknown>
>(
component: any,
options: MountOptions<HooksConfig, any> & { props: Props }
): Promise<MountResult<HooksConfig, Props>>;
options: MountOptions<HooksConfig, never> & { props: Props }
): Promise<MountResult<Props>>;
}

export const test: TestType<
Expand Down
4 changes: 3 additions & 1 deletion packages/playwright-ct-vue2/registerSource.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,12 @@ const instanceKey = Symbol('instanceKey');
const wrapperKey = Symbol('wrapperKey');

window.playwrightMount = async (component, rootElement, hooksConfig) => {
let options = {};
for (const hook of /** @type {any} */(window).__pw_hooks_before_mount || [])
await hook({ hooksConfig });
options = await hook({ hooksConfig, Vue });

const instance = new Vue({
...options,
render: h => {
const wrapper = createWrapper(component, h);
/** @type {any} */ (rootElement)[wrapperKey] = wrapper;
Expand Down
1 change: 0 additions & 1 deletion tests/components/ct-vue-vite/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const config: PlaywrightTestConfig = {
retries: process.env.CI ? 2 : 0,
reporter: 'html',
use: {
ctTemplateDir: 'playwright',
trace: 'on-first-retry'
},
projects: [
Expand Down
3 changes: 2 additions & 1 deletion tests/components/ct-vue2-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
},
"dependencies": {
"core-js": "^3.8.3",
"vue": "^2.7.13"
"vue": "^2.7.13",
"vue-router": "^3.6.5"
},
"devDependencies": {
"@babel/core": "^7.12.16",
Expand Down
12 changes: 8 additions & 4 deletions tests/components/ct-vue2-cli/playwright/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import '../src/assets/index.css';
import { beforeMount, afterMount } from '@playwright/experimental-ct-vue2/hooks';
import Router from 'vue-router';
import { router } from '../src/router';
import '../src/assets/index.css';

export type hooksConfig = {
export type HooksConfig = {
route: string;
}

beforeMount(async ({ hooksConfig }) => {
beforeMount<HooksConfig>(async ({ Vue, hooksConfig }) => {
console.log(`Before mount: ${JSON.stringify(hooksConfig)}`);
Vue.use(Router as any); // TODO: remove any and fix the various installed conflicting Vue versions
return { router }
});

afterMount(async ({ instance }) => {
afterMount<HooksConfig>(async ({ instance }) => {
console.log(`After mount el: ${instance.$el.constructor.name}`);
});
32 changes: 7 additions & 25 deletions tests/components/ct-vue2-cli/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<HelloWorld msg="Welcome to Your Vue.js App"/>
<header>
<img alt="Vue logo" src="./assets/logo.png" width="60" height="60">
<router-link to="/">Login</router-link>
<router-link to="/dashboard">Dashboard</router-link>
</header>
<router-view />
</div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'

export default {
name: 'App',
components: {
HelloWorld
}
}
</script>

<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
</template>
58 changes: 0 additions & 58 deletions tests/components/ct-vue2-cli/src/components/HelloWorld.vue

This file was deleted.

13 changes: 9 additions & 4 deletions tests/components/ct-vue2-cli/src/main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import Vue from 'vue'
import App from './App.vue'
import Vue from 'vue';
import Router from 'vue-router';
import App from './App.vue';
import { router } from './router';
import './assets/index.css';

Vue.config.productionTip = false
Vue.config.productionTip = false;

Vue.use(Router);

new Vue({
router,
render: h => h(App),
}).$mount('#app')
}).$mount('#app');
24 changes: 17 additions & 7 deletions tests/components/ct-vue2-cli/src/notation-jsx.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { test, expect } from '@playwright/experimental-ct-vue2'
import Button from './components/Button.vue'
import Counter from './components/Counter.vue'
import DefaultSlot from './components/DefaultSlot.vue'
import NamedSlots from './components/NamedSlots.vue'
import EmptyTemplate from './components/EmptyTemplate.vue'
import type { hooksConfig } from '../playwright'
import App from './App.vue';
import Button from './components/Button.vue';
import Counter from './components/Counter.vue';
import DefaultSlot from './components/DefaultSlot.vue';
import NamedSlots from './components/NamedSlots.vue';
import EmptyTemplate from './components/EmptyTemplate.vue';
import type { HooksConfig } from '../playwright';

test.use({ viewport: { width: 500, height: 500 } })

Expand Down Expand Up @@ -121,7 +122,7 @@ test('emit a event when a slot is clicked', async ({ mount }) => {
test('run hooks', async ({ page, mount }) => {
const messages: string[] = []
page.on('console', m => messages.push(m.text()))
await mount<hooksConfig>(<Button title="Submit" />, {
await mount<HooksConfig>(<Button title="Submit" />, {
hooksConfig: { route: 'A' }
})
expect(messages).toEqual(['Before mount: {\"route\":\"A\"}', 'After mount el: HTMLButtonElement'])
Expand All @@ -140,3 +141,12 @@ test('get textContent of the empty template', async ({ mount }) => {
expect(await component.textContent()).toBe('');
await expect(component).toHaveText('');
});

test('navigate to a page by clicking a link', async ({ page, mount }) => {
const component = await mount(<App />);
await expect(component.getByRole('main')).toHaveText('Login');
await expect(page).toHaveURL('/');
await component.getByRole('link', { name: 'Dashboard' }).click();
await expect(component.getByRole('main')).toHaveText('Dashboard');
await expect(page).toHaveURL('/dashboard');
});
30 changes: 20 additions & 10 deletions tests/components/ct-vue2-cli/src/notation-vue.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { test, expect } from '@playwright/experimental-ct-vue2'
import Button from './components/Button.vue'
import Counter from './components/Counter.vue'
import DefaultSlot from './components/DefaultSlot.vue'
import NamedSlots from './components/NamedSlots.vue'
import Component from './components/Component.vue'
import EmptyTemplate from './components/EmptyTemplate.vue'
import type { hooksConfig } from '../playwright'
import { test, expect } from '@playwright/experimental-ct-vue2';
import App from './App.vue';
import Button from './components/Button.vue';
import Counter from './components/Counter.vue';
import DefaultSlot from './components/DefaultSlot.vue';
import NamedSlots from './components/NamedSlots.vue';
import Component from './components/Component.vue';
import EmptyTemplate from './components/EmptyTemplate.vue';
import type { HooksConfig } from '../playwright';

test.use({ viewport: { width: 500, height: 500 } })

test('render props', async ({ mount }) => {
test('render props', async ({ page, mount }) => {
const component = await mount(Button, {
props: {
title: 'Submit'
Expand Down Expand Up @@ -123,7 +124,7 @@ test('render a component without options', async ({ mount }) => {
test('run hooks', async ({ page, mount }) => {
const messages: string[] = []
page.on('console', m => messages.push(m.text()))
await mount<hooksConfig>(Button, {
await mount<HooksConfig>(Button, {
props: {
title: 'Submit'
},
Expand All @@ -149,3 +150,12 @@ test('get textContent of the empty template', async ({ mount }) => {
expect(await component.textContent()).toBe('');
await expect(component).toHaveText('');
});

test('navigate to a page by clicking a link', async ({ page, mount }) => {
const component = await mount(App);
await expect(component.getByRole('main')).toHaveText('Login');
await expect(page).toHaveURL('/');
await component.getByRole('link', { name: 'Dashboard' }).click();
await expect(component.getByRole('main')).toHaveText('Dashboard');
await expect(page).toHaveURL('/dashboard');
});
3 changes: 3 additions & 0 deletions tests/components/ct-vue2-cli/src/pages/DashboardPage.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<main>Dashboard</main>
</template>
3 changes: 3 additions & 0 deletions tests/components/ct-vue2-cli/src/pages/LoginPage.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<main>Login</main>
</template>
12 changes: 12 additions & 0 deletions tests/components/ct-vue2-cli/src/router/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Router from 'vue-router';
import LoginPage from '../pages/LoginPage.vue';
import DashboardPage from '../pages/DashboardPage.vue';

export const router = new Router({
mode: 'history',
base: '/',
routes: [
{ path: '/', component: LoginPage },
{ path: '/dashboard', component: DashboardPage }
]
});
4 changes: 2 additions & 2 deletions tests/components/ct-vue2-cli/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"extends": "./tsconfig.app.json",
"include": ["playwright"],
"exclude": [],
"include": ["playwright", "src/**/*"],
"compilerOptions": {
"allowJs": true,
"composite": true,
"lib": [],
"types": ["node"]
Expand Down