import { Helpers } from '../helpers'; // Mock navigator for testing const mockNavigator = (userAgent: string, maxTouchPoints: number = 0) => { Object.defineProperty(window, 'navigator', { value: { userAgent, maxTouchPoints, }, writable: true, }); }; // Mock touch support const mockTouchSupport = (hasTouch: boolean) => { if (hasTouch) { Object.defineProperty(window, 'ontouchstart', { value: () => {}, writable: true, }); } else { delete (window as any).ontouchstart; } }; describe('helpers', () => { describe('chunkArray', () => { it('should split array into chunks', () => { const arr = [1, 2, 3, 4, 5, 6, 7, 8]; const chunks = Helpers.chunkArray(arr, 3); expect(chunks).toStrictEqual([ [1, 2, 3], [4, 5, 6], [7, 8], ]); }); it('should return only one chunk', () => { const arr = [1, 2, 3, 4, 5, 6, 7, 8]; const chunks = Helpers.chunkArray(arr, 10); expect(chunks).toStrictEqual([[1, 2, 3, 4, 5, 6, 7, 8]]); }); it('should return an empty array', () => { const arr: any[] = []; const chunks = Helpers.chunkArray(arr, 10); expect(chunks).toStrictEqual([]); }); }); describe('isMobileDevice', () => { afterEach(() => { // Reset to default desktop state mockNavigator('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'); mockTouchSupport(false); }); it('should return true for iPhone', () => { mockNavigator('Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15'); mockTouchSupport(true); expect(Helpers.isMobileDevice()).toBe(true); }); it('should return true for iPad', () => { mockNavigator('Mozilla/5.0 (iPad; CPU OS 14_0 like Mac OS X) AppleWebKit/605.1.15'); mockTouchSupport(true); expect(Helpers.isMobileDevice()).toBe(true); }); it('should return true for Android', () => { mockNavigator('Mozilla/5.0 (Linux; Android 10; SM-G975F) AppleWebKit/537.36'); mockTouchSupport(true); expect(Helpers.isMobileDevice()).toBe(true); }); it('should return false for desktop without touch', () => { mockNavigator('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'); mockTouchSupport(false); expect(Helpers.isMobileDevice()).toBe(false); }); it('should return false for desktop with touch but no mobile user agent', () => { mockNavigator('Mozilla/5.0 (Windows NT 10.0; Touch; Win64; x64) AppleWebKit/537.36'); mockTouchSupport(true); expect(Helpers.isMobileDevice()).toBe(false); }); it('should return false when navigator is undefined', () => { Object.defineProperty(window, 'navigator', { value: undefined, writable: true, }); expect(Helpers.isMobileDevice()).toBe(false); }); }); });