|
| 1 | +import { act, renderHook, waitFor } from '@testing-library/react'; |
| 2 | + |
| 3 | +import { useOrientation } from './useOrientation'; |
| 4 | + |
| 5 | +let events: Record<string, () => void>; |
| 6 | +let orientation: typeof orientationPreset; |
| 7 | + |
| 8 | +const orientationPreset = { |
| 9 | + type: 'landscape-primary', |
| 10 | + angle: 0, |
| 11 | + onchange: null, |
| 12 | + unlock: vi.fn(), |
| 13 | + addEventListener: (type: string, callback: () => void) => { |
| 14 | + events[type] = callback; |
| 15 | + }, |
| 16 | + removeEventListener: (type: string, callback: () => void) => { |
| 17 | + if (events[type] === callback) { |
| 18 | + delete events[type]; |
| 19 | + } |
| 20 | + }, |
| 21 | + dispatchEvent: (event: Event) => { |
| 22 | + events[event.type]?.(); |
| 23 | + } |
| 24 | +}; |
| 25 | + |
| 26 | +beforeEach(() => { |
| 27 | + events = {}; |
| 28 | + orientation = { ...orientationPreset }; |
| 29 | + Object.assign(window, { |
| 30 | + screen: { orientation } |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +it('should unsubscribe from event listener on unmount', () => { |
| 35 | + const { unmount } = renderHook(useOrientation); |
| 36 | + |
| 37 | + const mockRemoveEventListener = vi.fn(); |
| 38 | + orientation.removeEventListener = mockRemoveEventListener; |
| 39 | + |
| 40 | + act(() => { |
| 41 | + unmount(); |
| 42 | + }); |
| 43 | + |
| 44 | + expect(mockRemoveEventListener).toHaveBeenCalledWith('change', expect.any(Function)); |
| 45 | +}); |
| 46 | + |
| 47 | +it('should subscribe on event listener on mount', () => { |
| 48 | + const mockAddEventListener = vi.fn(); |
| 49 | + orientation.addEventListener = mockAddEventListener; |
| 50 | + |
| 51 | + renderHook(useOrientation); |
| 52 | + |
| 53 | + expect(mockAddEventListener).toHaveBeenCalledWith('change', expect.any(Function)); |
| 54 | +}); |
| 55 | + |
| 56 | +it('Should return current window orientation', () => { |
| 57 | + const { result } = renderHook(useOrientation); |
| 58 | + |
| 59 | + expect(typeof result.current).toBe('object'); |
| 60 | + expect(typeof result.current.type).toBe('string'); |
| 61 | + expect(typeof result.current.angle).toBe('number'); |
| 62 | +}); |
| 63 | + |
| 64 | +it('Should use orientation', () => { |
| 65 | + const { result } = renderHook(useOrientation); |
| 66 | + |
| 67 | + expect(result.current).toEqual({ |
| 68 | + angle: 0, |
| 69 | + type: 'landscape-primary' |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +it('Should handle change event with angle 90 and type landscape-primary', async () => { |
| 74 | + const { result } = renderHook(useOrientation); |
| 75 | + |
| 76 | + act(() => { |
| 77 | + orientation.angle = 90; |
| 78 | + orientation.type = 'landscape-primary'; |
| 79 | + orientation.dispatchEvent(new Event('change')); |
| 80 | + }); |
| 81 | + |
| 82 | + await waitFor(() => |
| 83 | + expect(result.current).toEqual({ |
| 84 | + angle: 90, |
| 85 | + type: 'landscape-primary' |
| 86 | + }) |
| 87 | + ); |
| 88 | +}); |
| 89 | + |
| 90 | +it('Should handle change event with angle 180 and type portrait-secondary', async () => { |
| 91 | + const { result } = renderHook(useOrientation); |
| 92 | + |
| 93 | + act(() => { |
| 94 | + orientation.angle = 180; |
| 95 | + orientation.type = 'portrait-secondary'; |
| 96 | + orientation.dispatchEvent(new Event('change')); |
| 97 | + }); |
| 98 | + |
| 99 | + await waitFor(() => |
| 100 | + expect(result.current).toEqual({ |
| 101 | + angle: 180, |
| 102 | + type: 'portrait-secondary' |
| 103 | + }) |
| 104 | + ); |
| 105 | +}); |
| 106 | + |
| 107 | +it('Should handle change event with angle 270 and type landscape-secondary', async () => { |
| 108 | + const { result } = renderHook(useOrientation); |
| 109 | + |
| 110 | + act(() => { |
| 111 | + orientation.angle = 270; |
| 112 | + orientation.type = 'landscape-secondary'; |
| 113 | + orientation.dispatchEvent(new Event('change')); |
| 114 | + }); |
| 115 | + |
| 116 | + await waitFor(() => |
| 117 | + expect(result.current).toEqual({ |
| 118 | + angle: 270, |
| 119 | + type: 'landscape-secondary' |
| 120 | + }) |
| 121 | + ); |
| 122 | +}); |
0 commit comments