17a82832ab
Replace the username/password stub with Balinyaar's real credential (phone-OTP) and add the role router every authenticated screen sits behind. - services/auth rewritten for OTP over the b2 contract: types/keys/constants, clientApi+mockApi behind a config'd seam, hooks (useRequestOtp/useVerifyOtp/ useMe/useRefresh/useLogout/useSelectRole/useSessionRoleSync). Stub removed. - A1/A2 customer login + B1/B2 nurse switch as one OTP flow at /login (PhoneStep/OtpStep: auto-verify, resend countdown, wrong/expired/lockout states). - Role router: pure resolveRoleDestination + RoleRouter -> family / nurse / admin / select-role, with a splash while /me loads (no wrong-shell flash). - SelectRole first-use screen at /select-role. - Widened AuthState (roles via SessionUser), hydrated from /me by useSessionRoleSync. - Fetch-layer silent token refresh (single-flight + one retry) + shared persistAuthTokens/clearAuthTokens; useRefresh as the on-demand path. - auth i18n namespace in both locales; tests for routing branches, countdown, OtpStep state machine, RoleRouter branches; fixed jest @/ -> src alias. - Docs: client/CLAUDE.md, frontend STATUS/report, for-backend REQ-002..004. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
49 lines
1.6 KiB
TypeScript
49 lines
1.6 KiB
TypeScript
import { act, renderHook } from '@testing-library/react';
|
|
import { useCountdown } from './useCountdown';
|
|
|
|
describe('useCountdown', () => {
|
|
beforeEach(() => jest.useFakeTimers());
|
|
afterEach(() => jest.useRealTimers());
|
|
|
|
it('starts at zero and inactive', () => {
|
|
const { result } = renderHook(() => useCountdown());
|
|
expect(result.current.seconds).toBe(0);
|
|
expect(result.current.isActive).toBe(false);
|
|
});
|
|
|
|
it('counts down one second at a time and stops at zero', () => {
|
|
const { result } = renderHook(() => useCountdown());
|
|
act(() => result.current.start(3));
|
|
expect(result.current.seconds).toBe(3);
|
|
expect(result.current.isActive).toBe(true);
|
|
|
|
act(() => jest.advanceTimersByTime(1000));
|
|
expect(result.current.seconds).toBe(2);
|
|
|
|
act(() => jest.advanceTimersByTime(2000));
|
|
expect(result.current.seconds).toBe(0);
|
|
expect(result.current.isActive).toBe(false);
|
|
|
|
// No leaked interval keeps decrementing past zero.
|
|
act(() => jest.advanceTimersByTime(3000));
|
|
expect(result.current.seconds).toBe(0);
|
|
});
|
|
|
|
it('restarting resets the remaining seconds', () => {
|
|
const { result } = renderHook(() => useCountdown());
|
|
act(() => result.current.start(2));
|
|
act(() => jest.advanceTimersByTime(1000));
|
|
expect(result.current.seconds).toBe(1);
|
|
act(() => result.current.start(5));
|
|
expect(result.current.seconds).toBe(5);
|
|
});
|
|
|
|
it('stop() halts the countdown', () => {
|
|
const { result } = renderHook(() => useCountdown());
|
|
act(() => result.current.start(5));
|
|
act(() => result.current.stop());
|
|
act(() => jest.advanceTimersByTime(3000));
|
|
expect(result.current.seconds).toBe(5);
|
|
});
|
|
});
|