diff --git a/__tests__/hooks/internal/useIsDesktopInternal.test.ts b/__tests__/hooks/internal/useIsDesktopInternal.test.ts index 9f9b0e0c..b1ef42b6 100644 --- a/__tests__/hooks/internal/useIsDesktopInternal.test.ts +++ b/__tests__/hooks/internal/useIsDesktopInternal.test.ts @@ -1,9 +1,14 @@ import { renderHook } from '@testing-library/react'; + +import { useSettingsContext } from "../../../src/context/SettingsContext"; import { useIsDesktopInternal} from "../../../src/hooks/internal/useIsDesktopInternal"; const originalNavigator = window.navigator; const originalInnerWidth = window.innerWidth; +// Mock the contexts +jest.mock("../../../src/context/SettingsContext"); + const mockWindowProperty = (property: string, value: any) => { Object.defineProperty(window, property, { configurable: true, @@ -13,6 +18,13 @@ const mockWindowProperty = (property: string, value: any) => { }; describe('useIsDesktopInternal', () => { + beforeEach(() => { + // default mock values + (useSettingsContext as jest.Mock).mockReturnValue({ + settings: { device: { applyMobileOptimizations: true } }, + }); + }); + afterEach(() => { Object.defineProperty(window, 'navigator', { configurable: true, @@ -73,4 +85,18 @@ describe('useIsDesktopInternal', () => { const { result } = renderHook(() => useIsDesktopInternal()); expect(result.current).toBe(true); }); + + it('should return true if mobile optimizations are not applied even if user is on mobile', () => { + (useSettingsContext as jest.Mock).mockReturnValue({ + settings: { device: { applyMobileOptimizations: false } }, + }); + + 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(true); + }); }); diff --git a/src/hooks/internal/useIsDesktopInternal.ts b/src/hooks/internal/useIsDesktopInternal.ts index db8676f4..250bc62f 100644 --- a/src/hooks/internal/useIsDesktopInternal.ts +++ b/src/hooks/internal/useIsDesktopInternal.ts @@ -6,14 +6,15 @@ export const useIsDesktopInternal = () => { const { settings } = useSettingsContext(); const isDesktop = useMemo(() => { + if (typeof window === 'undefined' || !window.navigator) { + return false; // Default to false if running on server-side + } + // if not applying mobile optimizations, can just use desktop behavior if (!settings.device?.applyMobileOptimizations) { return true; } - if (typeof window === 'undefined' || !window.navigator) { - return false; // Default to false if running on server-side - } const userAgent = navigator.userAgent; const isNotMobileUA = !(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(userAgent)); const isWideEnough = window.innerWidth >= 768;