From 062d87a93a0504b1ca39769f4f825bdd1be39a0d Mon Sep 17 00:00:00 2001 From: saravanakumardb1 Date: Sat, 28 Feb 2026 11:48:48 -0800 Subject: [PATCH] test(react-auth): add tests for updateUser, onInit, and onInit-null-fallback --- .../src/__tests__/react-auth.test.tsx | 162 ++++++++++++++++++ 1 file changed, 162 insertions(+) diff --git a/packages/react-auth/src/__tests__/react-auth.test.tsx b/packages/react-auth/src/__tests__/react-auth.test.tsx index 3be4c881..e3c90aea 100644 --- a/packages/react-auth/src/__tests__/react-auth.test.tsx +++ b/packages/react-auth/src/__tests__/react-auth.test.tsx @@ -277,6 +277,168 @@ describe('createAuthProvider', () => { expect(screen.getByTestId('email').textContent).toBe('mock@test.com'); }); + it('updateUser merges partial updates into user state', async () => { + const apiResponse = { + user: { email: 'test@example.com', name: 'Original', role: 'user' }, + accessToken: 'at-1', + refreshToken: 'rt-1', + }; + mockFetch.mockResolvedValueOnce({ + ok: true, + json: async () => apiResponse, + headers: new Headers({ 'content-type': 'application/json' }), + status: 200, + }); + + const { AuthProvider, useAuth } = createTestAuth(); + let loginFn: (email: string, password: string) => Promise; + let updateUserFn: (updates: Partial) => void; + + function Component() { + const { login, updateUser, user } = useAuth(); + loginFn = login; + updateUserFn = updateUser; + return ( +
+ {user?.name ?? 'none'} + {user?.role ?? 'none'} +
+ ); + } + + render( + + + + ); + + await act(async () => { + await loginFn!('test@example.com', 'pass'); + }); + expect(screen.getByTestId('name').textContent).toBe('Original'); + + act(() => { + updateUserFn!({ name: 'Updated' }); + }); + + expect(screen.getByTestId('name').textContent).toBe('Updated'); + expect(screen.getByTestId('role').textContent).toBe('user'); + // Verify localStorage was updated too + const storedUser = JSON.parse(store['test_auth_user']); + expect(storedUser.name).toBe('Updated'); + expect(storedUser.role).toBe('user'); + }); + + it('updateUser is a no-op when no user is logged in', () => { + const { AuthProvider, useAuth } = createTestAuth(); + let updateUserFn: (updates: Partial) => void; + + function Component() { + const { updateUser, user } = useAuth(); + updateUserFn = updateUser; + return {user ? 'yes' : 'no'}; + } + + render( + + + + ); + + act(() => { + updateUserFn!({ name: 'Should not crash' }); + }); + + expect(screen.getByTestId('user').textContent).toBe('no'); + }); + + it('onInit provides initial session from external source', () => { + const initUser: TestUser = { email: 'sso@corp.com', name: 'SSO User', role: 'admin' }; + const { AuthProvider, useAuth } = createTestAuth({ + onInit: () => ({ + user: initUser, + accessToken: 'sso-at', + refreshToken: 'sso-rt', + }), + }); + + function Display() { + const { user, isAuthenticated } = useAuth(); + return ( +
+ {String(isAuthenticated)} + {user?.email ?? 'none'} +
+ ); + } + + render( + + + + ); + + expect(screen.getByTestId('auth').textContent).toBe('true'); + expect(screen.getByTestId('email').textContent).toBe('sso@corp.com'); + // Verify tokens were saved + expect(store['test_access_token']).toBe('sso-at'); + expect(store['test_refresh_token']).toBe('sso-rt'); + }); + + it('onInit returning null falls through to localStorage', () => { + store['test_auth_user'] = JSON.stringify({ + email: 'stored@local.com', + name: 'Local', + role: 'user', + }); + + const { AuthProvider, useAuth } = createTestAuth({ + onInit: () => null, + }); + + function Display() { + const { user } = useAuth(); + return {user?.email ?? 'none'}; + } + + render( + + + + ); + + expect(screen.getByTestId('email').textContent).toBe('stored@local.com'); + }); + + it('onInit takes priority over localStorage when it returns a session', () => { + store['test_auth_user'] = JSON.stringify({ + email: 'stored@local.com', + name: 'Local', + role: 'user', + }); + + const { AuthProvider, useAuth } = createTestAuth({ + onInit: () => ({ + user: { email: 'init@override.com', name: 'Init', role: 'admin' }, + accessToken: 'init-at', + refreshToken: 'init-rt', + }), + }); + + function Display() { + const { user } = useAuth(); + return {user?.email ?? 'none'}; + } + + render( + + + + ); + + expect(screen.getByTestId('email').textContent).toBe('init@override.com'); + }); + it('uses correct storage prefix for keys', () => { const storedUser: TestUser = { email: 'x@y.com', name: 'X', role: 'viewer' }; store['custom_auth_user'] = JSON.stringify(storedUser);