forked from mantinedev/mantine
/
use-color-scheme.test.tsx
71 lines (68 loc) · 2.63 KB
/
use-color-scheme.test.tsx
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
import React from 'react';
// import { renderHook } from '@testing-library/react-hooks';
import { render } from '@testing-library/react';
import { useColorScheme } from './use-color-scheme';
describe('@maintine/hooks/use-color-scheme', () => {
let trace = jest.fn<(colorScheme: string) => void, string[]>();
const mockmatchMedia = jest.fn().mockImplementation(() => ({
matches: true,
media: '',
onchange: null,
addListener: jest.fn(),
removeListener: jest.fn(),
}));
const retainMatchMedia = window.matchMedia;
beforeEach(() => {
trace = jest.fn();
window.matchMedia = retainMatchMedia;
});
function WrapperComponent({
initialValue,
getInitialValueInEffect = true,
}: {
initialValue?: 'light' | 'dark';
getInitialValueInEffect?: boolean;
}) {
const colorScheme = useColorScheme(initialValue, {
getInitialValueInEffect,
});
trace(colorScheme);
return <>{colorScheme}</>;
}
it('correctly returns initial dark state without useEffect', async () => {
render(<WrapperComponent initialValue="dark" getInitialValueInEffect={false} />);
expect(trace).toHaveBeenCalledTimes(2);
expect(trace.mock.calls[0][0]).toBe('dark');
expect(trace.mock.calls[1][0]).toBe('light');
});
it('correctly returns initial dark state state without useEffect', async () => {
window.matchMedia = mockmatchMedia;
render(<WrapperComponent initialValue="dark" getInitialValueInEffect={false} />);
expect(trace).toHaveBeenCalledTimes(1);
expect(trace.mock.calls[0][0]).toBe('dark');
});
it('correctly returns initial light state with useEffect', async () => {
render(<WrapperComponent initialValue="dark" getInitialValueInEffect />);
expect(trace).toHaveBeenCalledTimes(2);
expect(trace.mock.calls[0][0]).toBe('dark');
expect(trace.mock.calls[1][0]).toBe('light');
});
it('correctly returns initial dark state with useEffect', async () => {
window.matchMedia = mockmatchMedia;
render(<WrapperComponent initialValue="dark" getInitialValueInEffect />);
expect(trace).toHaveBeenCalledTimes(1);
expect(trace.mock.calls[0][0]).toBe('dark');
});
it('correctly returns initial light state with deafult props', async () => {
render(<WrapperComponent />);
expect(trace).toHaveBeenCalledTimes(1);
expect(trace.mock.calls[0][0]).toBe('light');
});
it('correctly returns initial dark state with deafult props', async () => {
window.matchMedia = mockmatchMedia;
render(<WrapperComponent />);
expect(trace).toHaveBeenCalledTimes(2);
expect(trace.mock.calls[0][0]).toBe('light');
expect(trace.mock.calls[1][0]).toBe('dark');
});
});