|
| 1 | +import { jest } from "@jest/globals"; |
| 2 | +import { act, renderHook } from "@testing-library/react"; |
| 3 | + |
| 4 | +const { useWindowResize } = await import("../src/hooks/useWindowResize"); |
| 5 | + |
| 6 | +describe("useWindowResize", () => { |
| 7 | + beforeEach(() => { |
| 8 | + jest.useFakeTimers(); |
| 9 | + }); |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + jest.clearAllTimers(); |
| 13 | + jest.useRealTimers(); |
| 14 | + }); |
| 15 | + |
| 16 | + test("returns current window dimensions", () => { |
| 17 | + // Set window dimensions |
| 18 | + Object.defineProperty(window, "innerWidth", { |
| 19 | + configurable: true, |
| 20 | + value: 1024, |
| 21 | + writable: true, |
| 22 | + }); |
| 23 | + Object.defineProperty(window, "innerHeight", { |
| 24 | + configurable: true, |
| 25 | + value: 768, |
| 26 | + writable: true, |
| 27 | + }); |
| 28 | + |
| 29 | + const { result } = renderHook(() => useWindowResize()); |
| 30 | + |
| 31 | + expect(result.current).toEqual({ height: 768, width: 1024 }); |
| 32 | + }); |
| 33 | + |
| 34 | + test("updates dimensions on window resize", () => { |
| 35 | + // Set initial dimensions |
| 36 | + Object.defineProperty(window, "innerWidth", { |
| 37 | + configurable: true, |
| 38 | + value: 1024, |
| 39 | + writable: true, |
| 40 | + }); |
| 41 | + Object.defineProperty(window, "innerHeight", { |
| 42 | + configurable: true, |
| 43 | + value: 768, |
| 44 | + writable: true, |
| 45 | + }); |
| 46 | + |
| 47 | + const { result } = renderHook(() => useWindowResize(100)); |
| 48 | + |
| 49 | + expect(result.current).toEqual({ height: 768, width: 1024 }); |
| 50 | + |
| 51 | + // Simulate window resize |
| 52 | + act(() => { |
| 53 | + Object.defineProperty(window, "innerWidth", { |
| 54 | + configurable: true, |
| 55 | + value: 1920, |
| 56 | + writable: true, |
| 57 | + }); |
| 58 | + Object.defineProperty(window, "innerHeight", { |
| 59 | + configurable: true, |
| 60 | + value: 1080, |
| 61 | + writable: true, |
| 62 | + }); |
| 63 | + window.dispatchEvent(new Event("resize")); |
| 64 | + |
| 65 | + // Fast-forward timeout |
| 66 | + jest.advanceTimersByTime(100); |
| 67 | + }); |
| 68 | + |
| 69 | + expect(result.current).toEqual({ height: 1080, width: 1920 }); |
| 70 | + }); |
| 71 | + |
| 72 | + test("debounces resize events with the specified timeout", () => { |
| 73 | + Object.defineProperty(window, "innerWidth", { |
| 74 | + configurable: true, |
| 75 | + value: 1024, |
| 76 | + writable: true, |
| 77 | + }); |
| 78 | + Object.defineProperty(window, "innerHeight", { |
| 79 | + configurable: true, |
| 80 | + value: 768, |
| 81 | + writable: true, |
| 82 | + }); |
| 83 | + |
| 84 | + const { result } = renderHook(() => useWindowResize(200)); |
| 85 | + |
| 86 | + // Simulate multiple rapid resize events |
| 87 | + act(() => { |
| 88 | + Object.defineProperty(window, "innerWidth", { |
| 89 | + configurable: true, |
| 90 | + value: 1920, |
| 91 | + writable: true, |
| 92 | + }); |
| 93 | + Object.defineProperty(window, "innerHeight", { |
| 94 | + configurable: true, |
| 95 | + value: 1080, |
| 96 | + writable: true, |
| 97 | + }); |
| 98 | + window.dispatchEvent(new Event("resize")); |
| 99 | + |
| 100 | + // Fast-forward only 100ms - should not update yet |
| 101 | + jest.advanceTimersByTime(100); |
| 102 | + }); |
| 103 | + |
| 104 | + // Should still have initial dimensions |
| 105 | + expect(result.current).toEqual({ height: 768, width: 1024 }); |
| 106 | + |
| 107 | + act(() => { |
| 108 | + // Fast-forward remaining 100ms |
| 109 | + jest.advanceTimersByTime(100); |
| 110 | + }); |
| 111 | + |
| 112 | + // Now should have updated dimensions |
| 113 | + expect(result.current).toEqual({ height: 1080, width: 1920 }); |
| 114 | + }); |
| 115 | + |
| 116 | + test("cleans up event listener on unmount", () => { |
| 117 | + const removeEventListenerSpy = jest.spyOn(window, "removeEventListener"); |
| 118 | + |
| 119 | + const { unmount } = renderHook(() => useWindowResize()); |
| 120 | + |
| 121 | + unmount(); |
| 122 | + |
| 123 | + expect(removeEventListenerSpy).toHaveBeenCalledWith( |
| 124 | + "resize", |
| 125 | + expect.any(Function), |
| 126 | + ); |
| 127 | + |
| 128 | + removeEventListenerSpy.mockRestore(); |
| 129 | + }); |
| 130 | +}); |
0 commit comments