202 lines
6.2 KiB
TypeScript
202 lines
6.2 KiB
TypeScript
/**
|
|
* HealthTestWrapper + UpdateChecker.
|
|
* Health asserts common.healthCheck (retry refetches). UpdateChecker gates on
|
|
* the backend's internalAppVersion vs the shipped constants.INTERNAL_APP_VERSION
|
|
* (and is skipped on dev builds).
|
|
*/
|
|
|
|
jest.mock('expo-constants', () => ({
|
|
__esModule: true,
|
|
default: { expoConfig: { version: '1.0.0' } },
|
|
}))
|
|
|
|
import React from 'react'
|
|
import { Alert, Text } from 'react-native'
|
|
import { fireEvent, screen, waitFor } from '@testing-library/react-native'
|
|
import * as Updates from 'expo-updates'
|
|
|
|
import HealthTestWrapper from '@/components/HealthTestWrapper'
|
|
import UpdateChecker from '@/components/UpdateChecker'
|
|
import constants from '@/src/constants'
|
|
import {
|
|
ESSENTIAL_CONSTS_DATA,
|
|
resetEssentialConsts,
|
|
resetStorage,
|
|
setupApiMocks,
|
|
} from '../mocks/api'
|
|
import { procedureCalls } from '../mocks/procedures'
|
|
import { renderWithProviders } from '../helpers/render'
|
|
|
|
describe('HealthTestWrapper', () => {
|
|
beforeEach(async () => {
|
|
await resetStorage()
|
|
})
|
|
|
|
it('renders children when the backend is healthy', async () => {
|
|
setupApiMocks({
|
|
procedures: { 'common.healthCheck': { status: 'ok' } },
|
|
consts: { ...ESSENTIAL_CONSTS_DATA, versionNum: '0.0.1' },
|
|
})
|
|
renderWithProviders(
|
|
<HealthTestWrapper>
|
|
<Text>app content</Text>
|
|
</HealthTestWrapper>,
|
|
{ centralStores: false }
|
|
)
|
|
|
|
await waitFor(() => expect(screen.getByText('app content')).toBeVisible())
|
|
})
|
|
|
|
it('shows the service-unavailable state and retries', async () => {
|
|
const api = setupApiMocks({
|
|
procedures: {
|
|
'common.healthCheck': () => {
|
|
throw new Error('backend down')
|
|
},
|
|
},
|
|
})
|
|
renderWithProviders(
|
|
<HealthTestWrapper>
|
|
<Text>app content</Text>
|
|
</HealthTestWrapper>,
|
|
{ centralStores: false }
|
|
)
|
|
|
|
await waitFor(() => expect(screen.getByText('Service Unavailable')).toBeVisible())
|
|
expect(screen.queryByText('app content')).toBeNull()
|
|
|
|
const before = procedureCalls(api.fetchMock, 'common.healthCheck').length
|
|
fireEvent.press(screen.getByText('Retry'))
|
|
|
|
await waitFor(() =>
|
|
expect(procedureCalls(api.fetchMock, 'common.healthCheck').length).toBeGreaterThan(
|
|
before
|
|
)
|
|
)
|
|
})
|
|
|
|
it('prompts to update when the backend version is ahead', async () => {
|
|
setupApiMocks({
|
|
procedures: { 'common.healthCheck': { status: 'ok' } },
|
|
consts: {
|
|
...ESSENTIAL_CONSTS_DATA,
|
|
versionNum: '99.0.0',
|
|
playStoreUrl: 'https://play.example/app',
|
|
},
|
|
})
|
|
renderWithProviders(
|
|
<HealthTestWrapper>
|
|
<Text>app content</Text>
|
|
</HealthTestWrapper>,
|
|
{ centralStores: false }
|
|
)
|
|
|
|
await waitFor(() => expect(screen.getByText('Update Available')).toBeVisible())
|
|
})
|
|
})
|
|
|
|
describe('UpdateChecker', () => {
|
|
const originalDev = (global as unknown as { __DEV__: boolean }).__DEV__
|
|
|
|
/** Invoke a button inside a React Native Alert from the alert spy. */
|
|
const pressAlertButton = (label: string) => {
|
|
const calls = (Alert.alert as jest.Mock).mock.calls
|
|
const buttons = calls[calls.length - 1]?.[2] as
|
|
| { text: string; onPress?: () => void }[]
|
|
| undefined
|
|
const button = buttons?.find((b) => b.text === label)
|
|
if (!button?.onPress) throw new Error(`[tests] no alert button "${label}"`)
|
|
button.onPress()
|
|
}
|
|
|
|
beforeEach(async () => {
|
|
// The gate only runs on production builds; jest sets __DEV__ true.
|
|
;(global as unknown as { __DEV__: boolean }).__DEV__ = false
|
|
await resetEssentialConsts()
|
|
await resetStorage()
|
|
jest.spyOn(Alert, 'alert').mockImplementation(() => {})
|
|
})
|
|
|
|
afterEach(() => {
|
|
;(global as unknown as { __DEV__: boolean }).__DEV__ = originalDev
|
|
})
|
|
|
|
it('shows a blocking restart dialog when the backend version is ahead', async () => {
|
|
setupApiMocks({
|
|
consts: { ...ESSENTIAL_CONSTS_DATA, internalAppVersion: constants.INTERNAL_APP_VERSION + 1 },
|
|
})
|
|
renderWithProviders(
|
|
<UpdateChecker>
|
|
<Text>app content</Text>
|
|
</UpdateChecker>,
|
|
{ centralStores: false }
|
|
)
|
|
|
|
await waitFor(() =>
|
|
expect(Alert.alert).toHaveBeenCalledWith(
|
|
'Update available',
|
|
expect.any(String),
|
|
expect.any(Array),
|
|
expect.objectContaining({ cancelable: false })
|
|
)
|
|
)
|
|
|
|
// Only the restart action — no "Later" escape hatch.
|
|
const buttons = (Alert.alert as jest.Mock).mock.calls.at(-1)![2] as { text: string }[]
|
|
expect(buttons.map((b) => b.text)).toEqual(['Update & Restart'])
|
|
expect(screen.getByText('app content')).toBeVisible()
|
|
})
|
|
|
|
it('does not prompt when the shipped version matches the backend', async () => {
|
|
const api = setupApiMocks({
|
|
consts: { ...ESSENTIAL_CONSTS_DATA, internalAppVersion: constants.INTERNAL_APP_VERSION },
|
|
})
|
|
renderWithProviders(
|
|
<UpdateChecker>
|
|
<Text>app content</Text>
|
|
</UpdateChecker>,
|
|
{ centralStores: false }
|
|
)
|
|
|
|
await waitFor(() =>
|
|
expect(procedureCalls(api.fetchMock, 'common.essentialConsts').length).toBeGreaterThan(0)
|
|
)
|
|
expect(Alert.alert).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('does not prompt when the shipped version is ahead of the backend', async () => {
|
|
const api = setupApiMocks({
|
|
consts: { ...ESSENTIAL_CONSTS_DATA, internalAppVersion: constants.INTERNAL_APP_VERSION - 1 },
|
|
})
|
|
renderWithProviders(
|
|
<UpdateChecker>
|
|
<Text>app content</Text>
|
|
</UpdateChecker>,
|
|
{ centralStores: false }
|
|
)
|
|
|
|
await waitFor(() =>
|
|
expect(procedureCalls(api.fetchMock, 'common.essentialConsts').length).toBeGreaterThan(0)
|
|
)
|
|
expect(Alert.alert).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('fetches then reloads the update when the action is pressed', async () => {
|
|
setupApiMocks({
|
|
consts: { ...ESSENTIAL_CONSTS_DATA, internalAppVersion: constants.INTERNAL_APP_VERSION + 1 },
|
|
})
|
|
renderWithProviders(
|
|
<UpdateChecker>
|
|
<Text>app content</Text>
|
|
</UpdateChecker>,
|
|
{ centralStores: false }
|
|
)
|
|
|
|
await waitFor(() => expect(Alert.alert).toHaveBeenCalled())
|
|
|
|
pressAlertButton('Update & Restart')
|
|
|
|
await waitFor(() => expect(Updates.fetchUpdateAsync).toHaveBeenCalledTimes(1))
|
|
expect(Updates.reloadAsync).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|