-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michael A Tomcal
committed
Sep 15, 2020
1 parent
eb2f271
commit a847d60
Showing
10 changed files
with
568 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* eslint-disable jest/no-export */ | ||
import { createStore } from 'redux'; | ||
import * as actions from '../../src/cache/actions'; | ||
import { ACTION_NAMESPACE } from '../../src/cache/constants'; | ||
|
||
export const createScenario = (dispatch, actionKeys, hash) => { | ||
const fakeError = new Error('Fake Error'); | ||
const fakeData = { | ||
status: 200, | ||
ok: true, | ||
body: { | ||
fakeData: true, | ||
}, | ||
}; | ||
const actionOrder = actionKeys.map((key) => actions[key]); | ||
actionOrder.forEach((actionCreator) => { | ||
dispatch( | ||
actionCreator({ | ||
hash, | ||
error: fakeError, | ||
value: fakeData, | ||
}) | ||
); | ||
}); | ||
}; | ||
|
||
export function testCacheInterface(CacheFunc) { | ||
const cache = CacheFunc(); | ||
let store; | ||
beforeEach(() => { | ||
store = createStore(cache.reducer, cache.reducer(undefined, { type: '' })); | ||
}); | ||
it('should reflect load state', () => { | ||
const { dispatch, getState } = store; | ||
createScenario(dispatch, ['loadingAction'], 'abc1234'); | ||
expect(getState()).toMatchSnapshot(); | ||
}); | ||
it('should reflect load to success state', () => { | ||
const { dispatch, getState } = store; | ||
createScenario(dispatch, ['loadingAction', 'setAction'], 'abc1234'); | ||
expect(getState()).toMatchSnapshot(); | ||
}); | ||
it('should reflect 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', () => { | ||
const { dispatch, getState } = store; | ||
createScenario(dispatch, ['loadingAction', 'setAction', 'deleteAction'], 'abc1234'); | ||
expect(getState()).toMatchSnapshot(); | ||
}); | ||
it('should reflect 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', () => { | ||
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`, () => { | ||
const { dispatch, getState } = store; | ||
dispatch({ type: ACTION_NAMESPACE }); | ||
expect(getState()).toMatchSnapshot(); | ||
}); | ||
|
||
describe('getCacheByKey', () => { | ||
it('should return data error loading', () => { | ||
const { dispatch, getState } = store; | ||
createScenario(dispatch, ['loadingAction', 'setAction'], 'abc1234'); | ||
createScenario(dispatch, ['loadingAction', 'errorAction'], 'def5678'); | ||
expect(cache.getCacheByKey(getState(), 'abc1234')).toMatchSnapshot(); | ||
}); | ||
it('should return empty data error loading if cache undefined', () => { | ||
expect(cache.getCacheByKey(undefined, 'abc1234')).toMatchSnapshot(); | ||
}); | ||
it('should accept 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('should return default cacheSelector', () => { | ||
const { dispatch, getState } = store; | ||
createScenario(dispatch, ['loadingAction', 'errorAction'], 'def5678'); | ||
const nextCache = CacheFunc(); | ||
expect(nextCache.cacheSelector(getState())).toMatchSnapshot(); | ||
}); | ||
}); | ||
} |
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,21 @@ | ||
/* | ||
* Copyright 2020 American Express Travel Related Services Company, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
import ImmutableCache from '../../src/cache/ImmutableCache'; | ||
import { testCacheInterface } from '../__utils__/testCacheInterface'; | ||
|
||
describe('ImmutableCache', () => { | ||
testCacheInterface(ImmutableCache); | ||
}); |
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,21 @@ | ||
/* | ||
* Copyright 2020 American Express Travel Related Services Company, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
import SimpleCache from '../../src/cache/SimpleCache'; | ||
import { testCacheInterface } from '../__utils__/testCacheInterface'; | ||
|
||
describe('SimpleCache', () => { | ||
testCacheInterface(SimpleCache); | ||
}); |
121 changes: 121 additions & 0 deletions
121
__tests__/cache/__snapshots__/ImmutableCache.spec.js.snap
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,121 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`ImmutableCache getCacheByKey should accept a cacheSelector 1`] = ` | ||
Immutable.Map { | ||
"errors": Immutable.Map { | ||
"def5678": [Error: Fake Error], | ||
}, | ||
"loading": Immutable.Set [], | ||
"data": 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`] = ` | ||
Object { | ||
"data": undefined, | ||
"error": undefined, | ||
"loading": false, | ||
} | ||
`; | ||
|
||
exports[`ImmutableCache should reflect load state 1`] = ` | ||
Immutable.Map { | ||
"errors": Immutable.Map {}, | ||
"loading": Immutable.Set [ | ||
"abc1234", | ||
], | ||
"data": Immutable.Map {}, | ||
} | ||
`; | ||
|
||
exports[`ImmutableCache should reflect load to error state 1`] = ` | ||
Immutable.Map { | ||
"errors": Immutable.Map { | ||
"abc1234": [Error: Fake Error], | ||
}, | ||
"loading": Immutable.Set [], | ||
"data": Immutable.Map {}, | ||
} | ||
`; | ||
|
||
exports[`ImmutableCache should reflect 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`] = ` | ||
Immutable.Map { | ||
"errors": Immutable.Map {}, | ||
"loading": Immutable.Set [], | ||
"data": Immutable.Map { | ||
"abc1234": Object { | ||
"body": Object { | ||
"fakeData": true, | ||
}, | ||
"ok": true, | ||
"status": 200, | ||
}, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`ImmutableCache should reflect 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`] = ` | ||
Immutable.Map { | ||
"errors": Immutable.Map { | ||
"def5678": [Error: Fake Error], | ||
}, | ||
"loading": Immutable.Set [], | ||
"data": Immutable.Map { | ||
"abc1234": Object { | ||
"body": Object { | ||
"fakeData": true, | ||
}, | ||
"ok": true, | ||
"status": 200, | ||
}, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`ImmutableCache should return default state if unknown @fetchye action type 1`] = ` | ||
Immutable.Map { | ||
"errors": Immutable.Map {}, | ||
"loading": Immutable.Set [], | ||
"data": Immutable.Map {}, | ||
} | ||
`; |
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,121 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`SimpleCache getCacheByKey should accept a cacheSelector 1`] = ` | ||
Object { | ||
"data": Object {}, | ||
"errors": Object { | ||
"def5678": [Error: Fake Error], | ||
}, | ||
"loading": 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`] = ` | ||
Object { | ||
"data": undefined, | ||
"error": undefined, | ||
"loading": false, | ||
} | ||
`; | ||
|
||
exports[`SimpleCache should reflect load state 1`] = ` | ||
Object { | ||
"data": Object {}, | ||
"errors": Object {}, | ||
"loading": Object { | ||
"abc1234": "abc1234", | ||
}, | ||
} | ||
`; | ||
|
||
exports[`SimpleCache should reflect load to error state 1`] = ` | ||
Object { | ||
"data": Object {}, | ||
"errors": Object { | ||
"abc1234": [Error: Fake Error], | ||
}, | ||
"loading": Object {}, | ||
} | ||
`; | ||
|
||
exports[`SimpleCache should reflect load to error to clear errors state 1`] = ` | ||
Object { | ||
"data": Object {}, | ||
"errors": Object {}, | ||
"loading": Object {}, | ||
} | ||
`; | ||
|
||
exports[`SimpleCache should reflect load 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 delete state 1`] = ` | ||
Object { | ||
"data": Object {}, | ||
"errors": Object {}, | ||
"loading": Object {}, | ||
} | ||
`; | ||
|
||
exports[`SimpleCache should reflect multiple hashes stored 1`] = ` | ||
Object { | ||
"data": Object { | ||
"abc1234": Object { | ||
"body": Object { | ||
"fakeData": true, | ||
}, | ||
"ok": true, | ||
"status": 200, | ||
}, | ||
}, | ||
"errors": Object { | ||
"def5678": [Error: Fake Error], | ||
}, | ||
"loading": Object {}, | ||
} | ||
`; | ||
|
||
exports[`SimpleCache should return default state if unknown @fetchye action type 1`] = ` | ||
Object { | ||
"data": Object {}, | ||
"errors": Object {}, | ||
"loading": Object {}, | ||
} | ||
`; |
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.