Skip to content

Commit

Permalink
Fix remaining broken unit test #1379
Browse files Browse the repository at this point in the history
  • Loading branch information
joelvdavies committed Mar 15, 2024
1 parent 7d1f68d commit 8608169
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
5 changes: 1 addition & 4 deletions __mocks__/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ const requests = {
if (path === '/settings.json') {
return Promise.resolve({
data: {
// Set provider to icat as that supports maintenance states needed for App.test.tsx
'auth-provider': expect.getState().testPath?.includes('App.test')
? 'icat'
: 'jwt',
'auth-provider': 'jwt',
'ui-strings': '/res/default.json',
plugins: [],
'help-tour-steps': [],
Expand Down
38 changes: 33 additions & 5 deletions src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,40 @@ vi.mock('@mui/material', async () => ({
useMediaQuery: vi.fn(),
}));

const testToken =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3QifQ.hNQI_r8BATy1LyXPr6Zuo9X_V0kSED8ngcqQ6G-WV5w';
// Needed for the maintenance state update test, has to be hoisted in order to be run before any imports
vi.hoisted(() => {
const testToken =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3QifQ.hNQI_r8BATy1LyXPr6Zuo9X_V0kSED8ngcqQ6G-WV5w';
vi.spyOn(window.localStorage.__proto__, 'getItem').mockImplementation(
(name) => (name === 'scigateway:token' ? testToken : null)
);
});

// needed for the maintenance state update test - for some reason it doesn't work when at the beginning of the test itself
window.localStorage.__proto__.getItem = vi.fn().mockImplementation((name) => {
return name === 'scigateway:token' ? testToken : null;
/* Have to remock to replace the auth-provider just for this file. We used to be able to use
expect.getState().testPath?.includes('App.test') inside the __mocks__ folder, but this is undefined
until the tests in this file are actually executed since migrating from Jest to Vitest */
vi.mock('axios', async () => {
return {
default: {
get: vi.fn((path) => {
if (path === '/settings.json') {
return Promise.resolve({
data: {
'auth-provider': 'icat',
'ui-strings': '/res/default.json',
plugins: [],
'help-tour-steps': [],
},
});
} else {
return Promise.resolve({
data: {},
});
}
}),
post: vi.fn(() => Promise.resolve({ data: {} })),
},
};
});

describe('App', () => {
Expand Down

0 comments on commit 8608169

Please sign in to comment.