Skip to content

Commit 30ea9c9

Browse files
committed
Added tests
1 parent 8fc7a8d commit 30ea9c9

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
"build:lib": "babel src --out-dir lib",
1818
"build:dist": "webpack src/index.js dist/react-async-loading.min.js",
1919
"build:example": "webpack --config ./example/webpack.config.js",
20+
"test": "ava --verbose",
2021
"clean": "rimraf lib dist",
21-
"prepublish": "npm run clean && npm run build"
22+
"prepublish": "npm run test && npm run clean && npm run build"
2223
},
2324
"repository": {
2425
"type": "git",
@@ -29,13 +30,22 @@
2930
"dist",
3031
"src"
3132
],
33+
"ava": {
34+
"require": "./test/helpers/setup-test-env.js",
35+
"babel": "inherit"
36+
},
3237
"devDependencies": {
38+
"ava": "0.16.0",
3339
"babel-cli": "6.14.0",
3440
"babel-core": "6.14.0",
3541
"babel-loader": "6.2.5",
3642
"babel-preset-es2015": "6.14.0",
3743
"babel-preset-react": "6.11.1",
44+
"babel-register": "6.14.0",
45+
"enzyme": "2.4.1",
46+
"jsdom": "9.4.5",
3847
"react": "15.3.1",
48+
"react-addons-test-utils": "15.3.1",
3949
"react-dom": "15.3.1",
4050
"rimraf": "2.5.4",
4151
"webpack": "2.1.0-beta.21"

test/asyncComponent.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import test from 'ava';
2+
import React from 'react';
3+
import { mount, shallow } from 'enzyme';
4+
import asyncComponent from '../src/asyncComponent.js';
5+
6+
const TestComponent = () => <div className='test-component'>TEST</div>;
7+
8+
test('asyncComponent: Test placeholder', t => {
9+
const Container = asyncComponent(() => Promise.resolve(null), { placeholder: <TestComponent /> });
10+
const tree = shallow(<Container />);
11+
12+
t.true(tree.contains(<TestComponent />));
13+
});
14+
15+
test('asyncComponent: Test loading module', t => {
16+
const promise = Promise.resolve(TestComponent);
17+
const Container = asyncComponent(() => promise);
18+
const tree = mount(<Container />);
19+
20+
return promise.then(() => {
21+
t.true(tree.contains(<TestComponent />));
22+
});
23+
});
24+
25+
test('asyncComponent: Test unmount', t => {
26+
const promise = Promise.resolve(TestComponent);
27+
const Container = asyncComponent(() => promise);
28+
const tree = mount(<Container />);
29+
30+
tree.unmount();
31+
32+
return promise.then(() => {
33+
t.is(tree.type(), undefined);
34+
});
35+
});

test/helpers/setup-test-env.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require('babel-register')
2+
3+
global.document = require('jsdom').jsdom('<body></body>')
4+
global.window = document.defaultView
5+
global.navigator = window.navigator

0 commit comments

Comments
 (0)