From 82ade861e65fd4e0009d37203422a53432151ba8 Mon Sep 17 00:00:00 2001 From: Martin Krulis Date: Thu, 5 Sep 2019 22:54:05 +0200 Subject: [PATCH] Fixing tests. --- package.json | 1 + .../resourceManager/actionCreators-test.js | 46 +++++++------- test/redux/middleware/apiMiddleware-test.js | 3 +- test/redux/modules/auth-test.js | 58 +++++++----------- test/redux/modules/submission-test.js | 22 ++++--- test/redux/modules/upload-test.js | 60 ++++++++++--------- 6 files changed, 88 insertions(+), 102 deletions(-) diff --git a/package.json b/package.json index 4e358b839..7e178862e 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "build:test": "npm run clean && npm run build:server && npm run build:client", "lint": "eslint src", "test": "npm run lint && mocha", + "test:mocha": "mocha", "test:watch": "mocha --watch --reporter nyan", "dev": "babel-node bin/dev.js --max-old-space-size=4096", "start": "node bin/server.js", diff --git a/test/redux/helpers/resourceManager/actionCreators-test.js b/test/redux/helpers/resourceManager/actionCreators-test.js index 5f7872df1..71ae2cd57 100644 --- a/test/redux/helpers/resourceManager/actionCreators-test.js +++ b/test/redux/helpers/resourceManager/actionCreators-test.js @@ -2,12 +2,12 @@ import chai from 'chai'; import spies from 'chai-spies'; import { fromJS } from 'immutable'; -chai.use(spies); -const expect = chai.expect; - import actionTypesFactory from '../../../../src/redux/helpers/resourceManager/actionTypesFactory'; import actionCreatorsFactory from '../../../../src/redux/helpers/resourceManager/actionCreatorsFactory'; +chai.use(spies); +const expect = chai.expect; + // prepared spies var globalNeedsRefetching = false; // this is really disgusting, I know... const needsRefetching = chai.spy(() => globalNeedsRefetching); @@ -21,7 +21,7 @@ const actionCreators = actionCreatorsFactory({ apiEndpointFactory: (id = '') => `url/${id}`, needsRefetching, createAction, - createApiAction + createApiAction, }); describe('Resource manager', () => { @@ -47,11 +47,11 @@ describe('Resource manager', () => { type: actionTypes.FETCH, method: 'GET', endpoint: 'url/abc', - meta: { id: 'abc' } + meta: { id: 'abc' }, }); }); - it('must create fetch action which checks if the resource is not already cached', (done) => { + it('must create fetch action which checks if the resource is not already cached', done => { const { fetchOneIfNeeded } = actionCreators; // fetchResource must be a function with one parameter @@ -63,11 +63,12 @@ describe('Resource manager', () => { expect(thunk).to.be.a('function'); expect(thunk.length).to.equal(2); - const getState = () => fromJS({ - resources: { - abc: { status: 'FULFILLED', data: { a: 'b' } } - } - }); + const getState = () => + fromJS({ + resources: { + abc: { status: 'FULFILLED', data: { a: 'b' } }, + }, + }); globalNeedsRefetching = false; const dispatch = chai.spy(); @@ -82,11 +83,12 @@ describe('Resource manager', () => { const { fetchOneIfNeeded, fetchResource } = actionCreators; const thunk = fetchOneIfNeeded('abc'); - const getState = () => fromJS({ - resources: { - abc: { status: '', data: { a: 'b' } } - } - }); + const getState = () => + fromJS({ + resources: { + abc: { status: '', data: { a: 'b' } }, + }, + }); globalNeedsRefetching = true; const dispatch = chai.spy(); @@ -111,7 +113,7 @@ describe('Resource manager', () => { expect(createApiAction).to.have.been.called.with({ type: actionTypes.FETCH_MANY, method: 'GET', - endpoint: 'url/' + endpoint: 'url/', }); }); }); @@ -123,7 +125,7 @@ describe('Resource manager', () => { expect(addResource).to.be.a('function'); expect(addResource.length).to.equal(1); - const body = { 'foo': 'bar', 'abc': 'xyz' }; + const body = { foo: 'bar', abc: 'xyz' }; const tmpId = 'random-tmp-id'; addResource(body, tmpId); expect(createApiAction).to.have.been.called.once(); @@ -132,7 +134,7 @@ describe('Resource manager', () => { method: 'POST', endpoint: 'url/', body, - meta: { tmpId, body } + meta: { tmpId, body }, }); }); @@ -152,7 +154,7 @@ describe('Resource manager', () => { expect(updateResource).to.be.a('function'); expect(updateResource.length).to.equal(2); - const body = { 'foo': 'bar', 'abc': 'xyz' }; + const body = { foo: 'bar', abc: 'xyz' }; const id = 'some-id'; updateResource(id, body); expect(createApiAction).to.have.been.called.once(); @@ -161,7 +163,7 @@ describe('Resource manager', () => { method: 'POST', endpoint: `url/${id}`, body, - meta: { id, body } + meta: { id, body }, }); }); }); @@ -180,7 +182,7 @@ describe('Resource manager', () => { type: actionTypes.REMOVE, method: 'DELETE', endpoint: `url/${id}`, - meta: { id } + meta: { id }, }); }); }); diff --git a/test/redux/middleware/apiMiddleware-test.js b/test/redux/middleware/apiMiddleware-test.js index 2e8e5d684..9e1a73bff 100644 --- a/test/redux/middleware/apiMiddleware-test.js +++ b/test/redux/middleware/apiMiddleware-test.js @@ -1,6 +1,5 @@ import chai from 'chai'; import spies from 'chai-spies'; - import fetchMock from 'fetch-mock'; import middleware, { CALL_API, createApiAction } from '../../../src/redux/middleware/apiMiddleware'; import { API_BASE } from '../../../src/helpers/config'; @@ -28,7 +27,7 @@ describe('API middleware and helper functions', () => { it('must intersect API call actions and create request', done => { const requestInfo = { type: 'A', endpoint: '/abc' }; - let action = createApiAction(requestInfo); + const action = createApiAction(requestInfo); const spy = chai.spy(); const next = action => { spy(); diff --git a/test/redux/modules/auth-test.js b/test/redux/modules/auth-test.js index a99c1e5a7..77810eef7 100644 --- a/test/redux/modules/auth-test.js +++ b/test/redux/modules/auth-test.js @@ -8,15 +8,9 @@ import reducerFactory, { logout, login, externalLogin, - loginServices + loginServices, } from '../../../src/redux/modules/auth'; -import { - isLoggedIn, - hasSucceeded, - hasFailed, - statusSelector -} from '../../../src/redux/selectors/auth'; -import { push } from 'react-router-redux'; +import { isLoggedIn, hasSucceeded, hasFailed, statusSelector } from '../../../src/redux/selectors/auth'; import decodeJwt from 'jwt-decode'; @@ -26,18 +20,8 @@ const expect = chai.expect; describe('Authentication', () => { describe('(Action creators)', () => { it('must create a valid logout action', () => { - const action = logout('https://anywhere'); - expect(action).to.be.a('function'); - expect(action.length).to.equal(1); - - const dispatchSpy = chai.spy(); - action(dispatchSpy); - - expect(dispatchSpy).to.have.been.called.twice(); - expect(dispatchSpy).to.have.been.called.with(push('https://anywhere')); - expect(dispatchSpy).to.have.been.called.with({ - type: actionTypes.LOGOUT - }); + const action = logout(); + expect(action).to.eql({ type: actionTypes.LOGOUT }); }); it('must create correct login request action', () => { @@ -47,7 +31,7 @@ describe('Authentication', () => { method: 'POST', endpoint: '/login', body: { username: 'usr', password: 'pwd' }, - meta: { service: loginServices.local } + meta: { service: loginServices.local }, }); }); @@ -55,7 +39,7 @@ describe('Authentication', () => { const serviceId = 'some-ext-service'; const action = externalLogin(serviceId)({ serviceToken: 'xyz', - otherData: 'uvw' + otherData: 'uvw', }); expect(action.request).to.eql({ type: actionTypes.LOGIN, @@ -63,9 +47,9 @@ describe('Authentication', () => { endpoint: `/login/${serviceId}`, body: { serviceToken: 'xyz', - otherData: 'uvw' + otherData: 'uvw', }, - meta: { service: serviceId, popupWindow: null } + meta: { service: serviceId, popupWindow: null }, }); }); }); @@ -80,7 +64,7 @@ describe('Authentication', () => { jwt: null, accessToken: null, userId: null, - instanceId: null + instanceId: null, }); expect(state).to.eql(expectedState); }); @@ -93,7 +77,7 @@ describe('Authentication', () => { jwt: null, accessToken: null, userId: null, - instanceId: null + instanceId: null, }); expect(state).to.eql(expectedState); }); @@ -111,7 +95,7 @@ describe('Authentication', () => { jwt: null, accessToken: null, userId: null, - instanceId: null + instanceId: null, }); expect(state).to.eql(expectedState); }); @@ -127,7 +111,7 @@ describe('Authentication', () => { jwt: validToken, accessToken: decodeJwt(validToken), userId: 123, - instanceId: 'instance-id' + instanceId: 'instance-id', }); expect(state).to.eql(expectedState); }); @@ -138,8 +122,8 @@ describe('Authentication', () => { it('must detect that the user is not logged in', () => { const state = { auth: fromJS({ - userId: null - }) + userId: null, + }), }; expect(isLoggedIn(state)).to.equal(false); @@ -150,9 +134,9 @@ describe('Authentication', () => { auth: fromJS({ userId: 123, accessToken: { - exp: Date.now() / 1000 + 100 - } - }) + exp: Date.now() / 1000 + 100, + }, + }), }; expect(isLoggedIn(state)).to.equal(true); @@ -161,8 +145,8 @@ describe('Authentication', () => { it('must select correct OK status', () => { const state = { auth: fromJS({ - status: { abc: statusTypes.LOGGED_IN } - }) + status: { abc: statusTypes.LOGGED_IN }, + }), }; expect(statusSelector('abc')(state)).to.equal(statusTypes.LOGGED_IN); @@ -173,8 +157,8 @@ describe('Authentication', () => { it('must select correct FAIL status', () => { const state = { auth: fromJS({ - status: { abc: statusTypes.LOGIN_FAILED } - }) + status: { abc: statusTypes.LOGIN_FAILED }, + }), }; expect(statusSelector('abc')(state)).to.equal(statusTypes.LOGIN_FAILED); diff --git a/test/redux/modules/submission-test.js b/test/redux/modules/submission-test.js index ee69c9a4e..e1f70f9f1 100644 --- a/test/redux/modules/submission-test.js +++ b/test/redux/modules/submission-test.js @@ -7,7 +7,7 @@ import reducer, { initialState, init, changeNote, - finishProcessing + finishProcessing, } from '../../../src/redux/modules/submission'; import { Map } from 'immutable'; @@ -22,7 +22,7 @@ describe("Submission of user's solution", () => { const id = 'yzsdalkj'; expect(init(userId, id)).to.eql({ type: actionTypes.INIT, - payload: { userId, id } + payload: { userId, id }, }); }); @@ -30,7 +30,7 @@ describe("Submission of user's solution", () => { const note = 'bla bla bla'; expect(changeNote(note)).to.eql({ type: actionTypes.CHANGE_NOTE, - payload: note + payload: note, }); }); }); @@ -51,7 +51,7 @@ describe("Submission of user's solution", () => { monitor: null, status: submissionStatus.NONE, warningMsg: null, - presubmit: null + presubmit: null, }) ); }); @@ -70,7 +70,7 @@ describe("Submission of user's solution", () => { monitor: null, status: submissionStatus.CREATING, warningMsg: null, - presubmit: null + presubmit: null, }) ); }); @@ -87,7 +87,7 @@ describe("Submission of user's solution", () => { monitor: null, status: submissionStatus.PROCESSING, warningMsg: 'This is not gonna end well!', - presubmit: [] + presubmit: [], }); const state = reducer(oldState, init(userId, id)); @@ -101,7 +101,7 @@ describe("Submission of user's solution", () => { monitor: null, status: submissionStatus.CREATING, warningMsg: null, - presubmit: null + presubmit: null, }) ); }); @@ -112,9 +112,7 @@ describe("Submission of user's solution", () => { const state = reducer(initialState, action); expect(state.get('note')).to.equal(note); expect(state.get('status')).to.equal(submissionStatus.CREATING); - expect( - state.set('note', '').set('status', submissionStatus.NONE) - ).to.equal(initialState); + expect(state.set('note', '').set('status', submissionStatus.NONE)).to.equal(initialState); }); describe('solution submission', () => { @@ -135,9 +133,9 @@ describe("Submission of user's solution", () => { type: actionTypes.SUBMIT_FULFILLED, payload: { solution: { id: '123' }, - webSocketChannel: { monitorUrl: 'ws://xyz.cz' } + webSocketChannel: { monitorUrl: 'ws://xyz.cz' }, }, - meta: { submissionType: 'assignmentSolution' } + meta: { submissionType: 'assignmentSolution' }, }); expect(state.get('status')).to.equal(submissionStatus.PROCESSING); expect(state.get('solutionId')).to.equal('123'); diff --git a/test/redux/modules/upload-test.js b/test/redux/modules/upload-test.js index cf3ad10a0..61308d593 100644 --- a/test/redux/modules/upload-test.js +++ b/test/redux/modules/upload-test.js @@ -1,30 +1,30 @@ -import chai from 'chai'; -import chaiImmutable from 'chai-immutable'; -chai.use(chaiImmutable); - -const expect = chai.expect; - import reducer, { actionTypes, initialState, - init, addFile, removeFile, removeFailedFile, returnFile, uploadSuccessful, - uploadFailed + uploadFailed, } from '../../../src/redux/modules/upload'; import { fromJS, Map, List } from 'immutable'; +import chai from 'chai'; +import chaiImmutable from 'chai-immutable'; + +chai.use(chaiImmutable); + +const expect = chai.expect; + describe('Uploading', () => { describe('(Action creators)', () => { it('must initialize the submission', () => { expect(init('blablabla')).to.eql({ type: actionTypes.INIT, - payload: 'blablabla' + payload: 'blablabla', }); }); @@ -34,7 +34,7 @@ describe('Uploading', () => { expect(removeFile(id, file)).to.eql({ type: actionTypes.REMOVE_FILE, payload: file, - meta: { id } + meta: { id }, }); }); }); @@ -51,15 +51,17 @@ describe('Uploading', () => { describe('file uploading', () => { it('must initialise a specific upload process', () => { const id = 'alsdjlaskjdalskjd'; - let state = reducer(undefined, {}); - expect(reducer(state, init(id))).to.eql(fromJS({ - [id]: { - uploaded: [], - uploading: [], - failed: [], - removed: [] - } - })); + const state = reducer(undefined, {}); + expect(reducer(state, init(id))).to.eql( + fromJS({ + [id]: { + uploaded: [], + uploading: [], + failed: [], + removed: [], + }, + }) + ); }); it('must add file among other files for upload', () => { @@ -68,9 +70,9 @@ describe('Uploading', () => { const action = { type: actionTypes.UPLOAD_PENDING, payload: { - [file.name]: file + [file.name]: file, }, - meta: { id, fileName: file.name } + meta: { id, fileName: file.name }, }; let state = reducer(reducer(undefined, {}), init(id)); @@ -80,7 +82,7 @@ describe('Uploading', () => { expect(filesToUpload.size).to.equal(1); expect(filesToUpload.first()).to.eql({ name: file.name, - file + file, }); }); @@ -102,7 +104,7 @@ describe('Uploading', () => { expect(uploadedFiles.size).to.equal(1); expect(uploadedFiles.first()).to.eql({ name: file.name, - file + file, }); }); @@ -124,7 +126,7 @@ describe('Uploading', () => { expect(failedFiles.size).to.equal(1); expect(failedFiles.first()).to.eql({ name: file.name, - file + file, }); }); @@ -138,8 +140,8 @@ describe('Uploading', () => { // now that the file is in the list, remove it! state = reducer(state, removeFile(id, file)); - const uploadedFiles = state.getIn([ id, 'uploaded' ]); - const removedFiles = state.getIn([ id, 'removed' ]); + const uploadedFiles = state.getIn([id, 'uploaded']); + const removedFiles = state.getIn([id, 'removed']); expect(uploadedFiles.size).to.equal(0); expect(removedFiles.size).to.equal(1); expect(removedFiles.first()).to.eql(file); @@ -156,8 +158,8 @@ describe('Uploading', () => { // now that the file is removed, return it back state = reducer(state, returnFile(id, file)); - const removedFiles = state.getIn([ id, 'removed' ]); - const uploadedFiles = state.getIn([ id, 'uploaded' ]); + const removedFiles = state.getIn([id, 'removed']); + const uploadedFiles = state.getIn([id, 'uploaded']); expect(removedFiles.size).to.equal(0); expect(uploadedFiles.size).to.equal(1); expect(uploadedFiles.first()).to.eql(file); @@ -173,7 +175,7 @@ describe('Uploading', () => { // now that the file is in the list, remove it! state = reducer(state, removeFailedFile(id, file)); - const failedFiles = state.getIn([ id, 'failed' ]); + const failedFiles = state.getIn([id, 'failed']); expect(failedFiles.size).to.equal(0); }); });