Add _hasHydrated flag to authStore and update AuthGuard to wait for store hydration, ensuring stability during loading phases in tests and app.

This commit is contained in:
2025-11-02 14:16:56 +01:00
parent 29f98f059b
commit 6d1b730ae7
3 changed files with 36 additions and 5 deletions

View File

@@ -23,10 +23,12 @@ let mockAuthState: {
isAuthenticated: boolean;
isLoading: boolean;
user: any;
_hasHydrated: boolean;
} = {
isAuthenticated: false,
isLoading: false,
user: null,
_hasHydrated: true, // In tests, assume store is always hydrated
};
jest.mock('@/lib/stores/authStore', () => ({
@@ -69,6 +71,7 @@ describe('AuthGuard', () => {
isAuthenticated: false,
isLoading: false,
user: null,
_hasHydrated: true, // In tests, assume store is always hydrated
};
mockMeState = {
isLoading: false,
@@ -82,6 +85,7 @@ describe('AuthGuard', () => {
isAuthenticated: false,
isLoading: true,
user: null,
_hasHydrated: true,
};
render(
@@ -100,6 +104,7 @@ describe('AuthGuard', () => {
isAuthenticated: true,
isLoading: false,
user: null,
_hasHydrated: true,
};
mockMeState = {
isLoading: true,
@@ -121,6 +126,7 @@ describe('AuthGuard', () => {
isAuthenticated: false,
isLoading: true,
user: null,
_hasHydrated: true,
};
render(
@@ -142,6 +148,7 @@ describe('AuthGuard', () => {
isAuthenticated: false,
isLoading: false,
user: null,
_hasHydrated: true,
};
render(
@@ -172,6 +179,7 @@ describe('AuthGuard', () => {
created_at: '2024-01-01',
updated_at: '2024-01-01',
},
_hasHydrated: true,
};
render(
@@ -197,10 +205,11 @@ describe('AuthGuard', () => {
first_name: 'Admin',
last_name: 'User',
is_active: true,
is_superuser: true,
is_superuser: true, // Admin user must have is_superuser: true
created_at: '2024-01-01',
updated_at: '2024-01-01',
},
_hasHydrated: true,
};
render(
@@ -219,7 +228,7 @@ describe('AuthGuard', () => {
isAuthenticated: true,
isLoading: false,
user: {
id: '1',
id: '1',
email: 'user@example.com',
first_name: 'Regular',
last_name: 'User',
@@ -228,6 +237,7 @@ describe('AuthGuard', () => {
created_at: '2024-01-01',
updated_at: '2024-01-01',
},
_hasHydrated: true,
};
render(
@@ -249,7 +259,7 @@ describe('AuthGuard', () => {
isAuthenticated: true,
isLoading: false,
user: {
id: '1',
id: '1',
email: 'user@example.com',
first_name: 'Regular',
last_name: 'User',
@@ -258,6 +268,7 @@ describe('AuthGuard', () => {
created_at: '2024-01-01',
updated_at: '2024-01-01',
},
_hasHydrated: true,
};
render(
@@ -278,6 +289,7 @@ describe('AuthGuard', () => {
isAuthenticated: false,
isLoading: false,
user: null,
_hasHydrated: true,
};
render(
@@ -301,6 +313,7 @@ describe('AuthGuard', () => {
isAuthenticated: true,
isLoading: false,
user: null,
_hasHydrated: true,
};
mockMeState = {
isLoading: true,
@@ -322,7 +335,7 @@ describe('AuthGuard', () => {
isAuthenticated: true,
isLoading: false,
user: {
id: '1',
id: '1',
email: 'user@example.com',
first_name: 'Test',
last_name: 'User',
@@ -331,6 +344,7 @@ describe('AuthGuard', () => {
created_at: '2024-01-01',
updated_at: '2024-01-01',
},
_hasHydrated: true,
};
mockMeState = {
isLoading: false,