From acdf6a9fcd53e741c05be50a4aa90af604976a56 Mon Sep 17 00:00:00 2001 From: Steve Hobbs Date: Tue, 3 Mar 2020 11:45:26 +0000 Subject: [PATCH] createAuth0Client now throws errors that are not login_required --- __tests__/index.test.ts | 51 +++++++++++++++++++++++++++++++++++++++++ src/index.ts | 4 +++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 9063ec9ff..64ec3214e 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -111,13 +111,64 @@ describe('Auth0', () => { domain: TEST_DOMAIN, client_id: TEST_CLIENT_ID }); + expect(auth0).toBeInstanceOf(Auth0Client); }); + it('should call `utils.validateCrypto`', async () => { const { utils } = await setup(); + expect(utils.validateCrypto).toHaveBeenCalled(); }); + + it('should absorb "login_required" errors', async () => { + const { utils, storage } = await setup(); + + utils.runIframe.mockImplementation(() => { + throw { + error: 'login_required', + error_message: 'Login required' + }; + }); + + storage.get.mockReturnValue(true); + + const auth0 = await createAuth0Client({ + domain: TEST_DOMAIN, + client_id: TEST_CLIENT_ID + }); + + expect(auth0).toBeInstanceOf(Auth0Client); + expect(utils.runIframe).toHaveBeenCalled(); + }); + + it('should throw other errors that are not "login_required"', async () => { + const { utils, storage } = await setup(); + + utils.runIframe.mockImplementation(() => { + throw { + error: 'some_other_error', + error_message: 'This is a different error to login_required' + }; + }); + + storage.get.mockReturnValue(true); + + // We expect one assertion, meaning that if the function under test + // does not throw, it will still fail the test. + expect.assertions(1); + + try { + await createAuth0Client({ + domain: TEST_DOMAIN, + client_id: TEST_CLIENT_ID + }); + } catch (e) { + expect(e.error).toEqual('some_other_error'); + } + }); }); + describe('loginWithPopup()', () => { it('opens popup', async () => { const { auth0, utils } = await setup(); diff --git a/src/index.ts b/src/index.ts index fe9b9c402..0e6242d97 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,7 +28,9 @@ export default async function createAuth0Client(options: Auth0ClientOptions) { ignoreCache: true }); } catch (error) { - // ignore + if (error.error !== 'login_required') { + throw error; + } } return auth0; }