From 2c05f17ec5579871b8c510fade8d72fcf7ea7e60 Mon Sep 17 00:00:00 2001 From: Felipe Cardoso Date: Sun, 2 Nov 2025 14:54:00 +0100 Subject: [PATCH] Fix authStore tests after reverting persist middleware - Replace deprecation tests with functional tests - Test loadAuthFromStorage actually loads tokens - Test initializeAuth calls loadAuthFromStorage - All 281 tests passing --- frontend/tests/lib/stores/authStore.test.ts | 62 +++++++++++++++------ 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/frontend/tests/lib/stores/authStore.test.ts b/frontend/tests/lib/stores/authStore.test.ts index 526de9b..d01d122 100644 --- a/frontend/tests/lib/stores/authStore.test.ts +++ b/frontend/tests/lib/stores/authStore.test.ts @@ -386,41 +386,67 @@ describe('Auth Store', () => { }); }); - describe('loadAuthFromStorage (deprecated)', () => { - it('should log deprecation warning', async () => { - const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(); + describe('loadAuthFromStorage', () => { + it('should load valid tokens from storage', async () => { + const mockTokens = { + accessToken: 'valid.access.token', + refreshToken: 'valid.refresh.token', + }; + (storage.getTokens as jest.Mock).mockResolvedValue(mockTokens); await useAuthStore.getState().loadAuthFromStorage(); - expect(consoleWarnSpy).toHaveBeenCalledWith( - 'loadAuthFromStorage() is deprecated and no longer necessary' - ); + expect(useAuthStore.getState().accessToken).toBe(mockTokens.accessToken); + expect(useAuthStore.getState().refreshToken).toBe(mockTokens.refreshToken); + expect(useAuthStore.getState().isAuthenticated).toBe(true); + expect(useAuthStore.getState().isLoading).toBe(false); + }); - consoleWarnSpy.mockRestore(); + it('should set isLoading to false when no tokens found', async () => { + (storage.getTokens as jest.Mock).mockResolvedValue(null); + + await useAuthStore.getState().loadAuthFromStorage(); + + expect(useAuthStore.getState().isAuthenticated).toBe(false); + 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(); + + await useAuthStore.getState().loadAuthFromStorage(); + + expect(useAuthStore.getState().isLoading).toBe(false); + expect(consoleErrorSpy).toHaveBeenCalled(); + + consoleErrorSpy.mockRestore(); }); }); - describe('initializeAuth (deprecated)', () => { - it('should log deprecation warning', async () => { - const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(); + describe('initializeAuth', () => { + it('should call loadAuthFromStorage', async () => { + const mockTokens = { + accessToken: 'valid.access.token', + refreshToken: 'valid.refresh.token', + }; + (storage.getTokens as jest.Mock).mockResolvedValue(mockTokens); const { initializeAuth } = await import('@/lib/stores/authStore'); await initializeAuth(); - expect(consoleWarnSpy).toHaveBeenCalledWith( - 'initializeAuth() is deprecated and no longer necessary' - ); - - consoleWarnSpy.mockRestore(); + expect(storage.getTokens).toHaveBeenCalled(); + expect(useAuthStore.getState().isAuthenticated).toBe(true); }); - it('should not throw', async () => { - const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(); + it('should not throw on 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(); - consoleWarnSpy.mockRestore(); + consoleErrorSpy.mockRestore(); }); }); });