11import { MockUseState } from 'testUtils' ;
2+ import { configuration } from 'config' ;
23
3- import { useInitializeLearnerHome } from 'data/hooks' ;
4+ import { useInitializeLearnerHome , useUnenrollFromCourse } from 'data/hooks' ;
5+ import { useCourseData } from 'hooks' ;
46
57import * as reasons from './reasons' ;
68import * as hooks from '.' ;
@@ -11,12 +13,26 @@ jest.mock('./reasons', () => ({
1113
1214jest . mock ( 'data/hooks' , ( ) => ( {
1315 useInitializeLearnerHome : jest . fn ( ) ,
16+ useUnenrollFromCourse : jest . fn ( ) ,
17+ } ) ) ;
18+
19+ jest . mock ( 'hooks' , ( ) => ( {
20+ useCourseData : jest . fn ( ) ,
21+ } ) ) ;
22+
23+ jest . mock ( 'config' , ( ) => ( {
24+ configuration : {
25+ SHOW_UNENROLL_SURVEY : true ,
26+ } ,
1427} ) ) ;
1528
1629const state = new MockUseState ( hooks ) ;
1730const testValue = 'test-value' ;
1831const mockRefreshList = jest . fn ( ) ;
32+ const unenrollFromCourse = jest . fn ( ) ;
1933useInitializeLearnerHome . mockReturnValue ( { refetch : mockRefreshList } ) ;
34+ useUnenrollFromCourse . mockReturnValue ( { mutate : unenrollFromCourse } ) ;
35+ useCourseData . mockReturnValue ( { courseRun : { courseId : 'test-course-id' } } ) ;
2036let out ;
2137
2238const mockReason = {
@@ -78,22 +94,66 @@ describe('UnenrollConfirmModal hooks', () => {
7894 expect ( mockRefreshList ) . toHaveBeenCalled ( ) ;
7995 } ) ;
8096 } ) ;
81- describe ( 'modalState' , ( ) => {
82- it ( 'returns modalStates.finished if confirmed and submitted' , ( ) => {
97+ } ) ;
98+
99+ describe ( 'SHOW_UNENROLL_SURVEY configuration tests' , ( ) => {
100+ beforeEach ( ( ) => {
101+ state . mock ( ) ;
102+ jest . clearAllMocks ( ) ;
103+ } ) ;
104+ afterEach ( ( ) => {
105+ state . restore ( ) ;
106+ } ) ;
107+
108+ describe ( 'when SHOW_UNENROLL_SURVEY is true (default)' , ( ) => {
109+ beforeEach ( ( ) => {
110+ configuration . SHOW_UNENROLL_SURVEY = true ;
111+ } ) ;
112+
113+ test ( 'confirm does not call unenrollFromCourse immediately' , ( ) => {
114+ out = createUseUnenrollData ( ) ;
115+ out . confirm ( ) ;
116+ expect ( unenrollFromCourse ) . not . toHaveBeenCalled ( ) ;
117+ expect ( state . setState . confirmed ) . toHaveBeenCalledWith ( true ) ;
118+ } ) ;
119+
120+ test ( 'modalState returns reason when confirmed but not submitted' , ( ) => {
121+ state . mockVal ( state . keys . confirmed , true ) ;
122+ reasons . useUnenrollReasons . mockReturnValueOnce ( { ...mockReason , isSubmitted : false } ) ;
123+ out = createUseUnenrollData ( ) ;
124+ expect ( out . modalState ) . toEqual ( hooks . modalStates . reason ) ;
125+ } ) ;
126+
127+ test ( 'modalState returns finished when confirmed and submitted' , ( ) => {
83128 state . mockVal ( state . keys . confirmed , true ) ;
84129 reasons . useUnenrollReasons . mockReturnValueOnce ( { ...mockReason , isSubmitted : true } ) ;
85130 out = createUseUnenrollData ( ) ;
86131 expect ( out . modalState ) . toEqual ( hooks . modalStates . finished ) ;
87132 } ) ;
88- it ( 'returns modalStates.reason if confirmed and not submitted' , ( ) => {
89- state . mockVal ( state . keys . confirmed , true ) ;
133+ } ) ;
134+
135+ describe ( 'when SHOW_UNENROLL_SURVEY is false' , ( ) => {
136+ beforeEach ( ( ) => {
137+ configuration . SHOW_UNENROLL_SURVEY = false ;
138+ } ) ;
139+
140+ afterEach ( ( ) => {
141+ // Reset to default
142+ configuration . SHOW_UNENROLL_SURVEY = true ;
143+ } ) ;
144+
145+ test ( 'confirm calls unenrollFromCourse immediately' , ( ) => {
90146 out = createUseUnenrollData ( ) ;
91- expect ( out . modalState ) . toEqual ( hooks . modalStates . reason ) ;
147+ out . confirm ( ) ;
148+ expect ( unenrollFromCourse ) . toHaveBeenCalled ( ) ;
149+ expect ( state . setState . confirmed ) . toHaveBeenCalledWith ( true ) ;
92150 } ) ;
93- it ( 'returns modalStates.confirm if not confirmed' , ( ) => {
94- state . mockVal ( state . keys . confirmed , false ) ;
151+
152+ test ( 'modalState returns finished when confirmed regardless of submission status' , ( ) => {
153+ state . mockVal ( state . keys . confirmed , true ) ;
154+ reasons . useUnenrollReasons . mockReturnValueOnce ( { ...mockReason , isSubmitted : false } ) ;
95155 out = createUseUnenrollData ( ) ;
96- expect ( out . modalState ) . toEqual ( hooks . modalStates . confirm ) ;
156+ expect ( out . modalState ) . toEqual ( hooks . modalStates . finished ) ;
97157 } ) ;
98158 } ) ;
99159 } ) ;
0 commit comments