Skip to content

Commit

Permalink
Mocha Test suite (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
gor181 committed Dec 6, 2016
1 parent 9369ebb commit e3978ab
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 50 deletions.
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,26 @@
"babel-preset-react": "^6.11.1",
"babel-preset-stage-1": "^6.13.0",
"babel-preset-stage-2": "^6.13.0",
"babel-register": "^6.18.0",
"babelify": "^7.3.0",
"brfs": "^1.4.3",
"chai": "^3.5.0",
"enzyme": "^2.4.1",
"eslint": "^1.6.0",
"eslint-plugin-react": "^3.5.1",
"gulp": "^3.9.0",
"jest": "^14.1.0",
"jest-cli": "^14.1.0",
"jsdom": "^9.8.3",
"lodash": "^4.14.2",
"mocha": "^3.2.0",
"react": "^0.14 || ^15.0.0-rc || ^15.0",
"react-addons-test-utils": "^15.3.1",
"react-component-gulp-tasks": "git+https://github.com/gor181/react-component-gulp-tasks.git",
"react-dom": "^0.14 || ^15.0.0-rc || ^15.0",
"react-notification-system": "^0.2.7",
"react-redux": "^4.4.5",
"redux": "^3.5.2"
"redux": "^3.5.2",
"sinon": "^1.17.6"
},
"dependencies": {
"react-notification-system": "^0.2.7"
Expand All @@ -56,14 +60,12 @@
"publish:site": "NODE_ENV=production gulp publish:examples",
"release": "NODE_ENV=production gulp release",
"start": "gulp dev",
"test": "jest",
"test": "mocha test/__tests__/**/*",
"test-dev": "mocha test/__tests__/**/* --watch",
"watch": "gulp watch:lib"
},
"keywords": [
"react",
"react-component"
],
"jest": {
"automock": false
}
"react-notification-system",
"redux"
]
}
22 changes: 0 additions & 22 deletions src/__tests__/actions.js

This file was deleted.

8 changes: 0 additions & 8 deletions src/__tests__/const.js

This file was deleted.

23 changes: 23 additions & 0 deletions test/__tests__/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { expect } from 'chai';
import * as Actions from '../../src/actions';

describe('actions', () => {
it('sets the correct notification level', () => {
expect(Actions.success().level).to.equal('success');
expect(Actions.warning().level).to.equal('warning');
expect(Actions.info().level).to.equal('info');
expect(Actions.error().level).to.equal('error');
});

it('accepts custom opts', () => {
expect(Actions.success({ custom: true }).custom).to.be.ok;
});

it('generates random uid when not provided', () => {
expect(Actions.success().uid).to.be.defined;
});

if('sets the custom uid when provided', () => {
expect(Actions.success({ uid: 1 }).uid).to.equal(1);
});
});
9 changes: 9 additions & 0 deletions test/__tests__/const.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expect } from 'chai';
import * as Constants from '../../src/const';

describe('constants', () => {
it('should be defined', () => {
expect(Constants.RNS_SHOW_NOTIFICATION).to.be.defined;
expect(Constants.RNS_HIDE_NOTIFICATION).to.be.defined;
});
});
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import React from 'react';
import { shallow } from 'enzyme';
import Component from '../notifications';
import { expect } from 'chai';
import sinon from 'sinon';
import Component from '../../src/notifications';
import NotifySystem from 'react-notification-system';

describe('NotificationsComponent', () => {
it('should render one <NotifySystem /> component', () => {
const wrapper = shallow(<Component />);
expect(wrapper.children()).toBeDefined();
expect(wrapper.children()).to.exist;
});

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

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

expect(warning).toMatch(/Invalid prop `notifications` of type `number` supplied to `Notifications`, expected `array`./);
expect(warning).to.match(/Invalid prop `notifications` of type `number` supplied to `Notifications`, expected `array`./);

c.restore();
});
});
14 changes: 8 additions & 6 deletions src/__tests__/reducer.js → test/__tests__/reducer.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import Reducer from '../reducer';
import * as Actions from '../actions';
import { expect } from 'chai';

import Reducer from '../../src/reducer';
import * as Actions from '../../src/actions';

describe('reducer', () => {
it('initializes state with an array', () => {
expect(Reducer()).toEqual([]);
expect(Reducer()).to.deep.equal([]);
});

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

expect(state.length).toEqual(1);
expect(state.length).to.equal(1);
});

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

const state = Reducer([], Actions.success({ uid }));
expect(state.length).toEqual(1);
expect(state.length).to.equal(1);

const newState = Reducer(state, Actions.hide(uid));
expect(newState.length).toEqual(0);
expect(newState.length).to.equal(0);
});
});
3 changes: 3 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--compilers js:babel-register
--require test/utils/dom.js
--reporter spec
34 changes: 34 additions & 0 deletions test/utils/dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const jsdom = require('jsdom');

// setup the simplest document possible
const doc = jsdom.jsdom('<!doctype html><html><body></body></html>');

// get the window object out of the document
const win = doc.defaultView;

// set globals for mocha that make access to document and window feel
// natural in the test environment
global.document = doc;
global.window = win;

//JSDOM doesn't support localStrage by default, so lets just fake it..
if (!global.window.localStorage) {
global.window.localStorage = {
getItem() { return '{}'; },
setItem() {}
};
}

// take all properties of the window object and also attach it to the
// mocha global object
propagateToGlobal(win);

// from mocha-jsdom https://github.com/rstacruz/mocha-jsdom/blob/master/index.js#L80
function propagateToGlobal (window) {
for (let key in window) {
if (!window.hasOwnProperty(key)) continue;
if (key in global) continue;

global[key] = window[key];
}
}

0 comments on commit e3978ab

Please sign in to comment.