-
-
Notifications
You must be signed in to change notification settings - Fork 639
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor fetching, tracking multiple narrows (#1100)
- Loading branch information
1 parent
3207878
commit 96ad38e
Showing
17 changed files
with
294 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import deepFreeze from 'deep-freeze'; | ||
|
||
import fetchingReducers from '../fetchingReducers'; | ||
import { homeNarrowStr, streamNarrow } from '../../utils/narrow'; | ||
import { SWITCH_NARROW, MESSAGE_FETCH_START, MESSAGE_FETCH_SUCCESS } from '../../actionConstants'; | ||
|
||
describe('fetchingReducers', () => { | ||
describe('SWITCH_NARROW', () => { | ||
test('resets state', () => { | ||
const initialState = deepFreeze({ | ||
'[]': { older: true, newer: true }, | ||
}); | ||
|
||
const action = deepFreeze({ | ||
type: SWITCH_NARROW, | ||
narrow: streamNarrow('some stream'), | ||
}); | ||
|
||
const expectedState = {}; | ||
|
||
const newState = fetchingReducers(initialState, action); | ||
|
||
expect(newState).toEqual(expectedState); | ||
}); | ||
}); | ||
|
||
describe('MESSAGE_FETCH_START', () => { | ||
test('if messages are fetched before or after the corresponding flag is set', () => { | ||
const initialState = deepFreeze({ | ||
[homeNarrowStr]: { older: false, newer: false }, | ||
}); | ||
|
||
const action = deepFreeze({ | ||
type: MESSAGE_FETCH_START, | ||
narrow: [], | ||
numBefore: 10, | ||
numAfter: 10, | ||
}); | ||
|
||
const expectedState = { | ||
[homeNarrowStr]: { older: true, newer: true }, | ||
}; | ||
|
||
const newState = fetchingReducers(initialState, action); | ||
|
||
expect(newState).toEqual(expectedState); | ||
}); | ||
|
||
test('if key for narrow does not exist, it is created and corresponding flags are set', () => { | ||
const initialState = deepFreeze({ | ||
[homeNarrowStr]: { older: false, newer: false }, | ||
}); | ||
|
||
const action = deepFreeze({ | ||
type: MESSAGE_FETCH_START, | ||
narrow: streamNarrow('some stream'), | ||
numBefore: 10, | ||
numAfter: 0, | ||
}); | ||
|
||
const expectedState = { | ||
[homeNarrowStr]: { older: false, newer: false }, | ||
[JSON.stringify(streamNarrow('some stream'))]: { older: true, newer: false }, | ||
}; | ||
|
||
const newState = fetchingReducers(initialState, action); | ||
|
||
expect(newState).toEqual(expectedState); | ||
}); | ||
}); | ||
|
||
describe('MESSAGE_FETCH_SUCCESS', () => { | ||
test('sets corresponding fetching flags to false, if messages are received before or after', () => { | ||
const initialState = deepFreeze({ | ||
[homeNarrowStr]: { older: true, newer: true }, | ||
}); | ||
|
||
const action = deepFreeze({ | ||
type: MESSAGE_FETCH_SUCCESS, | ||
narrow: [], | ||
messages: [{ id: 1 }], | ||
numBefore: 10, | ||
numAfter: 0, | ||
}); | ||
|
||
const expectedState = { | ||
[homeNarrowStr]: { older: false, newer: true }, | ||
}; | ||
|
||
const newState = fetchingReducers(initialState, action); | ||
|
||
expect(newState).toEqual(expectedState); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import deepFreeze from 'deep-freeze'; | ||
|
||
import { getFetchingForActiveNarrow } from '../fetchingSelectors'; | ||
import { homeNarrow, homeNarrowStr } from '../../utils/narrow'; | ||
|
||
describe('getFetchingForActiveNarrow', () => { | ||
test('if no narrow information exists in state, return a null fetching object', () => { | ||
const state = deepFreeze({ | ||
chat: { | ||
narrow: homeNarrow, | ||
}, | ||
fetching: {}, | ||
}); | ||
const expectedResult = { older: false, newer: false }; | ||
|
||
const actualResult = getFetchingForActiveNarrow(state); | ||
|
||
expect(actualResult).toEqual(expectedResult); | ||
}); | ||
|
||
test('if an entry matching current narrow exists, it is returned', () => { | ||
const state = deepFreeze({ | ||
chat: { | ||
narrow: homeNarrow, | ||
}, | ||
fetching: { | ||
[homeNarrowStr]: { older: true, newer: true }, | ||
}, | ||
}); | ||
const expectedResult = { older: true, newer: true }; | ||
|
||
const actualResult = getFetchingForActiveNarrow(state); | ||
|
||
expect(actualResult).toEqual(expectedResult); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* @flow */ | ||
import type { FetchingState, Action } from '../types'; | ||
import { | ||
LOGOUT, | ||
LOGIN_SUCCESS, | ||
ACCOUNT_SWITCH, | ||
SWITCH_NARROW, | ||
MESSAGE_FETCH_START, | ||
MESSAGE_FETCH_SUCCESS, | ||
} from '../actionConstants'; | ||
import { NULL_FETCHING } from '../nullObjects'; | ||
|
||
const initialState: FetchingState = {}; | ||
|
||
export default (state: FetchingState = initialState, action: Action) => { | ||
switch (action.type) { | ||
case LOGOUT: | ||
case LOGIN_SUCCESS: | ||
case SWITCH_NARROW: | ||
case ACCOUNT_SWITCH: | ||
return initialState; | ||
|
||
case MESSAGE_FETCH_START: { | ||
const key = JSON.stringify(action.narrow); | ||
const currentValue = state[key] || NULL_FETCHING; | ||
|
||
return { | ||
...state, | ||
[key]: { | ||
older: action.numBefore > 0 || currentValue.older, | ||
newer: action.numAfter > 0 || currentValue.newer, | ||
}, | ||
}; | ||
} | ||
|
||
case MESSAGE_FETCH_SUCCESS: { | ||
const key = JSON.stringify(action.narrow); | ||
const currentValue = state[key] || NULL_FETCHING; | ||
|
||
return { | ||
...state, | ||
[key]: { | ||
older: currentValue.older && action.numBefore === 0, | ||
newer: currentValue.newer && action.numAfter === 0, | ||
}, | ||
}; | ||
} | ||
|
||
default: | ||
return state; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* @flow */ | ||
import { createSelector } from 'reselect'; | ||
|
||
import { getActiveNarrowString, getApp, getFetching } from '../baseSelectors'; | ||
import { NULL_FETCHING } from '../nullObjects'; | ||
|
||
export const getFetchingForActiveNarrow = createSelector( | ||
getFetching, | ||
getActiveNarrowString, | ||
(fetching, activeNarrowString) => fetching[activeNarrowString] || NULL_FETCHING, | ||
); | ||
|
||
export const getIsFetching = createSelector( | ||
getApp, | ||
getFetchingForActiveNarrow, | ||
(app, fetching) => (app.needsInitialFetch && fetching.older) || fetching.older, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.