Skip to content

Commit 1c144cc

Browse files
committed
fix: override event listener on component.update
1 parent 4781024 commit 1c144cc

File tree

4 files changed

+45
-22
lines changed

4 files changed

+45
-22
lines changed

Diff for: ct-web-lit/src/tests.spec.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ test('update props without remounting', async ({ mount }) => {
3333
});
3434

3535
test('update event listeners without remounting', async ({ mount }) => {
36-
const component = await mount(Counter);
37-
3836
const messages: string[] = [];
37+
const component = await mount(Counter, {
38+
on: {
39+
submit: (data: string) => messages.push(data),
40+
},
41+
});
42+
3943
await component.update({
4044
on: {
4145
submit: (data: string) => messages.push(data),

Diff for: ct-web/src/tests.spec.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ test('update props without remounting', async ({ mount }) => {
3333
});
3434

3535
test('update event listeners without remounting', async ({ mount }) => {
36-
const component = await mount(Counter);
37-
3836
const messages: string[] = [];
37+
const component = await mount(Counter, {
38+
on: {
39+
submit: (data: string) => messages.push(data),
40+
},
41+
});
42+
3943
await component.update({
4044
on: {
4145
submit: (data: string) => messages.push(data),

Diff for: playwright-ct-web/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sand4rt/experimental-ct-web",
3-
"version": "0.0.13",
3+
"version": "0.0.14",
44
"description": "Playwright Component Testing for Web Components",
55
"homepage": "https://playwright.dev",
66
"repository": {

Diff for: playwright-ct-web/registerSource.mjs

+32-17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
/** @type {Map<string, FrameworkComponent>} */
88
const registry = new Map();
9+
const listeners = new Map();
910

1011
/**
1112
* @param {{[key: string]: FrameworkComponent}} components
@@ -23,14 +24,24 @@ function updateProps(webComponent, props = {}) {
2324
webComponent[key] = value;
2425
}
2526

27+
/**
28+
* @param {HTMLElement} webComponent
29+
*/
30+
function removeEvents(webComponent, events = {}) {
31+
for (const [key] of Object.entries(events)) {
32+
webComponent.removeEventListener(key, listeners.get(key));
33+
listeners.delete(key)
34+
}
35+
}
36+
2637
/**
2738
* @param {HTMLElement} webComponent
2839
*/
2940
function updateEvents(webComponent, events = {}) {
3041
for (const [key, listener] of Object.entries(events)) {
31-
webComponent.addEventListener(key, event =>
32-
listener(/** @type {CustomEvent} */ (event).detail)
33-
);
42+
const fn = event => listener(/** @type {CustomEvent} */ (event).detail);
43+
webComponent.addEventListener(key, fn)
44+
listeners.set(key, fn)
3445
}
3546
}
3647

@@ -81,7 +92,7 @@ function updateSlots(webComponent, slots = {}) {
8192
/**
8293
* @param {Component} component
8394
*/
84-
function createComponent(component) {
95+
function getComponent(component) {
8596
let Component = registry.get(component.type);
8697
if (!Component) {
8798
// Lookup by shorthand.
@@ -100,15 +111,28 @@ function createComponent(component) {
100111
}. Following components are registered: ${[...registry.keys()]}`
101112
);
102113

114+
return Component;
115+
}
116+
117+
window.playwrightMount = async (component, rootElement, hooksConfig) => {
103118
if (component.kind !== 'object')
104119
throw new Error('JSX mount notation is not supported');
105120

121+
const Component = getComponent(component);
122+
106123
const webComponent = new Component();
107124
updateProps(webComponent, component.options?.props);
108125
updateSlots(webComponent, component.options?.slots);
109126
updateEvents(webComponent, component.options?.on);
110-
return webComponent;
111-
}
127+
128+
for (const hook of window['__pw_hooks_before_mount'] || [])
129+
await hook({ hooksConfig });
130+
131+
rootElement.appendChild(webComponent);
132+
133+
for (const hook of window['__pw_hooks_after_mount'] || [])
134+
await hook({ hooksConfig });
135+
};
112136

113137
window.playwrightUpdate = async (rootElement, component) => {
114138
if (component.kind === 'jsx')
@@ -119,19 +143,10 @@ window.playwrightUpdate = async (rootElement, component) => {
119143

120144
updateProps(webComponent, component.options?.props);
121145
updateSlots(webComponent, component.options?.slots);
146+
removeEvents(webComponent, component.options?.on);
122147
updateEvents(webComponent, component.options?.on);
123148
};
124149

125150
window.playwrightUnmount = async (rootElement) => {
126151
rootElement.replaceChildren();
127-
};
128-
129-
window.playwrightMount = async (component, rootElement, hooksConfig) => {
130-
for (const hook of window['__pw_hooks_before_mount'] || [])
131-
await hook({ hooksConfig });
132-
133-
rootElement.appendChild(createComponent(component));
134-
135-
for (const hook of window['__pw_hooks_after_mount'] || [])
136-
await hook({ hooksConfig });
137-
};
152+
};

0 commit comments

Comments
 (0)