Skip to content

Commit

Permalink
test: document set params action for routers
Browse files Browse the repository at this point in the history
These tests document the behavior of using set params action for the
different routers. Even if the action is named `SET_PARAMS`, it merges
the params with the previous one: this behavior matches the `setState`
method used in React's components.
  • Loading branch information
oltrep authored and satya164 committed Sep 2, 2020
1 parent 06a69f1 commit 4249230
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions packages/core/src/routers/__tests__/Routers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,51 @@ Object.keys(ROUTERS).forEach((routerName) => {
);
expect(state0.routes[state0.index].params.foo).toEqual(42);
});

it('merges existing params when set params on existing state', () => {
const Screen = () => <div />;

const router = Router({
Foo: {
screen: Screen,
params: { a: 1 },
},
});
const key = 'Foo';
const state = router.getStateForAction({
type: NavigationActions.INIT,
key,
});

expect(state).toMatchObject({
index: 0,
routes: [{ key, params: { a: 1 } }],
});

const newState = router.getStateForAction(
NavigationActions.setParams({ key, params: { b: 2 } }),
state
);

expect(newState.routes[newState.index].params).toEqual({ a: 1, b: 2 });
});

it('merges params when setting params during init', () => {
const Screen = () => <div />;

const router = Router({
Foo: {
screen: Screen,
params: { a: 1 },
},
});

const newState = router.getStateForAction(
NavigationActions.setParams({ key: 'Foo', params: { b: 2 } })
);

expect(newState.routes[newState.index].params).toEqual({ a: 1, b: 2 });
});
});
});

Expand Down

0 comments on commit 4249230

Please sign in to comment.