Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored src/screens/OrganizationEvents.tsx from Jest to Vitest #2559 #3013

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from '@testing-library/react';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
import 'jest-location-mock';
import { I18nextProvider } from 'react-i18next';

import OrganizationEvents from './OrganizationEvents';
Expand All @@ -23,14 +22,31 @@ import { ThemeProvider } from 'react-bootstrap';
import { LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { MOCKS } from './OrganizationEventsMocks';

import { describe, test, expect, vi } from 'vitest';
const theme = createTheme({
palette: {
primary: {
main: '#31bb6b',
},
},
});
Object.defineProperty(window, 'location', {
value: {
href: 'http://localhost/',
assign: vi.fn((url) => {
const urlObj = new URL(url, 'http://localhost');
window.location.href = urlObj.href;
window.location.pathname = urlObj.pathname;
window.location.search = urlObj.search;
window.location.hash = urlObj.hash;
}),
reload: vi.fn(),
pathname: '/',
search: '',
hash: '',
origin: 'http://localhost',
},
});

const link = new StaticMockLink(MOCKS, true);
const link2 = new StaticMockLink([], true);
Expand All @@ -42,7 +58,6 @@ async function wait(ms = 100): Promise<void> {
});
});
}

const translations = {
...JSON.parse(
JSON.stringify(
Expand All @@ -53,19 +68,20 @@ const translations = {
...JSON.parse(JSON.stringify(i18n.getDataByLanguage('en')?.errors ?? {})),
};

jest.mock('@mui/x-date-pickers/DateTimePicker', () => {
vi.mock('@mui/x-date-pickers/DateTimePicker', async () => {
const actual = await vi.importActual(
'@mui/x-date-pickers/DesktopDateTimePicker',
);
return {
DateTimePicker: jest.requireActual(
'@mui/x-date-pickers/DesktopDateTimePicker',
).DesktopDateTimePicker,
DateTimePicker: actual.DesktopDateTimePicker,
};
});

jest.mock('react-toastify', () => ({
vi.mock('react-toastify', () => ({
toast: {
success: jest.fn(),
warning: jest.fn(),
error: jest.fn(),
success: vi.fn(),
warning: vi.fn(),
error: vi.fn(),
},
}));

Expand All @@ -80,7 +96,7 @@ describe('Organisation Events Page', () => {
endTime: '05:00 PM',
};

global.alert = jest.fn();
global.alert = vi.fn();

test('It is necessary to query the correct mock data.', async () => {
const dataQuery1 = MOCKS[0]?.result?.data?.eventsByOrganizationConnection;
Expand Down Expand Up @@ -148,7 +164,7 @@ describe('Organisation Events Page', () => {
expect(container.textContent).not.toBe('Loading data...');
await wait();
expect(container.textContent).toMatch('Month');
expect(window.location).toBeAt('/orglist');
expect(window.location.pathname).toBe('/orglist');
});

test('No mock data', async () => {
Expand Down
Loading