Skip to content

Commit

Permalink
fix(ct): vue3 fallback events
Browse files Browse the repository at this point in the history
  • Loading branch information
sand4rt committed Jun 11, 2023
1 parent dd9a496 commit 36ab9c2
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 19 deletions.
12 changes: 6 additions & 6 deletions packages/playwright-ct-vue/registerSource.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function isComponent(component) {
*/
async function __pwResolveComponent(component) {
if (!isComponent(component))
return
return;

let componentFactory = __pwLoaderRegistry.get(component.type);
if (!componentFactory) {
Expand All @@ -68,11 +68,11 @@ async function __pwResolveComponent(component) {
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 (componentFactory)
__pwRegistry.set(component.type, await componentFactory());

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

const __pwAllListeners = new Map();
Expand Down Expand Up @@ -217,7 +217,7 @@ function __pwWrapFunctions(slots) {
function __pwCreateWrapper(component) {
const { Component, props, slots, listeners } = __pwCreateComponent(component);
// @ts-ignore
const wrapper = __pwH(Component, props, slots);
const wrapper = __pwH(Component, { ...props, ...listeners }, slots);
__pwAllListeners.set(wrapper, listeners);
return wrapper;
}
Expand Down Expand Up @@ -281,7 +281,7 @@ window.playwrightUpdate = async (rootElement, component) => {

if (!wrapper.component)
throw new Error('Updating a native HTML element is not supported');

const { slots, listeners, props } = __pwCreateComponent(component);

wrapper.component.slots = __pwWrapFunctions(slots);
Expand Down
16 changes: 9 additions & 7 deletions tests/components/ct-vue-cli/src/components/Button.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<script lang="ts" setup>
defineProps({
title: {
type: String,
required: true
}
})
import { useAttrs } from 'vue';
defineProps<{ title: string }>();
const attrs = useAttrs();
</script>

<template>
<button @click="$emit('submit', 'hello')">{{ title }}</button>
<button
@click="$emit('submit', 'hello')"
@dblclick="() => attrs.dbclick('fallthroughEvent')"
>
{{ title }}
</button>
</template>
14 changes: 14 additions & 0 deletions tests/components/ct-vue-cli/tests/events/events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,17 @@ test('emit an submit event when the button is clicked', async ({ mount }) => {
await component.click();
expect(messages).toEqual(['hello']);
});

test('emit a falltrough event when the button is double clicked', async ({ mount }) => {
const messages: string[] = [];
const component = await mount(Button, {
props: {
title: 'Submit',
},
on: {
dbclick: (message: string) => messages.push(message),
},
});
await component.dblclick();
expect(messages).toEqual(['fallthroughEvent']);
});
14 changes: 14 additions & 0 deletions tests/components/ct-vue-cli/tests/events/events.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ test('emit an submit event when the button is clicked', async ({ mount }) => {
expect(messages).toEqual(['hello']);
});

test('emit a falltrough event when the button is double clicked', async ({ mount }) => {
const messages: string[] = [];
const component = await mount(
<Button
title="Submit"
v-on:dbclick={(message: string) => {
messages.push(message)
}}
/>
);
await component.dblclick();
expect(messages).toEqual(['fallthroughEvent']);
});

test('emit a event when a slot is clicked', async ({ mount }) => {
let clickFired = false;
const component = await mount(
Expand Down
11 changes: 9 additions & 2 deletions tests/components/ct-vue-vite/src/components/Button.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
<script lang="ts" setup>
defineProps<{ title: string }>()
import { useAttrs } from 'vue';
defineProps<{ title: string }>();
const attrs: Record<string, any> = useAttrs();
</script>

<template>
<button @click="$emit('submit', 'hello')">{{ title }}</button>
<button
@click="$emit('submit', 'hello')"
@dblclick="() => attrs.dbclick('fallthroughEvent')"
>
{{ title }}
</button>
</template>
16 changes: 15 additions & 1 deletion tests/components/ct-vue-vite/tests/events/events.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,23 @@ test('emit a submit event when the button is clicked', async ({ mount }) => {
title: 'Submit',
},
on: {
submit: (data) => messages.push(data),
submit: (message) => messages.push(message),
},
});
await component.click();
expect(messages).toEqual(['hello']);
});

test('emit a falltrough event when the button is double clicked', async ({ mount }) => {
const messages = [];
const component = await mount(Button, {
props: {
title: 'Submit',
},
on: {
dbclick: (message) => messages.push(message),
},
});
await component.dblclick();
expect(messages).toEqual(['fallthroughEvent']);
});
16 changes: 15 additions & 1 deletion tests/components/ct-vue-vite/tests/events/events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,23 @@ test('emit a submit event when the button is clicked', async ({ mount }) => {
title: 'Submit',
},
on: {
submit: (data: string) => messages.push(data),
submit: (message: string) => messages.push(message),
},
});
await component.click();
expect(messages).toEqual(['hello']);
});

test('emit a falltrough event when the button is double clicked', async ({ mount }) => {
const messages: string[] = [];
const component = await mount(Button, {
props: {
title: 'Submit',
},
on: {
dbclick: (message: string) => messages.push(message),
},
});
await component.dblclick();
expect(messages).toEqual(['fallthroughEvent']);
});
18 changes: 16 additions & 2 deletions tests/components/ct-vue-vite/tests/events/events.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,29 @@ test('emit a submit event when the button is clicked', async ({ mount }) => {
const component = await mount(
<Button
title="Submit"
v-on:submit={(data: string) => {
messages.push(data);
v-on:submit={(message: string) => {
messages.push(message);
}}
/>
);
await component.click();
expect(messages).toEqual(['hello']);
});

test('emit a falltrough event when the button is double clicked', async ({ mount }) => {
const messages: string[] = [];
const component = await mount(
<Button
title="Submit"
v-on:dbclick={(message: string) => {
messages.push(message)
}}
/>
);
await component.dblclick();
expect(messages).toEqual(['fallthroughEvent']);
});

test('emit a event when a slot is clicked', async ({ mount }) => {
let clickFired = false;
const component = await mount(
Expand Down

0 comments on commit 36ab9c2

Please sign in to comment.