react-native/Libraries/NavigationExperimental/Reducer/__tests__/NavigationScenesReducer-tes...

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 {
routes: 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'
},
},
]);
});
});