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
This commit is contained in:
Benjamin Elo
2019-07-18 02:45:18 -07:00
committed by Facebook Github Bot
parent a6fb1a9d86
commit beb656c84e
2 changed files with 116 additions and 7 deletions

View File

@@ -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();
}
});

View File

@@ -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<NavigationEvent>,
|};
export default class extends FlipperPlugin<State, {}, PersistedState> {
@@ -35,7 +35,7 @@ export default class extends FlipperPlugin<State, {}, PersistedState> {
static persistedStateReducer = (
persistedState: PersistedState,
method: string,
payload: Object,
payload: NavigationEvent,
): $Shape<PersistedState> => {
switch (method) {
case 'nav_event':
@@ -43,7 +43,10 @@ export default class extends FlipperPlugin<State, {}, PersistedState> {
...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<State, {}, PersistedState> {
onKeyboardAction = (action: string) => {
if (action === 'clear') {
this.props.setPersistedState({data: []});
this.props.setPersistedState({navigationEvents: []});
}
};