1
1
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' ;
3
9
4
10
import { MockedProvider } from '@apollo/react-testing' ;
5
11
import { I18nextProvider } from 'react-i18next' ;
@@ -49,6 +55,7 @@ vi.mock('react-toastify', () => ({
49
55
} ) ) ;
50
56
51
57
const MOCKS = [
58
+ // Successful membership request
52
59
{
53
60
request : {
54
61
query : SEND_MEMBERSHIP_REQUEST ,
@@ -71,6 +78,7 @@ const MOCKS = [
71
78
} ,
72
79
} ,
73
80
} ,
81
+ // Successful public organization join
74
82
{
75
83
request : {
76
84
query : JOIN_PUBLIC_ORGANIZATION ,
@@ -86,6 +94,27 @@ const MOCKS = [
86
94
} ,
87
95
} ,
88
96
} ,
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
89
118
{
90
119
request : {
91
120
query : USER_JOINED_ORGANIZATIONS ,
@@ -109,6 +138,7 @@ const MOCKS = [
109
138
} ,
110
139
} ,
111
140
} ,
141
+ // Organization connection data
112
142
{
113
143
request : {
114
144
query : USER_ORGANIZATION_CONNECTION ,
@@ -349,6 +379,64 @@ describe('Testing OrganizationCard Component [User Portal]', () => {
349
379
expect ( toast . success ) . toHaveBeenCalledTimes ( 2 ) ;
350
380
} ) ;
351
381
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
+
352
440
it ( 'withdraw membership request' , async ( ) => {
353
441
const cardProps = {
354
442
...props ,
0 commit comments