Skip to content

Commit

Permalink
fix: Fix desktop hook test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
tjtanjin committed Oct 22, 2024
1 parent 5464151 commit 7f82d47
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
26 changes: 26 additions & 0 deletions __tests__/hooks/internal/useIsDesktopInternal.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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);
});
});
7 changes: 4 additions & 3 deletions src/hooks/internal/useIsDesktopInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 7f82d47

Please sign in to comment.