Skip to content

Commit

Permalink
refactor: Improve createContext setup
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Typescript users might not expect the Context to be 'null'. Now you have to check
it.
  • Loading branch information
simenandre committed May 15, 2020
1 parent 893eeb9 commit 6b715f5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 44 deletions.
5 changes: 1 addition & 4 deletions src/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import {
AuthProviderSignOutProps,
} from './AuthContextInterface';

export const AuthContext = React.createContext<AuthContextProps>({
signIn: /* istanbul ignore next */ () => {},
signOut: /* istanbul ignore next */ () => {},
});
export const AuthContext = React.createContext<AuthContextProps|null>(null);

/**
* @private
Expand Down
74 changes: 35 additions & 39 deletions src/__tests__/AuthContext.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('AuthContext', () => {
<AuthProvider userManager={u} autoSignIn={false}>
<AuthContext.Consumer>
{(value) => {
value.signIn();
value?.signIn();
return <p>Bjerk</p>;
}}
</AuthContext.Consumer>
Expand Down Expand Up @@ -74,7 +74,7 @@ describe('AuthContext', () => {
<AuthProvider userManager={userManager}>
<AuthContext.Consumer>
{(value) =>
value.userData && (
value?.userData && (
<span>Received: {value.userData.access_token}</span>
)
}
Expand Down Expand Up @@ -126,7 +126,7 @@ describe('AuthContext', () => {
>
<AuthContext.Consumer>
{(value) => {
value.signOut();
value?.signOut();
return <p>Bjerk</p>;
}}
</AuthContext.Consumer>
Expand All @@ -151,9 +151,7 @@ describe('AuthContext', () => {
>
<AuthContext.Consumer>
{(value) => {
value.signOut({
signoutRedirect: true
});
value?.signOutRedirect();
return <p>Bjerk</p>;
}}
</AuthContext.Consumer>
Expand All @@ -162,37 +160,35 @@ describe('AuthContext', () => {
await waitFor(() => expect(onSignOut).toHaveBeenCalled());
await waitFor(() => expect(userManager.signoutRedirect).toHaveBeenCalled());
});
it('should end session and logout the user when signoutRedirect is an object', async () => {
const userManager = {
getUser: async () => ({
access_token: 'token',
}),
signoutRedirect: jest.fn(),
} as any;
const onSignOut = jest.fn();
render(
<AuthProvider
onSignOut={onSignOut}
userManager={userManager}
location={location}
>
<AuthContext.Consumer>
{(value) => {
value.signOut({
signoutRedirect: {
state: 'thebranches'
},
});
return <p>Bjerk</p>;
}}
</AuthContext.Consumer>
</AuthProvider>,
);
await waitFor(() => expect(onSignOut).toHaveBeenCalled());
await waitFor(() =>
expect(userManager.signoutRedirect).toHaveBeenCalledWith({
state: 'thebranches'
}),
);
});
it('should end session and logout the user when signoutRedirect is an object', async () => {
const userManager = {
getUser: async () => ({
access_token: 'token',
}),
signoutRedirect: jest.fn(),
} as any;
const onSignOut = jest.fn();
render(
<AuthProvider
onSignOut={onSignOut}
userManager={userManager}
location={location}
>
<AuthContext.Consumer>
{(value) => {
value?.signOutRedirect({
state: 'thebranches',
});
return <p>Bjerk</p>;
}}
</AuthContext.Consumer>
</AuthProvider>,
);
await waitFor(() => expect(onSignOut).toHaveBeenCalled());
await waitFor(() =>
expect(userManager.signoutRedirect).toHaveBeenCalledWith({
state: 'thebranches',
}),
);
});
});
2 changes: 1 addition & 1 deletion src/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import { AuthContextProps } from './AuthContextInterface';
import { AuthContext } from './AuthContext';

export const useAuth = () => {
return useContext<AuthContextProps>(AuthContext);
return useContext<AuthContextProps|null>(AuthContext);
};

0 comments on commit 6b715f5

Please sign in to comment.