Skip to content

Commit

Permalink
Fix account switching. Do not navigate if last route is the same (#1418)
Browse files Browse the repository at this point in the history
  • Loading branch information
borisyankov authored Nov 10, 2017
1 parent 43ad08f commit cbbd429
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
17 changes: 16 additions & 1 deletion src/nav/__tests__/navReducers-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import deepFreeze from 'deep-freeze';

import { RESET_NAVIGATION, LOGIN_SUCCESS } from '../../actionConstants';
import { RESET_NAVIGATION, LOGIN_SUCCESS, INITIAL_FETCH_COMPLETE } from '../../actionConstants';
import navReducers from '../navReducers';
import { getStateForRoute } from '../navSelectors';

describe('navReducers', () => {
describe('RESET_NAVIGATION', () => {
Expand Down Expand Up @@ -72,4 +73,18 @@ describe('navReducers', () => {
expect(newState.routes[0].routeName).toEqual(expectedState.routes[0].routeName);
});
});

describe('INITIAL_FETCH_COMPLETE', () => {
test('do not mutate navigation state if already at the same route', () => {
const prevState = getStateForRoute('main');

const action = deepFreeze({
type: INITIAL_FETCH_COMPLETE,
});

const newState = navReducers(prevState, action);

expect(newState).toBe(prevState);
});
});
});
16 changes: 15 additions & 1 deletion src/nav/navReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ import { REHYDRATE } from 'redux-persist/constants';

import type { NavigationState, Action } from '../types';
import { navigateToAccountPicker } from './navActions';
import { getFirstIfDeepEqual } from '../utils/immutability';
import { getStateForRoute, getInitialRoute } from './navSelectors';
import AppNavigator from './AppNavigator';
import { RESET_NAVIGATION, ACCOUNT_SWITCH, LOGIN_SUCCESS, LOGOUT } from '../actionConstants';
import {
RESET_NAVIGATION,
INITIAL_FETCH_COMPLETE,
ACCOUNT_SWITCH,
LOGIN_SUCCESS,
LOGOUT,
} from '../actionConstants';

export default (
state: NavigationState = getStateForRoute('loading'),
Expand All @@ -24,6 +31,13 @@ export default (
case LOGIN_SUCCESS:
return getStateForRoute('main');

case INITIAL_FETCH_COMPLETE:
return getFirstIfDeepEqual(
state,
getStateForRoute('main'),
(a, b) => state.routes[0].routeName === 'main',
);

case LOGOUT:
return AppNavigator.router.getStateForAction(navigateToAccountPicker(), state);

Expand Down
45 changes: 44 additions & 1 deletion src/utils/__tests__/immutability-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import deepFreeze from 'deep-freeze';

import { removeItemsFromArray, filterArray, replaceItemInArray } from '../immutability';
import {
removeItemsFromArray,
filterArray,
replaceItemInArray,
getFirstIfDeepEqual,
} from '../immutability';

describe('removeItemsFromArray', () => {
test('return a new array with items removed, do not mutate input', () => {
Expand Down Expand Up @@ -81,3 +86,41 @@ describe('replaceItemInArray', () => {
expect(result).toEqual(expectedResult);
});
});

describe('getFirstIfDeepEqual', () => {
test('return second parameter if two parameters differ', () => {
const first = 123;
const second = 456;

const result = getFirstIfDeepEqual(first, second);

expect(result).toBe(second);
});

test('return second parameter if two arrays are deep equal', () => {
const first = [1, 2, 3];
const second = [1, 2, 3];

const result = getFirstIfDeepEqual(first, second);

expect(result).toBe(first);
});

test('return second parameter if two objects are deep equal', () => {
const first = { hello: 'yo' };
const second = { hello: 'yo' };

const result = getFirstIfDeepEqual(first, second);

expect(result).toBe(first);
});

test('123', () => {
const first = { hello: 'yo', ignored: 123 };
const second = { hello: 'yo', ignored: 456 };

const result = getFirstIfDeepEqual(first, second, (a, b) => a.hello === b.hello);

expect(result).toBe(first);
});
});
8 changes: 8 additions & 0 deletions src/utils/immutability.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* @flow */
import isEqual from 'lodash.isequal';

export const removeItemsFromArray = (input: number[], itemsToRemove: number[]): number[] => {
const output = input.filter(item => !itemsToRemove.includes(item));
return input.length === output.length ? input : output;
Expand Down Expand Up @@ -35,3 +37,9 @@ export const replaceItemInArray = (

return newArray;
};

export const getFirstIfDeepEqual = (
first: any,
second: any,
predicate: (a: any, b: any) => boolean = isEqual,
): any => (predicate(first, second) ? first : second);

0 comments on commit cbbd429

Please sign in to comment.