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.
This commit is contained in:
Felipe Cardoso
2025-11-05 11:45:54 +01:00
parent f23fdb974a
commit 63650f563d
2 changed files with 8 additions and 33 deletions

View File

@@ -35,12 +35,8 @@ export function AuthInitializer() {
const loadAuthFromStorage = useAuth((state) => state.loadAuthFromStorage); const loadAuthFromStorage = useAuth((state) => state.loadAuthFromStorage);
useEffect(() => { 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 // Load auth state from encrypted storage on mount
// E2E tests use the real flow with mocked API responses
loadAuthFromStorage(); loadAuthFromStorage();
}, [loadAuthFromStorage]); }, [loadAuthFromStorage]);

View File

@@ -41,20 +41,10 @@ interface AuthState {
/** /**
* Type of the Zustand hook function * 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; 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<AuthStoreHook | null>(null); const AuthContext = createContext<AuthStoreHook | null>(null);
interface AuthProviderProps { interface AuthProviderProps {
@@ -70,36 +60,25 @@ interface AuthProviderProps {
* Authentication Context Provider * Authentication Context Provider
* *
* Wraps Zustand auth store in React Context for dependency injection. * Wraps Zustand auth store in React Context for dependency injection.
* Enables test isolation by allowing mock stores to be injected via: * Enables test isolation by allowing mock stores to be injected via the `store` prop.
* 1. `store` prop (unit tests)
* 2. `window.__TEST_AUTH_STORE__` (E2E tests)
* 3. Production singleton (default)
* *
* @example * @example
* ```tsx * ```tsx
* // In root layout * // In production (root layout)
* <AuthProvider> * <AuthProvider>
* <App /> * <App />
* </AuthProvider> * </AuthProvider>
* *
* // In unit tests * // In unit tests (with mock store)
* <AuthProvider store={mockStore}> * <AuthProvider store={mockStore}>
* <ComponentUnderTest /> * <ComponentUnderTest />
* </AuthProvider> * </AuthProvider>
*
* // In E2E tests (before navigation)
* window.__TEST_AUTH_STORE__ = mockAuthStoreHook;
* ``` * ```
*/ */
export function AuthProvider({ children, store }: AuthProviderProps) { export function AuthProvider({ children, store }: AuthProviderProps) {
// Check for E2E test store injection (SSR-safe) // Use provided store for unit tests, otherwise use production singleton
const testStore = // E2E tests use the real auth store with mocked API routes
typeof window !== "undefined" && window.__TEST_AUTH_STORE__ const authStore = store ?? useAuthStoreImpl;
? window.__TEST_AUTH_STORE__
: null;
// Priority: explicit prop > E2E test store > production singleton
const authStore = store ?? testStore ?? useAuthStoreImpl;
return <AuthContext.Provider value={authStore}>{children}</AuthContext.Provider>; return <AuthContext.Provider value={authStore}>{children}</AuthContext.Provider>;
} }