Add tests for ThemeProvider and authStore behavior refinements

- Added tests to validate `ThemeProvider` updates resolved theme on system preference changes and ignores changes for non-system themes.
- Introduced tests to ensure `authStore` gracefully handles invalid tokens, storage errors, and logs errors appropriately during authentication state transitions.
- Improved test coverage by adding defensive error handling cases and refining token validation logic.
This commit is contained in:
2025-11-02 17:23:58 +01:00
parent ac3fac0426
commit 77594e478d
4 changed files with 147 additions and 1 deletions

View File

@@ -411,6 +411,21 @@ describe('Auth Store', () => {
expect(useAuthStore.getState().isLoading).toBe(false);
});
it('should ignore invalid tokens from storage', async () => {
const invalidTokens = {
accessToken: 'invalid-token', // Not in JWT format
refreshToken: 'valid.refresh.token',
};
(storage.getTokens as jest.Mock).mockResolvedValue(invalidTokens);
await useAuthStore.getState().loadAuthFromStorage();
// Should not set auth state with invalid tokens
expect(useAuthStore.getState().isAuthenticated).toBe(false);
expect(useAuthStore.getState().accessToken).toBeNull();
expect(useAuthStore.getState().isLoading).toBe(false);
});
it('should handle storage errors gracefully', async () => {
(storage.getTokens as jest.Mock).mockRejectedValue(new Error('Storage error'));
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
@@ -439,13 +454,43 @@ describe('Auth Store', () => {
expect(useAuthStore.getState().isAuthenticated).toBe(true);
});
it('should not throw on error', async () => {
it('should not throw on error and log error', async () => {
(storage.getTokens as jest.Mock).mockRejectedValue(new Error('Init error'));
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
const { initializeAuth } = await import('@/lib/stores/authStore');
await expect(initializeAuth()).resolves.not.toThrow();
// Verify error was logged by loadAuthFromStorage (which initializeAuth calls)
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Failed to load auth from storage:',
expect.any(Error)
);
consoleErrorSpy.mockRestore();
});
});
describe('Storage error handling', () => {
it('should handle saveTokens failure in setAuth', async () => {
const mockUser = createMockUser();
(storage.saveTokens as jest.Mock).mockRejectedValue(new Error('Storage error'));
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
await expect(
useAuthStore.getState().setAuth(
mockUser,
'valid.access.token',
'valid.refresh.token'
)
).rejects.toThrow('Storage error');
// Verify error was logged before throwing
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Failed to save auth state:',
expect.any(Error)
);
consoleErrorSpy.mockRestore();
});
});