Skip to content

Commit a32533b

Browse files
authored
Improving Code Coverage in src/components/UserPortal/OrganizationCard/OrganizationCard.tsx (#3370)
* test changes * removing comment
1 parent f7d28d5 commit a32533b

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

src/components/UserPortal/OrganizationCard/OrganizationCard.spec.tsx

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import React from 'react';
2-
import { act, fireEvent, render, screen } from '@testing-library/react';
2+
import {
3+
act,
4+
fireEvent,
5+
render,
6+
screen,
7+
waitFor,
8+
} from '@testing-library/react';
39

410
import { MockedProvider } from '@apollo/react-testing';
511
import { I18nextProvider } from 'react-i18next';
@@ -49,6 +55,7 @@ vi.mock('react-toastify', () => ({
4955
}));
5056

5157
const MOCKS = [
58+
// Successful membership request
5259
{
5360
request: {
5461
query: SEND_MEMBERSHIP_REQUEST,
@@ -71,6 +78,7 @@ const MOCKS = [
7178
},
7279
},
7380
},
81+
// Successful public organization join
7482
{
7583
request: {
7684
query: JOIN_PUBLIC_ORGANIZATION,
@@ -86,6 +94,27 @@ const MOCKS = [
8694
},
8795
},
8896
},
97+
// Error: User is already a member
98+
{
99+
request: {
100+
query: SEND_MEMBERSHIP_REQUEST,
101+
variables: {
102+
organizationId: '3', // A different org ID to test error handling
103+
},
104+
},
105+
error: new Error('User is already a member'),
106+
},
107+
// Error: Generic error occurred
108+
{
109+
request: {
110+
query: SEND_MEMBERSHIP_REQUEST,
111+
variables: {
112+
organizationId: '4', // Another org ID to test generic errors
113+
},
114+
},
115+
error: new Error('Some unexpected error occurred'),
116+
},
117+
// User joined organizations
89118
{
90119
request: {
91120
query: USER_JOINED_ORGANIZATIONS,
@@ -109,6 +138,7 @@ const MOCKS = [
109138
},
110139
},
111140
},
141+
// Organization connection data
112142
{
113143
request: {
114144
query: USER_ORGANIZATION_CONNECTION,
@@ -349,6 +379,64 @@ describe('Testing OrganizationCard Component [User Portal]', () => {
349379
expect(toast.success).toHaveBeenCalledTimes(2);
350380
});
351381

382+
it('Displays error when user is already a member', async () => {
383+
const errorProps = { ...props, id: '3' }; // Using organizationId '3'
384+
385+
render(
386+
<MockedProvider addTypename={false} link={link}>
387+
<BrowserRouter>
388+
<Provider store={store}>
389+
<I18nextProvider i18n={i18nForTest}>
390+
<OrganizationCard {...errorProps} />
391+
</I18nextProvider>
392+
</Provider>
393+
</BrowserRouter>
394+
</MockedProvider>,
395+
);
396+
397+
// Wait for component to render
398+
await waitFor(() =>
399+
expect(screen.getByTestId('joinBtn')).toBeInTheDocument(),
400+
);
401+
402+
// Simulate clicking the join button
403+
fireEvent.click(screen.getByTestId('joinBtn'));
404+
405+
// Wait for the error handling
406+
await waitFor(() => {
407+
expect(toast.error).toHaveBeenCalledWith('AlreadyJoined'); // Verify toast error
408+
});
409+
});
410+
411+
it('Displays generic error when a different error occurs', async () => {
412+
const errorProps = { ...props, id: '4' }; // Using organizationId '4'
413+
414+
render(
415+
<MockedProvider addTypename={false} link={link}>
416+
<BrowserRouter>
417+
<Provider store={store}>
418+
<I18nextProvider i18n={i18nForTest}>
419+
<OrganizationCard {...errorProps} />
420+
</I18nextProvider>
421+
</Provider>
422+
</BrowserRouter>
423+
</MockedProvider>,
424+
);
425+
426+
// Wait for component to render
427+
await waitFor(() =>
428+
expect(screen.getByTestId('joinBtn')).toBeInTheDocument(),
429+
);
430+
431+
// Simulate clicking the join button
432+
fireEvent.click(screen.getByTestId('joinBtn'));
433+
434+
// Wait for the error handling
435+
await waitFor(() => {
436+
expect(toast.error).toHaveBeenCalledWith('errorOccured'); // Verify generic error toast
437+
});
438+
});
439+
352440
it('withdraw membership request', async () => {
353441
const cardProps = {
354442
...props,

src/components/UserPortal/OrganizationCard/OrganizationCard.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ function organizationCard(props: InterfaceOrganizationCardProps): JSX.Element {
121121
}
122122
refetch();
123123
} catch (error: unknown) {
124-
/* istanbul ignore next */
125124
if (error instanceof Error) {
126125
if (error.message === 'User is already a member') {
127126
toast.error(t('AlreadyJoined') as string);

0 commit comments

Comments
 (0)