From b8245d66316b5a244bab7a5d9b63fb9c1609355d Mon Sep 17 00:00:00 2001 From: Leangseu Kim Date: Wed, 2 Nov 2022 14:13:41 -0400 Subject: [PATCH] chore: make sure to load initialize after change session --- src/data/redux/thunkActions/app.js | 21 +++++++++---- src/data/redux/thunkActions/app.test.js | 41 +++++++++++++++---------- 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/data/redux/thunkActions/app.js b/src/data/redux/thunkActions/app.js index edc854d..b2e127b 100644 --- a/src/data/redux/thunkActions/app.js +++ b/src/data/redux/thunkActions/app.js @@ -38,8 +38,11 @@ export const newEntitlementEnrollment = (cardId, selection) => (dispatch, getSta fromCourseRun: null, toCourseRun: selection, }); - dispatch(requests.newEntitlementEnrollment({ uuid, courseId: selection })); - dispatch(initialize()); + dispatch(requests.newEntitlementEnrollment({ + uuid, + courseId: selection, + onSuccess: () => dispatch(module.initialize()), + })); }; export const switchEntitlementEnrollment = (cardId, selection) => (dispatch, getState) => { @@ -49,8 +52,11 @@ export const switchEntitlementEnrollment = (cardId, selection) => (dispatch, get fromCourseRun: courseId, toCourseRun: selection, }); - dispatch(requests.switchEntitlementEnrollment({ uuid, courseId: selection })); - dispatch(initialize()); + dispatch(requests.switchEntitlementEnrollment({ + uuid, + courseId: selection, + onSuccess: () => dispatch(module.initialize()), + })); }; export const leaveEntitlementSession = (cardId) => (dispatch, getState) => { @@ -60,8 +66,11 @@ export const leaveEntitlementSession = (cardId) => (dispatch, getState) => { leaveCourseRun: courseId, isRefundable, }); - dispatch(requests.leaveEntitlementSession({ uuid, isRefundable })); - dispatch(initialize()); + dispatch(requests.leaveEntitlementSession({ + uuid, + isRefundable, + onSuccess: () => dispatch(module.initialize()), + })); }; export const unenrollFromCourse = (cardId, reason) => (dispatch, getState) => { diff --git a/src/data/redux/thunkActions/app.test.js b/src/data/redux/thunkActions/app.test.js index 768ad99..6bfd414 100644 --- a/src/data/redux/thunkActions/app.test.js +++ b/src/data/redux/thunkActions/app.test.js @@ -9,13 +9,6 @@ import * as module from './app'; jest.mock('data/services/segment/utils', () => ({ handleEvent: jest.fn(), })); -// jest.mock('data/services/segment/constants', () => ({ -// eventNames: { -// sessionChange: jest.fn(args => ({ sessionChange: args })), -// entitlementUnenroll: 'entitlement-unenroll', -// unenrollReason: 'unenroll-reason', -// }, -// })); jest.mock('data/services/lms/utils', () => ({ post: jest.fn(), })); @@ -121,10 +114,14 @@ describe('app thunk actions', () => { { fromCourseRun: null, toCourseRun: selection }, ); }); - it('dispatches newEntitlementEnrollment request action', () => { - expect(dispatch).toHaveBeenCalledWith( - requests.newEntitlementEnrollment({ uuid, courseId: selection }), - ); + it('dispatches newEntitlementEnrollment request then re-init on success', () => { + const request = dispatch.mock.calls[0][0]; + expect(request.newEntitlementEnrollment.uuid).toEqual(uuid); + expect(request.newEntitlementEnrollment.courseId).toEqual(selection); + expect(request.newEntitlementEnrollment.onSuccess).toBeDefined(); + expect(initializeSpy).not.toHaveBeenCalled(); + request.newEntitlementEnrollment.onSuccess(); + expect(initializeSpy).toHaveBeenCalled(); }); }); describe('switchEntitlementEnrollmnent', () => { @@ -139,10 +136,14 @@ describe('app thunk actions', () => { { fromCourseRun: courseId, toCourseRun: selection }, ); }); - it('dispatches switchEntitlementEnrollment request action', () => { - expect(dispatch).toHaveBeenCalledWith( - requests.switchEntitlementEnrollment({ uuid, courseId: selection }), - ); + it('dispatches switchEntitlementEnrollment request then re-init on success', () => { + const request = dispatch.mock.calls[0][0]; + expect(request.switchEntitlementEnrollment.uuid).toEqual(uuid); + expect(request.switchEntitlementEnrollment.courseId).toEqual(selection); + expect(request.switchEntitlementEnrollment.onSuccess).toBeDefined(); + expect(initializeSpy).not.toHaveBeenCalled(); + request.switchEntitlementEnrollment.onSuccess(); + expect(initializeSpy).toHaveBeenCalled(); }); }); describe('leaveEntitlementSession', () => { @@ -157,8 +158,14 @@ describe('app thunk actions', () => { { leaveCourseRun: courseId, isRefundable }, ); }); - it('dispatches leaveEntitlementEnrollment request action', () => { - expect(dispatch).toHaveBeenCalledWith(requests.leaveEntitlementSession({ uuid, isRefundable })); + it('dispatches leaveEntitlementEnrollment request then re-init on success', () => { + const request = dispatch.mock.calls[0][0]; + expect(request.leaveEntitlementSession.uuid).toEqual(uuid); + expect(request.leaveEntitlementSession.isRefundable).toEqual(isRefundable); + expect(request.leaveEntitlementSession.onSuccess).toBeDefined(); + expect(initializeSpy).not.toHaveBeenCalled(); + request.leaveEntitlementSession.onSuccess(); + expect(initializeSpy).toHaveBeenCalled(); }); }); describe('unenrollFromCourse', () => {