Skip to content

Commit 2f0b08d

Browse files
committed
fix(read) Fix READ after a delete operation, document through tests
1 parent 561d3bd commit 2f0b08d

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ladda-cache",
3-
"version": "0.2.11",
3+
"version": "0.2.12",
44
"description": "Data fetching layer with support for caching",
55
"main": "dist/bundle.js",
66
"dependencies": {

src/builder.spec.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,52 @@ describe('builder', () => {
122122

123123
Promise.resolve()
124124
.then(() => api.user.getUsers())
125-
.then(() => api.user.deleteUser(1))
125+
.then(() => api.user.deleteUser('1'))
126126
.then(() => api.user.getUsers())
127127
.then(expectUserToBeRemoved);
128128
});
129129

130+
describe('after deletion', () => {
131+
it('calls the apiFn again when marked as byId === true', () => {
132+
const myConfig = config();
133+
const spy = sinon.spy();
134+
135+
const getUserById = () => {
136+
spy();
137+
return Promise.resolve({ id: 4 });
138+
};
139+
getUserById.operation = 'READ';
140+
getUserById.byId = true;
141+
142+
myConfig.user.api.getUserById = getUserById;
143+
144+
const api = build(myConfig);
145+
146+
return Promise.resolve()
147+
.then(() => api.user.getUserById('1'))
148+
.then(() => api.user.deleteUser('1'))
149+
.then(() => api.user.getUserById('1'))
150+
.then(() => {
151+
expect(spy).to.have.been.calledTwice;
152+
});
153+
});
154+
155+
it('returns null for normal read operation when entity is requested again', () => {
156+
const myConfig = config();
157+
158+
const api = build(myConfig);
159+
160+
return Promise.resolve()
161+
.then(() => api.user.getUser('1'))
162+
.then(() => api.user.deleteUser('1'))
163+
.then(() => api.user.getUser('1'))
164+
.then((user) => {
165+
expect(user).to.be.null;
166+
});
167+
});
168+
});
169+
170+
130171
it('TTL set to zero means we never get a cache hit', (done) => {
131172
const myConfig = config();
132173
myConfig.user.ttl = 0;

src/plugins/cache/operations/read.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const decorateReadQuery = (c, cache, notify, e, aFn) => {
6161
if (Cache.containsQueryResponse(cache, e, aFn, args) && !aFn.alwaysGetFreshData) {
6262
const v = Cache.getQueryResponseWithMeta(cache, c, e, aFn, args);
6363
if (!Cache.hasExpired(cache, e, v)) {
64-
return Promise.resolve(removeId(Cache.getQueryResponse(v.value)));
64+
return Promise.resolve(v.value ? removeId(Cache.getQueryResponse(v.value)) : null);
6565
}
6666
}
6767

0 commit comments

Comments
 (0)