Make NavigationLegacyNavigator more testable.

Summary:- Move the logics that manage the routes stack into `NavigationLegacyNavigatorRouteStack`
- Add more unit tests for NavigationLegacyNavigatorRouteStack.
- Keep NavigationLegacyNavigator as a pure view as possible as we could.

Reviewed By: fkgozali

Differential Revision: D3060459

fb-gh-sync-id: 2c6802115c3f6ca5e396903f0d314ff54129524c
shipit-source-id: 2c6802115c3f6ca5e396903f0d314ff54129524c
This commit is contained in:
Hedger Wang 2016-03-16 17:19:59 -07:00 committed by Facebook Github Bot 5
parent 897ec90778
commit 55477ffd67
3 changed files with 168 additions and 79 deletions

View File

@ -45,7 +45,6 @@ const NavigatorSceneConfigs = require('NavigatorSceneConfigs');
const React = require('react-native'); const React = require('react-native');
const ReactComponentWithPureRenderMixin = require('ReactComponentWithPureRenderMixin'); const ReactComponentWithPureRenderMixin = require('ReactComponentWithPureRenderMixin');
const invariant = require('fbjs/lib/invariant');
const guid = require('guid'); const guid = require('guid');
import type { import type {
@ -129,20 +128,15 @@ class NavigationLegacyNavigator extends React.Component<any, Props, State> {
} }
jumpTo(route: any): void { jumpTo(route: any): void {
const index = this._stack.indexOf(route); this._applyStack(this._stack.jumpTo(route));
invariant(
index > -1,
'Cannot jump to route that is not in the route stack'
);
this._jumpToIndex(index);
} }
jumpForward(): void { jumpForward(): void {
this._jumpToIndex(this._stack.index + 1); this._applyStack(this._stack.jumpForward());
} }
jumpBack(): void { jumpBack(): void {
this._jumpToIndex(this._stack.index - 1); this._applyStack(this._stack.jumpBack());
} }
push(route: any): void { push(route: any): void {
@ -150,25 +144,11 @@ class NavigationLegacyNavigator extends React.Component<any, Props, State> {
} }
pop(): void { pop(): void {
const stack = this._stack; this._applyStack(this._stack.pop());
if (stack.size > 1) {
this._applyStack(stack.pop());
}
} }
replaceAtIndex(route: any, index: number): void { replaceAtIndex(route: any, index: number): void {
const stack = this._stack; this._applyStack(this._stack.replaceAtIndex(index, route));
if (index < 0) {
index += stack.size;
}
if (index >= stack.size) {
// Nothing to replace.
return;
}
this._applyStack(stack.replaceAtIndex(index, route));
} }
replace(route: any): void { replace(route: any): void {
@ -184,49 +164,27 @@ class NavigationLegacyNavigator extends React.Component<any, Props, State> {
} }
popToRoute(route: any): void { popToRoute(route: any): void {
const stack = this._stack; this._applyStack(this._stack.popToRoute(route));
const nextIndex = stack.indexOf(route);
invariant(
nextIndex > -1,
'Calling popToRoute for a route that doesn\'t exist!'
);
this._applyStack(stack.slice(0, nextIndex + 1));
} }
replacePreviousAndPop(route: any): void { replacePreviousAndPop(route: any): void {
const stack = this._stack; this._applyStack(this._stack.replacePreviousAndPop(route));
const nextIndex = stack.index - 1;
if (nextIndex < 0) {
return;
}
this._applyStack(stack.replaceAtIndex(nextIndex, route).pop());
} }
resetTo(route: any): void { resetTo(route: any): void {
invariant(!!route, 'Must supply route'); this._applyStack(this._stack.resetTo(route));
this._applyStack(this._stack.slice(0).replaceAtIndex(0, route));
} }
immediatelyResetRouteStack(routes: Array<any>): void { immediatelyResetRouteStack(routes: Array<any>): void {
const index = routes.length - 1;
const stack = new RouteStack(index, routes);
// Immediately blow away all current scenes with a new key. // Immediately blow away all current scenes with a new key.
this._key = guid(); this._key = guid();
this._applyStack(stack); this._applyStack(this._stack.resetRoutes(routes));
} }
getCurrentRoutes(): Array<any> { getCurrentRoutes(): Array<any> {
return this._stack.toArray(); return this._stack.toArray();
} }
_jumpToIndex(index: number): void {
const stack = this._stack;
if (index < 0 || index >= stack.size) {
return;
}
this._applyStack(stack.jumpToIndex(index));
}
// Lyfe cycle and private methods below. // Lyfe cycle and private methods below.
shouldComponentUpdate(nextProps: Object, nextState: Object): boolean { shouldComponentUpdate(nextProps: Object, nextState: Object): boolean {

View File

@ -1,5 +1,10 @@
/** /**
* Copyright 2004-present Facebook. All Rights Reserved. * Copyright (c) 2013-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.
* *
* @providesModule NavigationLegacyNavigatorRouteStack * @providesModule NavigationLegacyNavigatorRouteStack
* @flow * @flow
@ -85,8 +90,7 @@ class RouteNode {
} }
toNavigationState(): NavigationState { toNavigationState(): NavigationState {
const state: NavigationState = this; return this;
return state;
} }
} }
@ -114,7 +118,7 @@ class RouteStack {
invariant( invariant(
index > -1 && index <= routes.length - 1, index > -1 && index <= routes.length - 1,
'index out of bound' 'RouteStack: index out of bound'
); );
@ -187,7 +191,7 @@ class RouteStack {
} }
for (let ii = 0, jj = this._routeNodes.length; ii < jj; ii++) { for (let ii = 0, jj = this._routeNodes.length; ii < jj; ii++) {
let node = this._routeNodes[ii]; const node = this._routeNodes[ii];
if (node.route === route) { if (node.route === route) {
return ii; return ii;
} }
@ -230,16 +234,29 @@ class RouteStack {
* excluding the last index in this stack. * excluding the last index in this stack.
*/ */
pop(): RouteStack { pop(): RouteStack {
invariant( if (this._routeNodes.length <= 1) {
this._routeNodes.length > 1, return this;
'should not pop routeNodes stack to empty' }
);
// When popping, removes the rest of the routes past the current index. // When popping, removes the rest of the routes past the current index.
const routeNodes = this._routeNodes.slice(0, this._index); const routeNodes = this._routeNodes.slice(0, this._index);
return this._update(routeNodes.length - 1, routeNodes); return this._update(routeNodes.length - 1, routeNodes);
} }
popToRoute(route: any): RouteStack {
const index = this.indexOf(route);
invariant(
index > -1,
'Calling popToRoute for a route that doesn\'t exist!'
);
return this.slice(0, index + 1);
}
jumpTo(route: any): RouteStack {
const index = this.indexOf(route);
return this.jumpToIndex(index);
}
jumpToIndex(index: number): RouteStack { jumpToIndex(index: number): RouteStack {
invariant( invariant(
index > -1 && index < this._routeNodes.length, index > -1 && index < this._routeNodes.length,
@ -249,6 +266,22 @@ class RouteStack {
return this._update(index, this._routeNodes); return this._update(index, this._routeNodes);
} }
jumpForward(): RouteStack {
const index = this._index + 1;
if (index >= this._routeNodes.length) {
return this;
}
return this._update(index, this._routeNodes);
}
jumpBack(): RouteStack {
const index = this._index - 1;
if (index < 0) {
return this;
}
return this._update(index, this._routeNodes);
}
/** /**
* Replace a route in the navigation stack. * Replace a route in the navigation stack.
* *
@ -267,20 +300,59 @@ class RouteStack {
invariant(this.indexOf(route) === -1, 'route must be unique'); invariant(this.indexOf(route) === -1, 'route must be unique');
const size = this._routeNodes.length;
if (index < 0) { if (index < 0) {
index += this._routeNodes.length; index += size;
} }
invariant( if (index < 0 || index >= size) {
index > -1 && index < this._routeNodes.length, return this;
'replaceAtIndex: index out of bound' }
);
const routeNodes = this._routeNodes.slice(0); const routeNodes = this._routeNodes.slice(0);
routeNodes[index] = new RouteNode(route); routeNodes[index] = new RouteNode(route);
return this._update(index, routeNodes); return this._update(index, routeNodes);
} }
replacePreviousAndPop(route: any): RouteStack {
if (this._index < 1) {
// stack is too small.
return this;
}
const index = this.indexOf(route);
invariant(
index === -1 || index === this._index - 1,
'route already exists in the stack'
);
return this.replaceAtIndex(this._index - 1, route).popToRoute(route);
}
// Reset
/**
* Replace the current active route with a new route, and pops out
* the rest routes after it.
*/
resetTo(route: any): RouteStack {
invariant(!isRouteEmpty(route), 'Must supply route');
const index = this.indexOf(route);
if (index === this._index) {
// Already has this active route.
return this;
}
invariant(index === -1, 'route already exists in the stack');
const routeNodes = this._routeNodes.slice(0, this._index);
routeNodes.push(new RouteNode(route));
return this._update(routeNodes.length - 1, routeNodes);
}
resetRoutes(routes: Array<any>): RouteStack {
const index = routes.length - 1;
return new RouteStack(index, routes);
}
// Iterations // Iterations
forEach(callback: IterationCallback, context: ?Object): void { forEach(callback: IterationCallback, context: ?Object): void {
this._routeNodes.forEach((node, index) => { this._routeNodes.forEach((node, index) => {

View File

@ -1,5 +1,10 @@
/** /**
* Copyright (c) 2015, Facebook, Inc. All rights reserved. * Copyright (c) 2013-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.
* *
* Facebook, Inc. ("Facebook") owns all right, title and interest, including * Facebook, Inc. ("Facebook") owns all right, title and interest, including
* all intellectual property and other proprietary rights, in and to the React * all intellectual property and other proprietary rights, in and to the React
@ -24,9 +29,7 @@
*/ */
'use strict'; 'use strict';
jest jest.dontMock('NavigationLegacyNavigatorRouteStack');
.autoMockOff()
.mock('ErrorUtils');
const NavigationLegacyNavigatorRouteStack = require('NavigationLegacyNavigatorRouteStack'); const NavigationLegacyNavigatorRouteStack = require('NavigationLegacyNavigatorRouteStack');
@ -263,11 +266,20 @@ describe('NavigationLegacyNavigatorRouteStack:', () => {
expect(stack2.index).toBe(0); expect(stack2.index).toBe(0);
}); });
it('throws when popping to empty stack', () => { it('does nothing while popping to empty', () => {
expect(() => { const stack = new NavigationLegacyNavigatorRouteStack(0, ['a']);
const stack = new NavigationLegacyNavigatorRouteStack(0, ['a']); expect(stack.pop()).toBe(stack);
stack.pop(); expect(stack.pop().pop()).toBe(stack);
}).toThrow(); });
it('pops to route', () => {
const stack = new NavigationLegacyNavigatorRouteStack(1, ['a', 'b', 'c']);
expect(stack.popToRoute('b').toArray()).toEqual(['a', 'b']);
expect(stack.popToRoute('b').index).toBe(1);
expect(stack.popToRoute('a').toArray()).toEqual(['a']);
expect(stack.popToRoute('a').index).toBe(0);
expect(() => {stack.popToRoute('x');}).toThrow();
}); });
// Jump // Jump
@ -291,6 +303,24 @@ describe('NavigationLegacyNavigatorRouteStack:', () => {
}).toThrow(); }).toThrow();
}); });
it('jumps to route', () => {
const stack = new NavigationLegacyNavigatorRouteStack(0, ['a', 'b']);
expect(stack.jumpTo('b').index).toBe(1);
});
it('jumps backward', () => {
const stack = new NavigationLegacyNavigatorRouteStack(1, ['a', 'b']);
expect(stack.jumpBack().index).toBe(0);
expect(stack.jumpBack().jumpBack().jumpBack().index).toBe(0);
});
it('jumps forward', () => {
const stack = new NavigationLegacyNavigatorRouteStack(0, ['a', 'b']);
expect(stack.jumpForward().index).toBe(1);
expect(stack.jumpForward().jumpForward().jumpForward().index).toBe(1);
});
// Replace // Replace
it('replaces route at index', () => { it('replaces route at index', () => {
const stack1 = new NavigationLegacyNavigatorRouteStack(1, ['a', 'b']); const stack1 = new NavigationLegacyNavigatorRouteStack(1, ['a', 'b']);
@ -317,11 +347,40 @@ describe('NavigationLegacyNavigatorRouteStack:', () => {
}).toThrow(); }).toThrow();
}); });
it('throws when replacing at index out of bound', () => { it('does nothing when replacing at index out of bound', () => {
expect(() => { const stack = new NavigationLegacyNavigatorRouteStack(1, ['a', 'b']);
const stack = new NavigationLegacyNavigatorRouteStack(1, ['a', 'b']); expect(stack.replaceAtIndex(100, 'x')).toBe(stack);
stack.replaceAtIndex(100, 'x'); expect(stack.replaceAtIndex(-100, 'x')).toBe(stack);
}).toThrow(); });
it('replaces previous and pop route', () => {
const stack = new NavigationLegacyNavigatorRouteStack(1, ['a', 'b', 'c']);
expect(stack.replacePreviousAndPop('x').toArray()).toEqual(['x']);
expect(stack.replacePreviousAndPop('x').index).toBe(0);
});
it('does nothing when there is nothing to replace', () => {
const stack = new NavigationLegacyNavigatorRouteStack(0, ['a', 'b', 'c']);
expect(stack.replacePreviousAndPop('x')).toBe(stack);
});
// Reset
it('resets route', () => {
const stack = new NavigationLegacyNavigatorRouteStack(1, ['a', 'b', 'c']);
expect(stack.resetTo('b')).toBe(stack);
expect(stack.resetTo('x').toArray()).toEqual(['a', 'x']);
expect(stack.resetTo('x').index).toBe(1);
expect(() => {stack.resetTo(null);}).toThrow();
expect(() => {stack.resetTo('a');}).toThrow();
});
it('resets routes', () => {
const stack = new NavigationLegacyNavigatorRouteStack(0, ['a']);
expect(stack.resetRoutes(['x', 'y']).toArray()).toEqual(['x', 'y']);
expect(stack.resetRoutes(['x', 'y']).index).toBe(1);
}); });
// Iteration // Iteration