test: added test case for navigation bar api

This commit is contained in:
sundasnoreen12
2023-04-02 16:45:42 +05:00
parent 9b60c68b1d
commit 7f9ddda706
3 changed files with 29 additions and 18 deletions

View File

@@ -8,15 +8,17 @@ Factory.define('navigationBar')
.attr('celebrations', null, {
first_section: false, streak_discount_enabled: false, streak_length_to_celebrate: null, weekly_goal: false,
})
.attr('course_access', null, {
.option('hasCourseAccess', null, true)
.attr('course_access', ['hasCourseAccess'], (hasCourseAccess) => ({
additional_context_user_message: null,
developer_message: null,
error_code: null,
has_access: true,
has_access: hasCourseAccess,
user_fragment: null,
user_message: null,
})
.sequence('course_id', (idx) => `course-v1-${idx}`)
}))
.option('course_id', null, 'course-v1:edX+DemoX+Demo_Course')
.attr('is_enrolled', null, false)
.attr('is_self_paced', null, false)
.attr('is_staff', null, true)
@@ -25,9 +27,7 @@ Factory.define('navigationBar')
.attr('original_user_is_staff', null, true)
.attr('title', null, 'Demonstration Course')
.attr('username', null, 'edx')
.option('courseId', null, 'course-v1:edX+DemoX+Demo_Course')
.attr('tabs', ['courseId'], (idx, courseId) => [
.attr('tabs', ['course_id'], (idx, courseId) => [
{
tab_id: 'courseware',
title: 'Course',

View File

@@ -12,7 +12,6 @@ import { fetchTab } from './thunks';
import './__factories__';
const courseId = 'course-v1:edX+TestX+Test_Course';
let axiosMock = null;
let store;
@@ -34,7 +33,7 @@ describe('Navigation bar api tests', () => {
axiosMock.reset();
});
it('successfully get navigation tabs', async () => {
it('Successfully get navigation tabs', async () => {
axiosMock.onGet(`${getCourseMetadataApiUrl(courseId)}`).reply(200, (Factory.build('navigationBar', 1)));
await executeThunk(fetchTab(courseId, 'outline'), store.dispatch, store.getState);
@@ -42,17 +41,25 @@ describe('Navigation bar api tests', () => {
expect(store.getState().courseTabs.courseStatus).toEqual('loaded');
});
it('failed to get navigation tabs', async () => {
it('Failed to get navigation tabs', async () => {
axiosMock.onGet(`${getCourseMetadataApiUrl(courseId)}`).reply(404);
await executeThunk(fetchTab(courseId, 'outline'), store.dispatch, store.getState);
expect(store.getState().courseTabs.courseStatus).toEqual('failed');
});
it('denied to get navigation tabs', async () => {
it('Denied to get navigation tabs', async () => {
axiosMock.onGet(`${getCourseMetadataApiUrl(courseId)}`).reply(403, {});
await executeThunk(fetchTab(courseId, 'outline'), store.dispatch, store.getState);
expect(store.getState().courseTabs.courseStatus).toEqual('denied');
});
it('Denied to get navigation bar when user has no access on course', async () => {
axiosMock.onGet(`${getCourseMetadataApiUrl(courseId)}`).reply(200,
(Factory.build('navigationBar', 1, { hasCourseAccess: false })));
await executeThunk(fetchTab(courseId, 'outline'), store.dispatch, store.getState);
expect(store.getState().courseTabs.courseStatus).toEqual('denied');
});
});

View File

@@ -15,13 +15,17 @@ export function fetchTab(courseId, rootSlug) {
dispatch(fetchTabRequest({ courseId }));
try {
const courseHomeCourseMetadata = await getCourseHomeCourseMetadata(courseId, rootSlug);
dispatch(fetchTabSuccess({
courseId,
tabs: courseHomeCourseMetadata.tabs,
org: courseHomeCourseMetadata.org,
courseNumber: courseHomeCourseMetadata.number,
courseTitle: courseHomeCourseMetadata.title,
}));
if (!courseHomeCourseMetadata.courseAccess.hasAccess) {
dispatch(fetchTabDenied({ courseId }));
} else {
dispatch(fetchTabSuccess({
courseId,
tabs: courseHomeCourseMetadata.tabs,
org: courseHomeCourseMetadata.org,
courseNumber: courseHomeCourseMetadata.number,
courseTitle: courseHomeCourseMetadata.title,
}));
}
} catch (e) {
if (getHttpErrorStatus(e) === 403) {
dispatch(fetchTabDenied({ courseId }));