From adfefac85dbf3e788013d7441675673ce0c30eb1 Mon Sep 17 00:00:00 2001 From: Simon Chen Date: Wed, 5 Dec 2018 13:52:33 -0500 Subject: [PATCH] feat(test): Setup unit testing --- Makefile | 3 ++ package-lock.json | 9 ++++ package.json | 1 + src/data/actions/cohorts.test.js | 73 ++++++++++++++++++++++++++++++++ src/setupTest.js | 4 ++ 5 files changed, 90 insertions(+) create mode 100644 src/data/actions/cohorts.test.js diff --git a/Makefile b/Makefile index ae3a709..eaed283 100755 --- a/Makefile +++ b/Makefile @@ -30,3 +30,6 @@ restart-detached: validate-no-uncommitted-package-lock-changes: git diff --exit-code package-lock.json + +test: + docker exec -it edx.gradebook jest diff --git a/package-lock.json b/package-lock.json index 65cda63..f7141a5 100755 --- a/package-lock.json +++ b/package-lock.json @@ -4161,6 +4161,15 @@ "is-buffer": "^1.1.5" } }, + "axios-mock-adapter": { + "version": "1.15.0", + "resolved": "https://registry.npmjs.org/axios-mock-adapter/-/axios-mock-adapter-1.15.0.tgz", + "integrity": "sha1-+8BoJdgwLJXDM00hAju6mWJV1F0=", + "dev": true, + "requires": { + "deep-equal": "^1.0.1" + } + }, "axobject-query": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-0.1.0.tgz", diff --git a/package.json b/package.json index 8b66f49..24ff635 100755 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "whatwg-fetch": "^2.0.3" }, "devDependencies": { + "axios-mock-adapter": "^1.15.0", "babel-cli": "^6.26.0", "babel-eslint": "^8.2.2", "babel-jest": "^22.4.0", diff --git a/src/data/actions/cohorts.test.js b/src/data/actions/cohorts.test.js new file mode 100644 index 0000000..0baed3e --- /dev/null +++ b/src/data/actions/cohorts.test.js @@ -0,0 +1,73 @@ +import configureMockStore from 'redux-mock-store'; +import MockAdapter from 'axios-mock-adapter'; +import thunk from 'redux-thunk'; + +import apiClient from '../apiClient'; +import { fetchCohorts } from './cohorts'; +import { + STARTED_FETCHING_COHORTS, + GOT_COHORTS, + ERROR_FETCHING_COHORTS, +} from '../constants/actionTypes/cohorts'; + +const mockStore = configureMockStore([thunk]); +const axiosMock = new MockAdapter(apiClient); + +describe('actions', () => { + afterEach(() => { + axiosMock.reset(); + }); + + describe('fetchCohorts', () => { + const courseId = 'course-v1:edX+DemoX+Demo_Course'; + + it('dispatches success action after fetching cohorts', () => { + const responseData = { + cohorts: [ + { + assignment_type: 'manual', + group_id: null, + id: 1, + name: 'default_group', + user_count: 2, + user_partition_id: null, + }, + { + assignment_type: 'auto', + group_id: null, + id: 2, + name: 'auto_group', + user_count: 5, + user_partition_id: null, + }], + }; + const expectedActions = [ + { type: STARTED_FETCHING_COHORTS }, + { type: GOT_COHORTS, cohorts: responseData.cohorts }, + ]; + const store = mockStore(); + + axiosMock.onGet(`http://localhost:18000/courses/${courseId}/cohorts/`) + .replyOnce(200, JSON.stringify(responseData)); + + return store.dispatch(fetchCohorts(courseId)).then(() => { + expect(store.getActions()).toEqual(expectedActions); + }); + }); + + it('dispatches failure action after fetching cohorts', () => { + const expectedActions = [ + { type: STARTED_FETCHING_COHORTS }, + { type: ERROR_FETCHING_COHORTS }, + ]; + const store = mockStore(); + + axiosMock.onGet(`http://localhost:18000/courses/${courseId}/cohorts/`) + .replyOnce(500, JSON.stringify({})); + + return store.dispatch(fetchCohorts(courseId)).then(() => { + expect(store.getActions()).toEqual(expectedActions); + }); + }); + }); +}); diff --git a/src/setupTest.js b/src/setupTest.js index 93cbadb..70c7014 100755 --- a/src/setupTest.js +++ b/src/setupTest.js @@ -4,3 +4,7 @@ import Enzyme from 'enzyme'; import Adapter from 'enzyme-adapter-react-16'; Enzyme.configure({ adapter: new Adapter() }); + +// These configuration values are usually set in webpack's EnvironmentPlugin however +// Jest does not use webpack so we need to set these so for testing +process.env.LMS_BASE_URL = 'http://localhost:18000';