fix: parse login

This commit is contained in:
2026-08-11 23:10:00 +07:00
parent d82255bc5b
commit b44092153d
9 changed files with 81 additions and 26 deletions
+15
View File
@@ -23,10 +23,12 @@ describe('authStore', () => {
vi.clearAllMocks();
mockedAuthApi.login.mockResolvedValue({
accessToken: 'access-123',
refreshToken: 'refresh-123',
user: { id: 'u1', email: 'learner@aplp.io', name: 'Learner' },
});
mockedAuthApi.refresh.mockResolvedValue({
accessToken: 'access-refreshed',
refreshToken: 'refresh-refreshed',
user: { id: 'u1', email: 'learner@aplp.io', name: 'Learner' },
});
});
@@ -64,15 +66,28 @@ describe('authStore', () => {
});
it('init restores session from refresh token', async () => {
tokenStorage.set('old-access', 'refresh-123');
await useAuthStore.getState().init();
expect(mockedAuthApi.refresh).toHaveBeenCalledOnce();
expect(mockedAuthApi.refresh).toHaveBeenCalledWith('refresh-123');
expect(tokenStorage.get()).toBe('access-refreshed');
expect(useAuthStore.getState().user?.id).toBe('u1');
expect(useAuthStore.getState().isInitializing).toBe(false);
});
it('init skips refresh when no refresh token is stored', async () => {
await useAuthStore.getState().init();
expect(mockedAuthApi.refresh).not.toHaveBeenCalled();
expect(tokenStorage.get()).toBeNull();
expect(useAuthStore.getState().user).toBeNull();
expect(useAuthStore.getState().isInitializing).toBe(false);
});
it('init handles expired refresh token gracefully', async () => {
tokenStorage.set('old-access', 'refresh-expired');
mockedAuthApi.refresh.mockRejectedValue(new Error('401'));
await useAuthStore.getState().init();