|
| 1 | +import $ from 'jquery'; |
| 2 | +import ts from 'treibstoff'; |
| 3 | +import {ColorMode, ColorToggler} from '../src/colormode.js'; |
| 4 | + |
| 5 | +QUnit.module('cone.app.colormode.ColorMode', hooks => { |
| 6 | + |
| 7 | + let stored_theme_origin, |
| 8 | + theme_attr_origin; |
| 9 | + |
| 10 | + hooks.beforeEach(() => { |
| 11 | + stored_theme_origin = localStorage.getItem('cone-app-color-theme'); |
| 12 | + theme_attr_origin = document.documentElement.getAttribute('data-bs-theme'); |
| 13 | + localStorage.removeItem('cone-app-color-theme'); |
| 14 | + document.documentElement.removeAttribute('data-bs-theme'); |
| 15 | + }); |
| 16 | + |
| 17 | + hooks.afterEach(() => { |
| 18 | + if (stored_theme_origin) { |
| 19 | + localStorage.setItem('cone-app-color-theme', stored_theme_origin); |
| 20 | + } else { |
| 21 | + localStorage.removeItem('cone-app-color-theme'); |
| 22 | + } |
| 23 | + if (theme_attr_origin) { |
| 24 | + document.documentElement.setAttribute('data-bs-theme', theme_attr_origin); |
| 25 | + } else { |
| 26 | + document.documentElement.removeAttribute('data-bs-theme'); |
| 27 | + } |
| 28 | + ColorMode.unbind(); |
| 29 | + }); |
| 30 | + |
| 31 | + QUnit.test('media_query returns MediaQueryList', assert => { |
| 32 | + let mq = ColorMode.media_query; |
| 33 | + assert.ok(mq, 'media_query returns object'); |
| 34 | + assert.strictEqual(typeof mq.matches, 'boolean', 'has matches property'); |
| 35 | + }); |
| 36 | + |
| 37 | + QUnit.test('stored_theme getter returns null when not set', assert => { |
| 38 | + assert.strictEqual(ColorMode.stored_theme, null, 'returns null'); |
| 39 | + }); |
| 40 | + |
| 41 | + QUnit.test('stored_theme setter/getter works correctly', assert => { |
| 42 | + ColorMode.stored_theme = 'dark'; |
| 43 | + assert.strictEqual(ColorMode.stored_theme, 'dark', 'stored value retrieved'); |
| 44 | + assert.strictEqual(localStorage.getItem('cone-app-color-theme'), 'dark', |
| 45 | + 'value in localStorage'); |
| 46 | + }); |
| 47 | + |
| 48 | + QUnit.test('preferred_theme returns stored theme if set', assert => { |
| 49 | + ColorMode.stored_theme = 'light'; |
| 50 | + assert.strictEqual(ColorMode.preferred_theme, 'light', |
| 51 | + 'returns stored theme'); |
| 52 | + }); |
| 53 | + |
| 54 | + QUnit.test('preferred_theme falls back to media query', assert => { |
| 55 | + localStorage.removeItem('cone-app-color-theme'); |
| 56 | + let preferred = ColorMode.preferred_theme; |
| 57 | + assert.ok(preferred === 'dark' || preferred === 'light', |
| 58 | + 'returns valid theme based on media query'); |
| 59 | + }); |
| 60 | + |
| 61 | + QUnit.test('watch adds event listener', assert => { |
| 62 | + let called = false; |
| 63 | + let handle = function() { called = true; }; |
| 64 | + |
| 65 | + ColorMode.watch(handle); |
| 66 | + assert.ok(true, 'watch does not throw'); |
| 67 | + }); |
| 68 | + |
| 69 | + QUnit.test('set_theme sets data-bs-theme attribute', assert => { |
| 70 | + ColorMode.set_theme('dark'); |
| 71 | + assert.strictEqual( |
| 72 | + document.documentElement.getAttribute('data-bs-theme'), |
| 73 | + 'dark', |
| 74 | + 'dark theme set' |
| 75 | + ); |
| 76 | + |
| 77 | + ColorMode.set_theme('light'); |
| 78 | + assert.strictEqual( |
| 79 | + document.documentElement.getAttribute('data-bs-theme'), |
| 80 | + 'light', |
| 81 | + 'light theme set' |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + QUnit.test('set_theme handles auto mode', assert => { |
| 86 | + ColorMode.set_theme('auto'); |
| 87 | + let theme = document.documentElement.getAttribute('data-bs-theme'); |
| 88 | + assert.ok(theme === 'dark' || theme === 'auto', |
| 89 | + 'auto mode sets theme based on media query'); |
| 90 | + }); |
| 91 | + |
| 92 | + QUnit.test('bind sets up boundCallback', assert => { |
| 93 | + ColorMode.bind(); |
| 94 | + assert.ok(ColorMode.boundCallback, 'boundCallback set'); |
| 95 | + }); |
| 96 | + |
| 97 | + QUnit.test('callback updates theme when not explicitly set', assert => { |
| 98 | + localStorage.removeItem('cone-app-color-theme'); |
| 99 | + ColorMode.callback(); |
| 100 | + let theme = document.documentElement.getAttribute('data-bs-theme'); |
| 101 | + assert.ok(theme === 'dark' || theme === 'light' || theme === null, |
| 102 | + 'theme updated or left alone'); |
| 103 | + }); |
| 104 | + |
| 105 | + QUnit.test('callback does not change theme when light stored', assert => { |
| 106 | + ColorMode.stored_theme = 'light'; |
| 107 | + ColorMode.set_theme('light'); |
| 108 | + ColorMode.callback(); |
| 109 | + assert.strictEqual( |
| 110 | + document.documentElement.getAttribute('data-bs-theme'), |
| 111 | + 'light', |
| 112 | + 'theme unchanged when light stored' |
| 113 | + ); |
| 114 | + }); |
| 115 | + |
| 116 | + QUnit.test('callback does not change theme when dark stored', assert => { |
| 117 | + ColorMode.stored_theme = 'dark'; |
| 118 | + ColorMode.set_theme('dark'); |
| 119 | + ColorMode.callback(); |
| 120 | + assert.strictEqual( |
| 121 | + document.documentElement.getAttribute('data-bs-theme'), |
| 122 | + 'dark', |
| 123 | + 'theme unchanged when dark stored' |
| 124 | + ); |
| 125 | + }); |
| 126 | + |
| 127 | + QUnit.test('unbind removes event listener and attribute', assert => { |
| 128 | + ColorMode.bind(); |
| 129 | + ColorMode.set_theme('dark'); |
| 130 | + ColorMode.unbind(); |
| 131 | + |
| 132 | + assert.strictEqual( |
| 133 | + document.documentElement.getAttribute('data-bs-theme'), |
| 134 | + null, |
| 135 | + 'data-bs-theme attribute removed' |
| 136 | + ); |
| 137 | + }); |
| 138 | + |
| 139 | + QUnit.test('unbind handles missing boundCallback', assert => { |
| 140 | + ColorMode.boundCallback = null; |
| 141 | + ColorMode.unbind(); |
| 142 | + assert.ok(true, 'unbind does not throw without boundCallback'); |
| 143 | + }); |
| 144 | + |
| 145 | + QUnit.test('constructor calls bind and set_theme', assert => { |
| 146 | + localStorage.removeItem('cone-app-color-theme'); |
| 147 | + new ColorMode(); |
| 148 | + |
| 149 | + assert.ok(ColorMode.boundCallback, 'bind called'); |
| 150 | + let theme = document.documentElement.getAttribute('data-bs-theme'); |
| 151 | + assert.ok(theme === 'dark' || theme === 'light', |
| 152 | + 'theme set from preferred'); |
| 153 | + }); |
| 154 | +}); |
| 155 | + |
| 156 | +QUnit.module('cone.app.colormode.ColorToggler', hooks => { |
| 157 | + |
| 158 | + let container, |
| 159 | + stored_theme_origin, |
| 160 | + theme_attr_origin; |
| 161 | + |
| 162 | + hooks.beforeEach(() => { |
| 163 | + container = $('<div />').appendTo('body'); |
| 164 | + stored_theme_origin = localStorage.getItem('cone-app-color-theme'); |
| 165 | + theme_attr_origin = document.documentElement.getAttribute('data-bs-theme'); |
| 166 | + localStorage.removeItem('cone-app-color-theme'); |
| 167 | + }); |
| 168 | + |
| 169 | + hooks.afterEach(() => { |
| 170 | + container.remove(); |
| 171 | + if (stored_theme_origin) { |
| 172 | + localStorage.setItem('cone-app-color-theme', stored_theme_origin); |
| 173 | + } else { |
| 174 | + localStorage.removeItem('cone-app-color-theme'); |
| 175 | + } |
| 176 | + if (theme_attr_origin) { |
| 177 | + document.documentElement.setAttribute('data-bs-theme', theme_attr_origin); |
| 178 | + } else { |
| 179 | + document.documentElement.removeAttribute('data-bs-theme'); |
| 180 | + } |
| 181 | + ColorMode.unbind(); |
| 182 | + }); |
| 183 | + |
| 184 | + QUnit.test('initialize returns early without toggle switch', assert => { |
| 185 | + ColorToggler.initialize(container); |
| 186 | + assert.ok(true, 'no error without toggle switch'); |
| 187 | + }); |
| 188 | + |
| 189 | + QUnit.test('initialize creates instance when toggle exists', assert => { |
| 190 | + let toggle = $('<input type="checkbox" id="colortoggle-switch" />') |
| 191 | + .appendTo(container); |
| 192 | + |
| 193 | + ColorToggler.initialize(container); |
| 194 | + |
| 195 | + let events = $._data(toggle.get(0), 'events'); |
| 196 | + assert.ok(events && events.change, 'change event bound'); |
| 197 | + }); |
| 198 | + |
| 199 | + QUnit.test('constructor stores elem and binds events', assert => { |
| 200 | + let toggle = $('<input type="checkbox" id="colortoggle-switch" />') |
| 201 | + .appendTo(container); |
| 202 | + |
| 203 | + let toggler = new ColorToggler(toggle); |
| 204 | + |
| 205 | + assert.ok(toggler.elem, 'elem stored'); |
| 206 | + let events = $._data(toggle.get(0), 'events'); |
| 207 | + assert.ok(events && events.change, 'change event bound'); |
| 208 | + }); |
| 209 | + |
| 210 | + QUnit.test('update checks toggle when dark theme preferred', assert => { |
| 211 | + ColorMode.stored_theme = 'dark'; |
| 212 | + |
| 213 | + let toggle = $('<input type="checkbox" id="colortoggle-switch" />') |
| 214 | + .appendTo(container); |
| 215 | + |
| 216 | + let toggler = new ColorToggler(toggle); |
| 217 | + toggler.update(); |
| 218 | + |
| 219 | + assert.true(toggle.is(':checked'), 'toggle checked for dark theme'); |
| 220 | + }); |
| 221 | + |
| 222 | + QUnit.test('update unchecks toggle when light theme preferred', assert => { |
| 223 | + ColorMode.stored_theme = 'light'; |
| 224 | + |
| 225 | + let toggle = $('<input type="checkbox" id="colortoggle-switch" checked />') |
| 226 | + .appendTo(container); |
| 227 | + |
| 228 | + let toggler = new ColorToggler(toggle); |
| 229 | + toggler.update(); |
| 230 | + |
| 231 | + assert.false(toggle.is(':checked'), 'toggle unchecked for light theme'); |
| 232 | + }); |
| 233 | + |
| 234 | + QUnit.test('update leaves toggle unchanged when already correct', assert => { |
| 235 | + ColorMode.stored_theme = 'dark'; |
| 236 | + |
| 237 | + let toggle = $('<input type="checkbox" id="colortoggle-switch" checked />') |
| 238 | + .appendTo(container); |
| 239 | + |
| 240 | + let toggler = new ColorToggler(toggle); |
| 241 | + toggler.update(); |
| 242 | + |
| 243 | + assert.true(toggle.is(':checked'), 'toggle remains checked'); |
| 244 | + }); |
| 245 | + |
| 246 | + QUnit.test('on_change sets dark theme when checked', assert => { |
| 247 | + let toggle = $('<input type="checkbox" id="colortoggle-switch" />') |
| 248 | + .appendTo(container); |
| 249 | + |
| 250 | + let toggler = new ColorToggler(toggle); |
| 251 | + toggle.prop('checked', true); |
| 252 | + toggler.on_change(); |
| 253 | + |
| 254 | + assert.strictEqual(ColorMode.stored_theme, 'dark', 'dark theme stored'); |
| 255 | + assert.strictEqual( |
| 256 | + document.documentElement.getAttribute('data-bs-theme'), |
| 257 | + 'dark', |
| 258 | + 'dark theme set on document' |
| 259 | + ); |
| 260 | + }); |
| 261 | + |
| 262 | + QUnit.test('on_change sets light theme when unchecked', assert => { |
| 263 | + let toggle = $('<input type="checkbox" id="colortoggle-switch" />') |
| 264 | + .appendTo(container); |
| 265 | + |
| 266 | + let toggler = new ColorToggler(toggle); |
| 267 | + toggler.on_change(); |
| 268 | + |
| 269 | + assert.strictEqual(ColorMode.stored_theme, 'light', 'light theme stored'); |
| 270 | + assert.strictEqual( |
| 271 | + document.documentElement.getAttribute('data-bs-theme'), |
| 272 | + 'light', |
| 273 | + 'light theme set on document' |
| 274 | + ); |
| 275 | + }); |
| 276 | + |
| 277 | + QUnit.test('toggler responds to change event', assert => { |
| 278 | + let toggle = $('<input type="checkbox" id="colortoggle-switch" />') |
| 279 | + .appendTo(container); |
| 280 | + |
| 281 | + new ColorToggler(toggle); |
| 282 | + |
| 283 | + toggle.prop('checked', true).trigger('change'); |
| 284 | + |
| 285 | + assert.strictEqual(ColorMode.stored_theme, 'dark', |
| 286 | + 'change event triggers on_change'); |
| 287 | + }); |
| 288 | +}); |
0 commit comments