1+ import { readFileSync } from "node:fs" ;
12import { describe , expect , test } from "bun:test" ;
23import {
34 applyDarkTheme ,
@@ -15,13 +16,53 @@ describe("theme preference", () => {
1516 test ( "persists and applies the selected theme" , ( ) => {
1617 const writes : Array < [ string , string ] > = [ ] ;
1718 const toggles : Array < [ string , boolean ] > = [ ] ;
19+ const schemes : Array < string > = [ ] ;
1820
1921 applyDarkTheme ( true , {
2022 setStoredValue : ( key , value ) => writes . push ( [ key , value ] ) ,
2123 toggleRootClass : ( name , force ) => toggles . push ( [ name , force ] ) ,
24+ setRootColorScheme : ( scheme ) => schemes . push ( scheme ) ,
2225 } ) ;
2326
2427 expect ( writes ) . toEqual ( [ [ THEME_STORAGE_KEY , "dark" ] ] ) ;
2528 expect ( toggles ) . toEqual ( [ [ "dark" , true ] ] ) ;
29+ expect ( schemes ) . toEqual ( [ "dark" ] ) ;
30+ } ) ;
31+ } ) ;
32+
33+ describe ( "pre-paint theme boot" , ( ) => {
34+ const html = readFileSync ( new URL ( "../index.html" , import . meta. url ) , "utf8" ) ;
35+
36+ test ( "the boot script reads the same storage key the app writes" , ( ) => {
37+ expect ( html ) . toContain ( THEME_STORAGE_KEY ) ;
38+ } ) ;
39+
40+ test ( "the boot script runs before the first paint" , ( ) => {
41+ const boot = html . match ( / < s c r i p t (? ! [ ^ > ] * \b s r c = ) [ ^ > ] * > / ) ;
42+
43+ expect ( boot ) . not . toBeNull ( ) ;
44+ expect ( boot ?. [ 0 ] ) . not . toContain ( "module" ) ;
45+ expect ( boot ?. [ 0 ] ) . not . toContain ( "defer" ) ;
46+ } ) ;
47+
48+ test ( "the boot script applies the dark class itself" , ( ) => {
49+ expect ( html ) . toContain ( "documentElement" ) ;
50+ expect ( html ) . toMatch ( / c l a s s L i s t [ \s \S ] * d a r k / ) ;
51+ } ) ;
52+
53+ test ( "the document declares a color scheme before the stylesheet arrives" , ( ) => {
54+ expect ( html ) . toContain ( "colorScheme" ) ;
55+ } ) ;
56+ } ) ;
57+
58+ describe ( "color scheme" , ( ) => {
59+ const styles = readFileSync (
60+ new URL ( "../src/styles.css" , import . meta. url ) ,
61+ "utf8" ,
62+ ) ;
63+
64+ test ( "both themes tell the browser which one they are" , ( ) => {
65+ expect ( styles ) . toMatch ( / : r o o t \s * \{ [ \s \S ] * ?c o l o r - s c h e m e : \s * l i g h t / ) ;
66+ expect ( styles ) . toMatch ( / \. d a r k \s * \{ [ \s \S ] * ?c o l o r - s c h e m e : \s * d a r k / ) ;
2667 } ) ;
2768} ) ;
0 commit comments