forked from Assortment/darkmodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
102 lines (92 loc) · 2.96 KB
/
index.test.js
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
import darkmode from '.';
const themes = {
DARK: 'dark',
LIGHT: 'light',
NO_PREF: 'no-preference',
NO_SUPP: 'no-support'
};
const onChange = jest.fn();
const darkModeQuery = '(prefers-color-scheme: dark)';
const lightModeQuery = '(prefers-color-scheme: light)';
const noPrefQuery = '(prefers-color-scheme: no-preference)';
beforeAll(() => {
window.matchMedia = jest.fn(() => ({
matches: false,
addListener: f => f,
removeListener: f => f
}));
});
describe('darkmode', () => {
afterEach(() => jest.clearAllMocks());
describe('initial calls', () => {
test('should call onChange correctly when in dark mode', () => {
window.matchMedia.mockImplementation(media => ({
matches: media === darkModeQuery,
media,
addListener: f => f,
removeListener: f => f
}));
darkmode({ onChange });
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith(themes.DARK, themes);
});
test('should call onChange correctly when in light mode', () => {
window.matchMedia.mockImplementation(media => ({
matches: media === lightModeQuery,
media,
addListener: f => f,
removeListener: f => f
}));
darkmode({ onChange });
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith(themes.LIGHT, themes);
});
test('should call onChange correctly when in no preference mode', () => {
window.matchMedia.mockImplementation(media => ({
matches: media === noPrefQuery,
media,
addListener: f => f,
removeListener: f => f
}));
darkmode({ onChange });
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith(themes.NO_PREF, themes);
});
test('should call onChange correctly when no prefers-color-scheme support', () => {
window.matchMedia.mockImplementation(media => ({
matches: media === '',
media,
addListener: f => f,
removeListener: f => f
}));
darkmode({ onChange });
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith(themes.NO_SUPP, themes);
});
});
describe('listeners', () => {
test('should call addListener to add both listeners', () => {
const addListener = jest.fn();
window.matchMedia.mockImplementation(media => ({
matches: true,
media,
addListener,
removeListener: f => f
}));
darkmode({ onChange });
expect(addListener).toHaveBeenCalledTimes(2);
});
test('should call removeListener to remove both listeners', () => {
const removeListener = jest.fn();
window.matchMedia.mockImplementation(media => ({
matches: true,
media,
addListener: f => f,
removeListener
}));
const dmjs = darkmode({ onChange });
dmjs.removeListeners();
expect(removeListener).toHaveBeenCalledTimes(2);
});
});
});