- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
60 lines
2.0 KiB
TypeScript
60 lines
2.0 KiB
TypeScript
import { beforeEach,describe,expect,it } from 'vitest';
|
|
import type { AdminUser } from '../types/admin';
|
|
import { useAdminAuthStore } from './adminAuthStore';
|
|
|
|
describe('useAdminAuthStore', () => {
|
|
beforeEach(() => {
|
|
useAdminAuthStore.getState().clearAuth();
|
|
localStorage.clear();
|
|
});
|
|
|
|
const mockAdmin: AdminUser = {
|
|
id: 'admin-123',
|
|
mobile: '09123456789',
|
|
email: 'admin@canina.ir',
|
|
firstName: 'Super',
|
|
lastName: 'Admin',
|
|
role: 'SUPER_ADMIN',
|
|
};
|
|
|
|
it('initializes with unauthenticated default state', () => {
|
|
const state = useAdminAuthStore.getState();
|
|
expect(state.accessToken).toBeNull();
|
|
expect(state.adminUser).toBeNull();
|
|
expect(state.isAuthenticated).toBe(false);
|
|
});
|
|
|
|
it('sets authentication state correctly upon setAuth', () => {
|
|
useAdminAuthStore.getState().setAuth('mock-access-token', mockAdmin);
|
|
|
|
const state = useAdminAuthStore.getState();
|
|
expect(state.accessToken).toBe('mock-access-token');
|
|
expect(state.adminUser).toEqual(mockAdmin);
|
|
expect(state.isAuthenticated).toBe(true);
|
|
});
|
|
|
|
it('resets authentication state to null on clearAuth', () => {
|
|
useAdminAuthStore.getState().setAuth('mock-access-token', mockAdmin);
|
|
expect(useAdminAuthStore.getState().isAuthenticated).toBe(true);
|
|
|
|
useAdminAuthStore.getState().clearAuth();
|
|
|
|
const state = useAdminAuthStore.getState();
|
|
expect(state.accessToken).toBeNull();
|
|
expect(state.adminUser).toBeNull();
|
|
expect(state.isAuthenticated).toBe(false);
|
|
});
|
|
|
|
it('persists credentials into localStorage under canina_admin_auth key', () => {
|
|
useAdminAuthStore.getState().setAuth('token-abc-999', mockAdmin);
|
|
|
|
const storedRaw = localStorage.getItem('canina_admin_auth');
|
|
expect(storedRaw).toBeTruthy();
|
|
|
|
const storedData = JSON.parse(storedRaw!);
|
|
expect(storedData.state.accessToken).toBe('token-abc-999');
|
|
expect(storedData.state.adminUser.id).toBe('admin-123');
|
|
expect(storedData.state.isAuthenticated).toBe(true);
|
|
});
|
|
});
|