11import { MockUseState } from '@src/testUtils' ;
2+ import { useAppConfig } from '@openedx/frontend-base' ;
3+
4+ import { useCourseData } from '@src/hooks' ;
5+ import { useUnenrollFromCourse } from '@src/data/hooks' ;
26
37import * as reasons from './reasons' ;
48import * as hooks from '.' ;
@@ -7,8 +11,23 @@ jest.mock('./reasons', () => ({
711 useUnenrollReasons : jest . fn ( ) ,
812} ) ) ;
913
14+ jest . mock ( '@src/data/hooks' , ( ) => ( {
15+ useUnenrollFromCourse : jest . fn ( ) ,
16+ } ) ) ;
17+
18+ jest . mock ( '@src/hooks' , ( ) => ( {
19+ useCourseData : jest . fn ( ) ,
20+ } ) ) ;
21+
22+ jest . mock ( '@openedx/frontend-base' , ( ) => ( {
23+ useAppConfig : jest . fn ( ) ,
24+ } ) ) ;
25+
1026const state = new MockUseState ( hooks ) ;
1127const testValue = 'test-value' ;
28+ const unenrollFromCourse = jest . fn ( ) ;
29+ useUnenrollFromCourse . mockReturnValue ( { mutate : unenrollFromCourse } ) ;
30+ useCourseData . mockReturnValue ( { courseRun : { courseId : 'test-course-id' } } ) ;
1231let out ;
1332
1433const mockReason = {
@@ -22,6 +41,7 @@ const useUnenrollReasons = jest.fn(() => mockReason);
2241describe ( 'UnenrollConfirmModal hooks' , ( ) => {
2342 beforeEach ( ( ) => {
2443 reasons . useUnenrollReasons . mockImplementation ( useUnenrollReasons ) ;
44+ useAppConfig . mockReturnValue ( { SHOW_UNENROLL_SURVEY : true } ) ;
2545 } ) ;
2646 const closeModal = jest . fn ( ) ;
2747 const cardId = 'test-card-id' ;
@@ -66,22 +86,67 @@ describe('UnenrollConfirmModal hooks', () => {
6686 expect ( mockReason . handleClear ) . toHaveBeenCalled ( ) ;
6787 } ) ;
6888 } ) ;
69- describe ( 'modalState' , ( ) => {
70- it ( 'returns modalStates.finished if confirmed and submitted' , ( ) => {
89+ } ) ;
90+
91+ describe ( 'SHOW_UNENROLL_SURVEY configuration tests' , ( ) => {
92+ beforeEach ( ( ) => {
93+ state . mock ( ) ;
94+ jest . clearAllMocks ( ) ;
95+ } ) ;
96+ afterEach ( ( ) => {
97+ state . restore ( ) ;
98+ } ) ;
99+
100+ describe ( 'when SHOW_UNENROLL_SURVEY is true (default)' , ( ) => {
101+ beforeEach ( ( ) => {
102+ useAppConfig . mockReturnValue ( { SHOW_UNENROLL_SURVEY : true } ) ;
103+ useCourseData . mockReturnValue ( { courseRun : { courseId : 'test-course-id' } } ) ;
104+ useUnenrollFromCourse . mockReturnValue ( { mutate : unenrollFromCourse } ) ;
105+ reasons . useUnenrollReasons . mockImplementation ( useUnenrollReasons ) ;
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' , ( ) => {
71123 state . mockVal ( state . keys . confirmed , true ) ;
72124 reasons . useUnenrollReasons . mockReturnValueOnce ( { ...mockReason , isSubmitted : true } ) ;
73125 out = createUseUnenrollData ( ) ;
74126 expect ( out . modalState ) . toEqual ( hooks . modalStates . finished ) ;
75127 } ) ;
76- it ( 'returns modalStates.reason if confirmed and not submitted' , ( ) => {
77- state . mockVal ( state . keys . confirmed , true ) ;
128+ } ) ;
129+
130+ describe ( 'when SHOW_UNENROLL_SURVEY is false' , ( ) => {
131+ beforeEach ( ( ) => {
132+ useAppConfig . mockReturnValue ( { SHOW_UNENROLL_SURVEY : false } ) ;
133+ useCourseData . mockReturnValue ( { courseRun : { courseId : 'test-course-id' } } ) ;
134+ useUnenrollFromCourse . mockReturnValue ( { mutate : unenrollFromCourse } ) ;
135+ reasons . useUnenrollReasons . mockImplementation ( useUnenrollReasons ) ;
136+ } ) ;
137+
138+ test ( 'confirm calls unenrollFromCourse immediately' , ( ) => {
78139 out = createUseUnenrollData ( ) ;
79- expect ( out . modalState ) . toEqual ( hooks . modalStates . reason ) ;
140+ out . confirm ( ) ;
141+ expect ( unenrollFromCourse ) . toHaveBeenCalled ( ) ;
142+ expect ( state . setState . confirmed ) . toHaveBeenCalledWith ( true ) ;
80143 } ) ;
81- it ( 'returns modalStates.confirm if not confirmed' , ( ) => {
82- state . mockVal ( state . keys . confirmed , false ) ;
144+
145+ test ( 'modalState returns finished when confirmed regardless of submission status' , ( ) => {
146+ state . mockVal ( state . keys . confirmed , true ) ;
147+ reasons . useUnenrollReasons . mockReturnValueOnce ( { ...mockReason , isSubmitted : false } ) ;
83148 out = createUseUnenrollData ( ) ;
84- expect ( out . modalState ) . toEqual ( hooks . modalStates . confirm ) ;
149+ expect ( out . modalState ) . toEqual ( hooks . modalStates . finished ) ;
85150 } ) ;
86151 } ) ;
87152 } ) ;
0 commit comments