Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ct): class and object components #30269

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion packages/playwright-ct-core/src/tsxTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { setTransformData } from 'playwright/lib/transform/transform';
const t: typeof T = types;

let jsxComponentNames: Set<string>;
let classComponentNames: Set<string>;
let importInfos: Map<string, ImportInfo>;

export default declare((api: BabelAPI) => {
Expand All @@ -32,6 +33,7 @@ export default declare((api: BabelAPI) => {
Program: {
enter(path) {
jsxComponentNames = collectJsxComponentUsages(path.node);
classComponentNames = collectClassMountUsages(path.node);
importInfos = new Map();
},
exit(path) {
Expand Down Expand Up @@ -93,7 +95,7 @@ export default declare((api: BabelAPI) => {
if (t.isImportNamespaceSpecifier(specifier))
continue;
const { localName, info } = importInfo(importNode, specifier, this.filename!);
if (jsxComponentNames.has(localName)) {
if (jsxComponentNames.has(localName) || classComponentNames.has(localName)) {
importInfos.set(localName, info);
++importCount;
}
Expand Down Expand Up @@ -141,6 +143,23 @@ function collectJsxComponentUsages(node: T.Node): Set<string> {
return names;
}

function collectClassMountUsages(node: T.Node): Set<string> {
const names = new Set<string>();
traverse(node, {
enter: p => {
// Treat calls to mount and all identifiers in arguments as component usages e.g. mount(Component)
if (t.isCallExpression(p.node) && t.isIdentifier(p.node.callee) && p.node.callee.name === 'mount') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We used to have this as you probably know. But the isIdentifier(p.node.callee) felt overly restrictive and limited us a couple of times.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how? What approach would you recommend to address this? In my opinion, relying solely on file extension is not a viable option.

p.traverse({
Identifier: p => {
names.add(p.node.name);
}
});
}
}
});
return names;
}

export type ImportInfo = {
id: string;
filename: string;
Expand Down
10 changes: 10 additions & 0 deletions tests/components/ct-vue-vite/src/components/Story.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineComponent, h } from 'vue';

export const Story = defineComponent(
(props) => {
return () => h('div', props.title);
},
{
props: ['title'],
}
);
10 changes: 10 additions & 0 deletions tests/components/ct-vue-vite/tests/render/render.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { test, expect } from '@playwright/experimental-ct-vue';
import Button from '@/components/Button.vue';
import EmptyTemplate from '@/components/EmptyTemplate.vue';
import Component from '@/components/Component.vue';
import { Story } from '@/components/Story';

test('render props', async ({ mount }) => {
const component = await mount(Button, {
Expand All @@ -23,3 +24,12 @@ test('render a component without options', async ({ mount }) => {
const component = await mount(Component);
await expect(component).toContainText('test');
});

test('render props with defineComponent syntax', async ({ mount }) => {
const component = await mount(Story, {
props: {
title: 'story/wrapper'
}
});
await expect(component).toContainText('story/wrapper');
});
10 changes: 10 additions & 0 deletions tests/components/ct-vue-vite/tests/render/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { test, expect } from '@playwright/experimental-ct-vue';
import Button from '@/components/Button.vue';
import EmptyTemplate from '@/components/EmptyTemplate.vue';
import Component from '@/components/Component.vue';
import { Story } from '@/components/Story';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How typical is this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends on the app, but I don't it's that typical; its useful when a wrapper/story needs to be created to test components, where Playwright cannot serialise the data sent to the browser (such as unserialisable props). It's possible to create multiple .vue files but that's quite cumbersome.

There are additional use cases where importing components from a .ts file would be beneficial tho, such as supporting Angular and web components (without relying on a specific file extension).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This use case is very typical for Vue and is not possible without this PR: #31217


test('render props', async ({ mount }) => {
const component = await mount(Button, {
Expand All @@ -23,3 +24,12 @@ test('render a component without options', async ({ mount }) => {
const component = await mount(Component);
await expect(component).toContainText('test');
});

test('render props with defineComponent syntax', async ({ mount }) => {
const component = await mount(Story, {
props: {
title: 'story/wrapper'
}
});
await expect(component).toContainText('story/wrapper');
});
6 changes: 6 additions & 0 deletions tests/components/ct-vue-vite/tests/render/render.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { test, expect } from '@playwright/experimental-ct-vue';
import Button from '@/components/Button.vue';
import EmptyTemplate from '@/components/EmptyTemplate.vue';
import { Story } from '@/components/Story';

test('render props', async ({ mount }) => {
const component = await mount(<Button title='Submit' />);
Expand All @@ -19,3 +20,8 @@ test('render an empty component', async ({ page, mount }) => {
expect(await component.textContent()).toBe('');
await expect(component).toHaveText('');
});

test('render props with defineComponent syntax', async ({ mount }) => {
const component = await mount(<Story title="story/wrapper" />);
await expect(component).toContainText('story/wrapper');
});