Skip to content

Commit

Permalink
Restore 0.35 JUMP handling (#1312)
Browse files Browse the repository at this point in the history
  • Loading branch information
wrozka authored and aksonov committed Oct 24, 2016
1 parent a1cf4c3 commit cff0ced
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
16 changes: 2 additions & 14 deletions src/Reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,8 @@ function inject(state, action, props, scenes) {
};
case ActionConst.JUMP: {
assert(state.tabs, `Parent=${state.key} is not tab bar, jump action is not valid`);
/* TODO: recursive key search (for sub-tab scenes)*/
ind = state.children.findIndex(el => el.sceneKey === action.key);
// variant #1
// get target scene with data passed in Actions.SCENE_KEY(PARAMS)
const targetSceneWithData = getInitialState(props, scenes, ind, action);
// find same scene in state.children
const targetSceneInState = state.children[ind];
// update child scene from state.children with pased data
state.children[ind] = { ...targetSceneInState, ...targetSceneWithData };

// variant #2 - NOT WORKING
// just update target scene in state tree with PARAMS
// state.children[ind] = { ...props, ...state.children[ind] };

ind = -1;
state.children.forEach((c, i) => { if (c.sceneKey === action.key) { ind = i; } });
assert(ind !== -1, `Cannot find route with key=${action.key} for parent=${state.key}`);

if (action.unmountScenes) {
Expand Down
54 changes: 47 additions & 7 deletions test/Reducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function getCurrent(state) {

let id = 0;
const guid = () => id++;
const noop = () => {};
const scenesData = (
<Scene
key="root"
Expand Down Expand Up @@ -110,9 +111,50 @@ describe('createReducer', () => {
});
});

describe('handling actions', () => {
let Actions;
let state;
let current;

beforeEach(() => {
const scene = (
<Scene key="root" component={noop}>
<Scene key="main" tabs>
<Scene key="hello" component={noop} initial />
<Scene key="world" component={noop} />
</Scene>
</Scene>
);

Actions = new ActionsTest();
const scenes = Actions.create(scene);
const initialState = getInitialState(scenes);
const reducer = createReducer({ initialState, scenes });

state = { ...initialState, scenes };
current = getCurrent(state);
Actions.callback = action => {
state = reducer(state, action);
current = getCurrent(state);
};
});

it('navigates to a correct scene on PUSH', () => {
Actions.main();

expect(current.key).to.eq('0_hello_');
});

it('switches to a corret tab on JUMP', () => {
Actions.main();
Actions.hello();

expect(current.key).to.eq('0_hello_');
});
});

describe('passing props from actions', () => {
it('passes props for normal scenes', () => {
const noop = () => {};
const scene = (
<Scene key="root" component={noop}>
<Scene key="hello" component={noop} initial />
Expand All @@ -136,12 +178,12 @@ describe('passing props from actions', () => {
expect(current.customProp).to.eq('Hello');
Actions.world({ customProp: 'World' });
expect(current.customProp).to.eq('World');

Actions.hello();
expect(current.customProp).to.eq(void 0);
});

it('passes props for tab scenes', () => {
const noop = () => {};
it.skip('passes props for tab scenes', () => {
const scene = (
<Scene key="root" component={noop} tabs>
<Scene key="home" component={noop} />
Expand Down Expand Up @@ -172,8 +214,7 @@ describe('passing props from actions', () => {
expect(current.anotherProp).to.eq(void 0);
});

it('passes props for nested tab scenes', () => {
const noop = () => {};
it.skip('passes props for nested tab scenes', () => {
const scene = (
<Scene key="root" component={noop} tabs>
<Scene key="home" component={noop} />
Expand Down Expand Up @@ -209,8 +250,7 @@ describe('passing props from actions', () => {
expect(current.anotherProp).to.eq(void 0);
});

it('passes props for very nested tab scenes', () => {
const noop = () => {};
it.skip('passes props for very nested tab scenes', () => {
const scene = (
<Scene key="root" component={noop} tabs>
<Scene key="home" component={noop} />
Expand Down

1 comment on commit cff0ced

@raminious
Copy link

@raminious raminious commented on cff0ced Oct 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wrozka
Pass data between Scenes not works after this commit
for example Actions.HELLO({ foo: 'bar' }) not pass foo property to HELLO Scene

Please sign in to comment.