Skip to content

Commit 2a15811

Browse files
committed
feat: events
1 parent 21dd633 commit 2a15811

File tree

6 files changed

+21
-11
lines changed

6 files changed

+21
-11
lines changed

Diff for: ct-web-lit/src/components/Button.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import { LitElement, html } from 'lit';
2-
import { customElement, property } from 'lit/decorators.js';
2+
import { customElement, eventOptions, property } from 'lit/decorators.js';
33

44
@customElement('pw-button')
55
export class Button extends LitElement {
66
@property({ type: String })
77
title = '';
88

9+
@eventOptions({ passive: true })
10+
onClick() {
11+
this.dispatchEvent(new CustomEvent('submit', { detail: 'hello' }));
12+
}
13+
914
render() {
10-
return html`<button>${this.title}</button>`;
15+
return html`<button @click=${this.onClick}>${this.title}</button>`;
1116
}
1217
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ test.fixme('update slots without remounting', async ({ mount }) => {
6262
await expect(component.locator('#remount-count')).toContainText('1');
6363
});
6464

65-
test.fixme('emit an submit event when the button is clicked', async ({ mount }) => {
65+
test('emit an submit event when the button is clicked', async ({ mount }) => {
6666
const messages: string[] = [];
6767
const component = await mount(Button, {
6868
props: {

Diff for: ct-web/src/components/Button.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
export class Button extends HTMLElement {
2+
constructor() {
3+
super();
4+
this.innerHTML = `<button>${this.title}</button>`;
5+
}
6+
27
set title(title: string) {
38
this.innerHTML = `<button>${title}</button>`
49
}
510

6-
constructor() {
7-
super();
8-
9-
this.innerHTML = `<button>${this.title}</button>`;
11+
connectedCallback() {
12+
this.addEventListener("click", () => {
13+
this.dispatchEvent(new CustomEvent('submit', { detail: 'hello' }));
14+
});
1015
}
1116
}
1217

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ test.fixme('update slots without remounting', async ({ mount }) => {
6262
await expect(component.locator('#remount-count')).toContainText('1');
6363
});
6464

65-
test.fixme('emit an submit event when the button is clicked', async ({ mount }) => {
65+
test('emit an submit event when the button is clicked', async ({ mount }) => {
6666
const messages: string[] = [];
6767
const component = await mount(Button, {
6868
props: {

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.4",
3+
"version": "0.0.5",
44
"description": "Playwright Component Testing for Web Components",
55
"homepage": "https://playwright.dev",
66
"engines": {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ function createComponent(component) {
4242

4343
const webComponent = new Component();
4444

45-
if (component.options?.on)
46-
throw new Error('events are not yet supported');
45+
for (const [key, listener] of Object.entries(component.options?.on || {}))
46+
webComponent.addEventListener(key, event => listener(/** @type {CustomEvent} */ (event).detail));
4747

4848
if (component.options?.slots)
4949
throw new Error('slots are not yet supported');

0 commit comments

Comments
 (0)