Skip to content

Commit

Permalink
export new factory function in entry point file.
Browse files Browse the repository at this point in the history
Fix test cases to check shape and expected length of the actionQueue
  • Loading branch information
LiquidSean committed Aug 28, 2019
1 parent a99744a commit 3d90433
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
return require('./components/NetworkConsumer').default;
},
get reducer() {
return require('./redux/reducer').default;
return require('./redux/reducer').createReducer;
},
get createNetworkMiddleware() {
return require('./redux/createNetworkMiddleware').default;
Expand Down
60 changes: 34 additions & 26 deletions test/reducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
networkSelector,
} from '../src/redux/reducer';
import * as actionCreators from '../src/redux/actionCreators';
import getSimilarActionInQueue from '../src/utils/getSimilarActionInQueue';

const networkReducer = createReducer();

const getState = (isConnected = false, ...actionQueue) => ({
isConnected,
Expand Down Expand Up @@ -58,22 +61,22 @@ const prevActionToRetry1WithDifferentPayload = {

describe('unknown action type', () => {
it('returns prevState on initialization', () => {
expect(createReducer()(undefined, { type: 'ACTION_I_DONT_CARE' })).toEqual(
expect(networkReducer(undefined, { type: 'ACTION_I_DONT_CARE' })).toEqual(
initialState,
);
});

it('returns prevState if the action is not handled', () => {
expect(
createReducer()(initialState, { type: 'ANOTHER_ACTION_I_DONT_CARE' }),
networkReducer(initialState, { type: 'ANOTHER_ACTION_I_DONT_CARE' }),
).toEqual(initialState);
});
});

describe('CONNECTION_CHANGE action type', () => {
it('changes isConnected state properly', () => {
const mockAction = actionCreators.connectionChange(false);
expect(createReducer()(initialState, mockAction)).toEqual({
expect(networkReducer(initialState, mockAction)).toEqual({
isConnected: false,
actionQueue: [],
});
Expand Down Expand Up @@ -102,10 +105,8 @@ describe('OFFLINE_ACTION action type', () => {
const action = actionCreators.fetchOfflineMode(prevAction);
const anotherAction = actionCreators.fetchOfflineMode(anotherPrevAction);

expect(createReducer()(initialState, action)).toEqual(initialState);
expect(createReducer()(initialState, anotherAction)).toEqual(
initialState,
);
expect(networkReducer(initialState, action)).toEqual(initialState);
expect(networkReducer(initialState, anotherAction)).toEqual(initialState);
});
});

Expand All @@ -114,17 +115,17 @@ describe('OFFLINE_ACTION action type', () => {
it('actions are pushed into the queue in order of arrival', () => {
const preAction = actionCreators.connectionChange(false);
const action1 = actionCreators.fetchOfflineMode(prevActionToRetry1);
const prevState = createReducer()(initialState, preAction);
const prevState = networkReducer(initialState, preAction);

let nextState = createReducer()(prevState, action1);
let nextState = networkReducer(prevState, action1);

expect(nextState).toEqual({
isConnected: false,
actionQueue: [prevActionToRetry1],
});

const action2 = actionCreators.fetchOfflineMode(prevActionToRetry2);
nextState = createReducer()(nextState, action2);
nextState = networkReducer(nextState, action2);

expect(nextState).toEqual(
getState(false, prevActionToRetry1, prevActionToRetry2),
Expand All @@ -139,6 +140,7 @@ describe('OFFLINE_ACTION action type', () => {
}
thunk.meta = {
args: { id, name, age },
retry: true,
};
return thunk;
};
Expand All @@ -148,23 +150,29 @@ describe('OFFLINE_ACTION action type', () => {
const thunk = actionCreators.fetchOfflineMode(
thunkFactory(2, 'Link', 54),
);
const nextState = createReducer(comparisonFn)(prevState, thunk);

expect(nextState).not.toEqual(
getState(false, thunkFactory(1, 'Bilbo', 55)),
expect(getSimilarActionInQueue(thunk, prevState.actionQueue)).toEqual(
prevState.actionQueue[0].action,
);

const nextState = createReducer(comparisonFn)(prevState, thunk);

expect(nextState.actionQueue).toHaveLength(2);
});

it(`should replace a thunk if thunk already exists to modify same item`, () => {
const prevState = getState(false, thunkFactory(1, 'Bilbo', 55));
const thunk = actionCreators.fetchOfflineMode(
thunkFactory(1, 'Bilbo', 65),
);
const nextState = createReducer(comparisonFn)(prevState, thunk);

expect(nextState).not.toEqual(
getState(false, thunkFactory(1, 'Bilbo', 55)),
expect(getSimilarActionInQueue(thunk, prevState.actionQueue)).toEqual(
prevState.actionQueue[0].action,
);

const nextState = createReducer(comparisonFn)(prevState, thunk);

expect(nextState.actionQueue).toHaveLength(1);
});
});

Expand All @@ -178,7 +186,7 @@ describe('OFFLINE_ACTION action type', () => {
);
const action = actionCreators.fetchOfflineMode(prevActionToRetry1);

const nextState = createReducer()(prevState, action);
const nextState = networkReducer(prevState, action);
expect(nextState).toEqual(
getState(false, prevActionToRetry2, prevActionToRetry1),
);
Expand All @@ -194,7 +202,7 @@ describe('OFFLINE_ACTION action type', () => {
prevActionToRetry1WithDifferentPayload,
);

expect(createReducer()(prevState, action)).toEqual(
expect(networkReducer(prevState, action)).toEqual(
getState(
false,
prevActionToRetry2,
Expand All @@ -220,7 +228,7 @@ describe('REMOVE_ACTION_FROM_QUEUE action type', () => {
...prevActionToRetry2,
});

expect(createReducer()(prevState, action)).toEqual(
expect(networkReducer(prevState, action)).toEqual(
getState(
false,
prevActionToRetry1,
Expand All @@ -239,7 +247,7 @@ describe('thunks', () => {
describe('action with meta.retry !== true', () => {
it('should NOT add the action to the queue', () => {
const action = actionCreators.fetchOfflineMode(fetchThunk);
expect(createReducer()(initialState, action)).toEqual(initialState);
expect(networkReducer(initialState, action)).toEqual(initialState);
});
});

Expand All @@ -251,7 +259,7 @@ describe('thunks', () => {
};
const action = actionCreators.fetchOfflineMode(fetchThunk);

expect(createReducer()(prevState, action)).toEqual(
expect(networkReducer(prevState, action)).toEqual(
getState(false, fetchThunk),
);
});
Expand All @@ -272,7 +280,7 @@ describe('thunks', () => {
retry: true,
};
const action = actionCreators.fetchOfflineMode(similarThunk);
const nextState = createReducer()(prevState, action);
const nextState = networkReducer(prevState, action);

expect(nextState).toEqual(getState(false, similarThunk));
});
Expand All @@ -284,7 +292,7 @@ describe('thunks', () => {
const prevState = getState(false, fetchThunk);
const action = actionCreators.removeActionFromQueue(fetchThunk);

expect(createReducer()(prevState, action)).toEqual(getState(false));
expect(networkReducer(prevState, action)).toEqual(getState(false));
});
});
});
Expand Down Expand Up @@ -329,7 +337,7 @@ describe('dismiss feature', () => {
);
const action = actionCreators.dismissActionsFromQueue('NAVIGATE_BACK');

expect(createReducer()(prevState, action)).toEqual(
expect(networkReducer(prevState, action)).toEqual(
getState(false, actionEnqueued2, actionEnqueued3),
);
});
Expand All @@ -343,7 +351,7 @@ describe('dismiss feature', () => {
);
const action = actionCreators.dismissActionsFromQueue('NAVIGATE_TO_LOGIN');

expect(createReducer()(prevState, action)).toEqual(
expect(networkReducer(prevState, action)).toEqual(
getState(false, actionEnqueued3),
);
});
Expand All @@ -357,7 +365,7 @@ describe('dismiss feature', () => {
);
const action = actionCreators.dismissActionsFromQueue('NAVIGATE_AWAY');

expect(createReducer()(prevState, action)).toEqual(
expect(networkReducer(prevState, action)).toEqual(
getState(false, actionEnqueued1, actionEnqueued2, actionEnqueued3),
);
});
Expand Down

0 comments on commit 3d90433

Please sign in to comment.