From be4b1ec663211074ac657774b0962198ce57de91 Mon Sep 17 00:00:00 2001 From: Stephen Tapia Date: Tue, 2 Apr 2024 13:19:04 -0600 Subject: [PATCH] Update use-auth0.spec.jsx --- src/hooks/__tests__/use-auth0.spec.jsx | 39 +++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/hooks/__tests__/use-auth0.spec.jsx b/src/hooks/__tests__/use-auth0.spec.jsx index 2870899d..24c73727 100644 --- a/src/hooks/__tests__/use-auth0.spec.jsx +++ b/src/hooks/__tests__/use-auth0.spec.jsx @@ -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), @@ -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)); @@ -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 () => {