Skip to content

Commit

Permalink
Add getInitialState API to scene
Browse files Browse the repository at this point in the history
  • Loading branch information
lelandrichardson committed Apr 12, 2016
1 parent 1ec99d5 commit 10cf5e0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 23 deletions.
26 changes: 25 additions & 1 deletion src/State.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,34 @@
*/
import assert from "assert";

function getStateFromScenes(route, scenes, props) {
const getters = [];
let result = {};
let scene = route;
while (scene) {
if (scene.getInitialState) {
getters.push(scene.getInitialState);
}
scene = scenes[scene.parent];
}

getters.reverse().forEach(fn => {
result = {...result, ...fn(props)};
});

return result;
}

export function getInitialState(route:{string: any},scenes:{string:any}, position=0, props={}){
const {key, style, type, ...parentProps} = props;
if (!route.children){
return { ...scenes.rootProps, ...route, key:position+"_"+route.sceneKey, ...parentProps,};
return {
...scenes.rootProps,
...route,
key:position+"_"+route.sceneKey,
...parentProps,
...getStateFromScenes(route, scenes, props),
};
}
let {children, ...res} = {...route, ...parentProps};
let index = 0;
Expand Down
51 changes: 29 additions & 22 deletions test/Actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,32 @@ import React from 'react-native';
import Scene from '../src/Scene';
import createReducer from '../src/Reducer';

const scenesData = <Scene component="Modal" key="modal">
<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"/>
</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>;

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");

Expand Down Expand Up @@ -57,6 +59,8 @@ describe('Actions', () => {
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');

Actions.messaging();
expect(currentScene.key).equal("messaging");
Expand All @@ -69,6 +73,9 @@ describe('Actions', () => {
Actions.pop();
expect(state.from.key).equal("1_login");

Actions.launch();
expect(state.children[1].foo).equal(3);
expect(state.children[1].bar).equal(undefined);
});

});

0 comments on commit 10cf5e0

Please sign in to comment.