From 229d41e7ce0a8f26e0b2df574580b1555076ae77 Mon Sep 17 00:00:00 2001 From: HelloYou Date: Sat, 13 May 2017 06:08:22 +0800 Subject: [PATCH] Init to Init-UUID (#1419) --- packages/react-navigation/jest-setup.js | 2 ++ .../src/routers/StackRouter.js | 4 +-- .../src/routers/__tests__/Routers-test.js | 14 +++++++++- .../src/routers/__tests__/StackRouter-test.js | 28 +++++++++++-------- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/packages/react-navigation/jest-setup.js b/packages/react-navigation/jest-setup.js index e20737e6..58434cc8 100644 --- a/packages/react-navigation/jest-setup.js +++ b/packages/react-navigation/jest-setup.js @@ -17,3 +17,5 @@ jest.mock('ScrollView', () => { } return ScrollView; }); + +Date.now = jest.fn(() => 0); diff --git a/packages/react-navigation/src/routers/StackRouter.js b/packages/react-navigation/src/routers/StackRouter.js index a16fa553..a67188ea 100644 --- a/packages/react-navigation/src/routers/StackRouter.js +++ b/packages/react-navigation/src/routers/StackRouter.js @@ -107,7 +107,7 @@ export default ( { ...action, type: undefined, - key: 'Init', + key: `Init-${_getUuid()}`, }, ], }; @@ -130,7 +130,7 @@ export default ( route = { ...route, routeName: initialRouteName, - key: 'Init', + key: `Init-${_getUuid()}`, ...(params ? { params } : {}), }; // eslint-disable-next-line no-param-reassign diff --git a/packages/react-navigation/src/routers/__tests__/Routers-test.js b/packages/react-navigation/src/routers/__tests__/Routers-test.js index 7a080474..e08344bc 100644 --- a/packages/react-navigation/src/routers/__tests__/Routers-test.js +++ b/packages/react-navigation/src/routers/__tests__/Routers-test.js @@ -90,6 +90,12 @@ test('Handles no-op actions with tabs within stack router', () => { type: NavigationActions.NAVIGATE, routeName: 'Qux', }); + /* $FlowFixMe */ + expect(state1.routes[0].key).toEqual('Init-id-0-0'); + /* $FlowFixMe */ + expect(state2.routes[0].key).toEqual('Init-id-0-1'); + /* $FlowFixMe */ + state1.routes[0].key = state2.routes[0].key; expect(state1).toEqual(state2); const state3 = TestRouter.getStateForAction( { type: NavigationActions.NAVIGATE, routeName: 'Zap' }, @@ -114,7 +120,7 @@ test('Handles deep action', () => { index: 0, routes: [ { - key: 'Init', + key: 'Init-id-0-2', routeName: 'Bar', }, ], @@ -153,6 +159,12 @@ test('Supports lazily-evaluated getScreen', () => { type: NavigationActions.NAVIGATE, routeName: 'Qux', }); + /* $FlowFixMe */ + expect(state1.routes[0].key).toEqual('Init-id-0-4'); + /* $FlowFixMe */ + expect(state2.routes[0].key).toEqual('Init-id-0-5'); + /* $FlowFixMe */ + state1.routes[0].key = state2.routes[0].key; expect(state1).toEqual(state2); const state3 = TestRouter.getStateForAction( { type: NavigationActions.NAVIGATE, routeName: 'Zap' }, diff --git a/packages/react-navigation/src/routers/__tests__/StackRouter-test.js b/packages/react-navigation/src/routers/__tests__/StackRouter-test.js index 0c60447f..15eacc30 100644 --- a/packages/react-navigation/src/routers/__tests__/StackRouter-test.js +++ b/packages/react-navigation/src/routers/__tests__/StackRouter-test.js @@ -325,7 +325,7 @@ describe('StackRouter', () => { const initState = TestRouter.getStateForAction(NavigationActions.init()); expect(initState).toEqual({ index: 0, - routes: [{ key: 'Init', routeName: 'foo' }], + routes: [{ key: 'Init-id-0-0', routeName: 'foo' }], }); const pushedState = TestRouter.getStateForAction( NavigationActions.navigate({ routeName: 'qux' }), @@ -355,7 +355,7 @@ describe('StackRouter', () => { index: 0, routes: [ { - key: 'Init', + key: 'Init-id-0-4', routeName: 'Foo', }, ], @@ -380,7 +380,7 @@ describe('StackRouter', () => { index: 0, routes: [ { - key: 'Init', + key: 'Init-id-0-4', routeName: 'Foo', }, ], @@ -408,7 +408,7 @@ describe('StackRouter', () => { index: 0, routes: [ { - key: 'Init', + key: 'Init-id-0-6', routeName: 'Foo', }, ], @@ -433,7 +433,7 @@ describe('StackRouter', () => { index: 0, routes: [ { - key: 'Init', + key: 'Init-id-0-6', routeName: 'Foo', }, ], @@ -499,7 +499,7 @@ describe('StackRouter', () => { index: 0, routes: [ { - key: 'Init', + key: 'Init-id-0-12', routeName: 'Bar', }, ], @@ -521,7 +521,7 @@ describe('StackRouter', () => { index: 0, routes: [ { - key: 'Init', + key: 'Init-id-0-13', routeName: 'Bar', params: { foo: 'bar' }, }, @@ -574,7 +574,7 @@ describe('StackRouter', () => { { type: NavigationActions.SET_PARAMS, params: { name: 'Qux' }, - key: 'Init', + key: 'Init-id-0-16', }, state, ); @@ -602,7 +602,7 @@ describe('StackRouter', () => { { type: NavigationActions.SET_PARAMS, params: { name: 'foobar' }, - key: 'Init', + key: 'Init-id-0-17', }, state, ); @@ -610,7 +610,7 @@ describe('StackRouter', () => { /* $FlowFixMe */ expect(state2 && state2.routes[0].routes[0].routes).toEqual([ { - key: 'Init', + key: 'Init-id-0-17', routeName: 'Quux', params: { name: 'foobar' }, }, @@ -753,7 +753,7 @@ describe('StackRouter', () => { /* $FlowFixMe */ expect(state2 && state2.routes[1].routes).toEqual([ { - key: 'Init', + key: 'Init-id-0-26', routeName: 'Baz', params: { foo: '42' }, }, @@ -818,7 +818,11 @@ describe('StackRouter', () => { state = router.getStateForAction(action); } expect(state && state.index).toEqual(0); - expect(state && state.routes[0]).toEqual({ key: 'Init', routeName: 'Bar' }); + expect(state && state.routes[0]).toEqual({ + key: 'Init-id-0-30', + routeName: 'Bar', + type: undefined, + }); }); test('Gets deep path', () => {