Skip to content

Commit 29ccf3d

Browse files
committed
main 🧊 add use media query test
1 parent 0f99bdf commit 29ccf3d

File tree

4 files changed

+70
-92
lines changed

4 files changed

+70
-92
lines changed

‎packages/core/src/bundle/hooks/useClipboard/useClipboard.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { copy } from '@/utils/helpers';
1111
* @returns {UseCopyToClipboardReturn} An object containing the boolean state value and utility functions to manipulate the state
1212
*
1313
* @example
14-
* const { supported, value, copy } = useClipboard();
14+
* const { value, copy } = useClipboard();
1515
*/
1616
export const useClipboard = (params) => {
1717
const [value, setValue] = useState(null);

‎packages/core/src/hooks/useClipboard/useClipboard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface UseCopyToClipboardParams {
2727
* @returns {UseCopyToClipboardReturn} An object containing the boolean state value and utility functions to manipulate the state
2828
*
2929
* @example
30-
* const { supported, value, copy } = useClipboard();
30+
* const { value, copy } = useClipboard();
3131
*/
3232
export const useClipboard = (params?: UseCopyToClipboardParams): UseCopyToClipboardReturn => {
3333
const [value, setValue] = useState<string | null>(null);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { act, renderHook } from '@testing-library/react';
2+
3+
import { createTrigger, renderHookServer } from '@/tests';
4+
5+
import { useMediaQuery } from './useMediaQuery';
6+
7+
const trigger = createTrigger<string, () => void>();
8+
const mockMatchMedia = {
9+
matches: false,
10+
media: '(max-width: 768px)',
11+
onchange: null,
12+
addListener: vi.fn(),
13+
removeListener: vi.fn(),
14+
addEventListener: (type: string, callback: () => void) => {
15+
trigger.add(type, callback);
16+
},
17+
removeEventListener: (type: string, callback: () => void) => {
18+
if (trigger.get(type) === callback) {
19+
trigger.delete(type);
20+
}
21+
},
22+
dispatchEvent: vi.fn()
23+
};
24+
25+
beforeEach(() => {
26+
Object.defineProperty(window, 'matchMedia', {
27+
writable: true,
28+
value: (query: string) => {
29+
mockMatchMedia.media = query;
30+
return mockMatchMedia;
31+
}
32+
});
33+
});
34+
35+
afterEach(() => {
36+
vi.restoreAllMocks();
37+
});
38+
39+
it('Should use media query"', () => {
40+
const { result } = renderHook(() => useMediaQuery('(max-width: 768px)'));
41+
42+
expect(result.current).toBe(false);
43+
});
44+
45+
it('Should use media query on server', () => {
46+
const { result } = renderHookServer(() => useMediaQuery('(max-width: 768px)'));
47+
48+
expect(result.current).toBe(false);
49+
});
50+
51+
it('Should return true if media query matches', () => {
52+
mockMatchMedia.matches = true;
53+
54+
const { result } = renderHook(() => useMediaQuery('(max-width: 768px)'));
55+
56+
expect(result.current).toEqual(true);
57+
});
58+
59+
it('returns false if media query does not match after change', async () => {
60+
const { result } = renderHook(() => useMediaQuery('(max-width: 768px)'));
61+
62+
expect(result.current).toEqual(true);
63+
64+
mockMatchMedia.matches = false;
65+
act(() => trigger.callback('change'));
66+
67+
expect(result.current).toEqual(false);
68+
});

‎src/hooks/useMediaQuery/useMediaQuery.test.ts

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)