From 63650f563df87a9c685ac3bd5321ebbaaffd5c7d Mon Sep 17 00:00:00 2001 From: Felipe Cardoso Date: Wed, 5 Nov 2025 11:45:54 +0100 Subject: [PATCH] Simplify `AuthProvider` implementation and remove E2E test store injection via `window` - Removed `window.__TEST_AUTH_STORE__` logic for E2E test store injection in `AuthProvider` and related comments. - Updated `AuthInitializer` to clarify E2E test behavior with mocked API responses. - Streamlined `AuthContext` handling by prioritizing explicit `store` prop or production singleton. --- .../src/components/auth/AuthInitializer.tsx | 6 +--- frontend/src/lib/auth/AuthContext.tsx | 35 ++++--------------- 2 files changed, 8 insertions(+), 33 deletions(-) diff --git a/frontend/src/components/auth/AuthInitializer.tsx b/frontend/src/components/auth/AuthInitializer.tsx index 736476d..65e458c 100644 --- a/frontend/src/components/auth/AuthInitializer.tsx +++ b/frontend/src/components/auth/AuthInitializer.tsx @@ -35,12 +35,8 @@ export function AuthInitializer() { const loadAuthFromStorage = useAuth((state) => state.loadAuthFromStorage); useEffect(() => { - // Skip loading from storage in E2E tests - test store is already injected - if (typeof window !== 'undefined' && (window as any).__E2E_TEST__) { - return; - } - // Load auth state from encrypted storage on mount + // E2E tests use the real flow with mocked API responses loadAuthFromStorage(); }, [loadAuthFromStorage]); diff --git a/frontend/src/lib/auth/AuthContext.tsx b/frontend/src/lib/auth/AuthContext.tsx index fd47778..1c449ac 100644 --- a/frontend/src/lib/auth/AuthContext.tsx +++ b/frontend/src/lib/auth/AuthContext.tsx @@ -41,20 +41,10 @@ interface AuthState { /** * Type of the Zustand hook function - * Used for Context storage and test injection + * Used for Context storage and test injection via props */ type AuthStoreHook = typeof useAuthStoreImpl; -/** - * Global window extension for E2E test injection - * E2E tests can set window.__TEST_AUTH_STORE__ before navigation - */ -declare global { - interface Window { - __TEST_AUTH_STORE__?: AuthStoreHook; - } -} - const AuthContext = createContext(null); interface AuthProviderProps { @@ -70,36 +60,25 @@ interface AuthProviderProps { * Authentication Context Provider * * Wraps Zustand auth store in React Context for dependency injection. - * Enables test isolation by allowing mock stores to be injected via: - * 1. `store` prop (unit tests) - * 2. `window.__TEST_AUTH_STORE__` (E2E tests) - * 3. Production singleton (default) + * Enables test isolation by allowing mock stores to be injected via the `store` prop. * * @example * ```tsx - * // In root layout + * // In production (root layout) * * * * - * // In unit tests + * // In unit tests (with mock store) * * * - * - * // In E2E tests (before navigation) - * window.__TEST_AUTH_STORE__ = mockAuthStoreHook; * ``` */ export function AuthProvider({ children, store }: AuthProviderProps) { - // Check for E2E test store injection (SSR-safe) - const testStore = - typeof window !== "undefined" && window.__TEST_AUTH_STORE__ - ? window.__TEST_AUTH_STORE__ - : null; - - // Priority: explicit prop > E2E test store > production singleton - const authStore = store ?? testStore ?? useAuthStoreImpl; + // Use provided store for unit tests, otherwise use production singleton + // E2E tests use the real auth store with mocked API routes + const authStore = store ?? useAuthStoreImpl; return {children}; }