Skip to content

Commit

Permalink
Adds button-counter.
Browse files Browse the repository at this point in the history
This exercises `comp.listen` and event handling.
  • Loading branch information
dgp1130 committed Dec 28, 2023
1 parent e60bec2 commit abbadc3
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/demo/counter/button-counter.test.html
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>
51 changes: 51 additions & 0 deletions src/demo/counter/button-counter.test.ts
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');
});
});
});
17 changes: 17 additions & 0 deletions src/demo/counter/button-counter.ts
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>;
}
}
10 changes: 10 additions & 0 deletions src/demo/counter/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,15 @@ <h2>Auto Counter</h2>

<script src="./auto-counter.js" type="module"></script>
</auto-counter>

<button-counter>
<h2>Button Counter</h2>

<div>The current count is: <span>10</span>.</div>
<button id="decrement">-</button>
<button id="increment">+</button>

<script src="./button-counter.js" type="module"></script>
</button-counter>
</body>
</html>

0 comments on commit abbadc3

Please sign in to comment.