Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavlo Aksonov authored and Pavlo Aksonov committed Oct 1, 2015
1 parent b1d23ab commit 756d7e0
Show file tree
Hide file tree
Showing 11 changed files with 719 additions and 56 deletions.
418 changes: 367 additions & 51 deletions .idea/workspace.xml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"react-native": "^0.11.4",
"react-native-button": "^1.2.1",
"react-native-navbar": "^0.8.0",
"react-native-router-flux": "^0.2.0",
"react-native-tabs": "^0.0.3"
"react-native-router-flux": "^0.2.4",
"react-native-tabs": "^0.0.5"
}
}
45 changes: 45 additions & 0 deletions __mocks__/react-native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';
var React = require('react');

var ReactNative = React;
ReactNative.StyleSheet = {
create: function(styles) {
return styles;
}
};
class View extends React.Component {
render(){
return <div {...this.props}/>
}
}

class Navigator extends React.Component {
constructor(props){
super(props);
this._currentRoutes = [props.initialRoute];
this.state = {route : props.initialRoute};
}
getCurrentRoutes(){
return this._currentRoutes;
}
push(route){
this._currentRoutes.push(route);
this.setState({route: route});
}
immediatelyResetRouteStack(routes){
this._currentRoutes = routes;
}
popToRoute(route){
while (this._currentRoutes[this._currentRoutes.length-1] != route){
this._currentRoutes.pop();
}
this.setState({route: route});
}
render(){
return this.props.renderScene(this.state.route, this);
}
}
ReactNative.View = View;
ReactNative.Navigator = Navigator;

module.exports = ReactNative;
98 changes: 98 additions & 0 deletions __tests__/router-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
jest.setMock('../Animations', {FlatFloatFromRight:{}, FlatFloatFromBottom:{}, None:{}});
jest.setMock('alt/components/AltNativeContainer');
var React = require('react/addons');
var TestUtils = React.addons.TestUtils;
jest.dontMock('../store');
jest.dontMock('../actions');
jest.dontMock('../index');

var Actions = require('../actions');

var {Router, Route, Schema, Action} = require('../index');

describe('Router', function() {
it('route', function () {
var router = TestUtils.renderIntoDocument(
<Router>
<Schema name="default" navBar="navBar1" customProp="a"/>
<Schema name="modal" navBar="navBar2" customProp="b" ownProp="c"/>

<Route name="launch" component="launchComponent" hideNavBar={true} customProp="a"/>
<Route name="signin" component="signinComponent" schema="modal"/>
<Route name="signup" component="signupComponent"/>
<Route name="main" component="mainComponent"/>
</Router>
);
expect(router.refs.nav.props.initialRoute.name).toEqual('launch');
var len = router.refs.nav.getCurrentRoutes().length;
expect(len).toEqual(1);
var launchComponent = TestUtils.findRenderedDOMComponentWithTag(
router, 'launchComponent');

expect(launchComponent.props.customProp).toEqual("a");

expect(function(){TestUtils.findRenderedDOMComponentWithTag(
router, 'navBar1')}).toThrow(new Error("Did not find exactly one match for tag:navBar1"));
expect(function(){TestUtils.findRenderedDOMComponentWithTag(
router, 'signinComponent')}).toThrow(new Error("Did not find exactly one match for tag:signinComponent"));

Actions.signin("Hello world!");
len = router.refs.nav.getCurrentRoutes().length;
expect(len).toEqual(2);
expect(router.refs.nav.getCurrentRoutes()[len-1].name).toEqual('signin');
expect(router.refs.nav.getCurrentRoutes()[len-1].passProps.data).toEqual("Hello world!");

Actions.signin({data:"Hello world2!", id:1});
len = router.refs.nav.getCurrentRoutes().length;
expect(len).toEqual(3);
expect(router.refs.nav.getCurrentRoutes()[len-1].name).toEqual('signin');
expect(router.refs.nav.getCurrentRoutes()[len-1].passProps.data).toEqual("Hello world2!");
expect(router.refs.nav.getCurrentRoutes()[len-1].passProps.id).toEqual(1);

var signinComponent = TestUtils.findRenderedDOMComponentWithTag(
router, 'signinComponent');

var navBar = TestUtils.findRenderedDOMComponentWithTag(
router, 'navBar2');

expect(React.findDOMNode(signinComponent).data).toEqual('Hello world2!');
expect(navBar.props.customProp).toEqual("b");
expect(navBar.props.ownProp).toEqual("c");
expect(navBar.props.data).toEqual('Hello world2!');
expect(navBar.props.id).toEqual(1);

Actions.main();
len = router.refs.nav.getCurrentRoutes().length;
expect(len).toEqual(4);
expect(router.refs.nav.getCurrentRoutes()[len-1].name).toEqual('main');
expect(router.refs.nav.getCurrentRoutes()[len-1].passProps.data).toEqual(undefined);
expect(router.refs.nav.getCurrentRoutes()[len-1].passProps.id).toEqual(undefined);

expect(function(){TestUtils.findRenderedDOMComponentWithTag(
router, 'navBar2')}).toThrow(new Error("Did not find exactly one match for tag:navBar2"));
navBar = TestUtils.findRenderedDOMComponentWithTag(
router, 'navBar1');

expect(navBar.props.customProp).toEqual("a");
expect(navBar.props.ownProp).toEqual(undefined);

Actions.pop(2);
len = router.refs.nav.getCurrentRoutes().length;
expect(len).toEqual(2);

expect(router.refs.nav.getCurrentRoutes()[len-1].name).toEqual('signin');
expect(router.refs.nav.getCurrentRoutes()[len-1].passProps.data).toEqual("Hello world!");
expect(router.refs.nav.getCurrentRoutes()[len-1].passProps.id).toEqual(undefined);

Actions.pop();
len = router.refs.nav.getCurrentRoutes().length;
expect(len).toEqual(1);

expect(router.refs.nav.getCurrentRoutes()[len-1].name).toEqual('launch');
launchComponent = TestUtils.findRenderedDOMComponentWithTag(
router, 'launchComponent');

expect(launchComponent.props.customProp).toEqual("a");

});
});
77 changes: 77 additions & 0 deletions __tests__/store-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
jest.dontMock('../store');
jest.dontMock('../actions');
var actions = require('../actions');
var alt = require('../alt');
var PageStore = require('../store');

describe('PageStore', function() {
it('init action', function() {
var state = PageStore.getState();
expect(state.routes.length).toBe(0);
expect(state.currentRoute).toBe(null);

actions.init("launch");
state = PageStore.getState();
expect(state.routes.length).toBe(1);
expect(state.routes[0]).toBe("launch");
expect(state.currentRoute).toBe("launch");

actions.init("home");
state = PageStore.getState();
expect(state.routes.length).toBe(1);
expect(state.routes[0]).toBe("home");
expect(state.currentRoute).toBe("home");
});

it('push action', function() {
actions.init("init");

var state = PageStore.getState();
expect(state.routes.length).toBe(1);
expect(state.currentRoute).toBe("init");

actions.push({name: "launch", data: "test"});
state = PageStore.getState();
expect(state.routes.length).toBe(2);
expect(state.routes[1]).toBe("launch");
expect(state.currentRoute).toBe("launch");
expect(state.mode).toBe("push");
expect(state.data).toBe("test");

actions.push({name: "home", data: "homeData"});
state = PageStore.getState();
expect(state.routes.length).toBe(3);
expect(state.routes[2]).toBe("home");
expect(state.mode).toBe("push");
expect(state.data).toBe("homeData");
expect(state.currentRoute).toBe("home");
});

it('pop action', function() {
actions.push({name: "launch", data: "test"});
actions.push({name: "home"});
actions.push({name: "home2"});
actions.push({name: "home3"});
actions.push({name: "home4"});
var state = PageStore.getState();
expect(state.currentRoute).toBe("home4");
actions.pop();
state = PageStore.getState();
expect(state.currentRoute).toBe("home3");
expect(state.mode).toBe("pop");
expect(state.num).toBe(1);

actions.pop(2);
state = PageStore.getState();
expect(state.currentRoute).toBe("home");
expect(state.mode).toBe("pop");
expect(state.num).toBe(2);

expect(state.customData).toBe(undefined);
actions.pop({customData: 'customData'});
state = PageStore.getState();
expect(state.currentRoute).toBe("launch");
expect(state.customData).toBe("customData");
});

});
3 changes: 3 additions & 0 deletions actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ function filterParam(data){
}

class Actions {
constructor(){

}
push(data){
this.dispatch(filterParam(data));
}
Expand Down
14 changes: 14 additions & 0 deletions jestSupport/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

window.__DEV__ = true;
window.Env = {};

require.requireActual('./setupEnvPolyfills');
16 changes: 16 additions & 0 deletions jestSupport/scriptPreprocess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

// Babel is an ES6 transpiler. Read more about it at https://babeljs.io/
var babel = require('babel-core');

module.exports = {
process: function (src, filename) {
// Don't transpile node modules.
if (filename.match(/node_modules/)) {
return src;
}

var result = babel.transform(src, {filename: filename});
return result.code;
}
};
67 changes: 67 additions & 0 deletions jestSupport/setupEnvPolyfills.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* This pipes all of our console logging functions to native logging so that
* JavaScript errors in required modules show up in Xcode via NSLog.
*/

'use strict';

// WARNING: This is an optimized version that fails on hasOwnProperty checks
// and non objects. It's not spec-compliant. It's a perf optimization.

Object.assign = function(target, sources) {
if (__DEV__) {
if (target == null) {
throw new TypeError('Object.assign target cannot be null or undefined');
}
if (typeof target !== 'object' && typeof target !== 'function') {
throw new TypeError(
'In this environment the target of assign MUST be an object.' +
'This error is a performance optimization and not spec compliant.'
);
}
}

for (var nextIndex = 1; nextIndex < arguments.length; nextIndex++) {
var nextSource = arguments[nextIndex];
if (nextSource == null) {
continue;
}

if (__DEV__) {
if (typeof nextSource !== 'object' &&
typeof nextSource !== 'function') {
throw new TypeError(
'In this environment the target of assign MUST be an object.' +
'This error is a performance optimization and not spec compliant.'
);
}
}

// We don't currently support accessors nor proxies. Therefore this
// copy cannot throw. If we ever supported this then we must handle
// exceptions and side-effects.

for (var key in nextSource) {
if (__DEV__) {
var hasOwnProperty = Object.prototype.hasOwnProperty;
if (!hasOwnProperty.call(nextSource, key)) {
throw new TypeError(
'One of the sources to assign has an enumerable key on the ' +
'prototype chain. This is an edge case that we do not support. ' +
'This error is a performance optimization and not spec compliant.'
);
}
}
target[key] = nextSource[key];
}
}

return target;
};
31 changes: 28 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "react-native-router-flux",
"version": "0.2.3",
"version": "0.2.4",
"description": "React Native Router using Flux architecture",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest"
},
"keywords": [
"react-native",
Expand Down Expand Up @@ -45,5 +45,30 @@
],
"directories": {},
"_resolved": "https://registry.npmjs.org/react-native-router-flux/-/react-native-router-flux-0.1.8.tgz",
"readme": "ERROR: No README data found!"
"readme": "ERROR: No README data found!",
"devDependencies": {
"alt": "^0.17.3",
"babel-core": "^5.8.25",
"jest-cli": "^0.5.8",
"react": "^0.13.3",
"react-tools": "^0.13.3"
},
"jest": {
"unmockedModulePathPatterns": [
"react",
"promise",
"node_modules/alt",
"alt.js",
"source-map"
],
"scriptPreprocessor": "jestSupport/scriptPreprocess.js",
"setupEnvScriptFile": "jestSupport/env.js",
"testPathIgnorePatterns": [
"/node_modules/",
"packager/react-packager/src/Activity/"
],
"testFileExtensions": [
"js"
]
}
}
2 changes: 2 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
npm test

0 comments on commit 756d7e0

Please sign in to comment.