192 lines
7.9 KiB
TypeScript
192 lines
7.9 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 3.2 — Login (`tests/pages/login.test.tsx`).
|
|
*/
|
|
import React from 'react'
|
|
import { Alert } from 'react-native'
|
|
import { fireEvent, waitFor } from '@testing-library/react-native'
|
|
|
|
import Login from '@/app/login'
|
|
import { renderWithProviders, flush } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import { setProcedure, lastMutationInput, procedureMutationCalls } from '../mocks/trpc-client'
|
|
import { router } from '../mocks/expo-router-mock'
|
|
import { __snapshot } from '../mocks/secure-store-mock'
|
|
import { STAFF, TEST_JWT } from '../fixtures'
|
|
|
|
describe('Login (ALOGIN-01..14)', () => {
|
|
let alertSpy: jest.SpyInstance
|
|
|
|
beforeEach(() => {
|
|
resetAllMocks()
|
|
alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
|
|
})
|
|
|
|
afterEach(() => alertSpy?.mockRestore())
|
|
|
|
it('ALOGIN-01: renders the login form', () => {
|
|
const view = renderWithProviders(<Login />, { auth: 'out' })
|
|
|
|
expect(view.getByText('Admin Login')).toBeTruthy()
|
|
expect(view.getByTestId('login-name-input')).toBeTruthy()
|
|
expect(view.getByTestId('login-password-input')).toBeTruthy()
|
|
expect(view.getByText('Login')).toBeTruthy()
|
|
})
|
|
|
|
it('ALOGIN-02: both fields empty shows the validation alert and makes no API call', () => {
|
|
const view = renderWithProviders(<Login />, { auth: 'out' })
|
|
|
|
fireEvent.press(view.getByTestId('login-button'))
|
|
|
|
expect(alertSpy).toHaveBeenCalledWith('Error', 'Please enter both name and password')
|
|
expect(procedureMutationCalls('admin.staffUser.login')).toHaveLength(0)
|
|
})
|
|
|
|
it('ALOGIN-03: whitespace-only name shows the validation alert', () => {
|
|
const view = renderWithProviders(<Login />, { auth: 'out' })
|
|
|
|
fireEvent.changeText(view.getByTestId('login-name-input'), ' ')
|
|
fireEvent.changeText(view.getByTestId('login-password-input'), 'secret')
|
|
fireEvent.press(view.getByTestId('login-button'))
|
|
|
|
expect(alertSpy).toHaveBeenCalledWith('Error', 'Please enter both name and password')
|
|
expect(procedureMutationCalls('admin.staffUser.login')).toHaveLength(0)
|
|
})
|
|
|
|
it('ALOGIN-04: whitespace-only password shows the validation alert', () => {
|
|
const view = renderWithProviders(<Login />, { auth: 'out' })
|
|
|
|
fireEvent.changeText(view.getByTestId('login-name-input'), 'Admin One')
|
|
fireEvent.changeText(view.getByTestId('login-password-input'), ' ')
|
|
fireEvent.press(view.getByTestId('login-button'))
|
|
|
|
expect(alertSpy).toHaveBeenCalledWith('Error', 'Please enter both name and password')
|
|
expect(procedureMutationCalls('admin.staffUser.login')).toHaveLength(0)
|
|
})
|
|
|
|
it('ALOGIN-05: a valid submit calls admin.staffUser.login and trims the name', async () => {
|
|
const view = renderWithProviders(<Login />, { auth: 'out' })
|
|
|
|
fireEvent.changeText(view.getByTestId('login-name-input'), ' Admin One ')
|
|
fireEvent.changeText(view.getByTestId('login-password-input'), 'secret')
|
|
fireEvent.press(view.getByTestId('login-button'))
|
|
await flush(2)
|
|
|
|
await waitFor(() =>
|
|
expect(lastMutationInput('admin.staffUser.login')).toEqual({ name: 'Admin One', password: 'secret' })
|
|
)
|
|
})
|
|
|
|
it('ALOGIN-06: the password is sent exactly as typed (not trimmed)', async () => {
|
|
const view = renderWithProviders(<Login />, { auth: 'out' })
|
|
|
|
fireEvent.changeText(view.getByTestId('login-name-input'), 'Admin One')
|
|
fireEvent.changeText(view.getByTestId('login-password-input'), ' pass word ')
|
|
fireEvent.press(view.getByTestId('login-button'))
|
|
await flush(2)
|
|
|
|
await waitFor(() =>
|
|
expect(lastMutationInput('admin.staffUser.login')).toEqual({
|
|
name: 'Admin One',
|
|
password: ' pass word ',
|
|
})
|
|
)
|
|
})
|
|
|
|
it('ALOGIN-07: success persists the JWT + staff and replaces to the dashboard', async () => {
|
|
setProcedure('admin.staffUser.login', { data: { token: TEST_JWT, staff: STAFF } })
|
|
const view = renderWithProviders(<Login />, { auth: 'out' })
|
|
|
|
fireEvent.changeText(view.getByTestId('login-name-input'), 'Admin One')
|
|
fireEvent.changeText(view.getByTestId('login-password-input'), 'secret')
|
|
fireEvent.press(view.getByTestId('login-button'))
|
|
await flush(3)
|
|
|
|
await waitFor(() => {
|
|
expect(__snapshot().jwt_token).toBe(TEST_JWT)
|
|
expect(JSON.parse(__snapshot().staff_session)).toEqual(STAFF)
|
|
})
|
|
expect(router.replace).toHaveBeenCalledWith('/(drawer)/dashboard')
|
|
})
|
|
|
|
it('ALOGIN-08: invalid credentials show loginError and stay on the page', async () => {
|
|
setProcedure('admin.staffUser.login', { mode: 'error', error: new Error('Invalid credentials') })
|
|
const view = renderWithProviders(<Login />, { auth: 'out' })
|
|
|
|
fireEvent.changeText(view.getByTestId('login-name-input'), 'Admin One')
|
|
fireEvent.changeText(view.getByTestId('login-password-input'), 'wrong')
|
|
fireEvent.press(view.getByTestId('login-button'))
|
|
await flush(3)
|
|
|
|
expect(view.getByText('Invalid credentials')).toBeTruthy()
|
|
expect(router.replace).not.toHaveBeenCalledWith('/(drawer)/dashboard')
|
|
})
|
|
|
|
it('ALOGIN-09: a server error without a message shows "Login failed"', async () => {
|
|
setProcedure('admin.staffUser.login', { mode: 'error', error: new Error('') })
|
|
const view = renderWithProviders(<Login />, { auth: 'out' })
|
|
|
|
fireEvent.changeText(view.getByTestId('login-name-input'), 'Admin One')
|
|
fireEvent.changeText(view.getByTestId('login-password-input'), 'wrong')
|
|
fireEvent.press(view.getByTestId('login-button'))
|
|
|
|
expect(await view.findByText('Login failed')).toBeTruthy()
|
|
})
|
|
|
|
it('ALOGIN-10: a pending login shows "Logging in..." and disables the button', async () => {
|
|
setProcedure('admin.staffUser.login', { mode: 'pending' })
|
|
const view = renderWithProviders(<Login />, { auth: 'out' })
|
|
|
|
fireEvent.changeText(view.getByTestId('login-name-input'), 'Admin One')
|
|
fireEvent.changeText(view.getByTestId('login-password-input'), 'secret')
|
|
fireEvent.press(view.getByTestId('login-button'))
|
|
await flush(2)
|
|
|
|
expect(view.getByText('Logging in...')).toBeTruthy()
|
|
})
|
|
|
|
it('ALOGIN-11: loginError is cleared on a new attempt', async () => {
|
|
setProcedure('admin.staffUser.login', { mode: 'error', error: new Error('Invalid credentials') })
|
|
const view = renderWithProviders(<Login />, { auth: 'out' })
|
|
|
|
fireEvent.changeText(view.getByTestId('login-name-input'), 'Admin One')
|
|
fireEvent.changeText(view.getByTestId('login-password-input'), 'wrong')
|
|
fireEvent.press(view.getByTestId('login-button'))
|
|
await flush(2)
|
|
expect(view.getByText('Invalid credentials')).toBeTruthy()
|
|
|
|
setProcedure('admin.staffUser.login', { mode: 'pending' })
|
|
fireEvent.press(view.getByTestId('login-button'))
|
|
await flush(2)
|
|
|
|
expect(view.queryByText('Invalid credentials')).toBeNull()
|
|
})
|
|
|
|
it('ALOGIN-12: the password field is masked', () => {
|
|
const view = renderWithProviders(<Login />, { auth: 'out' })
|
|
expect(view.getByTestId('login-password-input').props.secureTextEntry).toBe(true)
|
|
})
|
|
|
|
it('ALOGIN-13: a successful login while already signed in replaces to the dashboard', async () => {
|
|
const view = renderWithProviders(<Login />, { auth: 'in' })
|
|
await flush(2)
|
|
|
|
fireEvent.changeText(view.getByTestId('login-name-input'), 'Admin One')
|
|
fireEvent.changeText(view.getByTestId('login-password-input'), 'secret')
|
|
fireEvent.press(view.getByTestId('login-button'))
|
|
await flush(3)
|
|
|
|
expect(router.replace).toHaveBeenCalledWith('/(drawer)/dashboard')
|
|
})
|
|
|
|
it('ALOGIN-14: a storage save failure still completes the login redirect', async () => {
|
|
const view = renderWithProviders(<Login />, { auth: 'out' })
|
|
fireEvent.changeText(view.getByTestId('login-name-input'), 'Admin One')
|
|
fireEvent.changeText(view.getByTestId('login-password-input'), 'secret')
|
|
fireEvent.press(view.getByTestId('login-button'))
|
|
await flush(3)
|
|
|
|
expect(router.replace).toHaveBeenCalledWith('/(drawer)/dashboard')
|
|
expect(view.queryByText('Login')).toBeTruthy()
|
|
})
|
|
})
|