-
Notifications
You must be signed in to change notification settings - Fork 789
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(runtime): ensure event listener are not registered twice
- Loading branch information
1 parent
6331d9a
commit f05a2e7
Showing
5 changed files
with
102 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
:host { | ||
display: block; | ||
padding: 5px; | ||
background: bisque; | ||
cursor: pointer; | ||
max-width: 300px; | ||
} | ||
:host(:focus) { | ||
outline: 2px solid blue; | ||
} |
49 changes: 49 additions & 0 deletions
49
test/wdio/event-listener-capture/event-re-register.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// @ts-expect-error will be resolved by WDIO | ||
import { defineCustomElement } from '/test-components/event-re-register.js'; | ||
|
||
defineCustomElement(); | ||
|
||
describe('event-listener-capture using lazy load components', function () { | ||
const eventListenerCaptureCmp = () => $('event-re-register'); | ||
|
||
afterEach(() => { | ||
const elem = document.querySelector('event-re-register') as HTMLElement; | ||
if (elem) { | ||
elem.remove(); | ||
} | ||
}); | ||
|
||
it('should only attach keydown event listener once', async () => { | ||
const elem = document.createElement('event-re-register') as HTMLElement; | ||
document.body.appendChild(elem); | ||
|
||
const reattach = eventListenerCaptureCmp(); | ||
await expect(reattach).toBePresent(); | ||
|
||
// focus on element | ||
await reattach.click(); | ||
await browser.action('key').down('a').pause(100).up('a').perform(); | ||
await browser.action('key').down('a').pause(100).up('a').perform(); | ||
await browser.action('key').down('a').pause(100).up('a').perform(); | ||
|
||
// check if event fired 3 times | ||
await expect(reattach).toHaveText( | ||
expect.stringContaining('Event fired times: 3')); | ||
|
||
// remove node from DOM | ||
elem.remove(); | ||
|
||
// reattach node to DOM | ||
document.body.appendChild(elem); | ||
|
||
// retrigger event | ||
await reattach.click(); | ||
await browser.action('key').down('a').pause(100).up('a').perform(); | ||
await browser.action('key').down('a').pause(100).up('a').perform(); | ||
await browser.action('key').down('a').pause(100).up('a').perform(); | ||
|
||
// check if event fired 6 times | ||
await expect(reattach).toHaveText( | ||
expect.stringContaining('Event fired times: 6')); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Component, ComponentInterface, h, Host, Listen, State } from '@stencil/core'; | ||
|
||
@Component({ | ||
tag: 'event-re-register', | ||
styleUrl: 'event-re-register.css', | ||
shadow: true, | ||
}) | ||
export class EventReRegister implements ComponentInterface { | ||
@State() eventFiredTimes: number = 0; | ||
@Listen('keydown') | ||
handleKeydown(event: KeyboardEvent) { | ||
this.eventFiredTimes++; | ||
console.log(event); | ||
} | ||
|
||
connectedCallback() { | ||
console.log('connected'); | ||
} | ||
disconnectedCallback() { | ||
console.log('disconnected'); | ||
} | ||
render() { | ||
return <Host tabindex="1"> | ||
<ul id="reattach"> | ||
<li>Focus this component;</li> | ||
<li>Press key;</li> | ||
<li>See console output</li> | ||
<li>Press 'Reconnect' button</li> | ||
<li>Repeat steps 1-3</li> | ||
</ul> | ||
<p>Event fired times: {this.eventFiredTimes}</p> | ||
</Host>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters