|
| 1 | +import { renderHook, act } from "@testing-library/react"; |
| 2 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 3 | + |
| 4 | +import { useIsMobile } from "@/hooks/use-mobile"; |
| 5 | + |
| 6 | +describe("useIsMobile", () => { |
| 7 | + beforeEach(() => { |
| 8 | + // Mock window.innerWidth |
| 9 | + Object.defineProperty(window, "innerWidth", { |
| 10 | + writable: true, |
| 11 | + configurable: true, |
| 12 | + value: 1024, |
| 13 | + }); |
| 14 | + |
| 15 | + // Mock window.matchMedia |
| 16 | + Object.defineProperty(window, "matchMedia", { |
| 17 | + writable: true, |
| 18 | + configurable: true, |
| 19 | + value: vi.fn().mockImplementation((query) => ({ |
| 20 | + matches: false, |
| 21 | + media: query, |
| 22 | + onchange: null, |
| 23 | + addListener: vi.fn(), // deprecated |
| 24 | + removeListener: vi.fn(), // deprecated |
| 25 | + addEventListener: vi.fn(), |
| 26 | + removeEventListener: vi.fn(), |
| 27 | + dispatchEvent: vi.fn(), |
| 28 | + })), |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + vi.clearAllMocks(); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should return false when viewport width is >= 768px", () => { |
| 37 | + Object.defineProperty(window, "innerWidth", { value: 1024 }); |
| 38 | + const { result } = renderHook(() => useIsMobile()); |
| 39 | + expect(result.current).toBe(false); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should return true when viewport width is < 768px", () => { |
| 43 | + Object.defineProperty(window, "innerWidth", { value: 500 }); |
| 44 | + const { result } = renderHook(() => useIsMobile()); |
| 45 | + expect(result.current).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should return false when viewport width is exactly 768px (boundary condition)", () => { |
| 49 | + Object.defineProperty(window, "innerWidth", { value: 768 }); |
| 50 | + const { result } = renderHook(() => useIsMobile()); |
| 51 | + expect(result.current).toBe(false); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should update when window is resized", () => { |
| 55 | + Object.defineProperty(window, "innerWidth", { value: 1024, writable: true }); |
| 56 | + |
| 57 | + let changeCallback: () => void = () => {}; |
| 58 | + |
| 59 | + // Mock matchMedia to capture the change callback |
| 60 | + (window.matchMedia as any).mockImplementation((query: string) => ({ |
| 61 | + matches: false, |
| 62 | + media: query, |
| 63 | + onchange: null, |
| 64 | + addEventListener: vi.fn((event, cb) => { |
| 65 | + if (event === "change") changeCallback = cb; |
| 66 | + }), |
| 67 | + removeEventListener: vi.fn(), |
| 68 | + })); |
| 69 | + |
| 70 | + const { result } = renderHook(() => useIsMobile()); |
| 71 | + expect(result.current).toBe(false); |
| 72 | + |
| 73 | + // Simulate resize to mobile width |
| 74 | + act(() => { |
| 75 | + (window as any).innerWidth = 500; |
| 76 | + changeCallback(); |
| 77 | + }); |
| 78 | + expect(result.current).toBe(true); |
| 79 | + |
| 80 | + // Simulate resize back to desktop width |
| 81 | + act(() => { |
| 82 | + (window as any).innerWidth = 1024; |
| 83 | + changeCallback(); |
| 84 | + }); |
| 85 | + expect(result.current).toBe(false); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should clean up event listener on unmount", () => { |
| 89 | + const removeEventListenerMock = vi.fn(); |
| 90 | + |
| 91 | + (window.matchMedia as any).mockImplementation((query: string) => ({ |
| 92 | + matches: false, |
| 93 | + media: query, |
| 94 | + onchange: null, |
| 95 | + addEventListener: vi.fn(), |
| 96 | + removeEventListener: removeEventListenerMock, |
| 97 | + })); |
| 98 | + |
| 99 | + const { unmount } = renderHook(() => useIsMobile()); |
| 100 | + unmount(); |
| 101 | + |
| 102 | + expect(removeEventListenerMock).toHaveBeenCalledWith("change", expect.any(Function)); |
| 103 | + }); |
| 104 | +}); |
0 commit comments