fix: cache update not being executed correctly (#283)

* test: added flaky test

* fix: fixed flaky test

* style: formatted code
This commit is contained in:
Arthur Fiorette 2022-06-29 22:04:28 -03:00 committed by GitHub
parent 03cc615e67
commit cf97535d33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 12 deletions

View File

@ -1,6 +1,6 @@
{
"unreleased": true,
"commitLimit": false,
"sortCommits": "subject",
"template": "keepachangelog"
}
{
"unreleased": true,
"commitLimit": false,
"sortCommits": "subject",
"template": "keepachangelog"
}

View File

@ -65,6 +65,11 @@ export function defaultResponseInterceptor(
const config = response.config;
const cache = await axios.storage.get(id, config);
// Update other entries before updating himself
if (cacheConfig?.update) {
await updateCache(axios.storage, response, cacheConfig.update);
}
if (
// If the request interceptor had a problem or it wasn't cached
cache.state !== 'loading'
@ -72,7 +77,7 @@ export function defaultResponseInterceptor(
if (__ACI_DEV__) {
axios.debug?.({
id,
msg: 'Response not cached but storage is not loading',
msg: "Response not cached and storage isn't loading",
data: { cache, response }
});
}
@ -157,11 +162,6 @@ export function defaultResponseInterceptor(
});
}
// Update other entries before updating himself
if (cacheConfig?.update) {
await updateCache(axios.storage, response, cacheConfig.update);
}
const newCache: CachedStorageValue = {
state: 'cached',
ttl,
@ -171,6 +171,7 @@ export function defaultResponseInterceptor(
// Resolve all other requests waiting for this response
const waiting = axios.waiting[id];
if (waiting) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
waiting.resolve(newCache.data);

View File

@ -120,4 +120,24 @@ describe('Tests update-cache', () => {
const cacheValue = await axios.storage.get(cacheKey);
expect(cacheValue.state).toBe('loading');
});
it('tests updateCache with non cached updater', async () => {
const ID = 'cache-id';
const axios = mockAxios({ methods: ['get'] });
await axios.get('url', { id: ID });
// post isn't inside `methods`
await axios.post('url', true, {
cache: {
update: { [ID]: 'delete' }
}
});
// if the update did not get executed, the cache shouldn't be empty
const cacheValue = await axios.storage.get(ID);
expect(cacheValue.state).toBe('empty');
});
});