At the end of the last course, your ~/checks/refund.mjs caught Nadia’s short refund, but only on your machine and only when you remembered to run it. Edsger’s tests run every time anyone types npm test. You’ve read them as examples, listed what they leave out, and checked how a change edits them. Now you’ll add to them.
In this lesson, you’ll turn issue #41 into four acceptance criteria and write or reuse a test for each one in test/refunds.test.js, on a branch of your own. You’ll also rewrite a test so that it checks what Nadia sees instead of how the code works it out. Start with what your saved check already says.
Turn the Check Into Criteria
Your check makes one claim. These are its last lines:
~/checks/refund.mjs
console.log('Expected: 600p');
console.log('Actual: ' + refund.amount + 'p');
process.exitCode = refund.amount === 600 ? 0 : 1;
Issue #41 asks for more than that one number. Open docs/issues/41.md again. Grace adds, “Don’t change what the customer originally paid,” and one of the invariants Ada listed in docs/ARCHITECTURE.md, “Total refunds never exceed the order’s payment,” holds for every order. A fix that refunds 600p and breaks either of those wouldn’t close the issue.
So write the whole request down as given/when/then criteria, as you did for the student discount. Add them to the refunds entry in your map.md:
1. Given Nadia's order (Small Maps, The Long Way Home and a
complimentary A Bookshop Window, READ20, 2400p paid),
when she returns Small Maps, then she gets 600p back.
2. Given an order of one Small Maps with no code (750p),
when it's returned, then the refund is 750p.
3. Given Nadia's order, when she returns Small Maps,
then what she paid is still 2400p.
4. Given Nadia's order, when she returns every book one
at a time, then the total refunded never exceeds 2400p.
Criterion 1 describes the bug. The other three say what must not change, and a fix can break them while it repairs the first.
Suppose a fix deletes if (!line.complimentary) on line 12 of src/orders/pricing.js, so pricing charges for A Bookshop Window. Small Maps then refunds 600p, but Nadia’s order totals 3200p, which breaks criterion 3. Returning every book sends back 3200p, which breaks criterion 4.
Before you write any tests, check what the suite already has. The first test in test/refunds.test.js, on line 8, already covers criterion 2. It orders one Small Maps with basket(), returns it, and asserts 750p. Write “criterion 2: line 8” next to the criterion in map.md, and don’t duplicate the existing test.
You might be tempted to put the other three in one test, “Nadia’s refund is right”, but one test per criterion is better. An assertion that fails stops its test at once, so a failure on the 600p line would hide whether the 2400p ones held. Give each test the name of its criterion, and the report reads as a checklist of what passed and what didn’t.