Skip to content

Commit

Permalink
fix(read) Fix READ after a delete operation, document through tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LFDM committed Aug 3, 2017
1 parent 561d3bd commit 2f0b08d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
43 changes: 42 additions & 1 deletion src/builder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/cache/operations/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 2f0b08d

Please sign in to comment.