Skip to content

Commit

Permalink
Improving Code Coverage in src/components/UserPortal/OrganizationCard…
Browse files Browse the repository at this point in the history
…/OrganizationCard.tsx (#3370)

* test changes

* removing comment
  • Loading branch information
MayankJha014 authored Jan 21, 2025
1 parent f7d28d5 commit a32533b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -49,6 +55,7 @@ vi.mock('react-toastify', () => ({
}));

const MOCKS = [
// Successful membership request
{
request: {
query: SEND_MEMBERSHIP_REQUEST,
Expand All @@ -71,6 +78,7 @@ const MOCKS = [
},
},
},
// Successful public organization join
{
request: {
query: JOIN_PUBLIC_ORGANIZATION,
Expand All @@ -86,6 +94,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,
Expand All @@ -109,6 +138,7 @@ const MOCKS = [
},
},
},
// Organization connection data
{
request: {
query: USER_ORGANIZATION_CONNECTION,
Expand Down Expand Up @@ -349,6 +379,64 @@ describe('Testing OrganizationCard Component [User Portal]', () => {
expect(toast.success).toHaveBeenCalledTimes(2);
});

it('Displays error when user is already a member', async () => {
const errorProps = { ...props, id: '3' }; // Using organizationId '3'

render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<OrganizationCard {...errorProps} />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>,
);

// 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
});
});

it('Displays generic error when a different error occurs', async () => {
const errorProps = { ...props, id: '4' }; // Using organizationId '4'

render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<OrganizationCard {...errorProps} />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>,
);

// 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
});
});

it('withdraw membership request', async () => {
const cardProps = {
...props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a32533b

Please sign in to comment.