freshyo/apps/admin-ui/tests/mocks/staff-auth.ts
2026-10-01 00:33:03 +05:30

52 lines
1.4 KiB
TypeScript

/**
* Overridable staff-auth mock with spies.
*
* tests/setup.ts stubs @/components/context/staff-auth-context with this
* factory so screens that read the staff session mount. Tests can flip the
* state in a beforeEach; reset with resetStaffAuthMock().
*/
import type { ReactNode } from 'react'
export interface StaffAuthMockState {
isLoggedIn: boolean
isLoading: boolean
staff: unknown
isLoggingIn: boolean
loginError: string | null
login: jest.Mock
logout: jest.Mock
}
export const staffAuthMock: StaffAuthMockState = {
isLoggedIn: true,
isLoading: false,
staff: { id: 1, name: 'shafi' },
isLoggingIn: false,
loginError: null,
login: jest.fn(async () => {}),
logout: jest.fn(),
}
export function resetStaffAuthMock() {
staffAuthMock.isLoggedIn = true
staffAuthMock.isLoading = false
staffAuthMock.staff = { id: 1, name: 'shafi' }
staffAuthMock.isLoggingIn = false
staffAuthMock.loginError = null
}
export function staffAuthContextMock() {
return {
StaffAuthProvider: ({ children }: { children?: ReactNode }) => children ?? null,
useStaffAuth: () => ({
isLoggedIn: staffAuthMock.isLoggedIn,
isLoading: staffAuthMock.isLoading,
staff: staffAuthMock.staff,
isLoggingIn: staffAuthMock.isLoggingIn,
loginError: staffAuthMock.loginError,
login: staffAuthMock.login,
logout: staffAuthMock.logout,
}),
}
}