diff --git a/src/loginServices.js b/src/loginServices.js index a56b6c1fe..377010697 100644 --- a/src/loginServices.js +++ b/src/loginServices.js @@ -646,7 +646,9 @@ export function validateUser(okapiUrl, store, tenant, session) { atExpires: -1, rtExpires: Date.now() + (10 * 60 * 1000), }; - // provide token-expiration info to the service worker + // provide token-expiration info to the service-worker + // it returns a promise, but we don't await; the service-worker + // can operate asynchronously and that's just fine. postTokenExpiration(tokenExpiration); store.dispatch(setSessionData({ diff --git a/src/loginServices.test.js b/src/loginServices.test.js index 27c54f17c..aa254b193 100644 --- a/src/loginServices.test.js +++ b/src/loginServices.test.js @@ -33,7 +33,22 @@ import { import { defaultErrors } from './constants'; +// reassign console.log to keep things quiet +const consoleInterruptor = {}; +beforeAll(() => { + consoleInterruptor.log = global.console.log; + consoleInterruptor.error = global.console.error; + consoleInterruptor.warn = global.console.warn; + console.log = () => { }; + console.error = () => { }; + console.warn = () => { }; +}); +afterAll(() => { + global.console.log = consoleInterruptor.log; + global.console.error = consoleInterruptor.error; + global.console.warn = consoleInterruptor.warn; +}); jest.mock('localforage', () => ({ getItem: jest.fn(() => Promise.resolve({ user: {} })), @@ -63,6 +78,11 @@ const mockFetchCleanUp = () => { delete global.fetch; }; +const mockNavigatorCleanUp = () => { + window.navigator.mockClear(); + delete window.navigator; +} + describe('createOkapiSession', () => { it('clears authentication errors', async () => { @@ -75,6 +95,10 @@ describe('createOkapiSession', () => { }), }; + navigator.serviceWorker = { + ready: Promise.resolve({}) + }; + const data = { user: { id: 'user-id', @@ -87,7 +111,7 @@ describe('createOkapiSession', () => { mockFetchSuccess([]); - await createOkapiSession('url', store, 'tenant', 'token', data); + await createOkapiSession('url', store, 'tenant', data); expect(store.dispatch).toHaveBeenCalledWith(setAuthError(null)); expect(store.dispatch).toHaveBeenCalledWith(setLoginData(data)); expect(store.dispatch).toHaveBeenCalledWith(setCurrentPerms(permissionsMap)); @@ -195,7 +219,7 @@ describe('processOkapiSession', () => { mockFetchSuccess(); - await processOkapiSession('url', store, 'tenant', resp, 'token'); + await processOkapiSession('url', store, 'tenant', resp); expect(store.dispatch).toHaveBeenCalledWith(setAuthError(null)); expect(store.dispatch).toHaveBeenCalledWith(setOkapiReady()); @@ -212,7 +236,7 @@ describe('processOkapiSession', () => { } }; - await processOkapiSession('url', store, 'tenant', resp, 'token'); + await processOkapiSession('url', store, 'tenant', resp); expect(store.dispatch).toHaveBeenCalledWith(setOkapiReady()); expect(store.dispatch).toHaveBeenCalledWith(setAuthError([defaultErrors.DEFAULT_LOGIN_CLIENT_ERROR])); @@ -253,20 +277,22 @@ describe('validateUser', () => { const tenant = 'tenant'; const data = { monkey: 'bagel' }; - const token = 'token'; const user = { id: 'id' }; const perms = []; const session = { - token, user, perms, }; mockFetchSuccess(data); + navigator.serviceWorker = { + ready: Promise.resolve({}) + }; await validateUser('url', store, tenant, session); - expect(store.dispatch).toHaveBeenCalledWith(setLoginData(data)); - expect(store.dispatch).toHaveBeenCalledWith(setSessionData({ token, user, perms, tenant })); + + expect(store.dispatch).nthCalledWith(1, setAuthError(null)); + expect(store.dispatch).nthCalledWith(2, setLoginData(data)); mockFetchCleanUp(); }); @@ -279,21 +305,22 @@ describe('validateUser', () => { const tenant = 'tenant'; const sessionTenant = 'sessionTenant'; const data = { monkey: 'bagel' }; - const token = 'token'; const user = { id: 'id' }; const perms = []; const session = { - token, user, perms, tenant: sessionTenant, }; mockFetchSuccess(data); + navigator.serviceWorker = { + ready: Promise.resolve({}) + }; await validateUser('url', store, tenant, session); - expect(store.dispatch).toHaveBeenCalledWith(setLoginData(data)); - expect(store.dispatch).toHaveBeenCalledWith(setSessionData({ token, user, perms, tenant: sessionTenant })); + expect(store.dispatch).nthCalledWith(1, setAuthError(null)); + expect(store.dispatch).nthCalledWith(2, setLoginData(data)); mockFetchCleanUp(); }); diff --git a/src/okapiReducer.test.js b/src/okapiReducer.test.js index 94c1daf31..de9cd2827 100644 --- a/src/okapiReducer.test.js +++ b/src/okapiReducer.test.js @@ -24,7 +24,6 @@ describe('okapiReducer', () => { const initialState = { perms: [], user: {}, - token: 'qwerty', tenant: 'central', }; const session = { @@ -35,7 +34,6 @@ describe('okapiReducer', () => { username: 'admin', } }, - token: 'ytrewq', tenant: 'institutional', }; const o = okapiReducer(initialState, { type: 'SET_SESSION_DATA', session }); diff --git a/src/queries/useConfigurations.test.js b/src/queries/useConfigurations.test.js index 83baeef4b..a40725cff 100644 --- a/src/queries/useConfigurations.test.js +++ b/src/queries/useConfigurations.test.js @@ -11,6 +11,20 @@ import useOkapiKy from '../useOkapiKy'; jest.mock('../useOkapiKy'); jest.mock('../StripesContext'); +// reassign console.log to keep things quiet +const consoleInterruptor = {}; +beforeAll(() => { + consoleInterruptor.log = global.console.log; + consoleInterruptor.error = global.console.error; + console.log = () => { }; + console.error = () => { }; +}); + +afterAll(() => { + global.console.log = consoleInterruptor.log; + global.console.error = consoleInterruptor.error; +}); + // set query retries to false. otherwise, react-query will thoughtfully // (but unhelpfully, in the context of testing) retry a failed query // several times causing the test to timeout when what we really want diff --git a/src/queries/useOkapiEnv.test.js b/src/queries/useOkapiEnv.test.js index 3101a000c..28efc91cf 100644 --- a/src/queries/useOkapiEnv.test.js +++ b/src/queries/useOkapiEnv.test.js @@ -11,6 +11,20 @@ import useOkapiKy from '../useOkapiKy'; jest.mock('../useOkapiKy'); jest.mock('../StripesContext'); +// reassign console.log to keep things quiet +const consoleInterruptor = {}; +beforeAll(() => { + consoleInterruptor.log = global.console.log; + consoleInterruptor.error = global.console.error; + console.log = () => { }; + console.error = () => { }; +}); + +afterAll(() => { + global.console.log = consoleInterruptor.log; + global.console.error = consoleInterruptor.error; +}); + // set query retries to false. otherwise, react-query will thoughtfully // (but unhelpfully, in the context of testing) retry a failed query // several times causing the test to timeout when what we really want diff --git a/src/service-worker.test.js b/src/service-worker.test.js index b73be4bde..c8c3f024b 100644 --- a/src/service-worker.test.js +++ b/src/service-worker.test.js @@ -10,9 +10,6 @@ import { rtr, } from './service-worker'; - -global.fetch = jest.fn() - // reassign console.log to keep things quiet const consoleInterruptor = {}; beforeAll(() => {