From ebef52e75017dcbdf28f2e77abac4c773fade9a5 Mon Sep 17 00:00:00 2001 From: MayankJha014 Date: Sat, 18 Jan 2025 02:10:04 +0530 Subject: [PATCH 1/3] removing one comment to check effect --- src/components/UserPortal/OrganizationCard/OrganizationCard.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/UserPortal/OrganizationCard/OrganizationCard.tsx b/src/components/UserPortal/OrganizationCard/OrganizationCard.tsx index e1c2c23beb..b468f84a22 100644 --- a/src/components/UserPortal/OrganizationCard/OrganizationCard.tsx +++ b/src/components/UserPortal/OrganizationCard/OrganizationCard.tsx @@ -121,7 +121,6 @@ function organizationCard(props: InterfaceOrganizationCardProps): JSX.Element { } refetch(); } catch (error: unknown) { - /* istanbul ignore next */ if (error instanceof Error) { if (error.message === 'User is already a member') { toast.error(t('AlreadyJoined') as string); From 0dd35cc200a4ed7562fa81ab87ec9b2a6facd18b Mon Sep 17 00:00:00 2001 From: MayankJha014 Date: Tue, 21 Jan 2025 02:13:47 +0530 Subject: [PATCH 2/3] inc code coverage by handling error --- .../OrganizationCard.test.tsx | 90 ++++++++++++++++++- .../OrganizationCard/OrganizationCard.tsx | 1 + 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/components/UserPortal/OrganizationCard/OrganizationCard.test.tsx b/src/components/UserPortal/OrganizationCard/OrganizationCard.test.tsx index 77516ddc7a..56c60fa591 100644 --- a/src/components/UserPortal/OrganizationCard/OrganizationCard.test.tsx +++ b/src/components/UserPortal/OrganizationCard/OrganizationCard.test.tsx @@ -1,5 +1,11 @@ import React from 'react'; -import { act, fireEvent, render, screen } from '@testing-library/react'; +import { + act, + fireEvent, + render, + screen, + waitFor, +} from '@testing-library/react'; import { MockedProvider } from '@apollo/react-testing'; import { I18nextProvider } from 'react-i18next'; @@ -31,6 +37,7 @@ jest.mock('react-toastify', () => ({ })); const MOCKS = [ + // Successful membership request { request: { query: SEND_MEMBERSHIP_REQUEST, @@ -53,6 +60,7 @@ const MOCKS = [ }, }, }, + // Successful public organization join { request: { query: JOIN_PUBLIC_ORGANIZATION, @@ -68,6 +76,27 @@ const MOCKS = [ }, }, }, + // Error: User is already a member + { + request: { + query: SEND_MEMBERSHIP_REQUEST, + variables: { + organizationId: '3', // A different org ID to test error handling + }, + }, + error: new Error('User is already a member'), + }, + // Error: Generic error occurred + { + request: { + query: SEND_MEMBERSHIP_REQUEST, + variables: { + organizationId: '4', // Another org ID to test generic errors + }, + }, + error: new Error('Some unexpected error occurred'), + }, + // User joined organizations { request: { query: USER_JOINED_ORGANIZATIONS, @@ -91,6 +120,7 @@ const MOCKS = [ }, }, }, + // Organization connection data { request: { query: USER_ORGANIZATION_CONNECTION, @@ -316,6 +346,64 @@ describe('Testing OrganizationCard Component [User Portal]', () => { expect(toast.success).toHaveBeenCalledTimes(2); }); + test('Displays error when user is already a member', async () => { + const errorProps = { ...props, id: '3' }; // Using organizationId '3' + + render( + + + + + + + + + , + ); + + // Wait for component to render + await waitFor(() => + expect(screen.getByTestId('joinBtn')).toBeInTheDocument(), + ); + + // Simulate clicking the join button + fireEvent.click(screen.getByTestId('joinBtn')); + + // Wait for the error handling + await waitFor(() => { + expect(toast.error).toHaveBeenCalledWith('AlreadyJoined'); // Verify toast error + }); + }); + + test('Displays generic error when a different error occurs', async () => { + const errorProps = { ...props, id: '4' }; // Using organizationId '4' + + render( + + + + + + + + + , + ); + + // Wait for component to render + await waitFor(() => + expect(screen.getByTestId('joinBtn')).toBeInTheDocument(), + ); + + // Simulate clicking the join button + fireEvent.click(screen.getByTestId('joinBtn')); + + // Wait for the error handling + await waitFor(() => { + expect(toast.error).toHaveBeenCalledWith('errorOccured'); // Verify generic error toast + }); + }); + test('withdraw membership request', async () => { const cardProps = { ...props, diff --git a/src/components/UserPortal/OrganizationCard/OrganizationCard.tsx b/src/components/UserPortal/OrganizationCard/OrganizationCard.tsx index b468f84a22..e1c2c23beb 100644 --- a/src/components/UserPortal/OrganizationCard/OrganizationCard.tsx +++ b/src/components/UserPortal/OrganizationCard/OrganizationCard.tsx @@ -121,6 +121,7 @@ function organizationCard(props: InterfaceOrganizationCardProps): JSX.Element { } refetch(); } catch (error: unknown) { + /* istanbul ignore next */ if (error instanceof Error) { if (error.message === 'User is already a member') { toast.error(t('AlreadyJoined') as string); From b126ca5e926dd51fce36ec76281787fc4908fd8c Mon Sep 17 00:00:00 2001 From: MayankJha014 Date: Tue, 21 Jan 2025 02:38:26 +0530 Subject: [PATCH 3/3] jest to vitest --- .../UserPortal/OrganizationCard/OrganizationCard.spec.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/UserPortal/OrganizationCard/OrganizationCard.spec.tsx b/src/components/UserPortal/OrganizationCard/OrganizationCard.spec.tsx index 4c0e67fe02..ca68fbae26 100644 --- a/src/components/UserPortal/OrganizationCard/OrganizationCard.spec.tsx +++ b/src/components/UserPortal/OrganizationCard/OrganizationCard.spec.tsx @@ -379,7 +379,7 @@ describe('Testing OrganizationCard Component [User Portal]', () => { expect(toast.success).toHaveBeenCalledTimes(2); }); - test('Displays error when user is already a member', async () => { + it('Displays error when user is already a member', async () => { const errorProps = { ...props, id: '3' }; // Using organizationId '3' render( @@ -408,7 +408,7 @@ describe('Testing OrganizationCard Component [User Portal]', () => { }); }); - test('Displays generic error when a different error occurs', async () => { + it('Displays generic error when a different error occurs', async () => { const errorProps = { ...props, id: '4' }; // Using organizationId '4' render(