-
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.
This exercises `comp.listen` and event handling.
- Loading branch information
Showing
4 changed files
with
86 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>`button-counter` tests</title> | ||
<meta charset="utf8"> | ||
</head> | ||
<body></body> | ||
</html> |
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,51 @@ | ||
import { parseHtml } from 'hydroactive/testing.js'; | ||
import { ButtonCounter } from './button-counter.js'; | ||
|
||
describe('button-counter', () => { | ||
afterEach(() => { | ||
for (const node of document.body.childNodes) node.remove(); | ||
}); | ||
|
||
function render({ count }: { count: number }) { | ||
return parseHtml(ButtonCounter, ` | ||
<button-counter> | ||
<div>The current count is: <span>${count}</span>.</div> | ||
<button id="decrement">-</button> | ||
<button id="increment">+</button> | ||
</button-counter> | ||
`); | ||
} | ||
|
||
describe('ButtonCounter', () => { | ||
it('does not re-render on hydration', async () => { | ||
const el = render({ count: 5 }); | ||
document.body.appendChild(el); | ||
|
||
await el.stable(); | ||
|
||
expect(el.querySelector('span')!.textContent).toBe('5'); | ||
}); | ||
|
||
it('decrements on click', async () => { | ||
const el = render({ count: 5 }); | ||
document.body.appendChild(el); | ||
|
||
(el.querySelector('#decrement')! as HTMLElement).click(); | ||
|
||
await el.stable(); | ||
|
||
expect(el.querySelector('span')!.textContent).toBe('4'); | ||
}); | ||
|
||
it('increments on click', async () => { | ||
const el = render({ count: 5 }); | ||
document.body.appendChild(el); | ||
|
||
(el.querySelector('#increment')! as HTMLElement).click(); | ||
|
||
await el.stable(); | ||
|
||
expect(el.querySelector('span')!.textContent).toBe('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,17 @@ | ||
import { defineComponent } from 'hydroactive'; | ||
|
||
/** | ||
* A counter which increments and decrements the count based on button clicks. | ||
*/ | ||
export const ButtonCounter = defineComponent('button-counter', (comp) => { | ||
const count = comp.live('span', Number); | ||
|
||
comp.listen('#decrement', 'click', () => { count.set(count() - 1); }); | ||
comp.listen('#increment', 'click', () => { count.set(count() + 1); }); | ||
}); | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
'button-counter': InstanceType<typeof ButtonCounter>; | ||
} | ||
} |
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