@@ -125,6 +125,53 @@ describe('head/startup-styles', function () {
125125 } ) ;
126126 } ) ;
127127
128+ describe ( 'addDarkModeStyles' , function ( ) {
129+ const THEME_KEY = '__fxa_storage.theme_preference' ;
130+
131+ it ( 'adds `dark` when theme_preference is "dark"' , function ( ) {
132+ windowMock . localStorage . setItem ( THEME_KEY , JSON . stringify ( 'dark' ) ) ;
133+ startupStyles . addDarkModeStyles ( ) ;
134+ assert . isTrue ( / \b d a r k \b / . test ( startupStyles . getClassName ( ) ) ) ;
135+ } ) ;
136+
137+ it ( 'does not add `dark` when theme_preference is "light"' , function ( ) {
138+ windowMock . localStorage . setItem ( THEME_KEY , JSON . stringify ( 'light' ) ) ;
139+ startupStyles . addDarkModeStyles ( ) ;
140+ assert . isFalse ( / \b d a r k \b / . test ( startupStyles . getClassName ( ) ) ) ;
141+ } ) ;
142+
143+ it ( 'adds `dark` when theme_preference is "system" and OS prefers dark' , function ( ) {
144+ windowMock . localStorage . setItem ( THEME_KEY , JSON . stringify ( 'system' ) ) ;
145+ windowMock . matchMedia = function ( ) {
146+ return { matches : true } ;
147+ } ;
148+ startupStyles . addDarkModeStyles ( ) ;
149+ assert . isTrue ( / \b d a r k \b / . test ( startupStyles . getClassName ( ) ) ) ;
150+ } ) ;
151+
152+ it ( 'does not add `dark` when theme_preference is "system" and OS prefers light' , function ( ) {
153+ windowMock . localStorage . setItem ( THEME_KEY , JSON . stringify ( 'system' ) ) ;
154+ windowMock . matchMedia = function ( ) {
155+ return { matches : false } ;
156+ } ;
157+ startupStyles . addDarkModeStyles ( ) ;
158+ assert . isFalse ( / \b d a r k \b / . test ( startupStyles . getClassName ( ) ) ) ;
159+ } ) ;
160+
161+ it ( 'defaults to light (no `dark` class) when theme_preference is not set' , function ( ) {
162+ startupStyles . addDarkModeStyles ( ) ;
163+ assert . isFalse ( / \b d a r k \b / . test ( startupStyles . getClassName ( ) ) ) ;
164+ } ) ;
165+
166+ it ( 'defaults to light when localStorage is unavailable' , function ( ) {
167+ sinon
168+ . stub ( windowMock . localStorage , 'getItem' )
169+ . throws ( new Error ( 'unavailable' ) ) ;
170+ startupStyles . addDarkModeStyles ( ) ;
171+ assert . isFalse ( / \b d a r k \b / . test ( startupStyles . getClassName ( ) ) ) ;
172+ } ) ;
173+ } ) ;
174+
128175 describe ( 'initialize' , function ( ) {
129176 it ( 'runs all the tests' , function ( ) {
130177 startupStyles . initialize ( ) ;
0 commit comments