IntermediateLesson 12 of 128 min read

Capstone: The Finished Bakery Page, Tested and Audited

Build the complete Crumb & Co page with semantic HTML, responsive CSS and a script that renders the menu, then run it, test it with real events, audit it for semantics, accessibility and weight, and cross-check the audit independently.

In this lesson you will

  • Combine HTML, CSS and JavaScript
  • Test a page by running it
  • Audit semantics, accessibility and weight
  • Verify the audit independently

About 28 minutes

Everything in one page

The capstone is the whole page that Ravi has been building. It uses what the course taught: semantic landmarks and headings in order, mobile-first CSS with a grid and a media query, a menu rendered from data with textContent, a click handler on the list through delegation, a reservation form with built-in validation, and an accessible name for everything. Then the page is tested the way a professional would test it: the script runs, events are dispatched at real elements, and the result is audited by code.

Putting the page in a single file keeps the example small. A real project would split it into an HTML file, a CSS file and a JavaScript file, so that the browser can cache them separately, and it would add images. The checks below still apply. The audit has four parts: the outline of headings, the accessibility of images, fields and buttons, the contrast of the text colours, and the weight of the file, compressed. A second implementation in Python checks the same things from the serialised page.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Crumb &amp; Co bakery</title>
<style>
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; font: 1rem/1.5 system-ui, sans-serif; color: #3b2a1e; background: #fff8f0; }
header, main, footer { padding: 1rem; max-width: 60rem; margin: 0 auto; }
h1 { font-size: clamp(1.5rem, 1rem + 2.5vw, 3rem); margin: 0; }
nav a { color: #8b4513; margin-right: 1rem; }
.gallery { display: grid; gap: 16px; grid-template-columns: 1fr; padding: 0; list-style: none; }
.gallery li { border: 1px solid #8b4513; padding: 1rem; cursor: pointer; }
.gallery li.sold-out { color: #6b5b4d; text-decoration: line-through; }
@media (min-width: 600px) { .gallery { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 960px) { .gallery { grid-template-columns: repeat(3, 1fr); } }
</style>
</head>
<body>
<header><h1>Crumb &amp; Co</h1><nav aria-label="Main"><a href="#bread">Bread</a><a href="#reserve">Reserve</a></nav></header>
<main>
<h2 id="bread">Today's bread</h2>
<ul class="gallery" id="gallery"></ul>
<p id="total" aria-live="polite">Total: 0.00</p>
<h2 id="reserve">Reserve a loaf</h2>
<form id="order" novalidate>
<label for="name">Your name</label><input id="name" required minlength="2">
<label for="email">Email</label><input id="email" type="email" required>
<label for="qty">Quantity</label><input id="qty" type="number" min="1" max="10" value="1">
<button type="submit">Reserve</button>
<p id="msg" role="status"></p>
</form>
</main>
<footer><small>Made by Ravi. Open 7 to 3, Tuesday to Sunday.</small></footer>
<script>
const breads = [{ name: 'Sourdough', price: 4.5, left: 12 }, { name: 'Rye', price: 4, left: 3 }, { name: 'Baguette', price: 2.8, left: 0 }];
const gallery = document.getElementById('gallery'), total = document.getElementById('total');
let sum = 0;
for (const b of breads) {
  const li = document.createElement('li');
  li.textContent = b.name + ' - ' + b.price.toFixed(2);
  li.dataset.price = b.price;
  li.classList.toggle('sold-out', b.left === 0);
  gallery.append(li);
}
gallery.addEventListener('click', e => {
  const li = e.target.closest('li');
  if (!li || li.classList.contains('sold-out')) return;
  sum += Number(li.dataset.price);
  total.textContent = 'Total: ' + sum.toFixed(2);
});
const form = document.getElementById('order');
form.addEventListener('submit', e => {
  e.preventDefault();
  document.getElementById('msg').textContent = form.checkValidity() ? 'Reserved for ' + document.getElementById('name').value + '.' : 'Please fix the highlighted fields.';
});
</script>
</body>
</html>
const { JSDOM } = require('jsdom');
const zlib = require('zlib');
const html = require('fs').readFileSync('index.html', 'utf8');
const dom = new JSDOM(html, { runScripts: 'dangerously' });
const w = dom.window, d = w.document;
const results = [];
const test = (name, fn) => { try { fn(); results.push([name, 'pass']); } catch (e) { results.push([name, 'FAIL: ' + e.message]); } };
const eq = (a, b) => { if (JSON.stringify(a) !== JSON.stringify(b)) throw new Error(JSON.stringify(a) + ' !== ' + JSON.stringify(b)); };
const click = el => el.dispatchEvent(new w.MouseEvent('click', { bubbles: true }));
test('menu rendered from data', () => eq(d.querySelectorAll('#gallery li').length, 3));
test('sold out item marked', () => eq([...d.querySelectorAll('.sold-out')].map(x => x.textContent.split(' - ')[0]), ['Baguette']));
test('clicking adds to the total', () => { const li = d.querySelectorAll('#gallery li'); click(li[0]); click(li[1]); eq(d.getElementById('total').textContent, 'Total: 8.50'); });
test('sold out items cannot be added', () => { click(d.querySelectorAll('#gallery li')[2]); eq(d.getElementById('total').textContent, 'Total: 8.50'); });
test('empty form is invalid', () => { d.getElementById('order').dispatchEvent(new w.Event('submit', { bubbles: true, cancelable: true })); eq(d.getElementById('msg').textContent, 'Please fix the highlighted fields.'); });
test('valid form is accepted', () => { d.getElementById('name').value = 'Ravi'; d.getElementById('email').value = 'ravi@example.com'; d.getElementById('order').dispatchEvent(new w.Event('submit', { bubbles: true, cancelable: true })); eq(d.getElementById('msg').textContent, 'Reserved for Ravi.'); });
const final = '<!DOCTYPE html>' + d.documentElement.outerHTML;
const raw = Buffer.byteLength(html), gz = zlib.deflateSync(html, { level: 9 }).length;
results.forEach(r => console.log(r[0].padEnd(40), r[1]));
console.log('page size:', raw, 'bytes, compressed', gz, 'bytes');
console.log('DATA ' + JSON.stringify({ results, raw, gz, finalLen: final.length }));
menu rendered from data                  pass
sold out item marked                     pass
clicking adds to the total               pass
sold out items cannot be added           pass
empty form is invalid                    pass
valid form is accepted                   pass
page size: 2681 bytes, compressed 1318 bytes

The test run executed the real script of the page inside jsdom, so the menu was rendered by the code and not by hand, and then dispatched clicks and form submissions at the real elements. All six tests pass: “menu rendered from data”, “sold out item marked”, “clicking adds to the total”, “sold out items cannot be added”, “empty form is invalid”, “valid form is accepted”. The test of the total follows the arithmetic: clicking the sourdough and the rye adds 4.5 and 4.0, giving 8.50, and clicking the sold-out baguette changes nothing, because the handler ignores it. The form test shows both outcomes of the validation: the empty form is refused with a message, and the same form filled in is accepted.

The page is 2,681 bytes of HTML, CSS and JavaScript together, and 1,318 bytes after compression, well within the budget of 500 kilobytes from lesson 11 even before compression, and a few round trips to the server at most on a phone. Python’s zlib gave 1,313 bytes for the same file, within a few bytes of Node’s figure: two builds of the same algorithm can differ slightly in their output.

Independent audit: a second program, in Python, parsed the original file, with no scripts run, and confirmed the structure: the headings are [1, 2, 2], which means one h1 followed by two h2 headings and no gaps, there is exactly one main, the html element has a lang, the viewport meta tag is present, every input has a label linked with for, and the button has text. The contrast of the three colour pairs of the style sheet is 12.98 for the body text, 6.74 for the links and 6.17 for the sold-out text, all above 4.5 to 1, so the page passes AA for contrast, and the sold-out state is also shown by a line through the text, not only by colour.

The tests also caught a real bug while this lesson was written. The first draft of the script read the name with form.name.value, which looked natural. But form.name is the form’s own name property, not the input called name, so the message said “Reserved for undefined.” and the last test failed with exactly that text. The fix is to select the input explicitly with getElementById, or form.elements.name. This is why a page needs tests that run its real code.

Check by hand: 4.5 + 4.0 = 8.5, and the Baguette was not added because its data says 0 left, which the script turns into the sold-out class. The outline of headings is h1, h2, h2. The page has three landmarks, header, main and footer, which a screen reader lists for navigation.

Where to go next

  • Split the page into separate HTML, CSS and JavaScript files, and add real images with srcset and lazy loading.
  • Learn a framework such as React or Vue once the plain DOM feels comfortable, since the ideas are the same.
  • Learn the developer tools of your browser, especially the Elements, Network and Lighthouse panels.
  • Run an accessibility audit tool and test with a screen reader on a real page.
  • Publish the page on a host and test it on a real phone.
Tests in jsdom are not browser tests

jsdom is fast and good for logic, events and structure. It does not lay out the page, draw it or run every feature of a real browser, so test in the browsers your visitors use too.

Keep the audit in your project

Save the audit and test scripts with the page and run them before every change you publish. A check you run every time catches regressions that a one-time check does not.

About this course

Every program in this lesson was run while the course was written, with Node.js 22, jsdom 31 (a pure-JavaScript DOM used for tests, not a real browser) and Python 3.13. Browsers, tools and their menus change often, so check the official documentation (MDN, the Node.js docs, your provider) for current behaviour. You have now completed the Front-end development course.

Practice

Add a fourth bread whose name contains a less-than sign and check that it appears as text. Add a test for the minimum quantity. Change a colour to a pale grey and see the audit fail.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

Why run the page’s own script in the test?

  1. To test the real behaviour, not a copy
  2. To make it slower
  3. To avoid CSS
  4. It is required

Test the code that ships.

Why does clicking the sold-out item not change the total?

  1. The browser blocks it
  2. The price is zero
  3. The item is hidden
  4. The handler ignores items with the sold-out class

A rule in the click handler.

What do the audit results show about headings?

  1. Three h1
  2. Only h2
  3. No headings
  4. One h1 and two h2, no gaps

A clean outline.

Why audit in two languages?

  1. To double the page size
  2. To change the HTML
  3. To catch mistakes in one implementation
  4. It is a rule

Independent agreement.

What is jsdom not good for?

  1. Testing events
  2. Checking structure
  3. Checking layout and drawing
  4. Running scripts

Use a real browser for those.

Summary

  • A complete page combines semantic HTML, responsive CSS and behaviour in JavaScript.
  • Test pages by running them and dispatching real events.
  • Audit headings, accessibility, contrast and weight with code.
  • Verify the audit with an independent implementation, and test in real browsers.

Next lesson

You have finished the Front-end development course.

Questions

Can I use this page as a starting point?

Yes. Change the names, prices and colours, and keep the structure and tests. Keep checking contrast when you change colours.

Should I use a framework now?

Not necessarily. A small site is fine with plain HTML, CSS and JavaScript. A framework helps when the page has a lot of changing state.

How do I publish the page?

Upload the files to a web host or a static hosting service, as described in the Hosting and servers course, and test it on real devices. Check your provider's current instructions.

Progress is saved in this browser.