11import { apiHooks } from 'hooks' ;
22import { MockUseState } from 'testUtils' ;
3+ import { configuration } from 'config' ;
34
45import * as reasons from './reasons' ;
56import * as hooks from '.' ;
@@ -11,13 +12,22 @@ jest.mock('./reasons', () => ({
1112jest . mock ( 'hooks' , ( ) => ( {
1213 apiHooks : {
1314 useInitializeApp : jest . fn ( ) ,
15+ useUnenrollFromCourse : jest . fn ( ) ,
16+ } ,
17+ } ) ) ;
18+
19+ jest . mock ( 'config' , ( ) => ( {
20+ configuration : {
21+ SHOW_UNENROLL_SURVEY : true ,
1422 } ,
1523} ) ) ;
1624
1725const state = new MockUseState ( hooks ) ;
1826const testValue = 'test-value' ;
1927const initializeApp = jest . fn ( ) ;
28+ const unenrollFromCourse = jest . fn ( ) ;
2029apiHooks . useInitializeApp . mockReturnValue ( initializeApp ) ;
30+ apiHooks . useUnenrollFromCourse . mockReturnValue ( unenrollFromCourse ) ;
2131let out ;
2232
2333const mockReason = {
@@ -79,22 +89,66 @@ describe('UnenrollConfirmModal hooks', () => {
7989 expect ( initializeApp ) . toHaveBeenCalled ( ) ;
8090 } ) ;
8191 } ) ;
82- describe ( 'modalState' , ( ) => {
83- it ( 'returns modalStates.finished if confirmed and submitted' , ( ) => {
92+ } ) ;
93+
94+ describe ( 'SHOW_UNENROLL_SURVEY configuration tests' , ( ) => {
95+ beforeEach ( ( ) => {
96+ state . mock ( ) ;
97+ jest . clearAllMocks ( ) ;
98+ } ) ;
99+ afterEach ( ( ) => {
100+ state . restore ( ) ;
101+ } ) ;
102+
103+ describe ( 'when SHOW_UNENROLL_SURVEY is true (default)' , ( ) => {
104+ beforeEach ( ( ) => {
105+ configuration . SHOW_UNENROLL_SURVEY = true ;
106+ } ) ;
107+
108+ test ( 'confirm does not call unenrollFromCourse immediately' , ( ) => {
109+ out = createUseUnenrollData ( ) ;
110+ out . confirm ( ) ;
111+ expect ( unenrollFromCourse ) . not . toHaveBeenCalled ( ) ;
112+ expect ( state . setState . confirmed ) . toHaveBeenCalledWith ( true ) ;
113+ } ) ;
114+
115+ test ( 'modalState returns reason when confirmed but not submitted' , ( ) => {
116+ state . mockVal ( state . keys . confirmed , true ) ;
117+ reasons . useUnenrollReasons . mockReturnValueOnce ( { ...mockReason , isSubmitted : false } ) ;
118+ out = createUseUnenrollData ( ) ;
119+ expect ( out . modalState ) . toEqual ( hooks . modalStates . reason ) ;
120+ } ) ;
121+
122+ test ( 'modalState returns finished when confirmed and submitted' , ( ) => {
84123 state . mockVal ( state . keys . confirmed , true ) ;
85124 reasons . useUnenrollReasons . mockReturnValueOnce ( { ...mockReason , isSubmitted : true } ) ;
86125 out = createUseUnenrollData ( ) ;
87126 expect ( out . modalState ) . toEqual ( hooks . modalStates . finished ) ;
88127 } ) ;
89- it ( 'returns modalStates.reason if confirmed and not submitted' , ( ) => {
90- state . mockVal ( state . keys . confirmed , true ) ;
128+ } ) ;
129+
130+ describe ( 'when SHOW_UNENROLL_SURVEY is false' , ( ) => {
131+ beforeEach ( ( ) => {
132+ configuration . SHOW_UNENROLL_SURVEY = false ;
133+ } ) ;
134+
135+ afterEach ( ( ) => {
136+ // Reset to default
137+ configuration . SHOW_UNENROLL_SURVEY = true ;
138+ } ) ;
139+
140+ test ( 'confirm calls unenrollFromCourse immediately' , ( ) => {
91141 out = createUseUnenrollData ( ) ;
92- expect ( out . modalState ) . toEqual ( hooks . modalStates . reason ) ;
142+ out . confirm ( ) ;
143+ expect ( unenrollFromCourse ) . toHaveBeenCalled ( ) ;
144+ expect ( state . setState . confirmed ) . toHaveBeenCalledWith ( true ) ;
93145 } ) ;
94- it ( 'returns modalStates.confirm if not confirmed' , ( ) => {
95- state . mockVal ( state . keys . confirmed , false ) ;
146+
147+ test ( 'modalState returns finished when confirmed regardless of submission status' , ( ) => {
148+ state . mockVal ( state . keys . confirmed , true ) ;
149+ reasons . useUnenrollReasons . mockReturnValueOnce ( { ...mockReason , isSubmitted : false } ) ;
96150 out = createUseUnenrollData ( ) ;
97- expect ( out . modalState ) . toEqual ( hooks . modalStates . confirm ) ;
151+ expect ( out . modalState ) . toEqual ( hooks . modalStates . finished ) ;
98152 } ) ;
99153 } ) ;
100154 } ) ;
0 commit comments