From 5fea96ec516466d8946fd8afff843c09a5541ea8 Mon Sep 17 00:00:00 2001 From: Nandini Bajaj <90183734+Nandinibajaj16@users.noreply.github.com> Date: Mon, 21 Oct 2024 21:15:23 +0530 Subject: [PATCH] test: adds unit tests for the hook useIsDesktopInternal (#240) --- .../internal/useIsDesktopInternal.test.ts | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 __tests__/hooks/internal/useIsDesktopInternal.test.ts diff --git a/__tests__/hooks/internal/useIsDesktopInternal.test.ts b/__tests__/hooks/internal/useIsDesktopInternal.test.ts new file mode 100644 index 00000000..9f9b0e0c --- /dev/null +++ b/__tests__/hooks/internal/useIsDesktopInternal.test.ts @@ -0,0 +1,76 @@ +import { renderHook } from '@testing-library/react'; +import { useIsDesktopInternal} from "../../../src/hooks/internal/useIsDesktopInternal"; + +const originalNavigator = window.navigator; +const originalInnerWidth = window.innerWidth; + +const mockWindowProperty = (property: string, value: any) => { + Object.defineProperty(window, property, { + configurable: true, + writable: true, + value, + }); +}; + +describe('useIsDesktopInternal', () => { + afterEach(() => { + Object.defineProperty(window, 'navigator', { + configurable: true, + writable: true, + value: originalNavigator, + }); + Object.defineProperty(window, 'innerWidth', { + configurable: true, + writable: true, + value: originalInnerWidth, + }); + }); + + it('should return false when running on server-side (no window object)', () => { + mockWindowProperty('navigator', undefined); + mockWindowProperty('innerWidth', 0); + + const { result } = renderHook(() => useIsDesktopInternal()); + expect(result.current).toBe(false); + }); + + it('should return false for a mobile user agent', () => { + mockWindowProperty('navigator', { + userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15A372 Safari/604.1', + }); + mockWindowProperty('innerWidth', 800); + + const { result } = renderHook(() => useIsDesktopInternal()); + expect(result.current).toBe(false); + }); + + it('should return true for a desktop user agent and wide screen', () => { + mockWindowProperty('navigator', { + userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36', + }); + mockWindowProperty('innerWidth', 1024); + + const { result } = renderHook(() => useIsDesktopInternal()); + expect(result.current).toBe(true); + }); + + it('should return false for a narrow screen, even on a desktop user agent', () => { + mockWindowProperty('navigator', { + userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36', + }); + mockWindowProperty('innerWidth', 500); + + const { result } = renderHook(() => useIsDesktopInternal()); + expect(result.current).toBe(false); + }); + + it('should return true if user agent is not mobile and window width is exactly 768', () => { + mockWindowProperty('navigator', { + userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36', + }); + mockWindowProperty('innerWidth', 768); + + const { result } = renderHook(() => useIsDesktopInternal()); + expect(result.current).toBe(true); + }); +});