Skip to content

Commit

Permalink
Fixing tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Krulis committed Sep 9, 2019
1 parent f748346 commit 82ade86
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 102 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
46 changes: 24 additions & 22 deletions test/redux/helpers/resourceManager/actionCreators-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -21,7 +21,7 @@ const actionCreators = actionCreatorsFactory({
apiEndpointFactory: (id = '') => `url/${id}`,
needsRefetching,
createAction,
createApiAction
createApiAction,
});

describe('Resource manager', () => {
Expand All @@ -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
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -111,7 +113,7 @@ describe('Resource manager', () => {
expect(createApiAction).to.have.been.called.with({
type: actionTypes.FETCH_MANY,
method: 'GET',
endpoint: 'url/'
endpoint: 'url/',
});
});
});
Expand All @@ -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();
Expand All @@ -132,7 +134,7 @@ describe('Resource manager', () => {
method: 'POST',
endpoint: 'url/',
body,
meta: { tmpId, body }
meta: { tmpId, body },
});
});

Expand All @@ -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();
Expand All @@ -161,7 +163,7 @@ describe('Resource manager', () => {
method: 'POST',
endpoint: `url/${id}`,
body,
meta: { id, body }
meta: { id, body },
});
});
});
Expand All @@ -180,7 +182,7 @@ describe('Resource manager', () => {
type: actionTypes.REMOVE,
method: 'DELETE',
endpoint: `url/${id}`,
meta: { id }
meta: { id },
});
});
});
Expand Down
3 changes: 1 addition & 2 deletions test/redux/middleware/apiMiddleware-test.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();
Expand Down
58 changes: 21 additions & 37 deletions test/redux/modules/auth-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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', () => {
Expand All @@ -47,25 +31,25 @@ describe('Authentication', () => {
method: 'POST',
endpoint: '/login',
body: { username: 'usr', password: 'pwd' },
meta: { service: loginServices.local }
meta: { service: loginServices.local },
});
});

it('must create correct external login request action', () => {
const serviceId = 'some-ext-service';
const action = externalLogin(serviceId)({
serviceToken: 'xyz',
otherData: 'uvw'
otherData: 'uvw',
});
expect(action.request).to.eql({
type: actionTypes.LOGIN,
method: 'POST',
endpoint: `/login/${serviceId}`,
body: {
serviceToken: 'xyz',
otherData: 'uvw'
otherData: 'uvw',
},
meta: { service: serviceId, popupWindow: null }
meta: { service: serviceId, popupWindow: null },
});
});
});
Expand All @@ -80,7 +64,7 @@ describe('Authentication', () => {
jwt: null,
accessToken: null,
userId: null,
instanceId: null
instanceId: null,
});
expect(state).to.eql(expectedState);
});
Expand All @@ -93,7 +77,7 @@ describe('Authentication', () => {
jwt: null,
accessToken: null,
userId: null,
instanceId: null
instanceId: null,
});
expect(state).to.eql(expectedState);
});
Expand All @@ -111,7 +95,7 @@ describe('Authentication', () => {
jwt: null,
accessToken: null,
userId: null,
instanceId: null
instanceId: null,
});
expect(state).to.eql(expectedState);
});
Expand All @@ -127,7 +111,7 @@ describe('Authentication', () => {
jwt: validToken,
accessToken: decodeJwt(validToken),
userId: 123,
instanceId: 'instance-id'
instanceId: 'instance-id',
});
expect(state).to.eql(expectedState);
});
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
22 changes: 10 additions & 12 deletions test/redux/modules/submission-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import reducer, {
initialState,
init,
changeNote,
finishProcessing
finishProcessing,
} from '../../../src/redux/modules/submission';

import { Map } from 'immutable';
Expand All @@ -22,15 +22,15 @@ describe("Submission of user's solution", () => {
const id = 'yzsdalkj';
expect(init(userId, id)).to.eql({
type: actionTypes.INIT,
payload: { userId, id }
payload: { userId, id },
});
});

it('must place the note in the payload', () => {
const note = 'bla bla bla';
expect(changeNote(note)).to.eql({
type: actionTypes.CHANGE_NOTE,
payload: note
payload: note,
});
});
});
Expand All @@ -51,7 +51,7 @@ describe("Submission of user's solution", () => {
monitor: null,
status: submissionStatus.NONE,
warningMsg: null,
presubmit: null
presubmit: null,
})
);
});
Expand All @@ -70,7 +70,7 @@ describe("Submission of user's solution", () => {
monitor: null,
status: submissionStatus.CREATING,
warningMsg: null,
presubmit: null
presubmit: null,
})
);
});
Expand All @@ -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));
Expand All @@ -101,7 +101,7 @@ describe("Submission of user's solution", () => {
monitor: null,
status: submissionStatus.CREATING,
warningMsg: null,
presubmit: null
presubmit: null,
})
);
});
Expand All @@ -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', () => {
Expand All @@ -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');
Expand Down
Loading

0 comments on commit 82ade86

Please sign in to comment.