Skip to content

Commit

Permalink
Refactored src/screens/OrganizationEvents.tsx from Jest to Vitest #2559
Browse files Browse the repository at this point in the history
… (#3013)

* file name changed

* mocking window object

* migration success

* fixing datetimepicker error

* datepicker error fixed

* fixed the datepickerError
  • Loading branch information
Ramneet04 authored Dec 29, 2024
1 parent 78699b3 commit 1bc3c5e
Showing 1 changed file with 29 additions and 13 deletions.
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

0 comments on commit 1bc3c5e

Please sign in to comment.