Skip to content

Commit

Permalink
DSEGOG-341 Replace jest.clearAllMocks with vi.clearAllMocks and fix a…
Browse files Browse the repository at this point in the history
…pi unit tests
  • Loading branch information
joelvdavies committed Aug 13, 2024
1 parent 34d3ad2 commit 86fb22e
Show file tree
Hide file tree
Showing 43 changed files with 120 additions and 98 deletions.
42 changes: 21 additions & 21 deletions src/api/__snapshots__/records.test.tsx.snap

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions src/api/channels.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { renderHook, waitFor } from '@testing-library/react';
import { http } from 'msw';
import { http, HttpResponse } from 'msw';
import { FullChannelMetadata, timeChannelName } from '../app.types';
import { server } from '../mocks/server';
import { RootState } from '../state/store';
Expand All @@ -20,7 +20,7 @@ import {

describe('channels api functions', () => {
afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

describe('useAvailableColumns', () => {
Expand Down Expand Up @@ -77,8 +77,8 @@ describe('channels api functions', () => {

it('returns no columns if no data was present in the request response', async () => {
server.use(
http.get('/channels', (req, res, ctx) => {
return res(ctx.status(200), ctx.json({ channels: {} }));
http.get('/channels', () => {
return HttpResponse.json({ channels: {} }, { status: 200 });
})
);

Expand Down Expand Up @@ -168,8 +168,8 @@ describe('channels api functions', () => {

it('returns no channels if no data was present in the request response', async () => {
server.use(
http.get('/channels', (req, res, ctx) => {
return res(ctx.status(200), ctx.json({ channels: {} }));
http.get('/channels', () => {
return HttpResponse.json({ channels: {} }, { status: 200 });
})
);

Expand Down
2 changes: 1 addition & 1 deletion src/api/experiment.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useExperiment } from './experiment';

describe('channels api functions', () => {
afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

describe('useExperiment', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/api/export.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('useExportData', () => {
});

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
document.createElement = document.originalCreateElement;
document.body.appendChild = document.body.originalAppendChild;
});
Expand Down
10 changes: 5 additions & 5 deletions src/api/images.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useColourBar, useColourMaps, useImage } from './images';

describe('images api functions', () => {
afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

describe('useImage', () => {
Expand Down Expand Up @@ -34,7 +34,7 @@ describe('images api functions', () => {
params.set('original_image', 'true');

expect(result.current.data).toEqual('testObjectUrl');
expect(request.url.searchParams).toEqual(params);
expect(new URL(request.url).searchParams).toEqual(params);
});

it('sends request to fetch original image with empty false colour params and returns successful response', async () => {
Expand All @@ -56,7 +56,7 @@ describe('images api functions', () => {
params.set('original_image', 'true');

expect(result.current.data).toEqual('testObjectUrl');
expect(request.url.searchParams).toEqual(params);
expect(new URL(request.url).searchParams).toEqual(params);
});

it('sends request to fetch false colour image and returns successful response', async () => {
Expand Down Expand Up @@ -88,7 +88,7 @@ describe('images api functions', () => {
params.set('upper_level', '200');

expect(result.current.data).toEqual('testObjectUrl');
expect(request.url.searchParams).toEqual(params);
expect(new URL(request.url).searchParams).toEqual(params);
});

it.todo(
Expand Down Expand Up @@ -129,7 +129,7 @@ describe('images api functions', () => {
params.set('upper_level', '200');

expect(result.current.data).toEqual('testObjectUrl');
expect(request.url.searchParams).toEqual(params);
expect(new URL(request.url).searchParams).toEqual(params);
});

it.todo(
Expand Down
50 changes: 36 additions & 14 deletions src/api/records.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('records api functions', () => {
});

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

describe('useRecordCount', () => {
Expand Down Expand Up @@ -103,7 +103,9 @@ describe('records api functions', () => {
'{"$and":[{"metadata.timestamp":{"$gte":"2022-01-01 00:00:00","$lte":"2022-01-02 00:00:00"}},{"metadata.shotnum":{"$gt":300}}]}'
);

expect(request.url.searchParams.toString()).toEqual(params.toString());
expect(new URL(request.url).searchParams.toString()).toEqual(
params.toString()
);
expect(result.current.data).toEqual(recordsJson.length);
});

Expand Down Expand Up @@ -132,7 +134,9 @@ describe('records api functions', () => {
const request = await pendingRequest;

// We should have made one call to /records/count
expect(request.url.searchParams.toString()).toEqual(params.toString());
expect(new URL(request.url).searchParams.toString()).toEqual(
params.toString()
);
expect(incomingRecordCountResult.current.data).toEqual(
recordsJson.length
);
Expand Down Expand Up @@ -306,7 +310,9 @@ describe('records api functions', () => {
'{"$and":[{"metadata.timestamp":{"$gte":"2022-01-01 00:00:00","$lte":"2022-01-02 00:00:00"}},{"metadata.shotnum":{"$gt":300}}]}'
);

expect(request.url.searchParams.toString()).toEqual(params.toString());
expect(new URL(request.url).searchParams.toString()).toEqual(
params.toString()
);
expect(result.current.data).toEqual(recordsJson.length);
});

Expand Down Expand Up @@ -353,7 +359,9 @@ describe('records api functions', () => {
'{"$and":[{"metadata.timestamp":{"$gte":"2022-01-01 00:00:00","$lte":"2022-01-02 00:00:00"}},{"metadata.shotnum":{"$gt":300}}]}'
);

expect(request.url.searchParams.toString()).toEqual(params.toString());
expect(new URL(request.url).searchParams.toString()).toEqual(
params.toString()
);
expect(result.current.data).toEqual(recordsJson.length);
});

Expand Down Expand Up @@ -387,7 +395,9 @@ describe('records api functions', () => {
params.append('skip', '0');
params.append('limit', '25');

expect(request.url.searchParams.toString()).toEqual(params.toString());
expect(new URL(request.url).searchParams.toString()).toEqual(
params.toString()
);

expect(result.current.data).toMatchSnapshot();
});
Expand Down Expand Up @@ -446,7 +456,9 @@ describe('records api functions', () => {
params.append('skip', '0');
params.append('limit', '25');

expect(request.url.searchParams.toString()).toEqual(params.toString());
expect(new URL(request.url).searchParams.toString()).toEqual(
params.toString()
);
});
});

Expand All @@ -470,7 +482,7 @@ describe('records api functions', () => {
});

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

it('uses a select function to format the results', async () => {
Expand Down Expand Up @@ -508,7 +520,9 @@ describe('records api functions', () => {
params.append('skip', '0');
params.append('limit', '50');

expect(request.url.searchParams.toString()).toEqual(params.toString());
expect(new URL(request.url).searchParams.toString()).toEqual(
params.toString()
);

const expectedData: PlotDataset[] = [
{
Expand Down Expand Up @@ -592,7 +606,9 @@ describe('records api functions', () => {
params.append('skip', '0');
params.append('limit', '1000');

expect(request.url.searchParams.toString()).toEqual(params.toString());
expect(new URL(request.url).searchParams.toString()).toEqual(
params.toString()
);

const expectedData: PlotDataset[] = [
{
Expand Down Expand Up @@ -659,7 +675,9 @@ describe('records api functions', () => {
'{"$or":' + JSON.stringify(existsConditions) + '}'
);

expect(request.url.searchParams.toString()).toEqual(params.toString());
expect(new URL(request.url).searchParams.toString()).toEqual(
params.toString()
);
});
});

Expand All @@ -671,7 +689,7 @@ describe('records api functions', () => {
});

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

it('sends request to fetch records with a projection and returns successful response', async () => {
Expand Down Expand Up @@ -699,7 +717,9 @@ describe('records api functions', () => {
params.append('skip', '25');
params.append('limit', '25');

expect(request.url.searchParams.toString()).toEqual(params.toString());
expect(new URL(request.url).searchParams.toString()).toEqual(
params.toString()
);

expect(result.current.data).toEqual(recordsJson);
});
Expand Down Expand Up @@ -757,7 +777,9 @@ describe('records api functions', () => {
params.append('skip', '0');
params.append('limit', '25');

expect(request.url.searchParams.toString()).toEqual(params.toString());
expect(new URL(request.url).searchParams.toString()).toEqual(
params.toString()
);
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/api/sessions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('session api functions', () => {
};
});
afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

describe('useSaveSession', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/api/userPreferences.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('user preferences api functions', () => {
const axiosDelete = jest.spyOn(axios, 'delete');

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

describe('useUserPreference', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/api/waveforms.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useWaveform } from './waveforms';

describe('waveform api functions', () => {
afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

describe('useWaveform', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/channels/channelTree.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('Channel Tree', () => {
};

beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

it('should render correctly for root', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/channels/channelsDialogue.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('Channels Dialogue', () => {
});

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

it('renders channels dialogue when dialogue is open', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/filtering/filterDialogue.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Filter dialogue component', () => {
});

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

it('renders filter dialogue when dialogue is open', async () => {
Expand Down
16 changes: 8 additions & 8 deletions src/images/falseColourPanel.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ describe('False colour panel component', () => {
changeUpperLevel,
};

jest.clearAllMocks();
vi.clearAllMocks();
});

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

const createView = () => {
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('False colour panel component', () => {

expect(changeColourMap).toHaveBeenCalledWith('cividis');

jest.clearAllMocks();
vi.clearAllMocks();

// check user can select "default" colourmap
await user.click(select);
Expand Down Expand Up @@ -120,7 +120,7 @@ describe('False colour panel component', () => {
expect(changeUpperLevel).toHaveBeenCalledWith(undefined);
expect(extendedColourMapSwitch).toBeDisabled();

jest.clearAllMocks();
vi.clearAllMocks();

await user.click(falseColourSwitch);

Expand All @@ -144,7 +144,7 @@ describe('False colour panel component', () => {
await user.click(within(dropdown).getByRole('option', { name: 'cividis' }));

await user.click(falseColourSwitch);
jest.clearAllMocks();
vi.clearAllMocks();
await user.click(falseColourSwitch);

expect(falseColourSwitch).toBeChecked();
Expand Down Expand Up @@ -182,7 +182,7 @@ describe('False colour panel component', () => {
expect(reverseColourSwitch).toBeChecked();
expect(changeColourMap).toHaveBeenCalledWith('inferno_r');

jest.clearAllMocks();
vi.clearAllMocks();

const select2 = screen.getByLabelText('Colour Map');
await user.click(select);
Expand All @@ -199,14 +199,14 @@ describe('False colour panel component', () => {

expect(changeColourMap).toHaveBeenCalledWith('cividis_r');

jest.clearAllMocks();
vi.clearAllMocks();

await user.click(reverseColourSwitch);
expect(changeColourMap).toHaveBeenCalledWith('cividis');

await user.click(reverseColourSwitch);

jest.clearAllMocks();
vi.clearAllMocks();

// the reverse colour is disabled when it doesn't exist

Expand Down
2 changes: 1 addition & 1 deletion src/images/imageWindow.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('Image Window component', () => {
});

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

const createView = () => {
Expand Down
4 changes: 2 additions & 2 deletions src/mocks/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const handlers = [

const validId = [1, 2, 3, 4];
if (validId.includes(Number(id))) {
return HttpResponse.json(null, { status: 204 });
return new HttpResponse(null, { status: 204 });
} else HttpResponse.json(null, { status: 422 });
}),
http.get('/sessions/list', async () => {
Expand Down Expand Up @@ -277,7 +277,7 @@ export const handlers = [
if (typeof preferredColourMap === 'undefined') {
return HttpResponse.json(
{ detail: 'No such attribute in database' },
{ status: 200 }
{ status: 404 }
);
} else {
return HttpResponse.json(preferredColourMap, { status: 200 });
Expand Down
Loading

0 comments on commit 86fb22e

Please sign in to comment.