-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(view): add test for listeners binding
- Loading branch information
1 parent
d96ad8c
commit b66e939
Showing
4 changed files
with
91 additions
and
2 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
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,81 @@ | ||
import { test } from '@cofn/test-lib/client'; | ||
import { fromView, nextTick } from './utils.js'; | ||
|
||
const debug = document.getElementById('debug'); | ||
test('attribute starting with a @ is an event listener', ({ eq }) => { | ||
let count = 0; | ||
|
||
const listener = () => (count += 1); | ||
|
||
const el = fromView( | ||
({ html }) => | ||
() => | ||
html`<button @click="${listener}">click</button>`, | ||
); | ||
|
||
debug.appendChild(el); | ||
eq(count, 0); | ||
|
||
el.firstElementChild.click(); | ||
|
||
eq(count, 1); | ||
}); | ||
|
||
test('when updated, legacy listener is removed while new listener is attached', ({ | ||
eq, | ||
}) => { | ||
let count = 0; | ||
|
||
const listener1 = () => (count += 1); | ||
const listener2 = () => (count += 2); | ||
|
||
const el = fromView( | ||
({ html }) => | ||
({ onclick = listener1 }) => | ||
html`<button @click="${onclick}">click</button>`, | ||
); | ||
|
||
debug.appendChild(el); | ||
eq(count, 0); | ||
|
||
const button = el.firstElementChild; | ||
|
||
button.click(); | ||
|
||
eq(count, 1); | ||
|
||
el.render({ | ||
onclick: listener2, | ||
}); | ||
|
||
button.click(); | ||
|
||
eq(count, 3); | ||
}); | ||
|
||
test('when element is removed all listeners are removed', async ({ eq }) => { | ||
let count = 0; | ||
const listener = () => { | ||
count += 1; | ||
}; | ||
|
||
const el = fromView(({ html, $signal }) => { | ||
return () => html`<button @click="${listener}">click</button>`; | ||
}); | ||
|
||
debug.appendChild(el); | ||
eq(count, 0); | ||
|
||
const button = el.firstElementChild; | ||
button.click(); | ||
|
||
eq(count, 1); | ||
|
||
el.remove(); | ||
|
||
await nextTick(); | ||
|
||
button.click(); | ||
|
||
eq(count, 1); | ||
}); |
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