Skip to content

Commit

Permalink
👽 [#724] Replace jest mocks with vitest mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-maertens committed Nov 26, 2024
1 parent 3cfd741 commit cfff05a
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 56 deletions.
12 changes: 6 additions & 6 deletions src/components/ButtonsToolbar/test.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const Wrap = ({children}) => (
);

it('Last step of submittable form, button is present', () => {
const mockFunction = jest.fn();
const mockFunction = vi.fn();

act(() => {
root.render(
Expand Down Expand Up @@ -76,7 +76,7 @@ it('Last step of submittable form, button is present', () => {
});

it('Last step of non-submittable form with overview, button is present', () => {
const mockFunction = jest.fn();
const mockFunction = vi.fn();

act(() => {
root.render(
Expand Down Expand Up @@ -107,7 +107,7 @@ it('Last step of non-submittable form with overview, button is present', () => {
});

it('Last step of non-submittable form without overview, button is NOT present', () => {
const mockFunction = jest.fn();
const mockFunction = vi.fn();

act(() => {
root.render(
Expand Down Expand Up @@ -137,7 +137,7 @@ it('Last step of non-submittable form without overview, button is NOT present',
});

it('Non-last step of non-submittable form without overview, button IS present', () => {
const mockFunction = jest.fn();
const mockFunction = vi.fn();

act(() => {
root.render(
Expand Down Expand Up @@ -168,7 +168,7 @@ it('Non-last step of non-submittable form without overview, button IS present',
});

it('Suspending form allowed, button is present', () => {
const mockFunction = jest.fn();
const mockFunction = vi.fn();

renderTest(
<Wrap>
Expand All @@ -191,7 +191,7 @@ it('Suspending form allowed, button is present', () => {
});

it('Suspending form not allowed, button is NOT present', () => {
const mockFunction = jest.fn();
const mockFunction = vi.fn();

renderTest(
<Wrap>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Form.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {routes} from 'components/App';

import {START_FORM_QUERY_PARAM} from './constants';

window.scrollTo = jest.fn();
window.scrollTo = vi.fn();

beforeEach(() => {
localStorage.clear();
Expand All @@ -23,7 +23,7 @@ afterEach(() => {
});

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

const Wrapper = ({form = buildForm(), initialEntry = '/startpagina'}) => {
Expand Down
20 changes: 10 additions & 10 deletions src/components/FormStart/tests.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import useQuery from 'hooks/useQuery';
import FormStart from '.';
import {testForm, testLoginForm} from './fixtures';

jest.mock('hooks/useQuery');
let scrollIntoViewMock = jest.fn();
vi.mock('hooks/useQuery');
let scrollIntoViewMock = vi.fn();
window.HTMLElement.prototype.scrollIntoView = scrollIntoViewMock;

const Wrap = ({children}) => (
Expand All @@ -24,8 +24,8 @@ it('Form start page start if _start parameter is present', async () => {
const testLocation = new URLSearchParams('?_start=1');
useQuery.mockReturnValue(testLocation);

const onFormStart = jest.fn();
const onDestroySession = jest.fn();
const onFormStart = vi.fn();
const onDestroySession = vi.fn();

render(
<Wrap>
Expand Down Expand Up @@ -55,8 +55,8 @@ it.each([
])(
'Form start does not start if there are auth errors / %s',
async (testQuery, expectedMessage) => {
const onFormStart = jest.fn();
const onDestroySession = jest.fn();
const onFormStart = vi.fn();
const onDestroySession = vi.fn();

const testLocation = new URLSearchParams(`?_start=1&${testQuery}`);
useQuery.mockReturnValue(testLocation);
Expand All @@ -74,8 +74,8 @@ it.each([

it('Form start page does not show login buttons if an active submission is present', async () => {
useQuery.mockReturnValue(new URLSearchParams());
const onFormStart = jest.fn();
const onDestroySession = jest.fn();
const onFormStart = vi.fn();
const onDestroySession = vi.fn();

render(
<Wrap>
Expand All @@ -95,8 +95,8 @@ it('Form start page does not show login buttons if an active submission is prese

it('Form start page with initial_data_reference', async () => {
useQuery.mockReturnValue(new URLSearchParams());
const onFormStart = jest.fn();
const onDestroySession = jest.fn();
const onFormStart = vi.fn();
const onDestroySession = vi.fn();

render(
<Wrap>
Expand Down
6 changes: 3 additions & 3 deletions src/components/LoginOptions/tests.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const Wrapper = ({form, onFormStart}) => {
it('Login not required, options wrapped in form tag', async () => {
const user = userEvent.setup();
const form = buildForm({loginRequired: false, loginOptions: [], cosignLoginOptions: []});
const onFormStart = jest.fn(e => e.preventDefault());
const onFormStart = vi.fn(e => e.preventDefault());

render(<Wrapper form={form} onFormStart={onFormStart} />);

Expand Down Expand Up @@ -69,7 +69,7 @@ it('Login required, options not wrapped in form tag', async () => {
],
cosignLoginOptions: [],
});
const onFormStart = jest.fn(e => e.preventDefault());
const onFormStart = vi.fn(e => e.preventDefault());

const {location} = window;
delete window.location;
Expand Down Expand Up @@ -112,7 +112,7 @@ it('Login button has the right URL after cancelling log in', async () => {
cosignLoginOptions: [],
});

const onFormStart = jest.fn(e => e.preventDefault());
const onFormStart = vi.fn(e => e.preventDefault());

const {location} = window;
delete window.location;
Expand Down
16 changes: 8 additions & 8 deletions src/components/Summary/test.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const SUBMISSION = {
isAuthenticated: false,
};

jest.mock('react-use');
jest.mock('hooks/useRefreshSubmission');
vi.mock('react-use');
vi.mock('hooks/useRefreshSubmission');

let container = null;
let root = null;
Expand Down Expand Up @@ -64,8 +64,8 @@ it('Summary displays logout button if isAuthenticated is true', () => {
...SUBMISSION,
isAuthenticated: true,
};
const onDestroySession = jest.fn();
const onConfirm = jest.fn();
const onDestroySession = vi.fn();
const onConfirm = vi.fn();

useAsync.mockReturnValue({loading: false, value: []});
useRefreshSubmission.mockReturnValue(submissionIsAuthenticated);
Expand All @@ -92,8 +92,8 @@ it('Summary does not display logout button if loginRequired is false', () => {
...testForm,
loginRequired: false,
};
const onDestroySession = jest.fn();
const onConfirm = jest.fn();
const onDestroySession = vi.fn();
const onConfirm = vi.fn();

useAsync.mockReturnValue({loading: false, value: []});
useRefreshSubmission.mockReturnValue({...SUBMISSION, isAuthenticated: false});
Expand All @@ -116,8 +116,8 @@ it('Summary does not display logout button if loginRequired is false', () => {
});

it('Summary displays abort button if isAuthenticated is false', () => {
const onDestroySession = jest.fn();
const onConfirm = jest.fn();
const onDestroySession = vi.fn();
const onConfirm = vi.fn();

useAsync.mockReturnValue({loading: false, value: []});
useRefreshSubmission.mockReturnValue({...SUBMISSION, isAuthenticated: false});
Expand Down
6 changes: 3 additions & 3 deletions src/components/SummaryConfirmation/test.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const Wrapper = ({children}) => (
<LiteralsProvider literals={LITERALS}>
<Formik
initialValues={{privacyPolicyAccepted: false, statementOfTruthAccepted: false}}
onSubmit={jest.fn()}
onSubmit={vi.fn()}
>
{children}
</Formik>
Expand All @@ -48,7 +48,7 @@ const Wrapper = ({children}) => (
);

it('Summary of non-submittable form, button is NOT present', () => {
const mockFunction = jest.fn();
const mockFunction = vi.fn();

renderTest(
<Wrapper>
Expand All @@ -66,7 +66,7 @@ it('Summary of non-submittable form, button is NOT present', () => {
});

it('Summary of submittable form, button IS present', () => {
const mockFunction = jest.fn();
const mockFunction = vi.fn();

renderTest(
<Wrapper>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import {
} from '../mocks';
import {SESSION_STORAGE_KEY as APPOINTMENT_SESSION_STORAGE_KEY} from './CreateAppointmentState';

// scrollIntoView is not not supported in Jest
let scrollIntoViewMock = jest.fn();
// scrollIntoView is not not supported in jest-dom
let scrollIntoViewMock = vi.fn();
window.HTMLElement.prototype.scrollIntoView = scrollIntoViewMock;

const routes = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {jest} from '@jest/globals';
import {render as realRender, screen, waitFor} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import messagesEN from 'i18n/compiled/en.json';
Expand Down Expand Up @@ -164,7 +163,7 @@ describe('The appointment summary', () => {
it('processes backend validation errors', async () => {
mswServer.use(mockAppointmentErrorPost);
const user = userEvent.setup({delay: null});
const errorHandler = jest.fn();
const errorHandler = vi.fn();

renderSummary(errorHandler);

Expand Down
7 changes: 3 additions & 4 deletions src/components/appointments/fields/DateSelect.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {jest} from '@jest/globals';
import {act, render as realRender, screen, waitFor} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {Formik} from 'formik';
Expand Down Expand Up @@ -41,15 +40,15 @@ const render = (comp, locationId) =>
);

beforeEach(() => {
jest.useFakeTimers({
vi.useFakeTimers({
advanceTimers: true,
now: new Date('2023-06-12T14:00:00Z'),
});
});

afterEach(() => {
jest.runOnlyPendingTimers();
jest.useRealTimers();
vi.runOnlyPendingTimers();
vi.useRealTimers();
});

describe('The appointment date select', () => {
Expand Down
7 changes: 3 additions & 4 deletions src/components/appointments/fields/TimeSelect.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {jest} from '@jest/globals';
import {act, render as realRender, screen, waitFor} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {Formik} from 'formik';
Expand Down Expand Up @@ -39,15 +38,15 @@ const render = (comp, locationId) =>
);

beforeEach(() => {
jest.useFakeTimers({
vi.useFakeTimers({
advanceTimers: true,
now: new Date('2023-06-12T14:00:00Z'),
});
});

afterEach(() => {
jest.runOnlyPendingTimers();
jest.useRealTimers();
vi.runOnlyPendingTimers();
vi.useRealTimers();
});

describe('The appointment time select', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {jest} from '@jest/globals';
import {act, render as realRender, screen, waitFor} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import messagesEN from 'i18n/compiled/en.json';
Expand Down Expand Up @@ -59,15 +58,15 @@ const render = initialValues => {
};

beforeEach(() => {
jest.useFakeTimers({
vi.useFakeTimers({
advanceTimers: true,
now: new Date('2023-06-12T14:00:00Z'),
});
});

afterEach(() => {
jest.runOnlyPendingTimers();
jest.useRealTimers();
vi.runOnlyPendingTimers();
vi.useRealTimers();
window.sessionStorage.clear();
});

Expand Down
2 changes: 1 addition & 1 deletion src/components/auth/AuthenticationErrors/tests.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {IntlProvider} from 'react-intl';

import {AuthenticationErrors} from '.';

let scrollIntoViewMock = jest.fn();
let scrollIntoViewMock = vi.fn();
window.HTMLElement.prototype.scrollIntoView = scrollIntoViewMock;

let container = null;
Expand Down
4 changes: 2 additions & 2 deletions src/jstests/formio/components/date.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ describe('Date Component', () => {
},
};

const mockTranslation = jest.fn((message, values) => message);
const mockTranslation = vi.fn((message, values) => message);

const componentInstance = new FormioComponent(component, {}, {});
componentInstance.t = mockTranslation;
componentInstance.options.intl = {
formatDate: jest.fn((date, options) => 'formatted date'),
formatDate: vi.fn((date, options) => 'formatted date'),
};

set(
Expand Down
4 changes: 2 additions & 2 deletions src/jstests/formio/components/datetime.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ describe('Datetime Component', () => {
},
};

const mockTranslation = jest.fn((message, values) => message);
const mockTranslation = vi.fn((message, values) => message);

const componentInstance = new FormioComponent(component, {}, {});
componentInstance.t = mockTranslation;
componentInstance.options.intl = {
formatDate: jest.fn((date, options) => 'formatted date'),
formatDate: vi.fn((date, options) => 'formatted date'),
};

set(
Expand Down
6 changes: 3 additions & 3 deletions src/sdk.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {mockFormioTranslations, mockLanguageInfoGet} from 'components/LanguageSe

import {OpenForm} from './sdk';

// scrollIntoView is not supported in Jest
let scrollIntoViewMock = jest.fn();
// scrollIntoView is not supported in jest-dom
let scrollIntoViewMock = vi.fn();
window.HTMLElement.prototype.scrollIntoView = scrollIntoViewMock;

const LANGUAGES = [
Expand Down Expand Up @@ -123,7 +123,7 @@ describe('OpenForm', () => {
mswServer.use(...apiMocks);
const formRoot = document.createElement('div');
const target = document.createElement('div');
const onLanguageChangeMock = jest.fn();
const onLanguageChangeMock = vi.fn();

const form = new OpenForm(formRoot, {
baseUrl: BASE_URL,
Expand Down

0 comments on commit cfff05a

Please sign in to comment.