Skip to content

Commit

Permalink
Merge pull request #595 from r0b1n/master
Browse files Browse the repository at this point in the history
Actions code styles and tests fixed.
  • Loading branch information
aksonov committed Apr 26, 2016
2 parents 857b133 + 1f6823d commit a54f266
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 74 deletions.
33 changes: 28 additions & 5 deletions src/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ function filterParam(data) {
return data;
}

const reservedKeys = [
POP_ACTION,
POP_ACTION2,
REFRESH_ACTION,
REPLACE_ACTION,
JUMP_ACTION,
PUSH_ACTION,
FOCUS_ACTION,
RESET_ACTION,
'create',
'callback',
'iterate',
'current',
];

class Actions {
constructor() {
this.callback = null;
Expand All @@ -43,16 +58,24 @@ class Actions {
const refs = refsParam;
assert(root.props, 'props should be defined for stack');
const key = root.key;
assert(key, 'unique key should be defined ', root);
assert([POP_ACTION, POP_ACTION2, REFRESH_ACTION, REPLACE_ACTION, JUMP_ACTION, PUSH_ACTION,
FOCUS_ACTION, RESET_ACTION, 'create', 'callback', 'iterate', 'current']
.indexOf(key) === -1, `${key} is not allowed as key name`);
assert(key, 'unique key should be defined ');
assert(
reservedKeys.indexOf(key) === -1,
`'${key}' is not allowed as key name. Reserved keys: [${reservedKeys.join(', ')}]`,
);
const { children, ...staticProps } = root.props;
let type = root.props.type || (parentProps.tabs ? JUMP_ACTION : PUSH_ACTION);
if (type === 'switch') {
type = JUMP_ACTION;
}
const res = { name: key, ...staticProps, key, sceneKey: key, type, parent: parentProps.key };
const res = {
key,
name: key,
sceneKey: key,
parent: parentProps.key,
type,
...staticProps,
};
let list = children || [];
if (!(list instanceof Array)) {
list = [list];
Expand Down
5 changes: 5 additions & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"env": {
"mocha": true
}
}
127 changes: 58 additions & 69 deletions test/Actions.test.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,70 @@
import { expect } from 'chai';
import Actions from '../src/Actions';
import getInitialState from '../src/State';
import React from 'react-native';
import Scene from '../src/Scene';
import createReducer from '../src/Reducer';

describe('Actions', () => {
it('should create needed actions', () => {
let id = 0;
const guid = () => id++;

const scenesData = <Scene component="Modal" key="modal" getInitialState={() => ({ foo: guid() })}>
<Scene key="launch" component="Launch"/>
<Scene key="sideMenu" component="Drawer" initial={true}>
<Scene component="CubeBar" key="cubeBar" type="tabs">
<Scene key="main" tabs={true}>
<Scene key="home" component="Home"/>
<Scene key="map" component="Map"/>
<Scene key="myAccount" component="MyAccount"/>
</Scene>
<Scene key="messaging" initial={true}>
<Scene key="conversations" component="Conversations" getInitialState={() => ({ foo: 'what', bar: guid() })}/>
</Scene>
</Scene>
</Scene>
<Scene key="privacyPolicy" component="PrivacyPolicy" type="modal"/>
<Scene key="termsOfService" component="TermsOfService" type="modal"/>
<Scene key="login">
<Scene key="loginModal1" component="Login1"/>
<Scene key="loginModal2" component="Login2"/>
</Scene>
</Scene>;
const scenes = Actions.create(scenesData);
expect(scenes.conversations.component).to.equal("Conversations");

let currentScene = null;
Actions.callback = scene=>{currentScene = scene};
Actions.conversations({param1: "Hello world"});
expect(currentScene.param1).equal("Hello world");
expect(currentScene.key).equal("conversations");
import React from 'react-native';

Actions.sideMenu({param2: "Hello world2"});
expect(currentScene.param1).equal(undefined);
expect(currentScene.param2).equal("Hello world2");
expect(currentScene.key).equal("sideMenu");
import Actions from '../src/Actions';
import Scene from '../src/Scene';

Actions.messaging({param3: "Hello world3"});
expect(currentScene.param3).equal("Hello world3");
expect(currentScene.key).equal("messaging");
let id = 0;
const guid = () => id++;

const initialState = getInitialState(scenes);
expect(initialState.component).equal("Modal");
expect(initialState.index).equal(0);
const scenesData = (
<Scene
key="root"
component="Modal"
getInitialState={() => ({ foo: guid() })}
>
<Scene key="launch" component="Launch" />
<Scene key="sideMenu" component="Drawer" initial>
<Scene component="CubeBar" key="cubeBar" type="tabs">
<Scene key="main" tabs>
<Scene key="home" component="Home" />
<Scene key="map" component="Map" />
<Scene key="myAccount" component="MyAccount" />
</Scene>
<Scene key="messaging" initial>
<Scene
key="conversations"
component="Conversations"
getInitialState={() => ({ foo: 'what', bar: guid() })}
/>
</Scene>
</Scene>
</Scene>
<Scene key="privacyPolicy" component="PrivacyPolicy" type="modal" />
<Scene key="termsOfService" component="TermsOfService" type="modal" />
<Scene key="login">
<Scene key="loginModal1" component="Login1" />
<Scene key="loginModal2" component="Login2" />
</Scene>
</Scene>);

const reducer = createReducer({initialState, scenes});
let state = undefined;
Actions.callback = scene=>state = reducer(state, scene);
expect(state).equal(undefined);
Actions.init();
expect(state.key).equal("0_modal");
expect(state.children[0].children[0].children[0].children[0].bar).equal(1);
expect(state.children[0].children[0].children[0].children[0].foo).equal('what');
let scenes;

Actions.messaging();
expect(currentScene.key).equal("messaging");
describe('Actions', () => {
before(() => {
scenes = Actions.create(scenesData);
});
it('should produce needed actions', () => {
// check scenes
expect(scenes.conversations.component).to.equal('Conversations');

Actions.login();
expect(state.children[1].key).equal("1_login");
expect(state.children[1].children.length).equal(1);
expect(state.children[1].children[0].key).equal("0_loginModal1");
// set callback which will extract generated action
let latestAction = null;
Actions.callback = scene => { latestAction = scene; };

Actions.pop();
expect(state.from.key).equal("1_login");
// test generated actions
Actions.conversations({ param1: 'Hello world' });
expect(latestAction.param1).equal('Hello world');
expect(latestAction.key).equal('conversations');

Actions.launch();
expect(state.children[1].foo).equal(3);
expect(state.children[1].bar).equal(undefined);
});
Actions.sideMenu({ param2: 'Hello world2' });
expect(latestAction.param1).equal(undefined);
expect(latestAction.param2).equal('Hello world2');
expect(latestAction.key).equal('sideMenu');

Actions.messaging({ param3: 'Hello world3' });
expect(latestAction.param3).equal('Hello world3');
expect(latestAction.key).equal('messaging');
});
});

0 comments on commit a54f266

Please sign in to comment.