Learn JavaScript · lesson 47 of 60 · about 10 minutes
Capstone: Events and Testing
Connect the flashcard buttons with event listeners, and check the logic with automated tests.
What you'll learn
- Add event listeners
- Connect events to actions and re-render
- Test logic with node:assert
The buttons
Three buttons sit under Sam's flashcard: Flip, Knew it and Still learning. Each one gets a listener: Flip flips the card; Knew it marks the card as known and moves on; Still learning takes it out of known and moves on. After the action, every listener calls render(deck), so the page always shows the deck as it is now.
Listening for events
Buttons, keys and forms fire events. element.addEventListener("click", handler) runs the handler each time. Every handler follows the same pattern:
1. Call an action to change the state
2. Re-render
Node has the same EventTarget system as browsers, so we can try it here with pretend buttons.
Clicks change state, then re-render
const state = { flipped: false, known: 0 };
const render = () => console.log(`flipped=${state.flipped} known=${state.known}`);
const flipButton = new EventTarget();
const knewButton = new EventTarget();
flipButton.addEventListener("click", () => {
state.flipped = !state.flipped;
render();
});
knewButton.addEventListener("click", () => {
state.known++;
render();
});
flipButton.dispatchEvent(new Event("click"));
knewButton.dispatchEvent(new Event("click"));
flipButton.dispatchEvent(new Event("click"));Output
flipped=true known=0
flipped=true known=1
flipped=false known=1Real buttons
On Sam's page the buttons are real elements, found with document.querySelector. A visitor's tap runs the same listeners, and button.click() does it from code, which is how the tests press them.
A button on the page
let taps = 0;
const button = document.querySelector("#more");
button.addEventListener("click", () => {
taps++;
document.querySelector("#taps").textContent = `Tapped ${taps} times`;
});
button.click();
button.click();
console.log(document.querySelector("#taps").textContent);Output
Tapped 2 timesAutomated tests
Tests run your functions with known inputs and check the results. node:assert/strict throws if a check fails. Because the actions are separate from the DOM, they're easy to test.
Testing the actions
import assert from "node:assert/strict";
const next = (s) => ({ ...s, index: (s.index + 1) % s.total, flipped: false });
const score = (known, total) => Math.round((known / total) * 100);
assert.equal(next({ index: 0, total: 3 }).index, 1);
assert.equal(next({ index: 2, total: 3 }).index, 0);
assert.equal(next({ index: 0, total: 3, flipped: true }).flipped, false);
assert.equal(score(2, 3), 67);
assert.deepEqual([1, 2], [1, 2]);
console.log("All 5 tests passed");Output
All 5 tests passedNow try it yourself
This lesson is free in Codiqo, with 10 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.