Skip to content

Commit

Permalink
Update use-auth0.spec.jsx
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenTANM committed Apr 2, 2024
1 parent 12aa267 commit be4b1ec
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/hooks/__tests__/use-auth0.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ const mockAuth0 = {
loginWithOTP: jest.fn().mockResolvedValue(mockCredentials),
loginWithRecoveryCode: jest.fn().mockResolvedValue(mockCredentials),
hasValidCredentials: jest.fn().mockResolvedValue(),
refreshToken: jest.fn().mockResolvedValue(),
refreshToken: jest
.fn()
.mockResolvedValue(updatedMockCredentialsWithIdToken),
},
credentialsManager: {
getCredentials: jest.fn().mockResolvedValue(mockCredentials),
Expand Down Expand Up @@ -265,12 +267,14 @@ describe('The useAuth0 hook', () => {
wrapper,
});
let credentials;
let newCredentials;
await act(async () => {
credentials = await result.current.getCredentials();
await result.current.refreshToken({
refreshToken: credentials.refreshToken,
scope: 'openid profile email offline_access',
});
newCredentials = await result.current.getCredentials();
});

await waitFor(() => expect(result.current.isLoading).toBe(false));
Expand All @@ -279,6 +283,39 @@ describe('The useAuth0 hook', () => {
refreshToken: credentials.refreshToken,
scope: 'openid profile email offline_access',
});
expect(newCredentials).toEqual({
idToken:
'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiaXNzIjoiaHR0cHM6Ly9hdXRoMC5jb20iLCJhdWQiOiJjbGllbnQxMjMiLCJuYW1lIjoiVGVzdCBVc2VyIiwiZmFtaWx5X25hbWUiOiJVc2VyIiwicGljdHVyZSI6Imh0dHBzOi8vaW1hZ2VzL3BpYy5wbmcifQ==.c2lnbmF0dXJl',
accessToken: 'ACCESS TOKEN',
refreshToken: 'REFRESH TOKEN',
});
});

it("can save the user's credentials using saveCredentials method", async () => {
const { result } = renderHook(() => useAuth0(), {
wrapper,
});
let credentials;
let newCredentials;

await act(async () => {
const { refreshToken } = await result.current.getCredentials();
newCredentials = await result.current.refreshToken({
refreshToken,
scope: 'openid profile email offline_access',
});
result.current.saveCredentials(newCredentials);
credentials = await result.current.getCredentials();
});

await waitFor(() => expect(result.current.isLoading).toBe(false));

expect(newCredentials).toEqual({
idToken:
'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiaXNzIjoiaHR0cHM6Ly9hdXRoMC5jb20iLCJhdWQiOiJjbGllbnQxMjMiLCJuYW1lIjoiVGVzdCBVc2VyIiwiZmFtaWx5X25hbWUiOiJVc2VyIiwicGljdHVyZSI6Imh0dHBzOi8vaW1hZ2VzL3BpYy5wbmcifQ==.c2lnbmF0dXJl',
accessToken: 'ACCESS TOKEN',
refreshToken: 'REFRESH TOKEN',
});
});

it('can authorize, passing through all options', async () => {
Expand Down

0 comments on commit be4b1ec

Please sign in to comment.