Skip to content

Commit 56a24ef

Browse files
test: new tests added for configurable survey
1 parent 948bb2f commit 56a24ef

File tree

2 files changed

+63
-9
lines changed

2 files changed

+63
-9
lines changed

src/containers/UnenrollConfirmModal/hooks/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import React from 'react';
33
import { StrictDict } from 'utils';
44
import { apiHooks } from 'hooks';
55

6+
import { configuration } from 'config';
67
import { useUnenrollReasons } from './reasons';
78
import * as module from '.';
8-
import { configuration } from 'config';
99

1010
export const state = StrictDict({
1111
confirmed: (val) => React.useState(val), // eslint-disable-line

src/containers/UnenrollConfirmModal/hooks/index.test.js

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { apiHooks } from 'hooks';
22
import { MockUseState } from 'testUtils';
3+
import { configuration } from 'config';
34

45
import * as reasons from './reasons';
56
import * as hooks from '.';
@@ -11,13 +12,22 @@ jest.mock('./reasons', () => ({
1112
jest.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

1725
const state = new MockUseState(hooks);
1826
const testValue = 'test-value';
1927
const initializeApp = jest.fn();
28+
const unenrollFromCourse = jest.fn();
2029
apiHooks.useInitializeApp.mockReturnValue(initializeApp);
30+
apiHooks.useUnenrollFromCourse.mockReturnValue(unenrollFromCourse);
2131
let out;
2232

2333
const 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

Comments
 (0)