From c42135bdca221a9eb04af9a91b648a78add892bf Mon Sep 17 00:00:00 2001 From: Rahul Chougule <87270395+rahulch07@users.noreply.github.com> Date: Thu, 26 Dec 2024 01:12:46 +0530 Subject: [PATCH] Refactor src/components/RequestsTableItem/RequestsTableItem.test.tsx from Jest to Vitest (#2873) --- ...em.test.tsx => RequestsTableItem.spec.tsx} | 85 ++++++++++++++++--- .../RequestsTableItemMocks.ts | 8 +- 2 files changed, 79 insertions(+), 14 deletions(-) rename src/components/RequestsTableItem/{RequestsTableItem.test.tsx => RequestsTableItem.spec.tsx} (62%) diff --git a/src/components/RequestsTableItem/RequestsTableItem.test.tsx b/src/components/RequestsTableItem/RequestsTableItem.spec.tsx similarity index 62% rename from src/components/RequestsTableItem/RequestsTableItem.test.tsx rename to src/components/RequestsTableItem/RequestsTableItem.spec.tsx index bbd895300b..b5194fcdce 100644 --- a/src/components/RequestsTableItem/RequestsTableItem.test.tsx +++ b/src/components/RequestsTableItem/RequestsTableItem.spec.tsx @@ -11,6 +11,7 @@ import { BrowserRouter } from 'react-router-dom'; const link = new StaticMockLink(MOCKS, true); import useLocalStorage from 'utils/useLocalstorage'; import userEvent from '@testing-library/user-event'; +import { vi } from 'vitest'; const { setItem } = useLocalStorage(); @@ -21,13 +22,13 @@ async function wait(ms = 100): Promise { }); }); } -const resetAndRefetchMock = jest.fn(); +const resetAndRefetchMock = vi.fn(); -jest.mock('react-toastify', () => ({ +vi.mock('react-toastify', () => ({ toast: { - success: jest.fn(), - error: jest.fn(), - warning: jest.fn(), + success: vi.fn(), + error: vi.fn(), + warning: vi.fn(), }, })); @@ -37,17 +38,17 @@ beforeEach(() => { afterEach(() => { localStorage.clear(); - jest.clearAllMocks(); + vi.clearAllMocks(); }); describe('Testing User Table Item', () => { - console.error = jest.fn((message) => { + console.error = vi.fn((message) => { if (message.includes('validateDOMNesting')) { return; } console.warn(message); }); - test('Should render props and text elements test for the page component', async () => { + it('Should render props and text elements it for the page component', async () => { const props: { request: InterfaceRequestsListItem; index: number; @@ -81,7 +82,7 @@ describe('Testing User Table Item', () => { expect(screen.getByText(/john@example.com/i)).toBeInTheDocument(); }); - test('Accept MembershipRequest Button works properly', async () => { + it('Accept MembershipRequest Button works properly', async () => { const props: { request: InterfaceRequestsListItem; index: number; @@ -113,7 +114,39 @@ describe('Testing User Table Item', () => { userEvent.click(screen.getByTestId('acceptMembershipRequestBtn123')); }); - test('Reject MembershipRequest Button works properly', async () => { + it('Accept MembershipRequest handles error', async () => { + const props: { + request: InterfaceRequestsListItem; + index: number; + resetAndRefetch: () => void; + } = { + request: { + _id: '1', + user: { + firstName: 'John', + lastName: 'Doe', + email: 'john@example.com', + }, + }, + index: 1, + resetAndRefetch: resetAndRefetchMock, + }; + + render( + + + + + + + , + ); + + await wait(); + userEvent.click(screen.getByTestId('acceptMembershipRequestBtn1')); + }); + + it('Reject MembershipRequest Button works properly', async () => { const props: { request: InterfaceRequestsListItem; index: number; @@ -144,4 +177,36 @@ describe('Testing User Table Item', () => { await wait(); userEvent.click(screen.getByTestId('rejectMembershipRequestBtn123')); }); + + it('Reject MembershipRequest handles error', async () => { + const props: { + request: InterfaceRequestsListItem; + index: number; + resetAndRefetch: () => void; + } = { + request: { + _id: '1', + user: { + firstName: 'John', + lastName: 'Doe', + email: 'john@example.com', + }, + }, + index: 1, + resetAndRefetch: resetAndRefetchMock, + }; + + render( + + + + + + + , + ); + + await wait(); + userEvent.click(screen.getByTestId('rejectMembershipRequestBtn1')); + }); }); diff --git a/src/components/RequestsTableItem/RequestsTableItemMocks.ts b/src/components/RequestsTableItem/RequestsTableItemMocks.ts index 22ea245d3a..22bd61d0ac 100644 --- a/src/components/RequestsTableItem/RequestsTableItemMocks.ts +++ b/src/components/RequestsTableItem/RequestsTableItemMocks.ts @@ -8,13 +8,13 @@ export const MOCKS = [ request: { query: ACCEPT_ORGANIZATION_REQUEST_MUTATION, variables: { - id: '1', + id: '123', }, }, result: { data: { acceptMembershipRequest: { - _id: '1', + _id: '123', }, }, }, @@ -23,13 +23,13 @@ export const MOCKS = [ request: { query: REJECT_ORGANIZATION_REQUEST_MUTATION, variables: { - id: '1', + id: '123', }, }, result: { data: { rejectMembershipRequest: { - _id: '1', + _id: '123', }, }, },