Skip to content

Commit

Permalink
Refactor src/components/RequestsTableItem/RequestsTableItem.test.tsx …
Browse files Browse the repository at this point in the history
…from Jest to Vitest (#2873)
  • Loading branch information
rahulch07 authored Dec 25, 2024
1 parent b9693d5 commit c42135b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -21,13 +22,13 @@ async function wait(ms = 100): Promise<void> {
});
});
}
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(),
},
}));

Expand All @@ -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;
Expand Down Expand Up @@ -81,7 +82,7 @@ describe('Testing User Table Item', () => {
expect(screen.getByText(/[email protected]/i)).toBeInTheDocument();
});

test('Accept MembershipRequest Button works properly', async () => {
it('Accept MembershipRequest Button works properly', async () => {
const props: {
request: InterfaceRequestsListItem;
index: number;
Expand Down Expand Up @@ -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: '[email protected]',
},
},
index: 1,
resetAndRefetch: resetAndRefetchMock,
};

render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<RequestsTableItem {...props} />
</I18nextProvider>
</BrowserRouter>
</MockedProvider>,
);

await wait();
userEvent.click(screen.getByTestId('acceptMembershipRequestBtn1'));
});

it('Reject MembershipRequest Button works properly', async () => {
const props: {
request: InterfaceRequestsListItem;
index: number;
Expand Down Expand Up @@ -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: '[email protected]',
},
},
index: 1,
resetAndRefetch: resetAndRefetchMock,
};

render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<RequestsTableItem {...props} />
</I18nextProvider>
</BrowserRouter>
</MockedProvider>,
);

await wait();
userEvent.click(screen.getByTestId('rejectMembershipRequestBtn1'));
});
});
8 changes: 4 additions & 4 deletions src/components/RequestsTableItem/RequestsTableItemMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
},
},
Expand All @@ -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',
},
},
},
Expand Down

0 comments on commit c42135b

Please sign in to comment.