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

getToken methods were being called in the wrong scope #24

Merged
merged 1 commit into from
Jun 1, 2020
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
24 changes: 24 additions & 0 deletions __tests__/auth-provider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,18 @@ describe('Auth0Provider', () => {
);
});

it('should call getAccessTokenSilently in the scope of the Auth0 client', async () => {
clientMock.getTokenSilently.mockReturnThis();
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
const returnedThis = await result.current.getAccessTokenSilently();
expect(returnedThis).toStrictEqual(clientMock);
});

it('should provide a getAccessTokenWithPopup method', async () => {
clientMock.getTokenWithPopup.mockResolvedValue('token');
const wrapper = createWrapper();
Expand All @@ -270,6 +282,18 @@ describe('Auth0Provider', () => {
expect(token).toBe('token');
});

it('should call getAccessTokenWithPopup in the scope of the Auth0 client', async () => {
clientMock.getTokenWithPopup.mockReturnThis();
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
const returnedThis = await result.current.getAccessTokenWithPopup();
expect(returnedThis).toStrictEqual(clientMock);
});

it('should normalize errors from getAccessTokenWithPopup method', async () => {
clientMock.getTokenWithPopup.mockRejectedValue(new ProgressEvent('error'));
const wrapper = createWrapper();
Expand Down
8 changes: 6 additions & 2 deletions src/auth0-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,12 @@ const Auth0Provider = ({
<Auth0Context.Provider
value={{
...state,
getAccessTokenSilently: wrappedGetToken(client.getTokenSilently),
getAccessTokenWithPopup: wrappedGetToken(client.getTokenWithPopup),
getAccessTokenSilently: wrappedGetToken((opts?) =>
client.getTokenSilently(opts)
),
getAccessTokenWithPopup: wrappedGetToken((opts?) =>
client.getTokenWithPopup(opts)
),
getIdTokenClaims: (opts): Promise<IdToken> =>
client.getIdTokenClaims(opts),
loginWithRedirect: (opts): Promise<void> =>
Expand Down