Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chentsulin committed Mar 7, 2016
1 parent e7e96bd commit 6d6c2ab
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"clean": "rimraf lib dist coverage",
"lint": "eslint src test examples",
"test": "mocha --compilers js:babel-register --recursive",
"test": "mocha --compilers js:babel-register --recursive --require ./test/setup.js",
"test:watch": "npm test -- --watch",
"test:cov": "babel-node $(npm bin)/isparta cover $(npm bin)/_mocha -- --recursive",
"check": "npm run lint && npm run test ",
Expand Down Expand Up @@ -61,6 +61,8 @@
"react": "^0.14.7"
},
"dependencies": {
"babel-polyfill": "^6.6.1",
"jsdom": "^7.2.2",
"lodash": "^4.5.1"
}
}
6 changes: 6 additions & 0 deletions test/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'babel-polyfill';
import { jsdom } from 'jsdom';

global.document = jsdom('<!doctype html><html><body></body></html>');
global.window = document.defaultView;
global.navigator = global.window.navigator;
34 changes: 25 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from 'chai';
import React, { Component } from 'react';
import {
createRenderer,
Simulate,
renderIntoDocument,
} from 'react-addons-test-utils';
import windowState from '../src';

Expand All @@ -13,9 +13,7 @@ class InnerComponent extends Component {
}
}

function setup() {
const props = {};

function setup(props = {}) {
const renderer = createRenderer();

const WrappedComponent = windowState(InnerComponent);
Expand All @@ -27,19 +25,37 @@ function setup() {
const output = renderer.getRenderOutput();

return {
props: props,
output: output,
renderer: renderer,
props,
output,
renderer,
};
}

describe('windowState', () => {
it('should render inner component', () => {
const { output } = setup();
expect(output.type).to.equal('div');
expect(output.type).to.equal(InnerComponent);
});

it('should ', async () => {
it('should all be 0 when component did mount', () => {
const { output } = setup();
expect(output.props.window).to.deep.equal({ height: 0, width: 0 });
expect(output.props.document).to.deep.equal({ height: 0, width: 0 });
});

it('should change state when resize event be triggered', (done) => {
window.innerHeight = 753;
window.innerWidth = 1403;
document.body.clientHeight = 5624;
document.body.clientWidth = 1403;
const WrappedComponent = windowState(InnerComponent);
const component = renderIntoDocument(<WrappedComponent />);
window.addEventListener('resize', () => {
expect(component.state.window).to.deep.equal({ height: 753, width: 1403 });
expect(component.state.document).to.deep.equal({ height: 5624, width: 1403 });
done();
});
const { Event } = window;
window.dispatchEvent(new Event('resize'));
});
});

0 comments on commit 6d6c2ab

Please sign in to comment.