Skip to content

test(theme): cover the page-load script and the switch - #371

Merged
yasinmiran merged 3 commits into
mainfrom
test/theme-persistence
Nov 17, 2026
Merged

yasinmiran merged 3 commits into
mainfrom
test/theme-persistence

Conversation

@yasinmiran

@yasinmiran yasinmiran commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up to #370, which fixed the theme switch not holding between pages. That PR was merged before I had tests for it, so they come here.

What I changed

  • src/lib/theme-init.js (new): the script that sets the theme before the page paints, moved out of src/layouts/base.astro without changing it. Inside the layout no test could run it. The layout now inserts the file as text, and the built page gets the same plain blocking script in <head> as before.
  • src/lib/theme-init.test.ts: runs that same script text over every pairing of stored choice and device theme, plus a page restored by the back button (twice in a row), a page shown after a normal load, and blocked storage.
  • src/components/theme-toggle.test.tsx: mounts the switch and covers the click in both directions, a theme change made outside the button, and blocked storage. Each case checks the label and which icon is showing.
  • src/components/theme-toggle.tsx: the two icons get a data-icon attribute so the test can tell them apart. Nothing else changes in the component.
  • scripts/test-pages.mjs: the page test now fails if a built page lacks the theme script in <head>. The unit tests only see the script file, so on their own they would stay green if the layout stopped including it or moved it to the end of the page.
  • .github/workflows/pr-test.yml: CI runs pnpm test. Until now the unit tests only ran locally. This is a separate commit in case you would rather not have it.

No behaviour changes for visitors.

Do the tests guard the fault?

I ran the first version of the tests against the code from before #370. Six of fifteen failed there, including the two faults that PR fixed (the first and the two back-button ones):

× with "dark" stored on a light device, dark is true
× stays light without failing when storage is blocked
× changes from light to the dark chosen on another page
× changes from dark to the light chosen on another page
× follows a theme change made outside the button
× still switches the page when storage is blocked

A review then found broken versions of the code that still passed, so I tightened the tests and checked each one now fails:

Broken version Now caught by
Back-button listener that fires only once both back-button tests
Storage read outside the try the blocked storage test
Script that only works inside a function (top-level return) 11 of 12 script tests
Sun and moon icons swapped 3 of 4 switch tests
Script moved from <head> to the end of one built page the page test, exit code 1

On this branch all 65 unit tests pass.

How I checked the move

After pnpm build, the script in dist/index.html is still a classic <script> in <head> with its source unescaped. I then repeated the browser run from #370 on the built site (headless Chrome, device theme emulated, real switch pressed, second page opened, theme switched there, back button): all five cases pass, and in each the browser reported the first page as restored from memory (pageshow with persisted: true), so the back-button case was the real one.

pnpm build, pnpm test:slugs and pnpm test:pages pass.

Worth knowing

Safari is still untested.

The fault was in the script in <head>, which no test could reach while it lived inside base.astro. I moved it to src/lib/theme-init.js, which base.astro inserts as text, so the test runs the same code the browser gets.

On the code from before the fix, six of the fifteen new tests fail: the dark choice on a light device, both back-button cases, blocked storage on load and on click, and the switch following a change made outside it.
Copilot AI balanced review requested due to automatic review settings November 17, 2026 18:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

A review listed broken versions of the code that still passed every test. The tests now fail for each of them:

- the script text is run as a script, so code that only works inside a function no longer slips through
- blocked storage throws when localStorage is read, as browsers do, instead of when a method is called
- the back-button cases restore the page twice, so a listener that fires once is caught
- the switch tests check which icon is showing, not only the label; the two icons carry a data-icon attribute for that

The page test, which CI runs on the built site, now also fails when a page lacks the theme script in <head>. Without that, removing the script from the layout would pass every check.
The unit tests only ran when someone remembered to run them locally, so a pull request that broke them still went green.
@yasinmiran
yasinmiran merged commit 008ef1e into main Nov 17, 2026
1 check passed
@yasinmiran
yasinmiran deleted the test/theme-persistence branch November 17, 2026 23:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants