Skip to content

Commit

Permalink
fix(cache): remove previous data or error on new run
Browse files Browse the repository at this point in the history
if there is now data remove the error from the previous run
if there is now an error remove the data from the previous run
  • Loading branch information
PixnBits committed Sep 22, 2022
1 parent 902c677 commit 743f3e0
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
33 changes: 32 additions & 1 deletion packages/fetchye-immutable-cache/__tests__/reducer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
deleteAction,
errorAction,
clearErrorsAction,

} from 'fetchye-core';
import reducer from '../src/reducer';

Expand Down Expand Up @@ -127,6 +126,38 @@ describe('Immutable Reducer', () => {
}
`);
});
it('should reflect load to success to error state', () => {
const { dispatch, getState } = store;
createScenario(dispatch, ['loadingAction', 'setAction', 'errorAction'], 'abc1234');
expect(getState()).toMatchInlineSnapshot(`
Immutable.Map {
"errors": Immutable.Map {
"abc1234": [Error: Fake Error],
},
"loading": Immutable.Set [],
"data": Immutable.Map {},
}
`);
});
it('should reflect load to error to success state', () => {
const { dispatch, getState } = store;
createScenario(dispatch, ['loadingAction', 'errorAction', 'setAction'], 'abc1234');
expect(getState()).toMatchInlineSnapshot(`
Immutable.Map {
"errors": Immutable.Map {},
"loading": Immutable.Set [],
"data": Immutable.Map {
"abc1234": Object {
"body": Object {
"fakeData": true,
},
"ok": true,
"status": 200,
},
},
}
`);
});
it('should reflect multiple hashes stored', () => {
const { dispatch, getState } = store;
createScenario(dispatch, ['loadingAction', 'setAction'], 'abc1234');
Expand Down
4 changes: 3 additions & 1 deletion packages/fetchye-immutable-cache/src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ export function fetchyeReducer(state = iMap({
return state.withMutations(
(nextState) => nextState
.setIn(['errors', action.hash], action.error)
.deleteIn(['data', action.hash])
.deleteIn(['loading', action.hash])
);
case IS_LOADING:
return state.updateIn(['loading'], (loading) => loading.add(action.hash));
case SET_DATA:
return state.withMutations(
(nextState) => nextState
.deleteIn(['loading', action.hash])
.setIn(['data', action.hash], action.value)
.deleteIn(['loading', action.hash])
.deleteIn(['errors', action.hash])
);
default:
return state;
Expand Down
10 changes: 10 additions & 0 deletions packages/fetchye-test-utils/src/testCacheInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ export function testCacheInterface(CacheFunc) {
createScenario(dispatch, ['loadingAction', 'errorAction', 'clearErrorsAction'], 'abc1234');
expect(getState()).toMatchSnapshot();
});
it('should reflect load to success to error state', () => {
const { dispatch, getState } = store;
createScenario(dispatch, ['loadingAction', 'setAction', 'errorAction'], 'abc1234');
expect(getState()).toMatchSnapshot();
});
it('should reflect load to error to success state', () => {
const { dispatch, getState } = store;
createScenario(dispatch, ['loadingAction', 'errorAction', 'setAction'], 'abc1234');
expect(getState()).toMatchSnapshot();
});
it('reflects multiple hashes stored', () => {
const { dispatch, getState } = store;
createScenario(dispatch, ['loadingAction', 'setAction'], 'abc1234');
Expand Down
26 changes: 26 additions & 0 deletions packages/fetchye/__tests__/__snapshots__/SimpleCache.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,29 @@ Object {
"loading": Object {},
}
`;

exports[`SimpleCache should reflect load to error to success state 1`] = `
Object {
"data": Object {
"abc1234": Object {
"body": Object {
"fakeData": true,
},
"ok": true,
"status": 200,
},
},
"errors": Object {},
"loading": Object {},
}
`;

exports[`SimpleCache should reflect load to success to error state 1`] = `
Object {
"data": Object {},
"errors": Object {
"abc1234": [Error: Fake Error],
},
"loading": Object {},
}
`;
12 changes: 10 additions & 2 deletions packages/fetchye/src/SimpleCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ function reducer(state = {
};
}
case ERROR: {
const { [action.hash]: deletedEntry, ...nextLoading } = state.loading;
const { [action.hash]: deletedLoadingEntry, ...nextLoading } = state.loading;
const { [action.hash]: deletedDataEntry, ...nextData } = state.data;
return {
...state,
data: {
...nextData,
},
errors: {
...state.errors,
[action.hash]: action.error,
Expand All @@ -75,13 +79,17 @@ function reducer(state = {
};
}
case SET_DATA: {
const { [action.hash]: deletedEntry, ...nextLoading } = state.loading;
const { [action.hash]: deletedLoadingEntry, ...nextLoading } = state.loading;
const { [action.hash]: deletedErrorEntry, ...nextErrors } = state.errors;
return {
...state,
data: {
...state.data,
[action.hash]: action.value,
},
errors: {
...nextErrors,
},
loading: {
...nextLoading,
},
Expand Down

0 comments on commit 743f3e0

Please sign in to comment.