freshyo/apps/user-ui/tests/mocks/auth.ts
2026-09-26 00:46:08 +05:30

42 lines
1.1 KiB
TypeScript

/**
* Overridable AuthContext mock.
*
* tests/setup.ts stubs @/src/contexts/AuthContext with this factory so a test
* can flip the signed-in shape. Auth is otherwise out of scope for these tests:
* the real provider would drag in secure-store, the tRPC login flow and redirect
* handling.
*
* Set `authMock.userDetails` in a beforeEach and reset it with resetAuthMock().
* NextOrderGlimpse (rendered by the home page) returns null unless it is set.
*/
import type { ReactNode } from 'react'
export interface AuthMockState {
user: unknown
userDetails: unknown
}
export const authMock: AuthMockState = {
user: null,
userDetails: null,
}
export function resetAuthMock() {
authMock.user = null
authMock.userDetails = null
}
export function authContextMock() {
return {
AuthProvider: ({ children }: { children?: ReactNode }) => children ?? null,
useAuth: () => ({
user: authMock.user,
userDetails: authMock.userDetails,
isAuthenticated: !!authMock.user,
isLoading: false,
token: null,
}),
useUserDetails: () => authMock.userDetails,
}
}