freshyo/apps/admin-ui/tests/mocks/staff-auth-context.tsx
2026-10-20 00:31:05 +05:30

65 lines
1.6 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Deterministic StaffAuthContext replacement.
*
* Tests that need an exact auth state (loading / signed-out / signed-in without
* the real provider's async storage bootstrap) mock the module:
*
* jest.mock('@/components/context/staff-auth-context', () => require('../../mocks/staff-auth-context'))
* __setStaffState({ isLoggedIn: true, isLoading: false, staff: STAFF })
*/
import * as React from 'react'
export interface MockStaff {
id: number
name: string
regionId?: number | null
regionName?: string | null
}
export interface MockStaffState {
isLoggedIn: boolean
isLoading: boolean
staff: MockStaff | null
loginError: string | null
isLoggingIn: boolean
}
const initialState: MockStaffState = {
isLoggedIn: false,
isLoading: false,
staff: null,
loginError: null,
isLoggingIn: false,
}
let state: MockStaffState = { ...initialState }
export const login = jest.fn(async () => undefined)
export const logout = jest.fn()
export function __setStaffState(next: Partial<MockStaffState>) {
state = { ...state, ...next }
}
export function __resetStaffState() {
state = { ...initialState }
login.mockClear()
logout.mockClear()
}
export function getStaffState(): MockStaffState {
return state
}
export const StaffAuthProvider = ({ children }: any) =>
React.createElement(React.Fragment, null, children)
export const useStaffAuth = () => ({
...state,
regionId: (state.staff as any)?.regionId ?? null,
login,
logout,
})
export default { StaffAuthProvider, useStaffAuth }