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:
committed by
Facebook Github Bot
parent
a6fb1a9d86
commit
beb656c84e
106
src/plugins/navigation/__tests__/testNavigationPlugin.node.js
Normal file
106
src/plugins/navigation/__tests__/testNavigationPlugin.node.js
Normal 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();
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -14,12 +14,12 @@ type State = {||};
|
|||||||
type Data = {||};
|
type Data = {||};
|
||||||
|
|
||||||
type NavigationEvent = {|
|
type NavigationEvent = {|
|
||||||
date: Date,
|
date: ?Date,
|
||||||
uri: ?String,
|
uri: ?string,
|
||||||
|};
|
|};
|
||||||
|
|
||||||
type PersistedState = {|
|
export type PersistedState = {|
|
||||||
navigationEvents: [NavigationEvent],
|
navigationEvents: Array<NavigationEvent>,
|
||||||
|};
|
|};
|
||||||
|
|
||||||
export default class extends FlipperPlugin<State, {}, PersistedState> {
|
export default class extends FlipperPlugin<State, {}, PersistedState> {
|
||||||
@@ -35,7 +35,7 @@ export default class extends FlipperPlugin<State, {}, PersistedState> {
|
|||||||
static persistedStateReducer = (
|
static persistedStateReducer = (
|
||||||
persistedState: PersistedState,
|
persistedState: PersistedState,
|
||||||
method: string,
|
method: string,
|
||||||
payload: Object,
|
payload: NavigationEvent,
|
||||||
): $Shape<PersistedState> => {
|
): $Shape<PersistedState> => {
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case 'nav_event':
|
case 'nav_event':
|
||||||
@@ -43,7 +43,10 @@ export default class extends FlipperPlugin<State, {}, PersistedState> {
|
|||||||
...persistedState,
|
...persistedState,
|
||||||
navigationEvents: [
|
navigationEvents: [
|
||||||
...persistedState.navigationEvents,
|
...persistedState.navigationEvents,
|
||||||
{uri: payload.uri, date: new Date()},
|
{
|
||||||
|
uri: payload.uri === undefined ? null : payload.uri,
|
||||||
|
date: payload.date || new Date(),
|
||||||
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
default:
|
default:
|
||||||
@@ -55,7 +58,7 @@ export default class extends FlipperPlugin<State, {}, PersistedState> {
|
|||||||
|
|
||||||
onKeyboardAction = (action: string) => {
|
onKeyboardAction = (action: string) => {
|
||||||
if (action === 'clear') {
|
if (action === 'clear') {
|
||||||
this.props.setPersistedState({data: []});
|
this.props.setPersistedState({navigationEvents: []});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user