-
Notifications
You must be signed in to change notification settings - Fork 470
Expand file tree
/
Copy pathdark-mode.test.ts
More file actions
115 lines (83 loc) · 3.56 KB
/
dark-mode.test.ts
File metadata and controls
115 lines (83 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { initTheme, isDarkMode, resetForTest } from '../../utils/dark-mode';
describe('isDarkMode', function () {
it('does not throw on access failure', function () {
resetForTest();
const getItem = jest.fn(() => {
throw new Error('dummy error');
});
jest.spyOn(Storage.prototype, 'getItem').mockImplementation(getItem);
// When localStorage throws, it should default to system preference
expect(isDarkMode()).toBe(false);
expect(getItem).toHaveBeenCalledWith('theme');
});
it('listens to storage event', function () {
resetForTest();
expect(isDarkMode()).toBe(false);
// The value is cached.
const getItem = jest.fn(() => 'dark');
jest.spyOn(Storage.prototype, 'getItem').mockImplementation(getItem);
expect(isDarkMode()).toBe(false);
// Different key should be ignored.
window.dispatchEvent(new StorageEvent('storage', { key: 'something' }));
expect(isDarkMode()).toBe(false);
window.dispatchEvent(new StorageEvent('storage', { key: 'theme' }));
expect(isDarkMode()).toBe(true);
// The value is cached.
const getItem2 = jest.fn(() => null);
jest.spyOn(Storage.prototype, 'getItem').mockImplementation(getItem2);
expect(isDarkMode()).toBe(true);
window.dispatchEvent(new StorageEvent('storage', { key: 'theme' }));
expect(isDarkMode()).toBe(false);
});
});
describe('profiler-theme-change event', function () {
it('is dispatched when the theme changes', function () {
resetForTest();
// Initialize in light mode.
isDarkMode();
const listener = jest.fn();
window.addEventListener('profiler-theme-change', listener);
// Switch to dark via a storage event.
jest.spyOn(Storage.prototype, 'getItem').mockImplementation(() => 'dark');
window.dispatchEvent(new StorageEvent('storage', { key: 'theme' }));
expect(listener).toHaveBeenCalledTimes(1);
window.removeEventListener('profiler-theme-change', listener);
});
it('is not dispatched during initialization', function () {
resetForTest();
const listener = jest.fn();
window.addEventListener('profiler-theme-change', listener);
isDarkMode(); // triggers setup
expect(listener).not.toHaveBeenCalled();
window.removeEventListener('profiler-theme-change', listener);
});
it('is not dispatched when the theme stays the same', function () {
resetForTest();
isDarkMode(); // initialize as light
const listener = jest.fn();
window.addEventListener('profiler-theme-change', listener);
// Storage event fires but the resolved theme is still light.
window.dispatchEvent(new StorageEvent('storage', { key: 'theme' }));
expect(listener).not.toHaveBeenCalled();
window.removeEventListener('profiler-theme-change', listener);
});
});
describe('initTheme', function () {
it('sets the document element class', function () {
resetForTest();
const getItem = jest.fn();
jest.spyOn(Storage.prototype, 'getItem').mockImplementation(getItem);
initTheme();
expect(getItem).toHaveBeenCalledWith('theme');
expect(document.documentElement.className).toBe('');
resetForTest();
const getItem2 = jest.fn(() => 'dark');
jest.spyOn(Storage.prototype, 'getItem').mockImplementation(getItem2);
initTheme();
expect(getItem).toHaveBeenCalledWith('theme');
expect(document.documentElement.className).toBe('dark-mode');
});
});