63 lines
1.7 KiB
TypeScript
63 lines
1.7 KiB
TypeScript
/**
|
|
* Overridable AuthContext mock with spies.
|
|
*
|
|
* tests/setup.ts stubs @/src/contexts/AuthContext with this factory so a test
|
|
* can flip the signed-in shape and assert that a button called login/register/
|
|
* logout. Auth is otherwise out of scope: the real provider would drag in
|
|
* secure-store, the tRPC login flow and redirect handling.
|
|
*
|
|
* Mutate the state fields in a beforeEach and reset with resetAuthMock().
|
|
* The jest.fn spies are cleared automatically by `clearMocks: true`.
|
|
*/
|
|
|
|
import type { ReactNode } from 'react'
|
|
|
|
export interface AuthMockState {
|
|
user: unknown
|
|
userDetails: unknown
|
|
isLoading: boolean
|
|
token: unknown
|
|
loginWithToken: jest.Mock
|
|
login: jest.Mock
|
|
register: jest.Mock
|
|
logout: jest.Mock
|
|
updateUserDetails: jest.Mock
|
|
}
|
|
|
|
export const authMock: AuthMockState = {
|
|
user: null,
|
|
userDetails: null,
|
|
isLoading: false,
|
|
token: null,
|
|
loginWithToken: jest.fn(),
|
|
login: jest.fn(),
|
|
register: jest.fn(),
|
|
logout: jest.fn(),
|
|
updateUserDetails: jest.fn(),
|
|
}
|
|
|
|
export function resetAuthMock() {
|
|
authMock.user = null
|
|
authMock.userDetails = null
|
|
authMock.isLoading = false
|
|
authMock.token = null
|
|
}
|
|
|
|
export function authContextMock() {
|
|
return {
|
|
AuthProvider: ({ children }: { children?: ReactNode }) => children ?? null,
|
|
useAuth: () => ({
|
|
user: authMock.user,
|
|
userDetails: authMock.userDetails,
|
|
isAuthenticated: !!authMock.user,
|
|
isLoading: authMock.isLoading,
|
|
token: authMock.token,
|
|
loginWithToken: authMock.loginWithToken,
|
|
login: authMock.login,
|
|
register: authMock.register,
|
|
logout: authMock.logout,
|
|
updateUserDetails: authMock.updateUserDetails,
|
|
}),
|
|
useUserDetails: () => authMock.userDetails,
|
|
}
|
|
}
|