From 1bc3c5ec7d1130a5e8ca86438a939e8cb3201d8b Mon Sep 17 00:00:00 2001 From: Ramneet Singh <144323012+Ramneet04@users.noreply.github.com> Date: Mon, 30 Dec 2024 00:38:26 +0530 Subject: [PATCH] Refactored src/screens/OrganizationEvents.tsx from Jest to Vitest #2559 (#3013) * file name changed * mocking window object * migration success * fixing datetimepicker error * datepicker error fixed * fixed the datepickerError --- ...s.test.tsx => OrganizationEvents.spec.tsx} | 42 +++++++++++++------ 1 file changed, 29 insertions(+), 13 deletions(-) rename src/screens/OrganizationEvents/{OrganizationEvents.test.tsx => OrganizationEvents.spec.tsx} (93%) diff --git a/src/screens/OrganizationEvents/OrganizationEvents.test.tsx b/src/screens/OrganizationEvents/OrganizationEvents.spec.tsx similarity index 93% rename from src/screens/OrganizationEvents/OrganizationEvents.test.tsx rename to src/screens/OrganizationEvents/OrganizationEvents.spec.tsx index 09e896e031..e329130895 100644 --- a/src/screens/OrganizationEvents/OrganizationEvents.test.tsx +++ b/src/screens/OrganizationEvents/OrganizationEvents.spec.tsx @@ -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'; @@ -23,7 +22,7 @@ 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: { @@ -31,6 +30,23 @@ const theme = createTheme({ }, }, }); +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); @@ -42,7 +58,6 @@ async function wait(ms = 100): Promise { }); }); } - const translations = { ...JSON.parse( JSON.stringify( @@ -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(), }, })); @@ -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; @@ -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 () => {