Skip to content

Commit

Permalink
fix(immitable-cache): correctly retrieve error
Browse files Browse the repository at this point in the history
  • Loading branch information
JAdshead authored Oct 21, 2020
1 parent 63c7e75 commit bd45e4d
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 215 deletions.
64 changes: 42 additions & 22 deletions __tests__/__utils__/testCacheInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { createStore } from 'redux';
import * as actions from '../../src/cache/actions';
import { ACTION_NAMESPACE } from '../../src/cache/constants';

const fakeError = new Error('Fake Error');
export const createScenario = (dispatch, actionKeys, hash) => {
const fakeError = new Error('Fake Error');
const fakeData = {
status: 200,
ok: true,
Expand All @@ -30,65 +30,85 @@ export function testCacheInterface(CacheFunc) {
beforeEach(() => {
store = createStore(cache.reducer, cache.reducer(undefined, { type: '' }));
});
it('should reflect load state', () => {
it('reflects load state', () => {
const { dispatch, getState } = store;
createScenario(dispatch, ['loadingAction'], 'abc1234');
expect(getState()).toMatchSnapshot();
});
it('should reflect load to success state', () => {
it('reflects load to success state', () => {
const { dispatch, getState } = store;
createScenario(dispatch, ['loadingAction', 'setAction'], 'abc1234');
expect(getState()).toMatchSnapshot();
});
it('should reflect load to error state', () => {
it('reflects load to error state', () => {
const { dispatch, getState } = store;
createScenario(dispatch, ['loadingAction', 'errorAction'], 'abc1234');
expect(getState()).toMatchSnapshot();
});
it('should reflect load to success to delete state', () => {
it('reflects load to success to delete state', () => {
const { dispatch, getState } = store;
createScenario(dispatch, ['loadingAction', 'setAction', 'deleteAction'], 'abc1234');
expect(getState()).toMatchSnapshot();
});
it('should reflect load to error to clear errors state', () => {
it('reflects load to error to clear errors state', () => {
const { dispatch, getState } = store;
createScenario(dispatch, ['loadingAction', 'errorAction', 'clearErrorsAction'], 'abc1234');
expect(getState()).toMatchSnapshot();
});
it('should reflect multiple hashes stored', () => {
it('reflects multiple hashes stored', () => {
const { dispatch, getState } = store;
createScenario(dispatch, ['loadingAction', 'setAction'], 'abc1234');
createScenario(dispatch, ['loadingAction', 'errorAction'], 'def5678');
expect(getState()).toMatchSnapshot();
});
it(`should return default state if unknown ${ACTION_NAMESPACE} action type`, () => {
it(`returns default state if unknown ${ACTION_NAMESPACE} action type`, () => {
const { dispatch, getState } = store;
dispatch({ type: ACTION_NAMESPACE });
expect(getState()).toMatchSnapshot();
});

it('accepts a cacheSelector', () => {
const { dispatch, getState } = store;
createScenario(dispatch, ['loadingAction', 'errorAction'], 'def5678');
const cacheSelector = (state) => state.someSliceOfState;
const nextCache = CacheFunc({ cacheSelector });
expect(nextCache.cacheSelector({ someSliceOfState: getState() })).toMatchSnapshot();
});

it('returns default cacheSelector', () => {
const { dispatch, getState } = store;
createScenario(dispatch, ['loadingAction', 'errorAction'], 'def5678');
const nextCache = CacheFunc();
expect(nextCache.cacheSelector(getState())).toMatchSnapshot();
});

describe('getCacheByKey', () => {
it('should return data error loading', () => {
it('returns correct data', () => {
const { dispatch, getState } = store;
createScenario(dispatch, ['loadingAction', 'setAction'], 'abc1234');
createScenario(dispatch, ['loadingAction', 'errorAction'], 'def5678');
expect(cache.getCacheByKey(getState(), 'abc1234')).toMatchSnapshot();
expect(cache.getCacheByKey(getState(), 'abc1234').loading).toEqual(false);
expect(cache.getCacheByKey(getState(), 'abc1234').data).toEqual({
body: { fakeData: true },
ok: true,
status: 200,
});
});
it('should return empty data error loading if cache undefined', () => {
expect(cache.getCacheByKey(undefined, 'abc1234')).toMatchSnapshot();
});
it('should accept a cacheSelector', () => {

it('returns correct loading value', () => {
const { dispatch, getState } = store;
createScenario(dispatch, ['loadingAction', 'errorAction'], 'def5678');
const cacheSelector = (state) => state.someSliceOfState;
const nextCache = CacheFunc({ cacheSelector });
expect(nextCache.cacheSelector({ someSliceOfState: getState() })).toMatchSnapshot();
createScenario(dispatch, ['loadingAction'], 'abc1234');
expect(cache.getCacheByKey(getState(), 'abc1234').loading).toEqual(true);
});
it('should return default cacheSelector', () => {

it('returns error', () => {
const { dispatch, getState } = store;
createScenario(dispatch, ['loadingAction', 'errorAction'], 'def5678');
const nextCache = CacheFunc();
expect(nextCache.cacheSelector(getState())).toMatchSnapshot();
expect(cache.getCacheByKey(getState(), 'def5678').loading).toEqual(false);
expect(cache.getCacheByKey(getState(), 'def5678').error).toEqual(fakeError);
});

it('returns empty data error loading if cache undefined', () => {
expect(cache.getCacheByKey(undefined, 'abc1234')).toMatchSnapshot();
});
});
}
52 changes: 19 additions & 33 deletions __tests__/cache/__snapshots__/ImmutableCache.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ImmutableCache getCacheByKey should accept a cacheSelector 1`] = `
exports[`ImmutableCache accepts a cacheSelector 1`] = `
Immutable.Map {
"errors": Immutable.Map {
"def5678": [Error: Fake Error],
Expand All @@ -10,39 +10,15 @@ Immutable.Map {
}
`;

exports[`ImmutableCache getCacheByKey should return data error loading 1`] = `
Object {
"data": Object {
"body": Object {
"fakeData": true,
},
"ok": true,
"status": 200,
},
"error": undefined,
"loading": false,
}
`;

exports[`ImmutableCache getCacheByKey should return default cacheSelector 1`] = `
Immutable.Map {
"errors": Immutable.Map {
"def5678": [Error: Fake Error],
},
"loading": Immutable.Set [],
"data": Immutable.Map {},
}
`;

exports[`ImmutableCache getCacheByKey should return empty data error loading if cache undefined 1`] = `
exports[`ImmutableCache getCacheByKey returns empty data error loading if cache undefined 1`] = `
Object {
"data": undefined,
"error": undefined,
"loading": false,
}
`;

exports[`ImmutableCache should reflect load state 1`] = `
exports[`ImmutableCache reflects load state 1`] = `
Immutable.Map {
"errors": Immutable.Map {},
"loading": Immutable.Set [
Expand All @@ -52,7 +28,7 @@ Immutable.Map {
}
`;

exports[`ImmutableCache should reflect load to error state 1`] = `
exports[`ImmutableCache reflects load to error state 1`] = `
Immutable.Map {
"errors": Immutable.Map {
"abc1234": [Error: Fake Error],
Expand All @@ -62,15 +38,15 @@ Immutable.Map {
}
`;

exports[`ImmutableCache should reflect load to error to clear errors state 1`] = `
exports[`ImmutableCache reflects load to error to clear errors state 1`] = `
Immutable.Map {
"errors": Immutable.Map {},
"loading": Immutable.Set [],
"data": Immutable.Map {},
}
`;

exports[`ImmutableCache should reflect load to success state 1`] = `
exports[`ImmutableCache reflects load to success state 1`] = `
Immutable.Map {
"errors": Immutable.Map {},
"loading": Immutable.Set [],
Expand All @@ -86,15 +62,15 @@ Immutable.Map {
}
`;

exports[`ImmutableCache should reflect load to success to delete state 1`] = `
exports[`ImmutableCache reflects load to success to delete state 1`] = `
Immutable.Map {
"errors": Immutable.Map {},
"loading": Immutable.Set [],
"data": Immutable.Map {},
}
`;

exports[`ImmutableCache should reflect multiple hashes stored 1`] = `
exports[`ImmutableCache reflects multiple hashes stored 1`] = `
Immutable.Map {
"errors": Immutable.Map {
"def5678": [Error: Fake Error],
Expand All @@ -112,7 +88,17 @@ Immutable.Map {
}
`;

exports[`ImmutableCache should return default state if unknown @fetchye action type 1`] = `
exports[`ImmutableCache returns default cacheSelector 1`] = `
Immutable.Map {
"errors": Immutable.Map {
"def5678": [Error: Fake Error],
},
"loading": Immutable.Set [],
"data": Immutable.Map {},
}
`;

exports[`ImmutableCache returns default state if unknown @fetchye action type 1`] = `
Immutable.Map {
"errors": Immutable.Map {},
"loading": Immutable.Set [],
Expand Down
52 changes: 19 additions & 33 deletions __tests__/cache/__snapshots__/SimpleCache.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`SimpleCache getCacheByKey should accept a cacheSelector 1`] = `
exports[`SimpleCache accepts a cacheSelector 1`] = `
Object {
"data": Object {},
"errors": Object {
Expand All @@ -10,39 +10,15 @@ Object {
}
`;

exports[`SimpleCache getCacheByKey should return data error loading 1`] = `
Object {
"data": Object {
"body": Object {
"fakeData": true,
},
"ok": true,
"status": 200,
},
"error": undefined,
"loading": false,
}
`;

exports[`SimpleCache getCacheByKey should return default cacheSelector 1`] = `
Object {
"data": Object {},
"errors": Object {
"def5678": [Error: Fake Error],
},
"loading": Object {},
}
`;

exports[`SimpleCache getCacheByKey should return empty data error loading if cache undefined 1`] = `
exports[`SimpleCache getCacheByKey returns empty data error loading if cache undefined 1`] = `
Object {
"data": undefined,
"error": undefined,
"loading": false,
}
`;

exports[`SimpleCache should reflect load state 1`] = `
exports[`SimpleCache reflects load state 1`] = `
Object {
"data": Object {},
"errors": Object {},
Expand All @@ -52,7 +28,7 @@ Object {
}
`;

exports[`SimpleCache should reflect load to error state 1`] = `
exports[`SimpleCache reflects load to error state 1`] = `
Object {
"data": Object {},
"errors": Object {
Expand All @@ -62,15 +38,15 @@ Object {
}
`;

exports[`SimpleCache should reflect load to error to clear errors state 1`] = `
exports[`SimpleCache reflects load to error to clear errors state 1`] = `
Object {
"data": Object {},
"errors": Object {},
"loading": Object {},
}
`;

exports[`SimpleCache should reflect load to success state 1`] = `
exports[`SimpleCache reflects load to success state 1`] = `
Object {
"data": Object {
"abc1234": Object {
Expand All @@ -86,15 +62,15 @@ Object {
}
`;

exports[`SimpleCache should reflect load to success to delete state 1`] = `
exports[`SimpleCache reflects load to success to delete state 1`] = `
Object {
"data": Object {},
"errors": Object {},
"loading": Object {},
}
`;

exports[`SimpleCache should reflect multiple hashes stored 1`] = `
exports[`SimpleCache reflects multiple hashes stored 1`] = `
Object {
"data": Object {
"abc1234": Object {
Expand All @@ -112,7 +88,17 @@ Object {
}
`;

exports[`SimpleCache should return default state if unknown @fetchye action type 1`] = `
exports[`SimpleCache returns default cacheSelector 1`] = `
Object {
"data": Object {},
"errors": Object {
"def5678": [Error: Fake Error],
},
"loading": Object {},
}
`;

exports[`SimpleCache returns default state if unknown @fetchye action type 1`] = `
Object {
"data": Object {},
"errors": Object {},
Expand Down
Loading

0 comments on commit bd45e4d

Please sign in to comment.