89 lines
3.2 KiB
TypeScript
89 lines
3.2 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 24 — "LoginWall" (`tests/components/login-wall.test.tsx`).
|
|
*
|
|
* The wall only records the redirect state and pushes the login route; the
|
|
* `expo-router` mock records navigation instead of performing it, and
|
|
* `StorageServiceCasual` runs against the in-memory SecureStore mock.
|
|
*/
|
|
import * as React from 'react'
|
|
import { fireEvent, waitFor } from '@testing-library/react-native'
|
|
import { StorageServiceCasual } from 'common-ui'
|
|
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import { renderWithProviders } from '../helpers/test-utils'
|
|
import { __snapshot } from '../mocks/secure-store-mock'
|
|
import { router, __setPathname } from '../mocks/expo-router-mock'
|
|
|
|
import LoginWall from '@/components/LoginWall'
|
|
import constants from '@/src/constants'
|
|
|
|
describe('LoginWall', () => {
|
|
beforeEach(() => resetAllMocks())
|
|
|
|
it('LW-01: renders the default copy', () => {
|
|
const { getByText } = renderWithProviders(<LoginWall />, { auth: false })
|
|
|
|
expect(getByText('Please log in')).toBeTruthy()
|
|
expect(getByText('Log in to continue.')).toBeTruthy()
|
|
})
|
|
|
|
it('LW-02: renders a custom title and message', () => {
|
|
const { getByText } = renderWithProviders(
|
|
<LoginWall title="Sign in required" message="Only members can see this page." />,
|
|
{ auth: false }
|
|
)
|
|
|
|
expect(getByText('Sign in required')).toBeTruthy()
|
|
expect(getByText('Only members can see this page.')).toBeTruthy()
|
|
expect(getByText('Log In')).toBeTruthy()
|
|
})
|
|
|
|
it('LW-03: Log In stores the redirect target/query/timestamp and pushes login', async () => {
|
|
__setPathname('/me')
|
|
const { getByTestId } = renderWithProviders(<LoginWall />, { auth: false })
|
|
|
|
fireEvent.press(getByTestId('login-wall-login'))
|
|
|
|
await waitFor(() => expect(router.push).toHaveBeenCalledWith('/(auth)/login'))
|
|
|
|
const stored = __snapshot()[constants.AUTH_REDIRECT_KEY]
|
|
expect(stored).toBeTruthy()
|
|
const parsed = JSON.parse(stored)
|
|
expect(parsed.targetUrl).toBe('/me')
|
|
expect(parsed.queryParams).toEqual({})
|
|
expect(typeof parsed.timestamp).toBe('number')
|
|
})
|
|
|
|
it('LW-04: still pushes login when storing the redirect fails', async () => {
|
|
const setItemSpy = jest
|
|
.spyOn(StorageServiceCasual, 'setItem')
|
|
.mockRejectedValueOnce(new Error('storage unavailable'))
|
|
|
|
const { getByTestId } = renderWithProviders(<LoginWall />, { auth: false })
|
|
|
|
fireEvent.press(getByTestId('login-wall-login'))
|
|
|
|
await waitFor(() => expect(router.push).toHaveBeenCalledWith('/(auth)/login'))
|
|
expect(setItemSpy).toHaveBeenCalled()
|
|
expect(__snapshot()[constants.AUTH_REDIRECT_KEY]).toBeUndefined()
|
|
|
|
setItemSpy.mockRestore()
|
|
})
|
|
|
|
it('LW-05: renders Go Back and calls onBack when pressed', () => {
|
|
const onBack = jest.fn()
|
|
const { getByTestId } = renderWithProviders(<LoginWall onBack={onBack} />, { auth: false })
|
|
|
|
fireEvent.press(getByTestId('login-wall-back'))
|
|
|
|
expect(onBack).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('LW-06: omits Go Back when no onBack handler is given', () => {
|
|
const { queryByTestId, queryByText } = renderWithProviders(<LoginWall />, { auth: false })
|
|
|
|
expect(queryByText('Go Back')).toBeNull()
|
|
expect(queryByTestId('login-wall-back')).toBeNull()
|
|
})
|
|
})
|