Hedger Wang fb5d0ff587 D3321403 [NavigationExperimental][CleanUp]: Rename scene.navigationState to scene.route.
Summary:
[public / experimental API breaking change]

The data type of  `scene.navigationState` is `NavigationRoute`. Rename  `scene.navigationState` to
`scene.route` to avoid confusion such as treating `scene.navigationState` as the actual global navigation
state (type: NavigationState).

Reviewed By: ericvicenti

Differential Revision: D3331076

fbshipit-source-id: 3ed989cc8492d398cbeb1b12186459deb261d1fb
2016-05-20 18:13:30 -07:00

204 lines
3.7 KiB
JavaScript

/**
* 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';
jest.unmock('NavigationScenesReducer');
const NavigationScenesReducer = require('NavigationScenesReducer');
/**
* Simulate scenes transtion with changes of navigation states.
*/
function testTransition(states) {
const routes = states.map(keys => {
return {
children: keys.map(key => {
return { key };
}),
};
});
let scenes = [];
let prevState = null;
routes.forEach((nextState) => {
scenes = NavigationScenesReducer(scenes, nextState, prevState);
prevState = nextState;
});
return scenes;
}
describe('NavigationScenesReducer', () => {
it('gets initial scenes', () => {
const scenes = testTransition([
['1', '2'],
]);
expect(scenes).toEqual([
{
'index': 0,
'isStale': false,
'key': 'scene_1',
'route': {
'key': '1'
},
},
{
'index': 1,
'isStale': false,
'key': 'scene_2',
'route': {
'key': '2'
},
},
]);
});
it('pushes new scenes', () => {
// Transition from ['1', '2'] to ['1', '2', '3'].
const scenes = testTransition([
['1', '2'],
['1', '2', '3'],
]);
expect(scenes).toEqual([
{
'index': 0,
'isStale': false,
'key': 'scene_1',
'route': {
'key': '1'
},
},
{
'index': 1,
'isStale': false,
'key': 'scene_2',
'route': {
'key': '2'
},
},
{
'index': 2,
'isStale': false,
'key': 'scene_3',
'route': {
'key': '3'
},
},
]);
});
it('pops scenes', () => {
// Transition from ['1', '2', '3'] to ['1', '2'].
const scenes = testTransition([
['1', '2', '3'],
['1', '2'],
]);
expect(scenes).toEqual([
{
'index': 0,
'isStale': false,
'key': 'scene_1',
'route': {
'key': '1'
},
},
{
'index': 1,
'isStale': false,
'key': 'scene_2',
'route': {
'key': '2'
},
},
{
'index': 2,
'isStale': true,
'key': 'scene_3',
'route': {
'key': '3'
},
},
]);
});
it('replaces scenes', () => {
const scenes = testTransition([
['1', '2'],
['3'],
]);
expect(scenes).toEqual([
{
'index': 0,
'isStale': true,
'key': 'scene_1',
'route': {
'key': '1'
},
},
{
'index': 0,
'isStale': false,
'key': 'scene_3',
'route': {
'key': '3'
},
},
{
'index': 1,
'isStale': true,
'key': 'scene_2',
'route': {
'key': '2'
},
},
]);
});
it('revives scenes', () => {
const scenes = testTransition([
['1', '2'],
['3'],
['2'],
]);
expect(scenes).toEqual([
{
'index': 0,
'isStale': true,
'key': 'scene_1',
'route': {
'key': '1'
},
},
{
'index': 0,
'isStale': false,
'key': 'scene_2',
'route': {
'key': '2'
},
},
{
'index': 0,
'isStale': true,
'key': 'scene_3',
'route': {
'key': '3'
},
},
]);
});
});