diff --git a/src/frontend/use-user.tsx b/src/frontend/use-user.tsx index 7bff2f494..c3076d893 100644 --- a/src/frontend/use-user.tsx +++ b/src/frontend/use-user.tsx @@ -71,7 +71,7 @@ const missingUserProvider = 'You forgot to wrap your app in '; /** * @ignore */ -const User = createContext({ +export const UserContext = createContext({ get user(): never { throw new Error(missingUserProvider); }, @@ -112,7 +112,7 @@ export type UseUser = () => UserContext; /** * @ignore */ -export const useUser: UseUser = () => useContext(User); +export const useUser: UseUser = () => useContext(UserContext); /** * To use the {@link useUser} hook. You must wrap your application in a `` component. @@ -161,7 +161,7 @@ export default ({ return ( - {children} + {children} ); }; diff --git a/tests/frontend/use-user.test.ts b/tests/frontend/use-user.test.tsx similarity index 93% rename from tests/frontend/use-user.test.ts rename to tests/frontend/use-user.test.tsx index 9f34b2aa1..44d68433c 100644 --- a/tests/frontend/use-user.test.ts +++ b/tests/frontend/use-user.test.tsx @@ -8,7 +8,8 @@ import { user } from '../fixtures/frontend'; import { useConfig } from '../../src/frontend'; -import { useUser } from '../../src'; +import { useUser, UserContext } from '../../src'; +import React from 'react'; jest.mock('next/router', () => ({ useRouter: (): any => ({ asPath: '/' }) @@ -152,4 +153,12 @@ describe('context wrapper', () => { expect(() => result.current.isLoading).toThrowError(expectedError); expect(result.current.checkSession).toThrowError(expectedError); }); + + test('should be able to stub UserProvider with UserContext.Provider', async () => { + const { result } = renderHook(() => useUser(), { + wrapper: (props: any): React.ReactElement => + }); + + expect(result.current.user).toEqual({ foo: 'bar' }); + }); });