Skip to content

Commit

Permalink
Chore/use jest framework (#361)
Browse files Browse the repository at this point in the history
As agreed upon, switching to jest for unit tests
  • Loading branch information
jenniferarnesen authored Oct 13, 2017
1 parent dee698e commit f902ccf
Show file tree
Hide file tree
Showing 33 changed files with 2,039 additions and 1,410 deletions.
7 changes: 6 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
],
"env": {
"test": {
"presets": ["es2015", "react", "stage-0"]
"presets": ["es2015", "react", "stage-0"],
"only": [
"src/*.js",
"src/**/*.js",
"config/*.js"
]
}
}
}
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "dhis2",
"env": {
"browser": true,
"mocha": true
"jest": true
},
"parser": "babel-eslint",
"parserOptions": {
Expand Down
22 changes: 4 additions & 18 deletions config/setup.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
require('babel-register')({
ignore: function (path) {
if (/node_modules\/d2-ui/.test(path)) {
return false;
}
import 'jest-enzyme';
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-15';

return /node_modules/.test(path);
}
});

/* istanbul ignore next */
global.chai = require('chai');
global.sinon = require('sinon');

// Chai plugins
global.chai.use(require('sinon-chai'));
global.chai.use(require('chai-enzyme')());

global.expect = global.chai.expect;
Enzyme.configure({ adapter: new Adapter() });
10 changes: 10 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
setupTestFrameworkScriptFile: '<rootDir>/config/setup.js',
testPathIgnorePatterns: [
'/node_modules/',
'<rootDir>/lib/',
],
transformIgnorePatterns: [
'/node_modules/(?!d2-ui).+\\.js$',
],
};
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"start": "webpack-dev-server",
"coverage": "NODE_ENV=test nyc --include src --exclude 'src/**/*.spec.js' --require babel-register --require babel-polyfill --require ignore-styles mocha --reporter dot --require config/setup.js --recursive 'src/**/*.spec.js'",
"postcoverage": " nyc report --reporter=lcov",
"test": "NODE_ENV=test mocha --require babel-register --require babel-polyfill --require ignore-styles --reporter spec --require config/setup.js --recursive './src/**/*.spec.js'",
"test": "jest",
"test:watch": "yarn test -- --watch",
"prebuild": "yarn test",
"build": "rm -rf build && NODE_ENV=production webpack --progress && npm run manifest",
Expand Down Expand Up @@ -81,19 +81,18 @@
},
"dependencies": {
"babel-polyfill": "^6.13.0",
"chai": "^3.5.0",
"chai-enzyme": "^0.6.1",
"enzyme": "^2.8.0",
"mocha": "^3.2.0",
"enzyme": "^3.0.0",
"enzyme-adapter-react-15": "^1.0.0",
"jest": "^21.2.1",
"jest-enzyme": "^4.0.0",
"nyc": "10.1.2",
"prop-types": "^15.6.0",
"react-redux": "^5.0.3",
"react-sortable-hoc": "^0.6.1",
"react-test-renderer": "15",
"redux": "^3.6.0",
"redux-observable": "^0.14.1",
"rxjs": "^5.2.0",
"sinon": "^2.1.0",
"sinon-chai": "^2.9.0"
"rxjs": "^5.2.0"
},
"pre-commit": [
"validate",
Expand Down
18 changes: 18 additions & 0 deletions src/EditModel/__tests__/actions.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as actions from '../actions';

describe('Model to edit actions', () => {
describe('for notifying users', () => {
test('should defined the notification constants', () => {
expect(actions.NOTIFY_USER).toBe('NOTIFY_USER');
});

test('should create a notify user action when calling notifyUser', () => {
const expectedAction = {
type: actions.NOTIFY_USER,
payload: undefined,
};

expect(actions.notifyUser()).toEqual(expectedAction);
});
});
});
18 changes: 0 additions & 18 deletions src/EditModel/actions.spec.js

This file was deleted.

122 changes: 122 additions & 0 deletions src/EditModel/event-program/__tests__/actions.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import * as actions from '../actions';

describe('Event Program actions', () => {
describe('for stepper', () => {
test('should have defined the stepper constants', () => {
expect(actions.EVENT_PROGRAM_STEP_PREVIOUS).toBe('EVENT_PROGRAM_STEP_PREVIOUS');
expect(actions.EVENT_PROGRAM_STEP_NEXT).toBe('EVENT_PROGRAM_STEP_NEXT');
expect(actions.EVENT_PROGRAM_STEP_CHANGE).toBe('EVENT_PROGRAM_STEP_CHANGE');
});

test('should create the EVENT_PROGRAM_STEP_CHANGE action', () => {
expect(actions.changeStep()).toEqual({ type: actions.EVENT_PROGRAM_STEP_CHANGE, payload: undefined });
});

test('should use the parameter as the payload', () => {
expect(actions.changeStep('details')).toEqual({ type: actions.EVENT_PROGRAM_STEP_CHANGE, payload: 'details' });
});

test('should create the EVENT_PROGRAM_STEP_NEXT action', () => {
expect(actions.nextStep()).toEqual({ type: actions.EVENT_PROGRAM_STEP_NEXT });
});

test('should create the EVENT_PROGRAM_STEP_PREVIOUS action', () => {
expect(actions.previousStep()).toEqual({ type: actions.EVENT_PROGRAM_STEP_PREVIOUS });
});
});

describe('for loading a event program', () => {
test('should have the event program load constants', () => {
expect(actions.EVENT_PROGRAM_LOAD).toBe('EVENT_PROGRAM_LOAD');
expect(actions.EVENT_PROGRAM_LOAD_SUCCESS).toBe('EVENT_PROGRAM_LOAD_SUCCESS');
expect(actions.EVENT_PROGRAM_LOAD_ERROR).toBe('EVENT_PROGRAM_LOAD_ERROR');
});

test('should create the EVENT_PROGRAM_LOAD action', () => {
expect(actions.loadEventProgram()).toEqual({ type: actions.EVENT_PROGRAM_LOAD, payload: undefined });
});

test('should create the EVENT_PROGRAM_LOAD_SUCCESS action', () => {
expect(actions.loadEventProgramSuccess()).toEqual({ type: actions.EVENT_PROGRAM_LOAD_SUCCESS, payload: undefined });
});

test('should create the EVENT_PROGRAM_LOAD_SUCCESS action', () => {
expect(actions.loadEventProgramFailure()).toEqual({ type: actions.EVENT_PROGRAM_LOAD_ERROR, payload: undefined });
});
});

describe('for editing models', () => {
test('should have defined the model constants', () => {
expect(actions.MODEL_TO_EDIT_LOADED).toBe('MODEL_TO_EDIT_LOADED');
expect(actions.MODEL_TO_EDIT_FIELD_CHANGED).toBe('MODEL_TO_EDIT_FIELD_CHANGED');
});

test('should create the action when calling editFieldChanged', () => {
const expectedAction = {
type: 'MODEL_TO_EDIT_FIELD_CHANGED',
payload: {
field: 'name',
value: 'John',
},
};

expect(actions.editFieldChanged('name', 'John')).toEqual(expectedAction);
});
});

describe('for saving an event program', () => {
test('should have defined the constants', () => {
expect(actions.EVENT_PROGRAM_SAVE).toBe('EVENT_PROGRAM_SAVE');
expect(actions.EVENT_PROGRAM_SAVE_SUCCESS).toBe('EVENT_PROGRAM_SAVE_SUCCESS');
expect(actions.EVENT_PROGRAM_SAVE_ERROR).toBe('EVENT_PROGRAM_SAVE_ERROR');
});

test('should create the save action when calling saveEventProgram', () => {
const expectedAction = {
type: actions.EVENT_PROGRAM_SAVE,
payload: undefined,
};

expect(actions.saveEventProgram()).toEqual(expectedAction);
});

test(
'should create the save success action when calling saveEventProgramSuccess',
() => {
const expectedAction = {
type: actions.EVENT_PROGRAM_SAVE_SUCCESS,
payload: undefined,
};

expect(actions.saveEventProgramSuccess()).toEqual(expectedAction);
}
);

test(
'should create the save error action when calling saveEventProgramError',
() => {
const expectedAction = {
type: actions.EVENT_PROGRAM_SAVE_ERROR,
payload: new Error('Could not load'),
};

expect(actions.saveEventProgramError(new Error('Could not load'))).toEqual(expectedAction);
}
);
});

describe('for notifying users', () => {
test('should defined the notification constants', () => {
expect(actions.NOTIFY_USER).toBe('NOTIFY_USER');
});

test('should create a notify user action when calling notifyUser', () => {
const expectedAction = {
type: actions.NOTIFY_USER,
payload: undefined,
};

expect(actions.notifyUser()).toEqual(expectedAction);
});
});
});
Loading

0 comments on commit f902ccf

Please sign in to comment.