Skip to content

Commit

Permalink
Colocate comparison function to where its called. Add back flow type …
Browse files Browse the repository at this point in the history
…for createReducer
  • Loading branch information
LiquidSean committed Sep 18, 2019
1 parent 7869dbf commit c9e3ca9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/redux/createReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function handleDismissActionsFromQueue(
export default (comparisonFn: Function = getSimilarActionInQueue) => (
state: NetworkState = initialState,
action: *,
) => {
): NetworkState => {
switch (action.type) {
case actionTypes.CONNECTION_CHANGE:
return {
Expand Down
29 changes: 14 additions & 15 deletions test/reducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,6 @@ const getState = (isConnected = false, ...actionQueue) => ({
actionQueue,
});

function comparisonFn(action, actionQueue) {
if (typeof action === 'object') {
return actionQueue.find(queued => isEqual(queued, action));
}
if (typeof action === 'function') {
return actionQueue.find(
queued =>
action.meta.name === queued.meta.name &&
action.meta.args.id === queued.meta.args.id,
);
}

return undefined;
}

/** Actions used from now on to test different scenarios */
const prevActionToRetry1 = {
type: 'FETCH_DATA_REQUEST',
Expand Down Expand Up @@ -133,6 +118,20 @@ describe('OFFLINE_ACTION action type', () => {
});

describe('thunks that are the same with custom comparison function', () => {
function comparisonFn(action, actionQueue) {
if (typeof action === 'object') {
return actionQueue.find(queued => isEqual(queued, action));
}
if (typeof action === 'function') {
return actionQueue.find(
queued =>
action.meta.name === queued.meta.name &&
action.meta.args.id === queued.meta.args.id,
);
}
return undefined;
}

const thunkFactory = (id, name, age) => {
function thunk(dispatch) {
dispatch({ type: 'UPDATE_DATA_REQUEST', payload: { id, name, age } });
Expand Down

0 comments on commit c9e3ca9

Please sign in to comment.