diff --git a/src/app/enrolled-course-details-page/enrolled-course-details-page.html b/src/app/enrolled-course-details-page/enrolled-course-details-page.html index def07a25a..903ff9dae 100644 --- a/src/app/enrolled-course-details-page/enrolled-course-details-page.html +++ b/src/app/enrolled-course-details-page/enrolled-course-details-page.html @@ -180,7 +180,7 @@ - @@ -212,7 +212,7 @@ -
@@ -260,7 +260,7 @@
-
+
diff --git a/src/app/enrolled-course-details-page/enrolled-course-details-page.spec.ts b/src/app/enrolled-course-details-page/enrolled-course-details-page.spec.ts index 20ed2a8df..d34e19bd8 100644 --- a/src/app/enrolled-course-details-page/enrolled-course-details-page.spec.ts +++ b/src/app/enrolled-course-details-page/enrolled-course-details-page.spec.ts @@ -536,12 +536,34 @@ describe('EnrolledCourseDetailsPage', () => { }); }); + describe('updateEnrolledCourseData', () => { + it('should update enroll details list by invoked updateEnrolledCourseData()', (done) => { + // arrange + mockCourseService.getEnrolledCourses = jest.fn(() => of([{ + batchId: 'sample-batch-id', + courseId: 'sample-course-id' + }])); + enrolledCourseDetailsPage.courseCardData = { + batchId: 'sample-batch-id' + }; + mockAppGlobalService.setEnrolledCourseList = jest.fn(); + // act + enrolledCourseDetailsPage.updateEnrolledCourseData(); + // assert + setTimeout(() => { + expect(mockAppGlobalService.setEnrolledCourseList).toHaveBeenCalled(); + expect(mockCourseService.getEnrolledCourses).toHaveBeenCalled(); + done(); + }, 0); + }); + }); + describe('subscribeUtilityEvents()', () => { it('#subscribeUtilityEvents should handle error condition', (done) => { // arrange mockUtilityService.getBuildConfigValue = jest.fn(() => Promise.reject(true)); mockEvents.subscribe = jest.fn(() => ({ batchId: 'SAMPLE_BATCH_ID', courseId: 'SAMPLE_COURSE_ID' })); - spyOn(enrolledCourseDetailsPage, 'updateEnrolledCourseList').and.stub(); + spyOn(enrolledCourseDetailsPage, 'updateEnrolledCourseData').and.stub(); spyOn(enrolledCourseDetailsPage, 'getBatchDetails').and.stub(); // assert enrolledCourseDetailsPage.subscribeUtilityEvents(); @@ -556,9 +578,13 @@ describe('EnrolledCourseDetailsPage', () => { it('should update courseCard data and return base url by invoked subscribeUtilityEvents()', (done) => { // arrange + mockAppGlobalService.getActiveProfileUid = jest.fn(() => Promise.resolve('some_uid')); + mockAppGlobalService.isUserLoggedIn = jest.fn(() => true); mockUtilityService.getBuildConfigValue = jest.fn(() => Promise.resolve('SAMPLE_BASE_URL')); mockEvents.subscribe = jest.fn((_, fn) => fn({ batchId: 'SAMPLE_BATCH_ID', courseId: 'SAMPLE_COURSE_ID' })); - spyOn(enrolledCourseDetailsPage, 'updateEnrolledCourseList').and.stub(); + jest.spyOn(enrolledCourseDetailsPage, 'updateEnrolledCourseData').mockImplementation(() => { + return Promise.resolve(); + }); enrolledCourseDetailsPage.course = { createdBy: 'SAMPLE_CREATOR' }; enrolledCourseDetailsPage.stickyPillsRef = { nativeElement: { @@ -578,11 +604,13 @@ describe('EnrolledCourseDetailsPage', () => { enrolledCourseDetailsPage.subscribeUtilityEvents(); // assert setTimeout(() => { + expect(mockAppGlobalService.getActiveProfileUid).toHaveBeenCalled(); + expect(mockAppGlobalService.isUserLoggedIn).toHaveBeenCalled(); expect(enrolledCourseDetailsPage.baseUrl).toBe('SAMPLE_BASE_URL'); expect(mockUtilityService.getBuildConfigValue).toHaveBeenCalled(); expect(enrolledCourseDetailsPage.course.createdBy).not.toEqual('SAMPLE_USER'); - expect(mockCourseService.getEnrolledCourses).toHaveBeenCalled(); - expect(enrolledCourseDetailsPage.updateEnrolledCourseList).toBeCalled(); + // expect(mockCourseService.getEnrolledCourses).toHaveBeenCalled(); + expect(enrolledCourseDetailsPage.updateEnrolledCourseData).toBeCalled(); expect(enrolledCourseDetailsPage.getAllBatches).toBeCalled(); expect(enrolledCourseDetailsPage.getBatchDetails).toHaveBeenCalled(); expect(enrolledCourseDetailsPage.joinTraining).toBeCalled(); @@ -592,23 +620,6 @@ describe('EnrolledCourseDetailsPage', () => { }); }); - describe('updateEnrolledCourseList', () => { - it('should update enroll details list by invoked updateEnrolledCourseList()', () => { - // arrange - const userDetails = mockAppGlobalService.getUserId = jest.fn(() => 'SAMPLE_USER_ID'); - const fetchEnrolledCourseRequest: FetchEnrolledCourseRequest = { - userId: 'SAMPLE_USER_ID' - }; - mockCourseService.getEnrolledCourses = jest.fn(() => of([{}])); - mockZone.run = jest.fn((fn) => fn()); - // act - enrolledCourseDetailsPage.updateEnrolledCourseList(mockEnrolledData.extras.state.content); - // assert - expect(mockAppGlobalService.getUserId).toHaveBeenCalled(); - expect(mockCourseService.getEnrolledCourses).toHaveBeenCalled(); - }); - }); - describe('extractApiResponse()', () => { it('should return last played content and license', () => { // assert @@ -656,7 +667,7 @@ describe('EnrolledCourseDetailsPage', () => { describe('checkCurrentUserType', () => { it('should checked current user type for cath part by invoked checkCurrentUserType()', () => { // arrange - enrolledCourseDetailsPage.guestUser = true; + enrolledCourseDetailsPage.isGuestUser = true; mockAppGlobalService.getGuestUserInfo = jest.fn(() => Promise.reject('SAMPLE_USER')); // act enrolledCourseDetailsPage.checkCurrentUserType(); @@ -717,7 +728,7 @@ describe('EnrolledCourseDetailsPage', () => { describe('rateContent()', () => { it('should not show user rating for content if guest user', (done) => { // arrange - enrolledCourseDetailsPage.guestUser = true; + enrolledCourseDetailsPage.isGuestUser = true; mockCommonUtilService.isAccessibleForNonStudentRole = jest.fn(() => true); enrolledCourseDetailsPage.profileType = ProfileType.TEACHER; mockCommonUtilService.showToast = jest.fn(); @@ -725,7 +736,7 @@ describe('EnrolledCourseDetailsPage', () => { enrolledCourseDetailsPage.rateContent(''); // assert setTimeout(() => { - expect(enrolledCourseDetailsPage.guestUser).toBeTruthy(); + expect(enrolledCourseDetailsPage.isGuestUser).toBeTruthy(); expect(mockCommonUtilService.isAccessibleForNonStudentRole).toHaveBeenCalledWith(ProfileType.TEACHER); expect(mockCommonUtilService.showToast).toHaveBeenCalledWith('SIGNIN_TO_USE_FEATURE'); done(); @@ -734,7 +745,7 @@ describe('EnrolledCourseDetailsPage', () => { it('should not show user rating for content if content is not available locally and user in not a guest user', (done) => { // arrange - enrolledCourseDetailsPage.guestUser = false; + enrolledCourseDetailsPage.isGuestUser = false; enrolledCourseDetailsPage.course = { isAvailableLocally: false }; @@ -743,7 +754,7 @@ describe('EnrolledCourseDetailsPage', () => { enrolledCourseDetailsPage.rateContent(''); // assert setTimeout(() => { - expect(enrolledCourseDetailsPage.guestUser).toBeFalsy(); + expect(enrolledCourseDetailsPage.isGuestUser).toBeFalsy(); expect(mockCommonUtilService.showToast).toHaveBeenCalledWith('TRY_BEFORE_RATING'); done(); }, 0); @@ -751,7 +762,7 @@ describe('EnrolledCourseDetailsPage', () => { it('should show user rating for content for loggedin user', (done) => { // arrange - enrolledCourseDetailsPage.guestUser = false; + enrolledCourseDetailsPage.isGuestUser = false; enrolledCourseDetailsPage.course = { isAvailableLocally: true }; @@ -763,7 +774,7 @@ describe('EnrolledCourseDetailsPage', () => { enrolledCourseDetailsPage.rateContent(''); // assert setTimeout(() => { - expect(enrolledCourseDetailsPage.guestUser).toBeFalsy(); + expect(enrolledCourseDetailsPage.isGuestUser).toBeFalsy(); expect(mockPopoverCtrl.create).not.toBeUndefined(); expect(enrolledCourseDetailsPage.userRating).toBe(2); expect(enrolledCourseDetailsPage.ratingComment).toBe('some_comment'); @@ -1457,7 +1468,7 @@ describe('EnrolledCourseDetailsPage', () => { describe('enrollIntoBatch()', () => { it('should call promptToLogin()', (done) => { // arrange - enrolledCourseDetailsPage.guestUser = true; + enrolledCourseDetailsPage.isGuestUser = true; const batch = { id: '121232312' }; @@ -1475,7 +1486,7 @@ describe('EnrolledCourseDetailsPage', () => { it('should should successfuly enroll', (done) => { // arrange - enrolledCourseDetailsPage.guestUser = false; + enrolledCourseDetailsPage.isGuestUser = false; mockLocalCourseService.prepareEnrollCourseRequest = jest.fn(() => ({ id: 'sample-id' })); const presentFn = jest.fn(() => Promise.resolve()); const dismissFn = jest.fn(() => Promise.resolve()); @@ -1512,7 +1523,7 @@ describe('EnrolledCourseDetailsPage', () => { it('should should fail', (done) => { // arrange - enrolledCourseDetailsPage.guestUser = false; + enrolledCourseDetailsPage.isGuestUser = false; mockLocalCourseService.prepareEnrollCourseRequest = jest.fn(() => ({ id: 'sample-id' })); const presentFn = jest.fn(() => Promise.resolve()); const dismissFn = jest.fn(() => Promise.resolve()); @@ -2175,24 +2186,21 @@ describe('EnrolledCourseDetailsPage', () => { // act enrolledCourseDetailsPage.subscribeSdkEvent(); // assert - expect(mockHeaderService.hideHeader).toBeCalled(); - expect(enrolledCourseDetailsPage.importContent).toBeCalled(); + expect(mockEventsBusService.events).toBeCalled(); + expect(mockZone.run).toBeCalled(); }); }); describe('ionViewWillEnter()', () => { - it('should be aguest user, ', (done) => { - mockAppGlobalService.getUserId = jest.fn(() => { - return 'sample_user_id'; - }); + it('should be a guest user, ', (done) => { + mockAppGlobalService.getActiveProfileUid = jest.fn(() => Promise.resolve('some_uid')); // act mockHeaderService.headerEventEmitted$ = { subscribe: jest.fn(() => { }) }; - enrolledCourseDetailsPage.guestUser = true; + enrolledCourseDetailsPage.isGuestUser = true; enrolledCourseDetailsPage.isAlreadyEnrolled = false; mockHeaderService.showHeaderWithBackButton = jest.fn(); - jest.spyOn(enrolledCourseDetailsPage, 'checkLoggedInOrGuestUser').mockImplementation(); jest.spyOn(enrolledCourseDetailsPage, 'checkCurrentUserType').mockImplementation(); spyOn(enrolledCourseDetailsPage, 'isCourseEnrolled').and.stub(); spyOn(enrolledCourseDetailsPage, 'subscribeSdkEvent').and.stub(); @@ -2205,9 +2213,11 @@ describe('EnrolledCourseDetailsPage', () => { jest.spyOn(enrolledCourseDetailsPage, 'handleHeaderEvents').mockImplementation(() => { return Promise.resolve(); }); + // assert enrolledCourseDetailsPage.ionViewWillEnter().then(() => { + expect(mockAppGlobalService.getActiveProfileUid).toHaveBeenCalled(); expect(enrolledCourseDetailsPage.checkCurrentUserType).toHaveBeenCalled(); - expect(enrolledCourseDetailsPage.guestUser).toEqual(true); + expect(enrolledCourseDetailsPage.isGuestUser).toEqual(true); expect(mockHeaderService.showHeaderWithBackButton).toBeCalled(); expect(enrolledCourseDetailsPage.isCourseEnrolled).toBeCalled(); expect(enrolledCourseDetailsPage.subscribeSdkEvent).toBeCalled(); @@ -2215,25 +2225,18 @@ describe('EnrolledCourseDetailsPage', () => { expect(enrolledCourseDetailsPage.handleBackButton).toBeCalled(); done(); }); - // assert - expect(enrolledCourseDetailsPage.checkLoggedInOrGuestUser).toHaveBeenCalled(); }); - it('should be a guest user, ', (done) => { + it('should not be a guest user, ', (done) => { // act - const data = { - - }; - mockAppGlobalService.setEnrolledCourseList = jest.fn(); - enrolledCourseDetailsPage.guestUser = false; + mockAppGlobalService.getActiveProfileUid = jest.fn(() => Promise.resolve('some_uid')); + mockAppGlobalService.isUserLoggedIn = jest.fn(() => true); + enrolledCourseDetailsPage.isGuestUser = false; enrolledCourseDetailsPage.isAlreadyEnrolled = false; enrolledCourseDetailsPage.courseCardData = mockCourseCardData_2; mockHeaderService.headerEventEmitted$ = { subscribe: jest.fn(() => { }) }; - jest.spyOn(enrolledCourseDetailsPage, 'checkLoggedInOrGuestUser').mockImplementation(() => { - return Promise.resolve(); - }); mockHeaderService.headerEventEmitted$ = of({ subscribe: jest.fn((fn) => fn({})) }); @@ -2247,13 +2250,17 @@ describe('EnrolledCourseDetailsPage', () => { jest.spyOn(enrolledCourseDetailsPage, 'getAllBatches').mockImplementation(() => { return Promise.resolve(); }); + jest.spyOn(enrolledCourseDetailsPage, 'updateEnrolledCourseData').mockImplementation(() => { + return Promise.resolve(); + }); mockCourseService.getEnrolledCourses = jest.fn(() => of(mockEnrolledCourses)); mockHeaderService.showHeaderWithBackButton = jest.fn(); // act enrolledCourseDetailsPage.ionViewWillEnter(); // assert setTimeout(() => { - expect(mockAppGlobalService.setEnrolledCourseList).toHaveBeenCalled(); + expect(mockAppGlobalService.getActiveProfileUid).toHaveBeenCalled(); + expect(mockAppGlobalService.isUserLoggedIn).toHaveBeenCalled(); done(); }, 0); }); diff --git a/src/app/enrolled-course-details-page/enrolled-course-details-page.ts b/src/app/enrolled-course-details-page/enrolled-course-details-page.ts index 09882f396..72306d250 100644 --- a/src/app/enrolled-course-details-page/enrolled-course-details-page.ts +++ b/src/app/enrolled-course-details-page/enrolled-course-details-page.ts @@ -177,7 +177,7 @@ export class EnrolledCourseDetailsPage implements OnInit, OnDestroy { ratingComment = ''; batchId = ''; baseUrl = ''; - guestUser = false; + isGuestUser = false; isAlreadyEnrolled = false; profileType = ''; objId; @@ -329,12 +329,12 @@ export class EnrolledCourseDetailsPage implements OnInit, OnDestroy { }); this.events.subscribe(EventTopics.ENROL_COURSE_SUCCESS, async (res) => { - this.updatedCourseCardData = await this.courseService - .getEnrolledCourses({ userId: this.appGlobalService.getUserId(), returnFreshCourses: true }) - .toPromise() - .then((cData) => { - return cData.find((element) => element.courseId === this.identifier); + await this.appGlobalService.getActiveProfileUid() + .then((uid) => { + this.userId = uid; }); + this.checkUserLoggedIn(); + await this.updateEnrolledCourseData(); this.courseCardData.batchId = res.batchId; await this.getBatchDetails(); this.segmentType = 'modules'; @@ -343,8 +343,7 @@ export class EnrolledCourseDetailsPage implements OnInit, OnDestroy { if (res && res.batchId) { this.batchId = res.batchId; if (this.identifier && res.courseId && this.identifier === res.courseId) { - this.isAlreadyEnrolled = true; - this.subscribeTrackDownloads(); + await this.isCourseEnrolled(this.identifier); this.zone.run(() => { this.getContentsSize(this.courseHeirarchy.children); if (this.loader) { @@ -356,11 +355,10 @@ export class EnrolledCourseDetailsPage implements OnInit, OnDestroy { } }); - - this.events.subscribe(EventTopics.UNENROL_COURSE_SUCCESS, () => { + this.events.subscribe(EventTopics.UNENROL_COURSE_SUCCESS, async () => { // to show 'Enroll in Course' button courseCardData.batchId should be undefined/null this.getAllBatches(); - this.updateEnrolledCourseList(this.courseCardData); // enrolled course list updated + await this.updateEnrolledCourseData(); // enrolled course list updated if (this.courseCardData) { delete this.courseCardData.batchId; } @@ -396,39 +394,33 @@ export class EnrolledCourseDetailsPage implements OnInit, OnDestroy { } - updateEnrolledCourseList(unenrolledCourse) { + private checkUserLoggedIn() { + this.isGuestUser = !this.appGlobalService.isUserLoggedIn(); + } + + async updateEnrolledCourseData() { const fetchEnrolledCourseRequest: FetchEnrolledCourseRequest = { - userId: this.appGlobalService.getUserId(), + userId: this.userId, + returnFreshCourses: true }; - this.courseService.getEnrolledCourses(fetchEnrolledCourseRequest).toPromise() - .then((enrolledCourses: any) => { - if (enrolledCourses) { - this.zone.run(() => { - // this.enrolledCourses = enrolledCourses.result.courses ? enrolledCourses.result.courses : []; - // maintain the list of courses that are enrolled, and store them in appglobal - if (enrolledCourses.length > 0) { - const courseList: Array = []; - for (const course of enrolledCourses) { - courseList.push(course); - } - this.appGlobalService.setEnrolledCourseList(courseList); - } - }); - } + console.log('updateEnrolledCourseData'); + this.updatedCourseCardData = await this.courseService.getEnrolledCourses(fetchEnrolledCourseRequest).toPromise() + .then((enrolledCourses) => { + + this.appGlobalService.setEnrolledCourseList(enrolledCourses || []); + + return enrolledCourses.find((element) => + (this.courseCardData.batchId && element.batchId === this.courseCardData.batchId) + || (!this.courseCardData.batchId && element.courseId === this.identifier)); }) - .catch(() => { + .catch(e => { + console.log(e); + return undefined; }); - } - /** - * Get the session to know if the user is logged-in or guest - * - */ - async checkLoggedInOrGuestUser() { - const session = await this.authService.getSession().toPromise(); - this.guestUser = !session; - if (session) { - this.userId = session.userToken; + if (this.updatedCourseCardData && !this.courseCardData.batch) { + this.courseCardData.batch = this.updatedCourseCardData.batch; + this.courseCardData.batchId = this.updatedCourseCardData.batchId; } } @@ -438,7 +430,7 @@ export class EnrolledCourseDetailsPage implements OnInit, OnDestroy { } checkCurrentUserType() { - if (this.guestUser) { + if (this.isGuestUser) { this.appGlobalService.getGuestUserInfo() .then((userType) => { this.profileType = userType; @@ -498,7 +490,7 @@ export class EnrolledCourseDetailsPage implements OnInit, OnDestroy { * Function to rate content */ async rateContent(event) { - if (!this.guestUser) { + if (!this.isGuestUser) { if (this.course.isAvailableLocally) { const popUp = await this.popoverCtrl.create({ component: ContentRatingAlertComponent, @@ -1322,7 +1314,11 @@ export class EnrolledCourseDetailsPage implements OnInit, OnDestroy { * Ionic life cycle hook */ async ionViewWillEnter() { - await this.checkLoggedInOrGuestUser(); + this.checkUserLoggedIn(); + await this.appGlobalService.getActiveProfileUid() + .then((uid) => { + this.userId = uid; + }); this.checkCurrentUserType(); this.todayDate = window.dayjs().format('YYYY-MM-DD'); this.identifier = this.courseCardData.contentId || this.courseCardData.identifier; @@ -1330,29 +1326,8 @@ export class EnrolledCourseDetailsPage implements OnInit, OnDestroy { this.objRollup = ContentUtil.generateRollUp(this.courseCardData.hierarchyInfo, this.identifier); this.headerService.showHeaderWithBackButton(); - if (!this.guestUser) { - this.updatedCourseCardData = await this.courseService.getEnrolledCourses({ userId: this.userId, returnFreshCourses: false }) - .toPromise() - .then((data) => { - if (data.length > 0) { - const courseList: Array = []; - for (const course of data) { - courseList.push(course); - } - this.appGlobalService.setEnrolledCourseList(courseList); - } - return data.find((element) => - (this.courseCardData.batchId && element.batchId === this.courseCardData.batchId) - || (!this.courseCardData.batchId && element.courseId === this.identifier)); - }) - .catch(e => { - console.log(e); - return null; - }); - if (this.updatedCourseCardData && !this.courseCardData.batch) { - this.courseCardData.batch = this.updatedCourseCardData.batch; - this.courseCardData.batchId = this.updatedCourseCardData.batchId; - } + if (!this.isGuestUser) { + await this.updateEnrolledCourseData(); } // check if the course is already enrolled @@ -1442,7 +1417,7 @@ export class EnrolledCourseDetailsPage implements OnInit, OnDestroy { if (enrolledCourses && enrolledCourses.length > 0) { for (const course of enrolledCourses) { if (course.courseId === identifier) { - if (!this.guestUser && this.courseCardData.batch && course.batchId + if (!this.isGuestUser && this.courseCardData.batch && course.batchId === this.courseCardData.batch.identifier) { this.isAlreadyEnrolled = true; this.subscribeTrackDownloads(); @@ -1852,7 +1827,7 @@ export class EnrolledCourseDetailsPage implements OnInit, OnDestroy { } async enrollIntoBatch(item: Batch) { - if (this.guestUser) { + if (this.isGuestUser) { this.promptToLogin(item); } else { const enrollCourseRequest = this.localCourseService.prepareEnrollCourseRequest(this.userId, item);