Skip to content

Commit

Permalink
additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zburke committed Nov 15, 2024
1 parent 9fe499f commit 0f4b867
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/loginServices.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import localforage from 'localforage';

import {
checkOkapiSession,
createOkapiSession,
getOkapiSession,
getTokenExpiry,
Expand Down Expand Up @@ -482,3 +483,84 @@ describe('localforage session wrapper', () => {
});
});
});

describe('checkOkapiSession', () => {
it('dispatches setOkapiReady', async () => {
const o = {
user: { id: 'id' },
tenant: 'tenant',
isAuthenticated: true,
};
localforage.getItem = jest.fn(() => Promise.resolve(o));
const store = {
dispatch: jest.fn(),
getState: () => ({
okapi: {
currentPerms: [],
}
}),
};

const data = { data: 'd' };

mockFetchSuccess(data);

await checkOkapiSession('url', store, o.tenant);
expect(store.dispatch).toHaveBeenCalledWith(setOkapiReady());

mockFetchCleanUp();
});

it('when getOkapiSession returns full session data, validates it', async () => {
const o = {
user: { id: 'id' },
tenant: 'tenant',
isAuthenticated: true,
};
localforage.getItem = jest.fn(() => Promise.resolve(o));
const store = {
dispatch: jest.fn(),
getState: () => ({
okapi: {
currentPerms: [],
}
}),
};

const data = { data: 'd' };

mockFetchSuccess(data);

await checkOkapiSession('url', store, 'tenant');

expect(store.dispatch).toHaveBeenCalledWith(setAuthError(null));
expect(store.dispatch).toHaveBeenCalledWith(setOkapiReady());

mockFetchCleanUp();
});

it('when getOkapiSession returns sparse session data, ignores it', async () => {
const o = { monkey: 'bagel' };
localforage.getItem = jest.fn(() => Promise.resolve(o));
const store = {
dispatch: jest.fn(),
getState: () => ({
okapi: {
currentPerms: [],
}
}),
};

const data = { data: 'd' };

mockFetchSuccess(data);

await checkOkapiSession('url', store, o.tenant);

expect(store.dispatch).not.toHaveBeenCalledWith(setAuthError(null));
expect(store.dispatch).not.toHaveBeenCalledWith(setLoginData(data));
expect(store.dispatch).toHaveBeenCalledWith(setOkapiReady(null));

mockFetchCleanUp();
});
});

0 comments on commit 0f4b867

Please sign in to comment.