Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export context for stubbing default Provider behaviour #325

Merged
merged 1 commit into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' });
});
});