Repository navigation
test(theme): cover the page-load script and the switch - #371
Merged
Merged
Conversation
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.
Contributor
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 ofsrc/layouts/base.astrowithout 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 adata-iconattribute 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 runspnpm 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):
A review then found broken versions of the code that still passed, so I tightened the tests and checked each one now fails:
tryreturn)<head>to the end of one built pageOn this branch all 65 unit tests pass.
How I checked the move
After
pnpm build, the script indist/index.htmlis 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 (pageshowwithpersisted: true), so the back-button case was the real one.pnpm build,pnpm test:slugsandpnpm test:pagespass.Worth knowing
Safari is still untested.