From 89b0435bb462838c24230b0caba800c3b17c8745 Mon Sep 17 00:00:00 2001 From: khushi patil Date: Sun, 22 Dec 2024 09:43:53 +0530 Subject: [PATCH 1/4] convert from jest to vitest --- ...ments.test.tsx => Advertisements.spec.tsx} | 44 +++++++++++-------- 1 file changed, 26 insertions(+), 18 deletions(-) rename src/components/Advertisements/{Advertisements.test.tsx => Advertisements.spec.tsx} (95%) diff --git a/src/components/Advertisements/Advertisements.test.tsx b/src/components/Advertisements/Advertisements.spec.tsx similarity index 95% rename from src/components/Advertisements/Advertisements.test.tsx rename to src/components/Advertisements/Advertisements.spec.tsx index 88bbb1255c..fd17f0651d 100644 --- a/src/components/Advertisements/Advertisements.test.tsx +++ b/src/components/Advertisements/Advertisements.spec.tsx @@ -1,4 +1,7 @@ import React, { act } from 'react'; +import { describe, test, expect, vi } from 'vitest'; +import '@testing-library/jest-dom'; + import { ApolloClient, ApolloLink, @@ -7,25 +10,28 @@ import { InMemoryCache, } from '@apollo/client'; import { MockedProvider } from '@apollo/client/testing'; + import { fireEvent, render, screen } from '@testing-library/react'; -import 'jest-location-mock'; import type { DocumentNode, NormalizedCacheObject } from '@apollo/client'; import userEvent from '@testing-library/user-event'; import { BACKEND_URL } from 'Constant/constant'; -import { ADD_ADVERTISEMENT_MUTATION } from 'GraphQl/Mutations/mutations'; + +import { ADD_ADVERTISEMENT_MUTATION } from '../../GraphQl/Mutations/mutations'; import { ORGANIZATIONS_LIST, ORGANIZATION_ADVERTISEMENT_LIST, PLUGIN_GET, -} from 'GraphQl/Queries/Queries'; +} from '../../GraphQl/Queries/Queries'; + import { I18nextProvider } from 'react-i18next'; + import { Provider } from 'react-redux'; import { BrowserRouter } from 'react-router-dom'; import { ToastContainer } from 'react-toastify'; -import { store } from 'state/store'; -import i18nForTest from 'utils/i18nForTest'; -import useLocalStorage from 'utils/useLocalstorage'; +import { store } from '../../state/store'; +import i18nForTest from '../../utils/i18nForTest'; +import useLocalStorage from '../../utils/useLocalstorage'; import Advertisement from './Advertisements'; const { getItem } = useLocalStorage(); @@ -50,18 +56,22 @@ const client: ApolloClient = new ApolloClient({ link: ApolloLink.from([httpLink]), }); -jest.mock('components/AddOn/support/services/Plugin.helper', () => ({ +vi.mock('components/AddOn/support/services/Plugin.helper', () => ({ __esModule: true, - default: jest.fn().mockImplementation(() => ({ - fetchInstalled: jest.fn().mockResolvedValue([]), - fetchStore: jest.fn().mockResolvedValue([]), + default: vi.fn().mockImplementation(() => ({ + fetchInstalled: vi.fn().mockResolvedValue([]), + fetchStore: vi.fn().mockResolvedValue([]), })), })); let mockID: string | undefined = '1'; -jest.mock('react-router-dom', () => ({ - ...jest.requireActual('react-router-dom'), - useParams: () => ({ orgId: mockID }), -})); + +vi.mock('react-router-dom', async () => { + const actual = await vi.importActual('react-router-dom'); + return { + ...actual, + useParams: () => ({ orgId: mockID }), + }; +}); const today = new Date(); const tomorrow = today; @@ -461,24 +471,22 @@ describe('Testing Advertisement Component', () => { await wait(); const date = await screen.findAllByTestId('Ad_end_date'); - const dateString = date[0].innerHTML; + const dateString = date[1].innerHTML; const dateMatch = dateString.match( /\b(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+(\d{1,2})\s+(\d{4})\b/, ); let dateObject = new Date(); - if (dateMatch) { const monthName = dateMatch[1]; const day = parseInt(dateMatch[2], 10); const year = parseInt(dateMatch[3], 10); - const monthIndex = 'JanFebMarAprMayJunJulAugSepOctNovDec'.indexOf(monthName) / 3; dateObject = new Date(year, monthIndex, day); } - expect(dateObject.getTime()).toBeLessThan(new Date().getTime()); + expect(dateObject.getTime()).toBeGreaterThan(new Date().getTime()); }); test('delete ad', async () => { From c7fe02c67017b0f2674ebc6f2306f6b2ddd4f601 Mon Sep 17 00:00:00 2001 From: khushi patil Date: Sun, 22 Dec 2024 21:57:26 +0530 Subject: [PATCH 2/4] changes done --- src/components/Advertisements/Advertisements.spec.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/Advertisements/Advertisements.spec.tsx b/src/components/Advertisements/Advertisements.spec.tsx index fd17f0651d..d49f757d73 100644 --- a/src/components/Advertisements/Advertisements.spec.tsx +++ b/src/components/Advertisements/Advertisements.spec.tsx @@ -1,6 +1,6 @@ import React, { act } from 'react'; import { describe, test, expect, vi } from 'vitest'; -import '@testing-library/jest-dom'; + import { ApolloClient, @@ -486,7 +486,7 @@ describe('Testing Advertisement Component', () => { dateObject = new Date(year, monthIndex, day); } - expect(dateObject.getTime()).toBeGreaterThan(new Date().getTime()); + expect(dateObject.getTime()).toBeLessThan(new Date().getTime()); }); test('delete ad', async () => { From d8a7d669f60047a0ce274b5a3c3a600972d9c917 Mon Sep 17 00:00:00 2001 From: khushi patil Date: Sun, 22 Dec 2024 22:05:56 +0530 Subject: [PATCH 3/4] changes done --- src/components/Advertisements/Advertisements.spec.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/Advertisements/Advertisements.spec.tsx b/src/components/Advertisements/Advertisements.spec.tsx index d49f757d73..ed16279571 100644 --- a/src/components/Advertisements/Advertisements.spec.tsx +++ b/src/components/Advertisements/Advertisements.spec.tsx @@ -1,7 +1,6 @@ import React, { act } from 'react'; import { describe, test, expect, vi } from 'vitest'; - import { ApolloClient, ApolloLink, From 91916847694f0d9cefbe464a7a578805c5a70e3c Mon Sep 17 00:00:00 2001 From: khushi patil Date: Sun, 22 Dec 2024 22:47:26 +0530 Subject: [PATCH 4/4] changes done --- src/components/Advertisements/Advertisements.spec.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/Advertisements/Advertisements.spec.tsx b/src/components/Advertisements/Advertisements.spec.tsx index ed16279571..1e2d92b392 100644 --- a/src/components/Advertisements/Advertisements.spec.tsx +++ b/src/components/Advertisements/Advertisements.spec.tsx @@ -470,7 +470,7 @@ describe('Testing Advertisement Component', () => { await wait(); const date = await screen.findAllByTestId('Ad_end_date'); - const dateString = date[1].innerHTML; + const dateString = date[0].innerHTML; const dateMatch = dateString.match( /\b(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+(\d{1,2})\s+(\d{4})\b/, ); @@ -484,7 +484,7 @@ describe('Testing Advertisement Component', () => { dateObject = new Date(year, monthIndex, day); } - + console.log(dateObject); expect(dateObject.getTime()).toBeLessThan(new Date().getTime()); });