forked from cardosofelipe/fast-next-template
- Replaced `useAuthStore` with `useAuth` from `AuthContext` across frontend components and tests to ensure dependency injection compliance. - Enhanced E2E test stability by delaying navigation until the auth context is fully initialized. - Updated Playwright configuration to use a single worker to prevent mock conflicts. - Refactored test setup to consistently inject `AuthProvider` for improved isolation and mocking. - Adjusted comments and documentation to clarify dependency injection and testability patterns.
76 lines
1.9 KiB
TypeScript
76 lines
1.9 KiB
TypeScript
/**
|
|
* Tests for AuthInitializer
|
|
* Verifies authentication state is loaded from storage on mount
|
|
*/
|
|
|
|
import { render, waitFor } from '@testing-library/react';
|
|
import { AuthInitializer } from '@/components/auth/AuthInitializer';
|
|
import { AuthProvider } from '@/lib/auth/AuthContext';
|
|
import { useAuthStore } from '@/lib/stores/authStore';
|
|
|
|
// Mock the auth store
|
|
jest.mock('@/lib/stores/authStore', () => ({
|
|
useAuthStore: jest.fn(),
|
|
}));
|
|
|
|
describe('AuthInitializer', () => {
|
|
const mockLoadAuthFromStorage = jest.fn();
|
|
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
|
|
(useAuthStore as unknown as jest.Mock).mockImplementation((selector: any) => {
|
|
const state = {
|
|
loadAuthFromStorage: mockLoadAuthFromStorage,
|
|
};
|
|
return selector(state);
|
|
});
|
|
});
|
|
|
|
describe('Initialization', () => {
|
|
it('renders nothing (null)', () => {
|
|
const { container } = render(
|
|
<AuthProvider>
|
|
<AuthInitializer />
|
|
</AuthProvider>
|
|
);
|
|
|
|
expect(container.firstChild).toBeNull();
|
|
});
|
|
|
|
it('calls loadAuthFromStorage on mount', async () => {
|
|
render(
|
|
<AuthProvider>
|
|
<AuthInitializer />
|
|
</AuthProvider>
|
|
);
|
|
|
|
await waitFor(() => {
|
|
expect(mockLoadAuthFromStorage).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
it('does not call loadAuthFromStorage again on re-render', async () => {
|
|
const { rerender } = render(
|
|
<AuthProvider>
|
|
<AuthInitializer />
|
|
</AuthProvider>
|
|
);
|
|
|
|
await waitFor(() => {
|
|
expect(mockLoadAuthFromStorage).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
// Force re-render
|
|
rerender(
|
|
<AuthProvider>
|
|
<AuthInitializer />
|
|
</AuthProvider>
|
|
);
|
|
|
|
// Should still only be called once (useEffect dependencies prevent re-call)
|
|
expect(mockLoadAuthFromStorage).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
});
|