forked from cardosofelipe/fast-next-template
Add unit tests for core components and layouts
- **ThemeToggle:** Introduce comprehensive tests to validate button functionality, dropdown options, and active theme indicators. - **ThemeProvider:** Add tests for theme management, localStorage persistence, system preferences, and DOM updates. - **Header & Footer:** Verify header rendering, user menu functionality, and footer content consistency. - **AuthInitializer:** Ensure authentication state is correctly loaded from storage on mount.
This commit is contained in:
58
frontend/tests/components/auth/AuthInitializer.test.tsx
Normal file
58
frontend/tests/components/auth/AuthInitializer.test.tsx
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* 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 { useAuthStore } from '@/stores/authStore';
|
||||
|
||||
// Mock the auth store
|
||||
jest.mock('@/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(<AuthInitializer />);
|
||||
|
||||
expect(container.firstChild).toBeNull();
|
||||
});
|
||||
|
||||
it('calls loadAuthFromStorage on mount', async () => {
|
||||
render(<AuthInitializer />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockLoadAuthFromStorage).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
it('does not call loadAuthFromStorage again on re-render', async () => {
|
||||
const { rerender } = render(<AuthInitializer />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockLoadAuthFromStorage).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
// Force re-render
|
||||
rerender(<AuthInitializer />);
|
||||
|
||||
// Should still only be called once (useEffect dependencies prevent re-call)
|
||||
expect(mockLoadAuthFromStorage).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user