Skip to content

Commit e3978ab

Browse files
authored
Mocha Test suite (#21)
1 parent 9369ebb commit e3978ab

File tree

9 files changed

+97
-50
lines changed

9 files changed

+97
-50
lines changed

package.json

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,26 @@
2222
"babel-preset-react": "^6.11.1",
2323
"babel-preset-stage-1": "^6.13.0",
2424
"babel-preset-stage-2": "^6.13.0",
25+
"babel-register": "^6.18.0",
2526
"babelify": "^7.3.0",
2627
"brfs": "^1.4.3",
28+
"chai": "^3.5.0",
2729
"enzyme": "^2.4.1",
2830
"eslint": "^1.6.0",
2931
"eslint-plugin-react": "^3.5.1",
3032
"gulp": "^3.9.0",
31-
"jest": "^14.1.0",
3233
"jest-cli": "^14.1.0",
34+
"jsdom": "^9.8.3",
3335
"lodash": "^4.14.2",
36+
"mocha": "^3.2.0",
3437
"react": "^0.14 || ^15.0.0-rc || ^15.0",
3538
"react-addons-test-utils": "^15.3.1",
3639
"react-component-gulp-tasks": "git+https://github.com/gor181/react-component-gulp-tasks.git",
3740
"react-dom": "^0.14 || ^15.0.0-rc || ^15.0",
3841
"react-notification-system": "^0.2.7",
3942
"react-redux": "^4.4.5",
40-
"redux": "^3.5.2"
43+
"redux": "^3.5.2",
44+
"sinon": "^1.17.6"
4145
},
4246
"dependencies": {
4347
"react-notification-system": "^0.2.7"
@@ -56,14 +60,12 @@
5660
"publish:site": "NODE_ENV=production gulp publish:examples",
5761
"release": "NODE_ENV=production gulp release",
5862
"start": "gulp dev",
59-
"test": "jest",
63+
"test": "mocha test/__tests__/**/*",
64+
"test-dev": "mocha test/__tests__/**/* --watch",
6065
"watch": "gulp watch:lib"
6166
},
6267
"keywords": [
63-
"react",
64-
"react-component"
65-
],
66-
"jest": {
67-
"automock": false
68-
}
68+
"react-notification-system",
69+
"redux"
70+
]
6971
}

src/__tests__/actions.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/__tests__/const.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

test/__tests__/actions.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { expect } from 'chai';
2+
import * as Actions from '../../src/actions';
3+
4+
describe('actions', () => {
5+
it('sets the correct notification level', () => {
6+
expect(Actions.success().level).to.equal('success');
7+
expect(Actions.warning().level).to.equal('warning');
8+
expect(Actions.info().level).to.equal('info');
9+
expect(Actions.error().level).to.equal('error');
10+
});
11+
12+
it('accepts custom opts', () => {
13+
expect(Actions.success({ custom: true }).custom).to.be.ok;
14+
});
15+
16+
it('generates random uid when not provided', () => {
17+
expect(Actions.success().uid).to.be.defined;
18+
});
19+
20+
if('sets the custom uid when provided', () => {
21+
expect(Actions.success({ uid: 1 }).uid).to.equal(1);
22+
});
23+
});

test/__tests__/const.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { expect } from 'chai';
2+
import * as Constants from '../../src/const';
3+
4+
describe('constants', () => {
5+
it('should be defined', () => {
6+
expect(Constants.RNS_SHOW_NOTIFICATION).to.be.defined;
7+
expect(Constants.RNS_HIDE_NOTIFICATION).to.be.defined;
8+
});
9+
});
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import React from 'react';
22
import { shallow } from 'enzyme';
3-
import Component from '../notifications';
3+
import { expect } from 'chai';
4+
import sinon from 'sinon';
5+
import Component from '../../src/notifications';
46
import NotifySystem from 'react-notification-system';
57

68
describe('NotificationsComponent', () => {
79
it('should render one <NotifySystem /> component', () => {
810
const wrapper = shallow(<Component />);
9-
expect(wrapper.children()).toBeDefined();
11+
expect(wrapper.children()).to.exist;
1012
});
1113

1214
it('should warn if prop:notifications is not array', () => {
13-
spyOn(console, 'error');
15+
const c = sinon.stub(console, 'error');
1416

1517
const wrapper = shallow(<Component notifications={1} />);
16-
const warning = console.error.calls.argsFor(0)[0];
18+
const warning = c.args[0][0];
1719

18-
expect(warning).toMatch(/Invalid prop `notifications` of type `number` supplied to `Notifications`, expected `array`./);
20+
expect(warning).to.match(/Invalid prop `notifications` of type `number` supplied to `Notifications`, expected `array`./);
21+
22+
c.restore();
1923
});
2024
});
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
import Reducer from '../reducer';
2-
import * as Actions from '../actions';
1+
import { expect } from 'chai';
2+
3+
import Reducer from '../../src/reducer';
4+
import * as Actions from '../../src/actions';
35

46
describe('reducer', () => {
57
it('initializes state with an array', () => {
6-
expect(Reducer()).toEqual([]);
8+
expect(Reducer()).to.deep.equal([]);
79
});
810

911
it('stores the notification to state', () => {
1012
const action = Actions.success();
1113
const state = Reducer([], action);
1214

13-
expect(state.length).toEqual(1);
15+
expect(state.length).to.equal(1);
1416
});
1517

1618
it('stores and removes notification', () => {
1719
const uid = 1;
1820

1921
const state = Reducer([], Actions.success({ uid }));
20-
expect(state.length).toEqual(1);
22+
expect(state.length).to.equal(1);
2123

2224
const newState = Reducer(state, Actions.hide(uid));
23-
expect(newState.length).toEqual(0);
25+
expect(newState.length).to.equal(0);
2426
});
2527
});

test/mocha.opts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--compilers js:babel-register
2+
--require test/utils/dom.js
3+
--reporter spec

test/utils/dom.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const jsdom = require('jsdom');
2+
3+
// setup the simplest document possible
4+
const doc = jsdom.jsdom('<!doctype html><html><body></body></html>');
5+
6+
// get the window object out of the document
7+
const win = doc.defaultView;
8+
9+
// set globals for mocha that make access to document and window feel
10+
// natural in the test environment
11+
global.document = doc;
12+
global.window = win;
13+
14+
//JSDOM doesn't support localStrage by default, so lets just fake it..
15+
if (!global.window.localStorage) {
16+
global.window.localStorage = {
17+
getItem() { return '{}'; },
18+
setItem() {}
19+
};
20+
}
21+
22+
// take all properties of the window object and also attach it to the
23+
// mocha global object
24+
propagateToGlobal(win);
25+
26+
// from mocha-jsdom https://github.com/rstacruz/mocha-jsdom/blob/master/index.js#L80
27+
function propagateToGlobal (window) {
28+
for (let key in window) {
29+
if (!window.hasOwnProperty(key)) continue;
30+
if (key in global) continue;
31+
32+
global[key] = window[key];
33+
}
34+
}

0 commit comments

Comments
 (0)