go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/testing_tools/fakes/fake_auth_state_provider.tsx (about) 1 import { useMemo } from 'react'; 2 import { useLatest } from 'react-use'; 3 4 import { AuthState } from '@/common/api/auth_state'; 5 import { AuthStateContext } from '@/common/components/auth_state_provider'; 6 import { createMockAuthState } from '@/testing_tools/mocks/authstate_mock'; 7 8 interface props { 9 value?: AuthState; 10 children: React.ReactElement; 11 } 12 13 export const FakeAuthStateProvider = ({ value, children }: props) => { 14 if (!value) { 15 value = createMockAuthState(); 16 } 17 const valueRef = useLatest(value); 18 const ctxValue = useMemo( 19 () => ({ 20 getAuthState: () => valueRef.current, 21 // This doesn't ensure the auth state is valid. But it should be good 22 // enough for most unit tests that is not testing <AuthStateProvider /> 23 // itself. 24 getValidAuthState: async () => valueRef.current, 25 }), 26 // eslint-disable-next-line react-hooks/exhaustive-deps 27 [value.identity], 28 ); 29 30 return ( 31 <AuthStateContext.Provider value={ctxValue}> 32 {children} 33 </AuthStateContext.Provider> 34 ); 35 };