|
1 | 1 | import { act, renderHook } from '@testing-library/react'; |
2 | 2 |
|
| 3 | +import { createTrigger } from '@/tests'; |
| 4 | + |
3 | 5 | import { useDevicePixelRatio } from './useDevicePixelRatio'; |
4 | 6 |
|
5 | | -describe('useDevicePixelRatio', () => { |
6 | | - describe('in unsupported environment', () => { |
7 | | - afterEach(() => void vi.unstubAllGlobals()); |
8 | | - |
9 | | - it('should return supported as false when devicePixelRatio is not present', () => { |
10 | | - vi.stubGlobal('devicePixelRatio', undefined); |
11 | | - const { result } = renderHook(() => useDevicePixelRatio()); |
12 | | - expect(result.current.supported).toBeFalsy(); |
13 | | - expect(result.current.ratio).toBeNull(); |
14 | | - }); |
15 | | - |
16 | | - it('should return supported as false when matchMedia is not a function', () => { |
17 | | - vi.stubGlobal('matchMedia', undefined); |
18 | | - const { result } = renderHook(() => useDevicePixelRatio()); |
19 | | - expect(result.current.supported).toBeFalsy(); |
20 | | - expect(result.current.ratio).toBeNull(); |
21 | | - }); |
22 | | - }); |
| 7 | +const trigger = createTrigger<() => void, string>(); |
| 8 | +const mockMediaQueryListAddEventListener = vi.fn(); |
| 9 | +const mockMediaQueryListRemoveEventListener = vi.fn(); |
| 10 | +const MockMediaQueryList = class MediaQueryList { |
| 11 | + query: string; |
| 12 | + |
| 13 | + constructor(query: string) { |
| 14 | + this.query = query; |
| 15 | + } |
| 16 | + |
| 17 | + addEventListener = (_type: keyof MediaQueryListEventMap, listener: any) => { |
| 18 | + trigger.add(this.query, listener); |
| 19 | + mockMediaQueryListAddEventListener(); |
| 20 | + }; |
| 21 | + removeEventListener = () => { |
| 22 | + trigger.delete(this.query); |
| 23 | + mockMediaQueryListRemoveEventListener(); |
| 24 | + }; |
| 25 | + |
| 26 | + matches = false; |
| 27 | + onchange = vi.fn(); |
| 28 | + addListener = vi.fn(); |
| 29 | + removeListener = vi.fn(); |
| 30 | + dispatchEvent = vi.fn(); |
| 31 | +}; |
| 32 | + |
| 33 | +beforeEach(() => { |
| 34 | + vi.stubGlobal('devicePixelRatio', 1); |
| 35 | + vi.stubGlobal( |
| 36 | + 'matchMedia', |
| 37 | + vi.fn<[string], MediaQueryList>().mockImplementation((query) => { |
| 38 | + const mockMediaQueryList = new MockMediaQueryList(query); |
| 39 | + return { ...mockMediaQueryList, media: query }; |
| 40 | + }) |
| 41 | + ); |
| 42 | +}); |
| 43 | + |
| 44 | +afterEach(() => { |
| 45 | + void vi.unstubAllGlobals(); |
| 46 | + mockMediaQueryListAddEventListener.mockClear(); |
| 47 | + mockMediaQueryListRemoveEventListener.mockClear(); |
| 48 | +}); |
| 49 | + |
| 50 | +it('Should use device pixel ratio', () => { |
| 51 | + const { result } = renderHook(useDevicePixelRatio); |
| 52 | + |
| 53 | + expect(result.current.ratio).toEqual(window.devicePixelRatio); |
| 54 | + expect(result.current.supported).toBeTruthy(); |
| 55 | +}); |
| 56 | + |
| 57 | +it('Should correct return for unsupported matchMedia', () => { |
| 58 | + vi.stubGlobal('matchMedia', undefined); |
| 59 | + const { result } = renderHook(useDevicePixelRatio); |
| 60 | + |
| 61 | + expect(result.current.ratio).toEqual(1); |
| 62 | + expect(result.current.supported).toBeFalsy(); |
| 63 | +}); |
23 | 64 |
|
24 | | - describe('in supported environment', () => { |
25 | | - const mediaQueryListMock = { |
26 | | - matches: false, |
27 | | - onchange: null, |
28 | | - addListener: vi.fn(), |
29 | | - removeListener: vi.fn(), |
30 | | - addEventListener: vi.fn(), |
31 | | - removeEventListener: vi.fn(), |
32 | | - dispatchEvent: vi.fn() |
33 | | - }; |
34 | | - |
35 | | - beforeEach(() => { |
36 | | - vi.stubGlobal('devicePixelRatio', 2); |
37 | | - vi.stubGlobal( |
38 | | - 'matchMedia', |
39 | | - vi |
40 | | - .fn<[string], MediaQueryList>() |
41 | | - .mockImplementation((query) => ({ ...mediaQueryListMock, media: query })) |
42 | | - ); |
43 | | - }); |
44 | | - |
45 | | - afterEach(() => void vi.unstubAllGlobals()); |
46 | | - |
47 | | - it('should return initial devicePixelRatio and supported', () => { |
48 | | - const { result } = renderHook(() => useDevicePixelRatio()); |
49 | | - expect(result.current.supported).toBeTruthy(); |
50 | | - expect(result.current.ratio).toEqual(2); |
51 | | - }); |
52 | | - |
53 | | - it('should return ratio when devicePixelRatio changes', () => { |
54 | | - const { result } = renderHook(() => useDevicePixelRatio()); |
55 | | - expect(result.current.ratio).toEqual(2); |
56 | | - |
57 | | - const listener = mediaQueryListMock.addEventListener.mock.calls[0][1]; |
58 | | - expect(typeof listener).toEqual('function'); |
59 | | - |
60 | | - Object.defineProperty(window, 'devicePixelRatio', { |
61 | | - value: 3, |
62 | | - configurable: true |
63 | | - }); |
64 | | - |
65 | | - act(() => { |
66 | | - listener(new MediaQueryListEvent('change')); |
67 | | - }); |
68 | | - |
69 | | - expect(result.current.ratio).toEqual(3); |
70 | | - }); |
71 | | - |
72 | | - it('should remove event listener on unmount', () => { |
73 | | - const { unmount } = renderHook(() => useDevicePixelRatio()); |
74 | | - |
75 | | - expect(mediaQueryListMock.addEventListener).toHaveBeenCalledWith( |
76 | | - 'change', |
77 | | - expect.any(Function) |
78 | | - ); |
79 | | - |
80 | | - unmount(); |
81 | | - |
82 | | - expect(mediaQueryListMock.removeEventListener).toHaveBeenCalledWith( |
83 | | - 'change', |
84 | | - expect.any(Function) |
85 | | - ); |
86 | | - }); |
| 65 | +it('Should correct return for unsupported devicePixelRatio', () => { |
| 66 | + vi.stubGlobal('devicePixelRatio', undefined); |
| 67 | + const { result } = renderHook(useDevicePixelRatio); |
| 68 | + |
| 69 | + expect(result.current.ratio).toEqual(1); |
| 70 | + expect(result.current.supported).toBeFalsy(); |
| 71 | +}); |
| 72 | + |
| 73 | +it('Should handle media query change', () => { |
| 74 | + const { result } = renderHook(useDevicePixelRatio); |
| 75 | + expect(result.current.ratio).toEqual(1); |
| 76 | + |
| 77 | + Object.defineProperty(window, 'devicePixelRatio', { |
| 78 | + value: 3, |
| 79 | + configurable: true |
87 | 80 | }); |
| 81 | + |
| 82 | + expect(mockMediaQueryListAddEventListener).toHaveBeenCalledTimes(1); |
| 83 | + expect(mockMediaQueryListRemoveEventListener).toHaveBeenCalledTimes(0); |
| 84 | + |
| 85 | + act(() => trigger.callback(`(resolution: 1dppx)`)); |
| 86 | + |
| 87 | + expect(mockMediaQueryListAddEventListener).toHaveBeenCalledTimes(2); |
| 88 | + expect(mockMediaQueryListRemoveEventListener).toHaveBeenCalledTimes(1); |
| 89 | + expect(result.current.ratio).toEqual(3); |
| 90 | +}); |
| 91 | + |
| 92 | +it('Should disconnect on onmount', () => { |
| 93 | + const { unmount } = renderHook(useDevicePixelRatio); |
| 94 | + |
| 95 | + unmount(); |
| 96 | + |
| 97 | + expect(mockMediaQueryListRemoveEventListener).toHaveBeenCalledTimes(1); |
88 | 98 | }); |
0 commit comments