Skip to content

Commit

Permalink
jest test clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
zburke committed Oct 9, 2023
1 parent eabe2e9 commit 5b3a49c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 17 deletions.
4 changes: 3 additions & 1 deletion src/loginServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
49 changes: 38 additions & 11 deletions src/loginServices.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {} })),
Expand Down Expand Up @@ -63,6 +78,11 @@ const mockFetchCleanUp = () => {
delete global.fetch;
};

const mockNavigatorCleanUp = () => {
window.navigator.mockClear();
delete window.navigator;
}


describe('createOkapiSession', () => {
it('clears authentication errors', async () => {
Expand All @@ -75,6 +95,10 @@ describe('createOkapiSession', () => {
}),
};

navigator.serviceWorker = {
ready: Promise.resolve({})
};

const data = {
user: {
id: 'user-id',
Expand All @@ -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));
Expand Down Expand Up @@ -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());

Expand All @@ -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]));
Expand Down Expand Up @@ -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();
});
Expand All @@ -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();
});
Expand Down
2 changes: 0 additions & 2 deletions src/okapiReducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe('okapiReducer', () => {
const initialState = {
perms: [],
user: {},
token: 'qwerty',
tenant: 'central',
};
const session = {
Expand All @@ -35,7 +34,6 @@ describe('okapiReducer', () => {
username: 'admin',
}
},
token: 'ytrewq',
tenant: 'institutional',
};
const o = okapiReducer(initialState, { type: 'SET_SESSION_DATA', session });
Expand Down
14 changes: 14 additions & 0 deletions src/queries/useConfigurations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/queries/useOkapiEnv.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions src/service-worker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import {
rtr,
} from './service-worker';


global.fetch = jest.fn()

// reassign console.log to keep things quiet
const consoleInterruptor = {};
beforeAll(() => {
Expand Down

0 comments on commit 5b3a49c

Please sign in to comment.