diff --git a/package.json b/package.json index 3d7bba9..72f4105 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ladda-cache", - "version": "0.2.11", + "version": "0.2.12", "description": "Data fetching layer with support for caching", "main": "dist/bundle.js", "dependencies": { diff --git a/src/builder.spec.js b/src/builder.spec.js index 515dd49..6a7ef35 100644 --- a/src/builder.spec.js +++ b/src/builder.spec.js @@ -122,11 +122,52 @@ describe('builder', () => { Promise.resolve() .then(() => api.user.getUsers()) - .then(() => api.user.deleteUser(1)) + .then(() => api.user.deleteUser('1')) .then(() => api.user.getUsers()) .then(expectUserToBeRemoved); }); + describe('after deletion', () => { + it('calls the apiFn again when marked as byId === true', () => { + const myConfig = config(); + const spy = sinon.spy(); + + const getUserById = () => { + spy(); + return Promise.resolve({ id: 4 }); + }; + getUserById.operation = 'READ'; + getUserById.byId = true; + + myConfig.user.api.getUserById = getUserById; + + const api = build(myConfig); + + return Promise.resolve() + .then(() => api.user.getUserById('1')) + .then(() => api.user.deleteUser('1')) + .then(() => api.user.getUserById('1')) + .then(() => { + expect(spy).to.have.been.calledTwice; + }); + }); + + it('returns null for normal read operation when entity is requested again', () => { + const myConfig = config(); + + const api = build(myConfig); + + return Promise.resolve() + .then(() => api.user.getUser('1')) + .then(() => api.user.deleteUser('1')) + .then(() => api.user.getUser('1')) + .then((user) => { + expect(user).to.be.null; + }); + }); + }); + + it('TTL set to zero means we never get a cache hit', (done) => { const myConfig = config(); myConfig.user.ttl = 0; diff --git a/src/plugins/cache/operations/read.js b/src/plugins/cache/operations/read.js index ea01d0e..1dcaeb1 100644 --- a/src/plugins/cache/operations/read.js +++ b/src/plugins/cache/operations/read.js @@ -61,7 +61,7 @@ const decorateReadQuery = (c, cache, notify, e, aFn) => { if (Cache.containsQueryResponse(cache, e, aFn, args) && !aFn.alwaysGetFreshData) { const v = Cache.getQueryResponseWithMeta(cache, c, e, aFn, args); if (!Cache.hasExpired(cache, e, v)) { - return Promise.resolve(removeId(Cache.getQueryResponse(v.value))); + return Promise.resolve(v.value ? removeId(Cache.getQueryResponse(v.value)) : null); } }