-
Notifications
You must be signed in to change notification settings - Fork 16
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
Showing
7 changed files
with
168 additions
and
49 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
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
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
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
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,9 @@ | ||
import {passThrough} from 'ladda-fp'; | ||
import {invalidateQuery} from '../cache'; | ||
|
||
export function decorateNotifier(c, cache, e, aFn) { | ||
return (...args) => { | ||
return aFn(...args) | ||
.then(passThrough(() => invalidateQuery(cache, e, aFn))); | ||
}; | ||
} |
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,62 @@ | ||
/* eslint-disable no-unused-expressions */ | ||
|
||
import sinon from 'sinon'; | ||
import {decorateNotifier} from './notifier'; | ||
import * as Cache from '../cache'; | ||
import {createSampleConfig} from '../test-helper'; | ||
|
||
const config = createSampleConfig(); | ||
|
||
describe('DecorateNotifier', () => { | ||
it('Invalidates as if it was the specified operation', (done) => { | ||
const cache = Cache.createCache(config); | ||
const e = config[0]; | ||
e.invalidatesOn = ['READ']; | ||
const xOrg = {__ladda__id: 1, name: 'Kalle'}; | ||
const aFn = sinon.spy(() => Promise.resolve({})); | ||
const getUsers = () => Promise.resolve(xOrg); | ||
aFn.operation = 'READ'; | ||
aFn.isNotifier = true; | ||
aFn.invalidates = []; | ||
Cache.storeQueryResponse(cache, e, getUsers, ['args'], xOrg); | ||
const res = decorateNotifier({}, cache, e, aFn); | ||
res(xOrg).then(() => { | ||
const killedCache = !Cache.containsQueryResponse(cache, e, getUsers, ['args']); | ||
expect(killedCache).to.be.true; | ||
done(); | ||
}); | ||
}); | ||
it('Does not invalidate if operation does not cause invalidation', (done) => { | ||
const cache = Cache.createCache(config); | ||
const e = config[0]; | ||
e.invalidatesOn = ['READ']; | ||
const xOrg = {__ladda__id: 1, name: 'Kalle'}; | ||
const aFn = sinon.spy(() => Promise.resolve({})); | ||
const getUsers = () => Promise.resolve(xOrg); | ||
aFn.operation = 'DELETE'; | ||
aFn.isNotifier = true; | ||
aFn.invalidates = []; | ||
Cache.storeQueryResponse(cache, e, getUsers, ['args'], xOrg); | ||
const res = decorateNotifier({}, cache, e, aFn); | ||
res(xOrg).then(() => { | ||
const killedCache = !Cache.containsQueryResponse(cache, e, getUsers, ['args']); | ||
expect(killedCache).to.be.false; | ||
done(); | ||
}); | ||
}); | ||
it('Does not write to cache', (done) => { | ||
const cache = Cache.createCache(config); | ||
const e = config[0]; | ||
const xOrg = {__ladda__id: 1, name: 'Kalle'}; | ||
const aFn = sinon.spy(() => Promise.resolve({})); | ||
aFn.operation = 'CREATE'; | ||
aFn.isNotifier = false; | ||
aFn.invalidates = []; | ||
const res = decorateNotifier({}, cache, e, aFn); | ||
res(xOrg).then(() => { | ||
const hasEntity = Cache.containsEntity(cache, e, 1); | ||
expect(hasEntity).to.be.false; | ||
done(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.