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
This commit is contained in:
@@ -386,41 +386,67 @@ describe('Auth Store', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('loadAuthFromStorage (deprecated)', () => {
|
describe('loadAuthFromStorage', () => {
|
||||||
it('should log deprecation warning', async () => {
|
it('should load valid tokens from storage', async () => {
|
||||||
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();
|
const mockTokens = {
|
||||||
|
accessToken: 'valid.access.token',
|
||||||
|
refreshToken: 'valid.refresh.token',
|
||||||
|
};
|
||||||
|
(storage.getTokens as jest.Mock).mockResolvedValue(mockTokens);
|
||||||
|
|
||||||
await useAuthStore.getState().loadAuthFromStorage();
|
await useAuthStore.getState().loadAuthFromStorage();
|
||||||
|
|
||||||
expect(consoleWarnSpy).toHaveBeenCalledWith(
|
expect(useAuthStore.getState().accessToken).toBe(mockTokens.accessToken);
|
||||||
'loadAuthFromStorage() is deprecated and no longer necessary'
|
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)', () => {
|
describe('initializeAuth', () => {
|
||||||
it('should log deprecation warning', async () => {
|
it('should call loadAuthFromStorage', async () => {
|
||||||
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();
|
const mockTokens = {
|
||||||
|
accessToken: 'valid.access.token',
|
||||||
|
refreshToken: 'valid.refresh.token',
|
||||||
|
};
|
||||||
|
(storage.getTokens as jest.Mock).mockResolvedValue(mockTokens);
|
||||||
|
|
||||||
const { initializeAuth } = await import('@/lib/stores/authStore');
|
const { initializeAuth } = await import('@/lib/stores/authStore');
|
||||||
await initializeAuth();
|
await initializeAuth();
|
||||||
|
|
||||||
expect(consoleWarnSpy).toHaveBeenCalledWith(
|
expect(storage.getTokens).toHaveBeenCalled();
|
||||||
'initializeAuth() is deprecated and no longer necessary'
|
expect(useAuthStore.getState().isAuthenticated).toBe(true);
|
||||||
);
|
|
||||||
|
|
||||||
consoleWarnSpy.mockRestore();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not throw', async () => {
|
it('should not throw on error', async () => {
|
||||||
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();
|
(storage.getTokens as jest.Mock).mockRejectedValue(new Error('Init error'));
|
||||||
|
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
|
||||||
|
|
||||||
const { initializeAuth } = await import('@/lib/stores/authStore');
|
const { initializeAuth } = await import('@/lib/stores/authStore');
|
||||||
await expect(initializeAuth()).resolves.not.toThrow();
|
await expect(initializeAuth()).resolves.not.toThrow();
|
||||||
|
|
||||||
consoleWarnSpy.mockRestore();
|
consoleErrorSpy.mockRestore();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user