From a812ee3816cf5972f242cf143cee58289df02aa7 Mon Sep 17 00:00:00 2001 From: Simon Chen Date: Fri, 7 Dec 2018 12:11:21 -0500 Subject: [PATCH] Add all the tests to reducers --- src/data/reducers/assignmentTypes.js | 1 + src/data/reducers/assignmentTypes.test.js | 54 +++++++ src/data/reducers/cohorts.test.js | 2 +- src/data/reducers/grades.js | 1 - src/data/reducers/grades.test.js | 186 ++++++++++++++++++++++ src/data/reducers/tracks.js | 1 + src/data/reducers/tracks.test.js | 76 +++++++++ 7 files changed, 319 insertions(+), 2 deletions(-) create mode 100644 src/data/reducers/assignmentTypes.test.js create mode 100644 src/data/reducers/grades.test.js create mode 100644 src/data/reducers/tracks.test.js diff --git a/src/data/reducers/assignmentTypes.js b/src/data/reducers/assignmentTypes.js index 6975fe3..13c5490 100644 --- a/src/data/reducers/assignmentTypes.js +++ b/src/data/reducers/assignmentTypes.js @@ -18,6 +18,7 @@ const assignmentTypes = (state = initialState, action) => { ...state, results: action.assignmentTypes, errorFetching: false, + finishedFetching: true, }; case STARTED_FETCHING_ASSIGNMENT_TYPES: return { diff --git a/src/data/reducers/assignmentTypes.test.js b/src/data/reducers/assignmentTypes.test.js new file mode 100644 index 0000000..37a2aeb --- /dev/null +++ b/src/data/reducers/assignmentTypes.test.js @@ -0,0 +1,54 @@ +import assignmentTypes from './assignmentTypes'; +import { + STARTED_FETCHING_ASSIGNMENT_TYPES, + ERROR_FETCHING_ASSIGNMENT_TYPES, + GOT_ASSIGNMENT_TYPES, +} from '../constants/actionTypes/assignmentTypes'; + +const initialState = { + results: [], + startedFetching: false, + errorFetching: false, +}; + +const assignmentTypesData = ['Exam', 'Homework']; + +describe('assignmentTypes reducer', () => { + it('has initial state', () => { + expect(assignmentTypes(undefined, {})).toEqual(initialState); + }); + + it('updates fetch assignmentTypes request state', () => { + const expected = { + ...initialState, + startedFetching: true, + }; + expect(assignmentTypes(undefined, { + type: STARTED_FETCHING_ASSIGNMENT_TYPES, + })).toEqual(expected); + }); + + it('updates fetch assignmentTypes success state', () => { + const expected = { + ...initialState, + results: assignmentTypesData, + errorFetching: false, + finishedFetching: true, + }; + expect(assignmentTypes(undefined, { + type: GOT_ASSIGNMENT_TYPES, + assignmentTypes: assignmentTypesData, + })).toEqual(expected); + }); + + it('updates fetch assignmentTypes failure state', () => { + const expected = { + ...initialState, + errorFetching: true, + finishedFetching: true, + }; + expect(assignmentTypes(undefined, { + type: ERROR_FETCHING_ASSIGNMENT_TYPES, + })).toEqual(expected); + }); +}); diff --git a/src/data/reducers/cohorts.test.js b/src/data/reducers/cohorts.test.js index 201a6e8..44de984 100644 --- a/src/data/reducers/cohorts.test.js +++ b/src/data/reducers/cohorts.test.js @@ -29,7 +29,7 @@ const cohortsData = [ user_partition_id: null, }]; -describe('dashboardAnalytics reducer', () => { +describe('cohorts reducer', () => { it('has initial state', () => { expect(cohorts(undefined, {})).toEqual(initialState); }); diff --git a/src/data/reducers/grades.js b/src/data/reducers/grades.js index 41d81d9..410f881 100644 --- a/src/data/reducers/grades.js +++ b/src/data/reducers/grades.js @@ -4,7 +4,6 @@ import { GOT_GRADES, TOGGLE_GRADE_FORMAT, FILTER_COLUMNS, - GRADE_UPDATE_SUCCESS, UPDATE_BANNER, SORT_GRADES, } from '../constants/actionTypes/grades'; diff --git a/src/data/reducers/grades.test.js b/src/data/reducers/grades.test.js new file mode 100644 index 0000000..c47f6c6 --- /dev/null +++ b/src/data/reducers/grades.test.js @@ -0,0 +1,186 @@ +import grades from './grades'; +import { + STARTED_FETCHING_GRADES, + ERROR_FETCHING_GRADES, + GOT_GRADES, + TOGGLE_GRADE_FORMAT, + FILTER_COLUMNS, + UPDATE_BANNER, + SORT_GRADES, +} from '../constants/actionTypes/grades'; + +const initialState = { + results: [], + headings: [], + startedFetching: false, + finishedFetching: false, + errorFetching: false, + gradeFormat: 'percent', + showSuccess: false, + prevPage: null, + nextPage: null, + showSpinner: true, +}; + +const courseId = 'course-v1:edX+DemoX+Demo_Course'; +const headingsData = [ + { name: 'exam' }, + { name: 'homework2' }, +]; +const gradesData = [ + { + course_id: courseId, + email: 'user1@example.com', + username: 'user1', + user_id: 1, + percent: 0.5, + letter_grade: null, + section_breakdown: [ + { + subsection_name: 'Demo Course Overview', + score_earned: 0, + score_possible: 0, + percent: 0, + displayed_value: '0.00', + grade_description: '(0.00/0.00)', + }, + { + subsection_name: 'Example Week 1: Getting Started', + score_earned: 1, + score_possible: 1, + percent: 1, + displayed_value: '1.00', + grade_description: '(0.00/0.00)', + }, + ], + }, + { + course_id: courseId, + email: 'user22@example.com', + username: 'user22', + user_id: 22, + percent: 0, + letter_grade: null, + section_breakdown: [ + { + subsection_name: 'Demo Course Overview', + score_earned: 0, + score_possible: 0, + percent: 0, + displayed_value: '0.00', + grade_description: '(0.00/0.00)', + }, + { + subsection_name: 'Example Week 1: Getting Started', + score_earned: 1, + score_possible: 1, + percent: 0, + displayed_value: '0.00', + grade_description: '(0.00/0.00)', + }, + ], + }]; + +describe('grades reducer', () => { + it('has initial state', () => { + expect(grades(undefined, {})).toEqual(initialState); + }); + + it('updates fetch grades request state', () => { + const expected = { + ...initialState, + startedFetching: true, + showSpinner: true, + }; + expect(grades(undefined, { + type: STARTED_FETCHING_GRADES, + })).toEqual(expected); + }); + + it('updates fetch grades success state', () => { + const expectedPrev = 'testPrevUrl'; + const expectedNext = 'testNextUrl'; + const expectedTrack = 'verified'; + const expectedCohortId = 2; + const expected = { + ...initialState, + results: gradesData, + headings: headingsData, + errorFetching: false, + finishedFetching: true, + selectedTrack: expectedTrack, + selectedCohort: expectedCohortId, + prevPage: expectedPrev, + nextPage: expectedNext, + showSpinner: false, + }; + expect(grades(undefined, { + type: GOT_GRADES, + grades: gradesData, + headings: headingsData, + prev: expectedPrev, + next: expectedNext, + track: expectedTrack, + cohort: expectedCohortId, + showSpinner: true, + })).toEqual(expected); + }); + + it('updates toggle grade format state success', () => { + const formatTypeData = 'percent'; + const expected = { + ...initialState, + gradeFormat: formatTypeData, + }; + expect(grades(undefined, { + type: TOGGLE_GRADE_FORMAT, + formatType: formatTypeData, + })).toEqual(expected); + }); + + it('updates filter columns state success', () => { + const expectedHeadings = headingsData; + const expected = { + ...initialState, + headings: expectedHeadings, + }; + expect(grades(undefined, { + type: FILTER_COLUMNS, + headings: expectedHeadings, + })).toEqual(expected); + }); + + it('updates update_banner state success', () => { + const expectedShowSuccess = true; + const expected = { + ...initialState, + showSuccess: expectedShowSuccess, + }; + expect(grades(undefined, { + type: UPDATE_BANNER, + showSuccess: expectedShowSuccess, + })).toEqual(expected); + }); + + it('updates sort grades state success', () => { + const expected = { + ...initialState, + results: gradesData, + }; + expect(grades(undefined, { + type: SORT_GRADES, + results: gradesData, + })).toEqual(expected); + }); + + it('updates fetch grades failure state', () => { + const expected = { + ...initialState, + errorFetching: true, + finishedFetching: true, + }; + expect(grades(undefined, { + type: ERROR_FETCHING_GRADES, + })).toEqual(expected); + }); +}); diff --git a/src/data/reducers/tracks.js b/src/data/reducers/tracks.js index 1c1836e..63fc4da 100644 --- a/src/data/reducers/tracks.js +++ b/src/data/reducers/tracks.js @@ -18,6 +18,7 @@ const tracks = (state = initialState, action) => { ...state, results: action.tracks, errorFetching: false, + finishedFetching: true, }; case STARTED_FETCHING_TRACKS: return { diff --git a/src/data/reducers/tracks.test.js b/src/data/reducers/tracks.test.js new file mode 100644 index 0000000..a3ae57c --- /dev/null +++ b/src/data/reducers/tracks.test.js @@ -0,0 +1,76 @@ +import tracks from './tracks'; +import { + STARTED_FETCHING_TRACKS, + ERROR_FETCHING_TRACKS, + GOT_TRACKS, +} from '../constants/actionTypes/tracks'; + +const initialState = { + results: [], + startedFetching: false, + errorFetching: false, +}; + +const tracksData = [ + { + slug: 'audit', + name: 'Audit', + min_price: 0, + suggested_prices: '', + currency: 'usd', + expiration_datetime: null, + description: null, + sku: '68EFFFF', + bulk_sku: null, + }, + { + slug: 'verified', + name: 'Verified Certificate', + min_price: 100, + suggested_prices: '', + currency: 'usd', + expiration_datetime: '2021-05-04T18:08:12.644361Z', + description: null, + sku: '8CF08E5', + bulk_sku: 'A5B6DBE', + }]; + +describe('tracks reducer', () => { + it('has initial state', () => { + expect(tracks(undefined, {})).toEqual(initialState); + }); + + it('updates fetch tracks request state', () => { + const expected = { + ...initialState, + startedFetching: true, + }; + expect(tracks(undefined, { + type: STARTED_FETCHING_TRACKS, + })).toEqual(expected); + }); + + it('updates fetch tracks success state', () => { + const expected = { + ...initialState, + results: tracksData, + errorFetching: false, + finishedFetching: true, + }; + expect(tracks(undefined, { + type: GOT_TRACKS, + tracks: tracksData, + })).toEqual(expected); + }); + + it('updates fetch tracks failure state', () => { + const expected = { + ...initialState, + errorFetching: true, + finishedFetching: true, + }; + expect(tracks(undefined, { + type: ERROR_FETCHING_TRACKS, + })).toEqual(expected); + }); +});