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 8dbfdc1
Show file tree
Hide file tree
Showing 42 changed files with 92 additions and 92 deletions.
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
18 changes: 9 additions & 9 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 All @@ -29,12 +29,12 @@ describe('images api functions', () => {
{ timeout: 5000 }
);

const request = await pendingRequest;
const requestURL = new URL((await pendingRequest).url);

params.set('original_image', 'true');

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

it('sends request to fetch original image with empty false colour params and returns successful response', async () => {
Expand All @@ -51,12 +51,12 @@ describe('images api functions', () => {
{ timeout: 5000 }
);

const request = await pendingRequest;
const requestURL = new URL((await pendingRequest).url);

params.set('original_image', 'true');

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

it('sends request to fetch false colour image and returns successful response', async () => {
Expand All @@ -81,14 +81,14 @@ describe('images api functions', () => {
{ timeout: 5000 }
);

const request = await pendingRequest;
const requestURL = new URL((await pendingRequest).url);

params.set('colourmap_name', 'red');
params.set('lower_level', '5');
params.set('upper_level', '200');

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

it.todo(
Expand Down Expand Up @@ -122,14 +122,14 @@ describe('images api functions', () => {
expect(result.current.isSuccess).toBeTruthy();
});

const request = await pendingRequest;
const requestURL = new URL((await pendingRequest).url);

params.set('colourmap_name', 'red');
params.set('lower_level', '5');
params.set('upper_level', '200');

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

it.todo(
Expand Down
50 changes: 25 additions & 25 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 @@ -96,14 +96,14 @@ describe('records api functions', () => {
expect(result.current.isSuccess).toBeTruthy();
});

const request = await pendingRequest;
const requestURL = new URL((await pendingRequest).url);

params.append(
'conditions',
'{"$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(requestURL.searchParams.toString()).toEqual(params.toString());
expect(result.current.data).toEqual(recordsJson.length);
});

Expand All @@ -129,10 +129,10 @@ describe('records api functions', () => {
expect(incomingRecordCountResult.current.isSuccess).toBeTruthy();
});

const request = await pendingRequest;
const requestURL = new URL((await pendingRequest).url);

// We should have made one call to /records/count
expect(request.url.searchParams.toString()).toEqual(params.toString());
expect(requestURL.searchParams.toString()).toEqual(params.toString());
expect(incomingRecordCountResult.current.data).toEqual(
recordsJson.length
);
Expand Down Expand Up @@ -299,14 +299,14 @@ describe('records api functions', () => {
expect(result.current.isSuccess).toBeTruthy();
});

const request = await pendingRequest;
const requestURL = new URL((await pendingRequest).url);

params.append(
'conditions',
'{"$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(requestURL.searchParams.toString()).toEqual(params.toString());
expect(result.current.data).toEqual(recordsJson.length);
});

Expand Down Expand Up @@ -346,14 +346,14 @@ describe('records api functions', () => {
expect(result.current.isSuccess).toBeTruthy();
});

const request = await pendingRequest;
const requestURL = new URL((await pendingRequest).url);

params.append(
'conditions',
'{"$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(requestURL.searchParams.toString()).toEqual(params.toString());
expect(result.current.data).toEqual(recordsJson.length);
});

Expand All @@ -380,14 +380,14 @@ describe('records api functions', () => {
expect(result.current.isSuccess).toBeTruthy();
});

const request = await pendingRequest;
const requestURL = new URL((await pendingRequest).url);

params.append('order', 'metadata.timestamp asc');
params.append('projection', `metadata.${timeChannelName}`);
params.append('skip', '0');
params.append('limit', '25');

expect(request.url.searchParams.toString()).toEqual(params.toString());
expect(requestURL.searchParams.toString()).toEqual(params.toString());

expect(result.current.data).toMatchSnapshot();
});
Expand Down Expand Up @@ -433,7 +433,7 @@ describe('records api functions', () => {
expect(result.current.isSuccess).toBeTruthy();
});

const request = await pendingRequest;
const requestURL = new URL((await pendingRequest).url);

params.append('order', 'metadata.timestamp asc');
params.append('order', 'channels.CHANNEL_1 desc');
Expand All @@ -446,7 +446,7 @@ describe('records api functions', () => {
params.append('skip', '0');
params.append('limit', '25');

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

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

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

it('uses a select function to format the results', async () => {
Expand All @@ -487,7 +487,7 @@ describe('records api functions', () => {
expect(result.current.isSuccess).toBeTruthy();
});

const request = await pendingRequest;
const requestURL = new URL((await pendingRequest).url);

// Default params for usePlotRecords
params.append('order', 'metadata.timestamp asc'); // Assume the user is plotting time-series graph
Expand All @@ -508,7 +508,7 @@ describe('records api functions', () => {
params.append('skip', '0');
params.append('limit', '50');

expect(request.url.searchParams.toString()).toEqual(params.toString());
expect(requestURL.searchParams.toString()).toEqual(params.toString());

const expectedData: PlotDataset[] = [
{
Expand Down Expand Up @@ -568,7 +568,7 @@ describe('records api functions', () => {
expect(result.current.isSuccess).toBeTruthy();
});

const request = await pendingRequest;
const requestURL = new URL((await pendingRequest).url);

params.append('order', 'metadata.shotnum asc');
params.append('projection', 'metadata.shotnum');
Expand All @@ -592,7 +592,7 @@ describe('records api functions', () => {
params.append('skip', '0');
params.append('limit', '1000');

expect(request.url.searchParams.toString()).toEqual(params.toString());
expect(requestURL.searchParams.toString()).toEqual(params.toString());

const expectedData: PlotDataset[] = [
{
Expand Down Expand Up @@ -642,7 +642,7 @@ describe('records api functions', () => {
expect(result.current.isSuccess).toBeTruthy();
});

const request = await pendingRequest;
const requestURL = new URL((await pendingRequest).url);

params.append('order', 'metadata.timestamp asc');
params.append('projection', `metadata.${timeChannelName}`);
Expand All @@ -659,7 +659,7 @@ describe('records api functions', () => {
'{"$or":' + JSON.stringify(existsConditions) + '}'
);

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

Expand All @@ -671,7 +671,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 All @@ -685,7 +685,7 @@ describe('records api functions', () => {
expect(result.current.isSuccess).toBeTruthy();
});

const request = await pendingRequest;
const requestURL = new URL((await pendingRequest).url);

params.append('order', 'metadata.timestamp asc');
params.append('projection', 'channels.TEST');
Expand All @@ -699,7 +699,7 @@ describe('records api functions', () => {
params.append('skip', '25');
params.append('limit', '25');

expect(request.url.searchParams.toString()).toEqual(params.toString());
expect(requestURL.searchParams.toString()).toEqual(params.toString());

expect(result.current.data).toEqual(recordsJson);
});
Expand Down Expand Up @@ -744,7 +744,7 @@ describe('records api functions', () => {
expect(result.current.isSuccess).toBeTruthy();
});

const request = await pendingRequest;
const requestURL = new URL((await pendingRequest).url);

params.append('order', 'metadata.timestamp asc');
params.append('order', 'channels.CHANNEL_1 desc');
Expand All @@ -757,7 +757,7 @@ describe('records api functions', () => {
params.append('skip', '0');
params.append('limit', '25');

expect(request.url.searchParams.toString()).toEqual(params.toString());
expect(requestURL.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
Loading

0 comments on commit 8dbfdc1

Please sign in to comment.