From beb656c84eac7469c8dbf98c5e2015f078e1c1bc Mon Sep 17 00:00:00 2001 From: Benjamin Elo Date: Thu, 18 Jul 2019 02:45:18 -0700 Subject: [PATCH] Added Nav Plugin tests Summary: Here I added two tests to test the persisted state reducer in the navigation pliugin. I uncovered that my state reducer is not pure, as it calls the Date constructor, but I will fix this in a future update by passing the date recorded on the Android device when the nav event occurs and sending that via the socket connection. For now I have modified the state reducer to take a date as part of the payload on a nav_event, or if none exists record the date as before. Also, if a page has no uri, but we want to record a nav event, I send null uri from the Android side. This doesn't send the uri as null to flipper; the uri object property simply doesn't exist. In this case I explicitly cast the undefined value for uri to null. Reviewed By: jknoxville Differential Revision: D16330958 fbshipit-source-id: fe338ea3a244df6ef33356bc7fdef8da9291dc68 --- .../__tests__/testNavigationPlugin.node.js | 106 ++++++++++++++++++ src/plugins/navigation/index.js | 17 +-- 2 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 src/plugins/navigation/__tests__/testNavigationPlugin.node.js diff --git a/src/plugins/navigation/__tests__/testNavigationPlugin.node.js b/src/plugins/navigation/__tests__/testNavigationPlugin.node.js new file mode 100644 index 000000000..bc8130431 --- /dev/null +++ b/src/plugins/navigation/__tests__/testNavigationPlugin.node.js @@ -0,0 +1,106 @@ +/** + * Copyright 2018-present Facebook. + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * @format + * @flow strict-local + */ + +import NavigationPlugin from '../'; + +import type {PersistedState} from '../'; + +function constructPersistedStateMock(): PersistedState { + return { + navigationEvents: [], + }; +} + +function constructPersistedStateMockWithEvents(): PersistedState { + return { + navigationEvents: [ + { + uri: 'mock://this_is_a_mock_uri/mock/1', + date: DATE_MOCK_2, + }, + { + uri: 'mock://this_is_a_mock_uri/mock/2', + date: DATE_MOCK_3, + }, + ], + }; +} + +const DATE_MOCK_1 = new Date(2019, 6, 17, 11, 10, 0, 0); +const DATE_MOCK_2 = new Date(2019, 6, 18, 11, 10, 0, 0); +const DATE_MOCK_3 = new Date(2019, 6, 19, 11, 10, 0, 0); + +const INCOMING_NAV_EVENT = { + uri: 'mock://this_is_a_mock_uri/mock', + date: DATE_MOCK_1, +}; + +const INCOMING_UNDEFINED_NAV_EVENT = { + date: DATE_MOCK_1, +}; + +test('add incoming nav event to persisted state', () => { + const persistedState = constructPersistedStateMock(); + const reducer = NavigationPlugin.persistedStateReducer; + if (reducer) { + const newPersistedState = reducer( + persistedState, + 'nav_event', + INCOMING_NAV_EVENT, + ); + expect(newPersistedState.navigationEvents).toEqual([ + { + uri: 'mock://this_is_a_mock_uri/mock', + date: DATE_MOCK_1, + }, + ]); + } else { + expect(reducer).not.toBeNull(); + } +}); + +test('add incoming nav event to persisted state with nav events', () => { + const persistedState = constructPersistedStateMockWithEvents(); + const reducer = NavigationPlugin.persistedStateReducer; + if (reducer) { + const newPersistedState = reducer( + persistedState, + 'nav_event', + INCOMING_NAV_EVENT, + ); + expect(newPersistedState.navigationEvents).toEqual([ + ...persistedState.navigationEvents, + { + uri: 'mock://this_is_a_mock_uri/mock', + date: DATE_MOCK_1, + }, + ]); + } else { + expect(reducer).not.toBeNull(); + } +}); + +test('add incoming nav event with undefined uri to persisted state', () => { + const persistedState = constructPersistedStateMock(); + const reducer = NavigationPlugin.persistedStateReducer; + if (reducer) { + const newPersistedState = reducer( + persistedState, + 'nav_event', + INCOMING_UNDEFINED_NAV_EVENT, + ); + expect(newPersistedState.navigationEvents).toEqual([ + { + uri: null, + date: DATE_MOCK_1, + }, + ]); + } else { + expect(reducer).not.toBeNull(); + } +}); diff --git a/src/plugins/navigation/index.js b/src/plugins/navigation/index.js index dd88a153a..f0835e293 100644 --- a/src/plugins/navigation/index.js +++ b/src/plugins/navigation/index.js @@ -14,12 +14,12 @@ type State = {||}; type Data = {||}; type NavigationEvent = {| - date: Date, - uri: ?String, + date: ?Date, + uri: ?string, |}; -type PersistedState = {| - navigationEvents: [NavigationEvent], +export type PersistedState = {| + navigationEvents: Array, |}; export default class extends FlipperPlugin { @@ -35,7 +35,7 @@ export default class extends FlipperPlugin { static persistedStateReducer = ( persistedState: PersistedState, method: string, - payload: Object, + payload: NavigationEvent, ): $Shape => { switch (method) { case 'nav_event': @@ -43,7 +43,10 @@ export default class extends FlipperPlugin { ...persistedState, navigationEvents: [ ...persistedState.navigationEvents, - {uri: payload.uri, date: new Date()}, + { + uri: payload.uri === undefined ? null : payload.uri, + date: payload.date || new Date(), + }, ], }; default: @@ -55,7 +58,7 @@ export default class extends FlipperPlugin { onKeyboardAction = (action: string) => { if (action === 'clear') { - this.props.setPersistedState({data: []}); + this.props.setPersistedState({navigationEvents: []}); } };