Skip to content

Commit 6fdc9e0

Browse files
committed
Tests
1 parent 3515c1c commit 6fdc9e0

File tree

4 files changed

+125
-5
lines changed

4 files changed

+125
-5
lines changed

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"test": "cross-env BABEL_ENV=commonjs nyc --require babel-register --require ./test/setup.js mocha --recursive",
1616
"test:watch": "npm test -- --watch",
1717
"test:cov": "nyc report --reporter=text-lcov | coveralls",
18-
"check:src": "npm run lint",
18+
"check:src": "npm run lint && npm run test",
1919
"build:commonjs": "cross-env BABEL_ENV=commonjs babel src --out-dir lib",
2020
"build:es": "cross-env BABEL_ENV=es babel src --out-dir es",
2121
"build:umd": "cross-env BABEL_ENV=commonjs NODE_ENV=development webpack src/index.js dist/react-cssom.js",
@@ -67,14 +67,14 @@
6767
"jsdom": "9.5.0",
6868
"mocha": "3.0.2",
6969
"nyc": "8.3.0",
70-
"react": "15.4.1",
71-
"react-addons-perf": "15.3.2",
72-
"react-addons-test-utils": "15.3.2",
70+
"react": "15.4.2",
71+
"react-addons-test-utils": "15.4.2",
72+
"react-dom": "15.4.2",
7373
"rimraf": "2.5.2",
7474
"webpack": "1.13.1"
7575
},
7676
"peerDependencies": {
77-
"react": "^15.4.1"
77+
"react": "^15.4.2"
7878
},
7979
"npmName": "react-cssom",
8080
"npmFileMap": [

test/classInjector.js

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import expect from 'expect';
2+
import React from 'react';
3+
import ReactCSSOM from '../src/index';
4+
import ReactTestUtils from 'react-addons-test-utils';
5+
6+
describe('Css class injector', () => {
7+
it('should not throw with null component', () => {
8+
//eslint-disable-next-line
9+
class Foo extends React.Component {
10+
render() {
11+
return null;
12+
}
13+
}
14+
15+
const renderer = ReactTestUtils.createRenderer();
16+
expect(() => renderer.render(<Foo />)).toNotThrow();
17+
});
18+
19+
it('should inject class in presentation components', () => {
20+
//eslint-disable-next-line
21+
class Foo extends React.Component {
22+
static propTypes = {
23+
style: React.PropTypes.string,
24+
className: React.PropTypes.string,
25+
}
26+
27+
state = {
28+
foo: 'bar',
29+
}
30+
31+
render() {
32+
return (
33+
<div {...this.props}>
34+
<p></p>
35+
</div>
36+
);
37+
}
38+
}
39+
40+
let renderer = ReactTestUtils.createRenderer();
41+
renderer.render(<Foo style="bar" />);
42+
let result = renderer.getRenderOutput();
43+
expect(result.props).toMatch({
44+
style: 'bar',
45+
className: '⚛Foo ',
46+
});
47+
48+
renderer = ReactTestUtils.createRenderer();
49+
renderer.render(<Foo className="bar" />);
50+
result = renderer.getRenderOutput();
51+
expect(result.props).toMatch({
52+
className: '⚛Foo bar',
53+
});
54+
55+
const rendered = ReactTestUtils.renderIntoDocument(<Foo />);
56+
const foo = ReactTestUtils.findRenderedComponentWithType(rendered, Foo);
57+
expect(foo.state).toEqual({
58+
foo: 'bar',
59+
});
60+
});
61+
62+
it('should inject class in container components', () => {
63+
//eslint-disable-next-line
64+
class Bar extends React.Component {
65+
render() {
66+
return (
67+
<span></span>
68+
);
69+
}
70+
}
71+
72+
//eslint-disable-next-line
73+
class Foo extends React.Component {
74+
render() {
75+
return (
76+
<Bar />
77+
);
78+
}
79+
}
80+
81+
const renderer = ReactTestUtils.createRenderer();
82+
renderer.render(<Foo />);
83+
const result = renderer.getRenderOutput();
84+
expect(result.type).toEqual('div');
85+
expect(result.props).toMatch({
86+
className: '⚛Foo',
87+
});
88+
});
89+
});

test/index.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import expect from 'expect';
2+
import { jsdom } from 'jsdom';
3+
import ReactCSSOM from '../src/index';
4+
5+
describe('Class', () => {
6+
afterEach(() => {
7+
global.document = jsdom('<!doctype html><html><head></head><body></body></html>');
8+
});
9+
10+
it('should mount', () => {
11+
expect(document.getElementsByTagName('style').length).toEqual(0);
12+
ReactCSSOM.mount('.body {}');
13+
const styles = document.getElementsByTagName('style');
14+
expect(styles.length).toEqual(1);
15+
expect(styles[0].type).toEqual('text/css');
16+
expect(styles[0].childNodes[0].data).toEqual('.body {}');
17+
});
18+
19+
it('should unmount', () => {
20+
expect(document.getElementsByTagName('style').length).toEqual(0);
21+
const style = ReactCSSOM.mount('');
22+
expect(document.getElementsByTagName('style').length).toEqual(1);
23+
ReactCSSOM.unmount(style);
24+
expect(document.getElementsByTagName('style').length).toEqual(0);
25+
});
26+
});

test/setup.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { jsdom } from 'jsdom';
2+
3+
global.document = jsdom('<!doctype html><html><head></head><body></body></html>');
4+
global.window = document.defaultView;
5+
global.navigator = global.window.navigator;

0 commit comments

Comments
 (0)