Skip to content

Commit

Permalink
Export context for stubbing default Provider behaviour (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjmcgrath authored Mar 4, 2021
1 parent 094babb commit 029aedf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/frontend/use-user.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const missingUserProvider = 'You forgot to wrap your app in <UserProvider>';
/**
* @ignore
*/
const User = createContext<UserContext>({
export const UserContext = createContext<UserContext>({
get user(): never {
throw new Error(missingUserProvider);
},
Expand Down Expand Up @@ -112,7 +112,7 @@ export type UseUser = () => UserContext;
/**
* @ignore
*/
export const useUser: UseUser = () => useContext<UserContext>(User);
export const useUser: UseUser = () => useContext<UserContext>(UserContext);

/**
* To use the {@link useUser} hook. You must wrap your application in a `<UserProvider>` component.
Expand Down Expand Up @@ -161,7 +161,7 @@ export default ({

return (
<ConfigProvider loginUrl={loginUrl}>
<User.Provider value={{ user, error, isLoading, checkSession }}>{children}</User.Provider>
<UserContext.Provider value={{ user, error, isLoading, checkSession }}>{children}</UserContext.Provider>
</ConfigProvider>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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: '/' })
Expand Down Expand Up @@ -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 => <UserContext.Provider {...props} value={{ user: { foo: 'bar' } }} />
});

expect(result.current.user).toEqual({ foo: 'bar' });
});
});

0 comments on commit 029aedf

Please sign in to comment.