From 006af307f0aa4020ad7aef09abc3bac397d9fc56 Mon Sep 17 00:00:00 2001 From: Arpit Chakraborty Date: Thu, 26 Dec 2024 11:48:05 +0530 Subject: [PATCH 1/3] Added test for prior calender update --- ...ags.test.tsx => OrganizationTags.spec.tsx} | 22 ++++++++++--------- .../OrganizationTags/OrganizationTagsMocks.ts | 12 +++++----- 2 files changed, 18 insertions(+), 16 deletions(-) rename src/screens/OrganizationTags/{OrganizationTags.test.tsx => OrganizationTags.spec.tsx} (96%) diff --git a/src/screens/OrganizationTags/OrganizationTags.test.tsx b/src/screens/OrganizationTags/OrganizationTags.spec.tsx similarity index 96% rename from src/screens/OrganizationTags/OrganizationTags.test.tsx rename to src/screens/OrganizationTags/OrganizationTags.spec.tsx index 0d426d20ac..c35b69f631 100644 --- a/src/screens/OrganizationTags/OrganizationTags.test.tsx +++ b/src/screens/OrganizationTags/OrganizationTags.spec.tsx @@ -11,7 +11,7 @@ import { waitForElementToBeRemoved, } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; -import 'jest-location-mock'; +import { vi } from 'vitest'; import { I18nextProvider } from 'react-i18next'; import { Provider } from 'react-redux'; import { MemoryRouter, Route, Routes } from 'react-router-dom'; @@ -44,10 +44,10 @@ async function wait(ms = 500): Promise { }); } -jest.mock('react-toastify', () => ({ +vi.mock('react-toastify', () => ({ toast: { - success: jest.fn(), - error: jest.fn(), + success: vi.fn(), + error: vi.fn(), }, })); @@ -77,14 +77,17 @@ const renderOrganizationTags = (link: ApolloLink): RenderResult => { describe('Organisation Tags Page', () => { beforeEach(() => { - jest.mock('react-router-dom', () => ({ - ...jest.requireActual('react-router-dom'), - useParams: () => ({ orgId: 'orgId' }), - })); + vi.mock('react-router-dom', async () => { + const actual = await vi.importActual('react-router-dom'); + return { + ...actual, + useParams: () => ({ orgId: 'orgId' }), + }; + }); }); afterEach(() => { - jest.clearAllMocks(); + vi.clearAllMocks(); cleanup(); }); @@ -129,7 +132,6 @@ describe('Organisation Tags Page', () => { screen.queryByTestId('closeCreateTagModal'), ); }); - test('navigates to sub tags screen after clicking on a tag', async () => { renderOrganizationTags(link); diff --git a/src/screens/OrganizationTags/OrganizationTagsMocks.ts b/src/screens/OrganizationTags/OrganizationTagsMocks.ts index 0fe48ca97f..7fa6a4ef0e 100644 --- a/src/screens/OrganizationTags/OrganizationTagsMocks.ts +++ b/src/screens/OrganizationTags/OrganizationTagsMocks.ts @@ -7,7 +7,7 @@ export const MOCKS = [ request: { query: ORGANIZATION_USER_TAGS_LIST, variables: { - id: '123', + id: 'orgId', first: TAGS_QUERY_DATA_CHUNK_SIZE, where: { name: { starts_with: '' } }, sortedBy: { id: 'DESCENDING' }, @@ -187,7 +187,7 @@ export const MOCKS = [ request: { query: ORGANIZATION_USER_TAGS_LIST, variables: { - id: '123', + id: 'orgId', first: TAGS_QUERY_DATA_CHUNK_SIZE, after: '10', where: { name: { starts_with: '' } }, @@ -248,7 +248,7 @@ export const MOCKS = [ request: { query: ORGANIZATION_USER_TAGS_LIST, variables: { - id: '123', + id: 'orgId', first: TAGS_QUERY_DATA_CHUNK_SIZE, where: { name: { starts_with: 'searchUserTag' } }, sortedBy: { id: 'DESCENDING' }, @@ -322,7 +322,7 @@ export const MOCKS = [ request: { query: ORGANIZATION_USER_TAGS_LIST, variables: { - id: '123', + id: 'orgId', first: TAGS_QUERY_DATA_CHUNK_SIZE, where: { name: { starts_with: 'searchUserTag' } }, sortedBy: { id: 'ASCENDING' }, @@ -397,7 +397,7 @@ export const MOCKS = [ query: CREATE_USER_TAG, variables: { name: 'userTag 12', - organizationId: '123', + organizationId: 'orgId', }, }, result: { @@ -415,7 +415,7 @@ export const MOCKS_ERROR = [ request: { query: ORGANIZATION_USER_TAGS_LIST, variables: { - id: '123', + id: 'orgId', first: TAGS_QUERY_DATA_CHUNK_SIZE, where: { name: { starts_with: '' } }, sortedBy: { id: 'DESCENDING' }, From 54dc1bc0ba6910741507fe0da39c3935f7dca6e0 Mon Sep 17 00:00:00 2001 From: Arpit Chakraborty Date: Sun, 29 Dec 2024 20:53:19 +0530 Subject: [PATCH 2/3] updated jest to vitest --- .../UsersTableItem/UserTableItem.spec.tsx | 29 +++++++------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/components/UsersTableItem/UserTableItem.spec.tsx b/src/components/UsersTableItem/UserTableItem.spec.tsx index a0b0c39c86..afa4a30046 100644 --- a/src/components/UsersTableItem/UserTableItem.spec.tsx +++ b/src/components/UsersTableItem/UserTableItem.spec.tsx @@ -1,4 +1,4 @@ -import React, { act } from 'react'; +import React from 'react'; import { MockedProvider } from '@apollo/react-testing'; import { fireEvent, render, screen } from '@testing-library/react'; import { I18nextProvider } from 'react-i18next'; @@ -9,21 +9,19 @@ import type { InterfaceQueryUserListItem } from 'utils/interfaces'; import { MOCKS, MOCKS2, MOCKS_UPDATE } from './UserTableItemMocks'; import UsersTableItem from './UsersTableItem'; import { BrowserRouter } from 'react-router-dom'; +import useLocalStorage from 'utils/useLocalstorage'; +import userEvent from '@testing-library/user-event'; +import { vi, beforeEach, afterEach, describe, it, expect } from 'vitest'; + const link = new StaticMockLink(MOCKS, true); const link2 = new StaticMockLink(MOCKS2, true); const link3 = new StaticMockLink(MOCKS_UPDATE, true); -import useLocalStorage from 'utils/useLocalstorage'; -import userEvent from '@testing-library/user-event'; -import { vi } from 'vitest'; -import type * as RouterTypes from 'react-router-dom'; const { setItem } = useLocalStorage(); async function wait(ms = 100): Promise { - await act(() => { - return new Promise((resolve) => { - setTimeout(resolve, ms); - }); + await new Promise((resolve) => { + setTimeout(resolve, ms); }); } const resetAndRefetchMock = vi.fn(); @@ -45,15 +43,10 @@ Object.defineProperty(window, 'location', { const mockNavgatePush = vi.fn(); -vi.mock('react-router-dom', async () => { - const actual = (await vi.importActual( - 'react-router-dom', - )) as typeof RouterTypes; - return { - ...actual, - useNavigate: () => mockNavgatePush, - }; -}); +jest.mock('react-router-dom', () => ({ + ...jest.requireActual('react-router-dom'), + useNavigate: () => mockNavgatePush, +})); beforeEach(() => { setItem('SuperAdmin', true); From 4341660255bec927bf8c5aafd0e412ca3897b6a8 Mon Sep 17 00:00:00 2001 From: Arpit Chakraborty Date: Tue, 31 Dec 2024 17:03:46 +0530 Subject: [PATCH 3/3] resolved conflicts v2 --- .../UsersTableItem/UserTableItem.spec.tsx | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/components/UsersTableItem/UserTableItem.spec.tsx b/src/components/UsersTableItem/UserTableItem.spec.tsx index afa4a30046..a0b0c39c86 100644 --- a/src/components/UsersTableItem/UserTableItem.spec.tsx +++ b/src/components/UsersTableItem/UserTableItem.spec.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { act } from 'react'; import { MockedProvider } from '@apollo/react-testing'; import { fireEvent, render, screen } from '@testing-library/react'; import { I18nextProvider } from 'react-i18next'; @@ -9,19 +9,21 @@ import type { InterfaceQueryUserListItem } from 'utils/interfaces'; import { MOCKS, MOCKS2, MOCKS_UPDATE } from './UserTableItemMocks'; import UsersTableItem from './UsersTableItem'; import { BrowserRouter } from 'react-router-dom'; -import useLocalStorage from 'utils/useLocalstorage'; -import userEvent from '@testing-library/user-event'; -import { vi, beforeEach, afterEach, describe, it, expect } from 'vitest'; - const link = new StaticMockLink(MOCKS, true); const link2 = new StaticMockLink(MOCKS2, true); const link3 = new StaticMockLink(MOCKS_UPDATE, true); +import useLocalStorage from 'utils/useLocalstorage'; +import userEvent from '@testing-library/user-event'; +import { vi } from 'vitest'; +import type * as RouterTypes from 'react-router-dom'; const { setItem } = useLocalStorage(); async function wait(ms = 100): Promise { - await new Promise((resolve) => { - setTimeout(resolve, ms); + await act(() => { + return new Promise((resolve) => { + setTimeout(resolve, ms); + }); }); } const resetAndRefetchMock = vi.fn(); @@ -43,10 +45,15 @@ Object.defineProperty(window, 'location', { const mockNavgatePush = vi.fn(); -jest.mock('react-router-dom', () => ({ - ...jest.requireActual('react-router-dom'), - useNavigate: () => mockNavgatePush, -})); +vi.mock('react-router-dom', async () => { + const actual = (await vi.importActual( + 'react-router-dom', + )) as typeof RouterTypes; + return { + ...actual, + useNavigate: () => mockNavgatePush, + }; +}); beforeEach(() => { setItem('SuperAdmin', true);