@@ -5,12 +5,17 @@ import { useCohorts, useCohortStatus, useToggleCohorts } from './data/apiHook';
55import { renderWithAlertAndIntl } from '@src/testUtils' ;
66import messages from './messages' ;
77import { CohortProvider } from './components/CohortContext' ;
8+ import * as AlertProvider from '@src/providers/AlertProvider' ;
89
910jest . mock ( 'react-router-dom' , ( ) => ( {
1011 ...jest . requireActual ( 'react-router-dom' ) ,
1112 useParams : ( ) => ( { courseId : 'course-v1:edX+Test+2024' } ) ,
1213} ) ) ;
1314
15+ jest . mock ( 'axios' , ( ) => ( {
16+ isAxiosError : ( error : any ) => error ?. isAxiosError === true ,
17+ } ) ) ;
18+
1419jest . mock ( './data/apiHook' , ( ) => ( {
1520 useCohorts : jest . fn ( ) ,
1621 useCohortStatus : jest . fn ( ) ,
@@ -82,4 +87,111 @@ describe('CohortsPage', () => {
8287 await user . click ( screen . getByRole ( 'button' , { name : messages . disableLabel . defaultMessage } ) ) ;
8388 expect ( disableMock ) . toHaveBeenCalled ( ) ;
8489 } ) ;
90+
91+ it ( 'shows error modal when enable cohorts fails' , async ( ) => {
92+ const showModalMock = jest . fn ( ) ;
93+ jest . spyOn ( AlertProvider , 'useAlert' ) . mockReturnValue ( {
94+ alerts : [ ] ,
95+ addAlert : jest . fn ( ) ,
96+ removeAlert : jest . fn ( ) ,
97+ clearAlerts : jest . fn ( ) ,
98+ showToast : jest . fn ( ) ,
99+ showModal : showModalMock ,
100+ showInlineAlert : jest . fn ( ) ,
101+ dismissInlineAlert : jest . fn ( ) ,
102+ inlineAlerts : [ ] ,
103+ } ) ;
104+
105+ const enableMock = jest . fn ( ) ;
106+ ( useCohorts as jest . Mock ) . mockReturnValue ( { data : [ ] } ) ;
107+ ( useCohortStatus as jest . Mock ) . mockReturnValue ( { data : { isCohorted : false } } ) ;
108+ ( useToggleCohorts as jest . Mock ) . mockReturnValue ( { mutate : enableMock } ) ;
109+
110+ renderWithCohortsProvider ( ) ;
111+ const user = userEvent . setup ( ) ;
112+ await user . click ( screen . getByRole ( 'button' , { name : messages . enableCohorts . defaultMessage } ) ) ;
113+
114+ // Simulate error callback
115+ const callArgs = enableMock . mock . calls [ 0 ] [ 1 ] ;
116+ callArgs . onError ( new Error ( 'Enable failed' ) ) ;
117+
118+ expect ( showModalMock ) . toHaveBeenCalledWith ( {
119+ confirmText : messages . closeButton . defaultMessage ,
120+ message : messages . enableCohortError . defaultMessage ,
121+ variant : 'danger' ,
122+ } ) ;
123+ } ) ;
124+
125+ it ( 'shows error modal with API message when enable cohorts fails with axios error' , async ( ) => {
126+ const showModalMock = jest . fn ( ) ;
127+ jest . spyOn ( AlertProvider , 'useAlert' ) . mockReturnValue ( {
128+ alerts : [ ] ,
129+ addAlert : jest . fn ( ) ,
130+ removeAlert : jest . fn ( ) ,
131+ clearAlerts : jest . fn ( ) ,
132+ showToast : jest . fn ( ) ,
133+ showModal : showModalMock ,
134+ showInlineAlert : jest . fn ( ) ,
135+ dismissInlineAlert : jest . fn ( ) ,
136+ inlineAlerts : [ ] ,
137+ } ) ;
138+
139+ const enableMock = jest . fn ( ) ;
140+ ( useCohorts as jest . Mock ) . mockReturnValue ( { data : [ ] } ) ;
141+ ( useCohortStatus as jest . Mock ) . mockReturnValue ( { data : { isCohorted : false } } ) ;
142+ ( useToggleCohorts as jest . Mock ) . mockReturnValue ( { mutate : enableMock } ) ;
143+
144+ renderWithCohortsProvider ( ) ;
145+ const user = userEvent . setup ( ) ;
146+ await user . click ( screen . getByRole ( 'button' , { name : messages . enableCohorts . defaultMessage } ) ) ;
147+
148+ // Simulate axios error with developer_message
149+ const axiosError = {
150+ isAxiosError : true ,
151+ response : { data : { developer_message : 'API specific error' } } ,
152+ } ;
153+ const callArgs = enableMock . mock . calls [ 0 ] [ 1 ] ;
154+ callArgs . onError ( axiosError ) ;
155+
156+ expect ( showModalMock ) . toHaveBeenCalledWith ( {
157+ confirmText : messages . closeButton . defaultMessage ,
158+ message : 'API specific error' ,
159+ variant : 'danger' ,
160+ } ) ;
161+ } ) ;
162+
163+ it ( 'shows error modal when disable cohorts fails' , async ( ) => {
164+ const showModalMock = jest . fn ( ) ;
165+ jest . spyOn ( AlertProvider , 'useAlert' ) . mockReturnValue ( {
166+ alerts : [ ] ,
167+ addAlert : jest . fn ( ) ,
168+ removeAlert : jest . fn ( ) ,
169+ clearAlerts : jest . fn ( ) ,
170+ showToast : jest . fn ( ) ,
171+ showModal : showModalMock ,
172+ showInlineAlert : jest . fn ( ) ,
173+ dismissInlineAlert : jest . fn ( ) ,
174+ inlineAlerts : [ ] ,
175+ } ) ;
176+
177+ const disableMock = jest . fn ( ) ;
178+ ( useCohorts as jest . Mock ) . mockReturnValue ( { data : [ { id : '1' , name : 'Cohort 1' } ] } ) ;
179+ ( useCohortStatus as jest . Mock ) . mockReturnValue ( { data : { isCohorted : true } } ) ;
180+ ( useToggleCohorts as jest . Mock ) . mockReturnValue ( { mutate : disableMock } ) ;
181+
182+ renderWithCohortsProvider ( ) ;
183+ const user = userEvent . setup ( ) ;
184+ await user . click ( screen . getByRole ( 'button' , { name : messages . disableCohorts . defaultMessage } ) ) ;
185+ await user . click ( screen . getByRole ( 'button' , { name : messages . disableLabel . defaultMessage } ) ) ;
186+
187+ // Simulate error callback
188+ const callArgs = disableMock . mock . calls [ 0 ] [ 1 ] ;
189+ callArgs . onError ( new Error ( 'Disable failed' ) ) ;
190+
191+ expect ( showModalMock ) . toHaveBeenCalledWith ( {
192+ confirmText : messages . closeButton . defaultMessage ,
193+ message : messages . disableCohortError . defaultMessage ,
194+ variant : 'danger' ,
195+ } ) ;
196+ } ) ;
85197} ) ;
0 commit comments