Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,18 @@
"output": "static",
"favicon": "./assets/icon.png"
},
"updates": {
"url": "https://u.expo.dev/teachlink-mobile",
"enabled": true,
"checkAutomatically": "ON_LOAD",
"fallbackToCacheTimeout": 0
},
"runtimeVersion": {
"policy": "appVersion"
},
"plugins": [
"expo-router",
"expo-updates",
[
"expo-splash-screen",
{
Expand Down
21 changes: 21 additions & 0 deletions app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
import '../global.css'; // NativeWind CSS
import { AnalyticsProvider, ErrorBoundary, OfflineIndicatorProvider } from '../src/components';
import { KeyboardDelegateProvider } from '../src/components/common/KeyboardDelegateProvider';
import { UpdateNotificationModal } from '../src/components/common/UpdateNotificationModal';
import { useAnalytics } from '../src/hooks';
import { useAppUpdate } from '../src/hooks/useAppUpdate';
import { useDeepLink } from '../src/hooks/useDeepLink';
import { preloadService } from '../src/services/preloadService';
import { sessionRestorationService } from '../src/services/sessionRestoration';
import { scrollPositionService } from '../src/services/scrollPositionService';

Check warning on line 19 in app/_layout.tsx

View workflow job for this annotation

GitHub Actions / ci

`../src/services/scrollPositionService` import should occur before import of `../src/services/sessionRestoration`
import { useAppStore } from '../src/store';
import { getPathFromDeepLink } from '../src/utils/linkParser';
import { prefetchExternalResources } from '../src/utils/resourceHints';
import AppLifecycleManager from '../src/components/AppLifecycleManager';

Check warning on line 23 in app/_layout.tsx

View workflow job for this annotation

GitHub Actions / ci

`../src/components/AppLifecycleManager` import should occur before import of `../src/components/common/KeyboardDelegateProvider`

// Kick off resource hints early
prefetchExternalResources();
Expand Down Expand Up @@ -65,6 +67,24 @@
return null;
};

const UpdateChecker = () => {
const { checkResult, isDownloading, error, applyUpdate, openStore, dismiss } = useAppUpdate(true);

const showModal = checkResult?.updateAvailable === true;

return (
<UpdateNotificationModal
visible={showModal}
checkResult={checkResult}
isDownloading={isDownloading}
error={error}
onApply={applyUpdate}
onOpenStore={openStore}
onDismiss={dismiss}
/>
);
};

const ThemeSync = () => {
const { theme } = useAppStore();
const { setColorScheme } = useColorScheme();
Expand Down Expand Up @@ -152,6 +172,7 @@
<AnalyticsProvider>
<ScreenTracker />
<ThemeSync />
<UpdateChecker />
<AppLifecycleManager />
<GestureHandlerRootView style={{ flex: 1 }}>
<OfflineIndicatorProvider>
Expand Down
20 changes: 20 additions & 0 deletions src/__mocks__/expo-updates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const isEmbeddedLaunch = false;

export const checkForUpdateAsync = jest.fn(async () => ({
isAvailable: false,
manifest: null,
}));

export const fetchUpdateAsync = jest.fn(async () => ({
isNew: true,
manifest: {},
}));

export const reloadAsync = jest.fn(async () => {});

export default {
isEmbeddedLaunch,
checkForUpdateAsync,
fetchUpdateAsync,
reloadAsync,
};
184 changes: 184 additions & 0 deletions src/__tests__/hooks/useAppUpdate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
import { act, renderHook } from '@testing-library/react-native';

import { useAppUpdate } from '../../hooks/useAppUpdate';
import { appUpdateService } from '../../services/appUpdateService';

jest.mock('../../services/appUpdateService', () => ({
appUpdateService: {
checkForUpdate: jest.fn(),
downloadAndApplyOtaUpdate: jest.fn(),
openStoreForUpdate: jest.fn(),
trackDismissed: jest.fn(),
shouldCheck: jest.fn(() => true),
getCurrentVersion: jest.fn(() => '1.4.0'),
},
}));

const mockService = appUpdateService as jest.Mocked<typeof appUpdateService>;

describe('useAppUpdate', () => {
beforeEach(() => {
jest.clearAllMocks();
mockService.shouldCheck.mockReturnValue(true);
});

describe('initial state', () => {
it('starts with no check result and no errors', () => {
mockService.checkForUpdate.mockResolvedValue({
updateAvailable: false,
updateType: 'none',
currentVersion: '1.4.0',
});

const { result } = renderHook(() => useAppUpdate(false));

expect(result.current.isChecking).toBe(false);
expect(result.current.isDownloading).toBe(false);
expect(result.current.checkResult).toBeNull();
expect(result.current.error).toBeNull();
});
});

describe('checkForUpdate', () => {
it('sets isChecking during check and clears it after', async () => {
mockService.checkForUpdate.mockResolvedValueOnce({
updateAvailable: false,
updateType: 'none',
currentVersion: '1.4.0',
});

const { result } = renderHook(() => useAppUpdate(false));

await act(async () => {
await result.current.checkForUpdate();
});

expect(result.current.isChecking).toBe(false);
expect(mockService.checkForUpdate).toHaveBeenCalledTimes(1);
});

it('populates checkResult when update is available', async () => {
const mockResult = {
updateAvailable: true,
updateType: 'ota' as const,
currentVersion: '1.4.0',
releaseNotes: 'Performance improvements',
isMandatory: false,
};
mockService.checkForUpdate.mockResolvedValueOnce(mockResult);

const { result } = renderHook(() => useAppUpdate(false));

await act(async () => {
await result.current.checkForUpdate();
});

expect(result.current.checkResult).toEqual(mockResult);
expect(result.current.error).toBeNull();
});

it('sets error when service throws', async () => {
mockService.checkForUpdate.mockRejectedValueOnce(new Error('Network timeout'));

const { result } = renderHook(() => useAppUpdate(false));

await act(async () => {
await result.current.checkForUpdate();
});

expect(result.current.error).toBe('Network timeout');
expect(result.current.isChecking).toBe(false);
});

it('auto-checks on mount when checkOnMount is true and shouldCheck returns true', async () => {
mockService.checkForUpdate.mockResolvedValueOnce({
updateAvailable: false,
updateType: 'none',
currentVersion: '1.4.0',
});

await act(async () => {
renderHook(() => useAppUpdate(true));
});

expect(mockService.checkForUpdate).toHaveBeenCalledTimes(1);
});

it('does not auto-check when shouldCheck returns false', async () => {
mockService.shouldCheck.mockReturnValue(false);

await act(async () => {
renderHook(() => useAppUpdate(true));
});

expect(mockService.checkForUpdate).not.toHaveBeenCalled();
});
});

describe('applyUpdate', () => {
it('calls downloadAndApplyOtaUpdate and clears downloading state', async () => {
mockService.downloadAndApplyOtaUpdate.mockResolvedValueOnce(true);

const { result } = renderHook(() => useAppUpdate(false));

await act(async () => {
await result.current.applyUpdate();
});

expect(mockService.downloadAndApplyOtaUpdate).toHaveBeenCalledTimes(1);
expect(result.current.isDownloading).toBe(false);
});

it('sets error when apply returns false', async () => {
mockService.downloadAndApplyOtaUpdate.mockResolvedValueOnce(false);

const { result } = renderHook(() => useAppUpdate(false));

await act(async () => {
await result.current.applyUpdate();
});

expect(result.current.error).toBe('Update could not be applied. Please try again.');
expect(result.current.isDownloading).toBe(false);
});
});

describe('openStore', () => {
it('delegates to service', async () => {
mockService.openStoreForUpdate.mockResolvedValueOnce(undefined);

const { result } = renderHook(() => useAppUpdate(false));

await act(async () => {
await result.current.openStore();
});

expect(mockService.openStoreForUpdate).toHaveBeenCalledTimes(1);
});
});

describe('dismiss', () => {
it('clears checkResult and tracks dismissal', async () => {
mockService.checkForUpdate.mockResolvedValueOnce({
updateAvailable: true,
updateType: 'ota',
currentVersion: '1.4.0',
});

const { result } = renderHook(() => useAppUpdate(false));

await act(async () => {
await result.current.checkForUpdate();
});

expect(result.current.checkResult).not.toBeNull();

act(() => {
result.current.dismiss();
});

expect(result.current.checkResult).toBeNull();
expect(mockService.trackDismissed).toHaveBeenCalledWith('ota');
});
});
});
Loading
Loading