-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuseMediaQuery.test.js
More file actions
198 lines (157 loc) · 5.79 KB
/
useMediaQuery.test.js
File metadata and controls
198 lines (157 loc) · 5.79 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { act, renderHook } from '@testing-library/react';
import { renderToString } from 'react-dom/server';
import { useMediaQuery } from './index.js';
describe('useMediaQuery', () => {
describe('renderHook', () => {
let matches = false;
const listeners = new Set();
beforeAll(() => {
vi.spyOn(window, 'matchMedia').mockImplementation((query) => ({
get matches() {
return matches;
},
media: query,
addEventListener: (event, cb) => listeners.add(cb),
removeEventListener: (event, cb) => listeners.delete(cb),
}));
});
beforeEach(() => {
vi.useFakeTimers();
matches = false;
listeners.clear();
});
afterAll(() => {
vi.restoreAllMocks();
});
it('should return true if the media query matches', () => {
matches = true;
const { result } = renderHook(() => useMediaQuery('(min-width: 600px)'));
expect(result.current).toBe(true);
});
it('should return false if the media query does not match', () => {
const { result } = renderHook(() => useMediaQuery('(min-width: 1200px)'));
expect(result.current).toBe(false);
});
it('should update when the media query changes', () => {
const { result } = renderHook(() => useMediaQuery('(min-width: 800px)'));
expect(result.current).toBe(false);
act(() => {
matches = true;
listeners.forEach((cb) => cb());
});
expect(result.current).toBe(true);
});
it('should handle debounce correctly', async () => {
const { result } = renderHook(() => useMediaQuery('(max-width: 768px)', { debounceMs: 100 }));
act(() => {
matches = true;
listeners.forEach((cb) => cb());
});
expect(result.current).toBe(false);
await act(() => vi.advanceTimersByTimeAsync(100));
expect(result.current).toBe(true);
});
it('should call onChange when media query changes', () => {
const onChange = vi.fn();
renderHook(() => useMediaQuery('(max-width: 768px)', { onChange }));
act(() => {
matches = true;
listeners.forEach((cb) => cb());
});
expect(onChange).toHaveBeenCalledExactlyOnceWith(true);
});
it('should cleanup listeners on unmount', () => {
const { unmount } = renderHook(() => useMediaQuery('(min-width: 800px)'));
expect(listeners.size).toBe(1);
unmount();
expect(listeners.size).toBe(0);
});
it('should cleanup debounce timers on unmount', () => {
const { unmount } = renderHook(() => useMediaQuery('(max-width: 768px)', { debounceMs: 100 }));
act(() => {
matches = true;
listeners.forEach((cb) => cb());
});
expect(vi.getTimerCount()).toBe(1);
unmount();
expect(vi.getTimerCount()).toBe(0);
});
describe('legacy addListener/removeListener support', () => {
beforeAll(() => {
window.matchMedia.mockImplementation((query) => ({
get matches() {
return matches;
},
media: query,
addListener: (cb) => listeners.add(cb),
removeListener: (cb) => listeners.delete(cb),
}));
});
it('should matches and update correctly using legacy methods', () => {
const { result } = renderHook(() => useMediaQuery('(min-width: 900px)'));
expect(result.current).toBe(false);
act(() => {
matches = true;
listeners.forEach((cb) => cb());
});
expect(result.current).toBe(true);
});
it('should cleanup listeners on unmount', () => {
const { unmount } = renderHook(() => useMediaQuery('(min-width: 800px)'));
expect(listeners.size).toBe(1);
unmount();
expect(listeners.size).toBe(0);
});
it('should cleanup debounce timers on unmount', () => {
const { unmount } = renderHook(() => useMediaQuery('(max-width: 768px)', { debounceMs: 100 }));
act(() => {
matches = true;
listeners.forEach((cb) => cb());
});
expect(vi.getTimerCount()).toBe(1);
unmount();
expect(vi.getTimerCount()).toBe(0);
});
});
});
describe('SSR', () => {
let originalWindow;
beforeAll(() => {
originalWindow = global.window;
delete global.window;
});
afterAll(() => {
global.window = originalWindow;
});
it('should default to false when no fallback is provided', () => {
const TestComponent = () => String(useMediaQuery('(min-width: 600px)'));
expect(renderToString(<TestComponent />)).toBe('false');
});
it('should use the constant boolean provided as fallback', () => {
const TestComponent = () => String(useMediaQuery('(max-width: 768px)', { fallback: true }));
expect(renderToString(<TestComponent />)).toBe('true');
});
it.each([true, false])('should execute the fallback function and use its return value (%s)', (fallbackValue) => {
const TestComponent = () => String(useMediaQuery('(max-width: 768px)', { fallback: () => fallbackValue }));
expect(renderToString(<TestComponent />)).toBe(String(fallbackValue));
});
});
describe('when window.matchMedia is not available', () => {
let originalMatchMedia;
beforeAll(() => {
originalMatchMedia = window.matchMedia;
delete window.matchMedia;
});
afterAll(() => {
window.matchMedia = originalMatchMedia;
});
it('should return false when no fallback is provided', () => {
const { result } = renderHook(() => useMediaQuery('(min-width: 600px)'));
expect(result.current).toBe(false);
});
it('should return the fallback value', () => {
const { result } = renderHook(() => useMediaQuery('(min-width: 600px)', { fallback: true }));
expect(result.current).toBe(true);
});
});
});