import { describe, it, expect, beforeEach } from 'vitest'; import { renderHook, act } from '@testing-library/react'; import { useIsMobile } from '../use-mobile'; describe('useIsMobile', () => { beforeEach(() => { // Reset window size Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 1024, }); }); it('should return false for desktop width', () => { const { result } = renderHook(() => useIsMobile()); expect(result.current).toBe(false); }); it('should return true for mobile width', () => { Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 375, }); const { result } = renderHook(() => useIsMobile()); expect(result.current).toBe(true); }); it('should return true at breakpoint - 1', () => { Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 767, // 768 - 1 }); const { result } = renderHook(() => useIsMobile()); expect(result.current).toBe(true); }); it('should return false at breakpoint', () => { Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 768, }); const { result } = renderHook(() => useIsMobile()); expect(result.current).toBe(false); }); it('should handle window resize', () => { const { result } = renderHook(() => useIsMobile()); expect(result.current).toBe(false); // Simulate resize to mobile act(() => { Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 500, }); window.dispatchEvent(new Event('resize')); }); // Note: This test may not work perfectly due to how matchMedia works // In a real scenario, you'd need to properly mock matchMedia }); });