Skip to content

Commit 756d7e0

Browse files
Pavlo AksonovPavlo Aksonov
authored andcommitted
add unit tests
1 parent b1d23ab commit 756d7e0

File tree

11 files changed

+719
-56
lines changed

11 files changed

+719
-56
lines changed

.idea/workspace.xml

Lines changed: 367 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"react-native": "^0.11.4",
1010
"react-native-button": "^1.2.1",
1111
"react-native-navbar": "^0.8.0",
12-
"react-native-router-flux": "^0.2.0",
13-
"react-native-tabs": "^0.0.3"
12+
"react-native-router-flux": "^0.2.4",
13+
"react-native-tabs": "^0.0.5"
1414
}
1515
}

__mocks__/react-native.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
var React = require('react');
3+
4+
var ReactNative = React;
5+
ReactNative.StyleSheet = {
6+
create: function(styles) {
7+
return styles;
8+
}
9+
};
10+
class View extends React.Component {
11+
render(){
12+
return <div {...this.props}/>
13+
}
14+
}
15+
16+
class Navigator extends React.Component {
17+
constructor(props){
18+
super(props);
19+
this._currentRoutes = [props.initialRoute];
20+
this.state = {route : props.initialRoute};
21+
}
22+
getCurrentRoutes(){
23+
return this._currentRoutes;
24+
}
25+
push(route){
26+
this._currentRoutes.push(route);
27+
this.setState({route: route});
28+
}
29+
immediatelyResetRouteStack(routes){
30+
this._currentRoutes = routes;
31+
}
32+
popToRoute(route){
33+
while (this._currentRoutes[this._currentRoutes.length-1] != route){
34+
this._currentRoutes.pop();
35+
}
36+
this.setState({route: route});
37+
}
38+
render(){
39+
return this.props.renderScene(this.state.route, this);
40+
}
41+
}
42+
ReactNative.View = View;
43+
ReactNative.Navigator = Navigator;
44+
45+
module.exports = ReactNative;

__tests__/router-tests.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
jest.setMock('../Animations', {FlatFloatFromRight:{}, FlatFloatFromBottom:{}, None:{}});
2+
jest.setMock('alt/components/AltNativeContainer');
3+
var React = require('react/addons');
4+
var TestUtils = React.addons.TestUtils;
5+
jest.dontMock('../store');
6+
jest.dontMock('../actions');
7+
jest.dontMock('../index');
8+
9+
var Actions = require('../actions');
10+
11+
var {Router, Route, Schema, Action} = require('../index');
12+
13+
describe('Router', function() {
14+
it('route', function () {
15+
var router = TestUtils.renderIntoDocument(
16+
<Router>
17+
<Schema name="default" navBar="navBar1" customProp="a"/>
18+
<Schema name="modal" navBar="navBar2" customProp="b" ownProp="c"/>
19+
20+
<Route name="launch" component="launchComponent" hideNavBar={true} customProp="a"/>
21+
<Route name="signin" component="signinComponent" schema="modal"/>
22+
<Route name="signup" component="signupComponent"/>
23+
<Route name="main" component="mainComponent"/>
24+
</Router>
25+
);
26+
expect(router.refs.nav.props.initialRoute.name).toEqual('launch');
27+
var len = router.refs.nav.getCurrentRoutes().length;
28+
expect(len).toEqual(1);
29+
var launchComponent = TestUtils.findRenderedDOMComponentWithTag(
30+
router, 'launchComponent');
31+
32+
expect(launchComponent.props.customProp).toEqual("a");
33+
34+
expect(function(){TestUtils.findRenderedDOMComponentWithTag(
35+
router, 'navBar1')}).toThrow(new Error("Did not find exactly one match for tag:navBar1"));
36+
expect(function(){TestUtils.findRenderedDOMComponentWithTag(
37+
router, 'signinComponent')}).toThrow(new Error("Did not find exactly one match for tag:signinComponent"));
38+
39+
Actions.signin("Hello world!");
40+
len = router.refs.nav.getCurrentRoutes().length;
41+
expect(len).toEqual(2);
42+
expect(router.refs.nav.getCurrentRoutes()[len-1].name).toEqual('signin');
43+
expect(router.refs.nav.getCurrentRoutes()[len-1].passProps.data).toEqual("Hello world!");
44+
45+
Actions.signin({data:"Hello world2!", id:1});
46+
len = router.refs.nav.getCurrentRoutes().length;
47+
expect(len).toEqual(3);
48+
expect(router.refs.nav.getCurrentRoutes()[len-1].name).toEqual('signin');
49+
expect(router.refs.nav.getCurrentRoutes()[len-1].passProps.data).toEqual("Hello world2!");
50+
expect(router.refs.nav.getCurrentRoutes()[len-1].passProps.id).toEqual(1);
51+
52+
var signinComponent = TestUtils.findRenderedDOMComponentWithTag(
53+
router, 'signinComponent');
54+
55+
var navBar = TestUtils.findRenderedDOMComponentWithTag(
56+
router, 'navBar2');
57+
58+
expect(React.findDOMNode(signinComponent).data).toEqual('Hello world2!');
59+
expect(navBar.props.customProp).toEqual("b");
60+
expect(navBar.props.ownProp).toEqual("c");
61+
expect(navBar.props.data).toEqual('Hello world2!');
62+
expect(navBar.props.id).toEqual(1);
63+
64+
Actions.main();
65+
len = router.refs.nav.getCurrentRoutes().length;
66+
expect(len).toEqual(4);
67+
expect(router.refs.nav.getCurrentRoutes()[len-1].name).toEqual('main');
68+
expect(router.refs.nav.getCurrentRoutes()[len-1].passProps.data).toEqual(undefined);
69+
expect(router.refs.nav.getCurrentRoutes()[len-1].passProps.id).toEqual(undefined);
70+
71+
expect(function(){TestUtils.findRenderedDOMComponentWithTag(
72+
router, 'navBar2')}).toThrow(new Error("Did not find exactly one match for tag:navBar2"));
73+
navBar = TestUtils.findRenderedDOMComponentWithTag(
74+
router, 'navBar1');
75+
76+
expect(navBar.props.customProp).toEqual("a");
77+
expect(navBar.props.ownProp).toEqual(undefined);
78+
79+
Actions.pop(2);
80+
len = router.refs.nav.getCurrentRoutes().length;
81+
expect(len).toEqual(2);
82+
83+
expect(router.refs.nav.getCurrentRoutes()[len-1].name).toEqual('signin');
84+
expect(router.refs.nav.getCurrentRoutes()[len-1].passProps.data).toEqual("Hello world!");
85+
expect(router.refs.nav.getCurrentRoutes()[len-1].passProps.id).toEqual(undefined);
86+
87+
Actions.pop();
88+
len = router.refs.nav.getCurrentRoutes().length;
89+
expect(len).toEqual(1);
90+
91+
expect(router.refs.nav.getCurrentRoutes()[len-1].name).toEqual('launch');
92+
launchComponent = TestUtils.findRenderedDOMComponentWithTag(
93+
router, 'launchComponent');
94+
95+
expect(launchComponent.props.customProp).toEqual("a");
96+
97+
});
98+
});

__tests__/store-tests.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
jest.dontMock('../store');
2+
jest.dontMock('../actions');
3+
var actions = require('../actions');
4+
var alt = require('../alt');
5+
var PageStore = require('../store');
6+
7+
describe('PageStore', function() {
8+
it('init action', function() {
9+
var state = PageStore.getState();
10+
expect(state.routes.length).toBe(0);
11+
expect(state.currentRoute).toBe(null);
12+
13+
actions.init("launch");
14+
state = PageStore.getState();
15+
expect(state.routes.length).toBe(1);
16+
expect(state.routes[0]).toBe("launch");
17+
expect(state.currentRoute).toBe("launch");
18+
19+
actions.init("home");
20+
state = PageStore.getState();
21+
expect(state.routes.length).toBe(1);
22+
expect(state.routes[0]).toBe("home");
23+
expect(state.currentRoute).toBe("home");
24+
});
25+
26+
it('push action', function() {
27+
actions.init("init");
28+
29+
var state = PageStore.getState();
30+
expect(state.routes.length).toBe(1);
31+
expect(state.currentRoute).toBe("init");
32+
33+
actions.push({name: "launch", data: "test"});
34+
state = PageStore.getState();
35+
expect(state.routes.length).toBe(2);
36+
expect(state.routes[1]).toBe("launch");
37+
expect(state.currentRoute).toBe("launch");
38+
expect(state.mode).toBe("push");
39+
expect(state.data).toBe("test");
40+
41+
actions.push({name: "home", data: "homeData"});
42+
state = PageStore.getState();
43+
expect(state.routes.length).toBe(3);
44+
expect(state.routes[2]).toBe("home");
45+
expect(state.mode).toBe("push");
46+
expect(state.data).toBe("homeData");
47+
expect(state.currentRoute).toBe("home");
48+
});
49+
50+
it('pop action', function() {
51+
actions.push({name: "launch", data: "test"});
52+
actions.push({name: "home"});
53+
actions.push({name: "home2"});
54+
actions.push({name: "home3"});
55+
actions.push({name: "home4"});
56+
var state = PageStore.getState();
57+
expect(state.currentRoute).toBe("home4");
58+
actions.pop();
59+
state = PageStore.getState();
60+
expect(state.currentRoute).toBe("home3");
61+
expect(state.mode).toBe("pop");
62+
expect(state.num).toBe(1);
63+
64+
actions.pop(2);
65+
state = PageStore.getState();
66+
expect(state.currentRoute).toBe("home");
67+
expect(state.mode).toBe("pop");
68+
expect(state.num).toBe(2);
69+
70+
expect(state.customData).toBe(undefined);
71+
actions.pop({customData: 'customData'});
72+
state = PageStore.getState();
73+
expect(state.currentRoute).toBe("launch");
74+
expect(state.customData).toBe("customData");
75+
});
76+
77+
});

actions.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ function filterParam(data){
1919
}
2020

2121
class Actions {
22+
constructor(){
23+
24+
}
2225
push(data){
2326
this.dispatch(filterParam(data));
2427
}

jestSupport/env.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
'use strict';
10+
11+
window.__DEV__ = true;
12+
window.Env = {};
13+
14+
require.requireActual('./setupEnvPolyfills');

jestSupport/scriptPreprocess.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
// Babel is an ES6 transpiler. Read more about it at https://babeljs.io/
4+
var babel = require('babel-core');
5+
6+
module.exports = {
7+
process: function (src, filename) {
8+
// Don't transpile node modules.
9+
if (filename.match(/node_modules/)) {
10+
return src;
11+
}
12+
13+
var result = babel.transform(src, {filename: filename});
14+
return result.code;
15+
}
16+
};

jestSupport/setupEnvPolyfills.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* This pipes all of our console logging functions to native logging so that
10+
* JavaScript errors in required modules show up in Xcode via NSLog.
11+
*/
12+
13+
'use strict';
14+
15+
// WARNING: This is an optimized version that fails on hasOwnProperty checks
16+
// and non objects. It's not spec-compliant. It's a perf optimization.
17+
18+
Object.assign = function(target, sources) {
19+
if (__DEV__) {
20+
if (target == null) {
21+
throw new TypeError('Object.assign target cannot be null or undefined');
22+
}
23+
if (typeof target !== 'object' && typeof target !== 'function') {
24+
throw new TypeError(
25+
'In this environment the target of assign MUST be an object.' +
26+
'This error is a performance optimization and not spec compliant.'
27+
);
28+
}
29+
}
30+
31+
for (var nextIndex = 1; nextIndex < arguments.length; nextIndex++) {
32+
var nextSource = arguments[nextIndex];
33+
if (nextSource == null) {
34+
continue;
35+
}
36+
37+
if (__DEV__) {
38+
if (typeof nextSource !== 'object' &&
39+
typeof nextSource !== 'function') {
40+
throw new TypeError(
41+
'In this environment the target of assign MUST be an object.' +
42+
'This error is a performance optimization and not spec compliant.'
43+
);
44+
}
45+
}
46+
47+
// We don't currently support accessors nor proxies. Therefore this
48+
// copy cannot throw. If we ever supported this then we must handle
49+
// exceptions and side-effects.
50+
51+
for (var key in nextSource) {
52+
if (__DEV__) {
53+
var hasOwnProperty = Object.prototype.hasOwnProperty;
54+
if (!hasOwnProperty.call(nextSource, key)) {
55+
throw new TypeError(
56+
'One of the sources to assign has an enumerable key on the ' +
57+
'prototype chain. This is an edge case that we do not support. ' +
58+
'This error is a performance optimization and not spec compliant.'
59+
);
60+
}
61+
}
62+
target[key] = nextSource[key];
63+
}
64+
}
65+
66+
return target;
67+
};

package.json

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "react-native-router-flux",
3-
"version": "0.2.3",
3+
"version": "0.2.4",
44
"description": "React Native Router using Flux architecture",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "jest"
88
},
99
"keywords": [
1010
"react-native",
@@ -45,5 +45,30 @@
4545
],
4646
"directories": {},
4747
"_resolved": "https://registry.npmjs.org/react-native-router-flux/-/react-native-router-flux-0.1.8.tgz",
48-
"readme": "ERROR: No README data found!"
48+
"readme": "ERROR: No README data found!",
49+
"devDependencies": {
50+
"alt": "^0.17.3",
51+
"babel-core": "^5.8.25",
52+
"jest-cli": "^0.5.8",
53+
"react": "^0.13.3",
54+
"react-tools": "^0.13.3"
55+
},
56+
"jest": {
57+
"unmockedModulePathPatterns": [
58+
"react",
59+
"promise",
60+
"node_modules/alt",
61+
"alt.js",
62+
"source-map"
63+
],
64+
"scriptPreprocessor": "jestSupport/scriptPreprocess.js",
65+
"setupEnvScriptFile": "jestSupport/env.js",
66+
"testPathIgnorePatterns": [
67+
"/node_modules/",
68+
"packager/react-packager/src/Activity/"
69+
],
70+
"testFileExtensions": [
71+
"js"
72+
]
73+
}
4974
}

0 commit comments

Comments
 (0)