Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/plugins/cache/operations/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function decorateUpdate(c, cache, notify, e, aFn) {
return (eValue, ...args) => {
return aFn(eValue, ...args)
.then(passThrough(() => Cache.invalidateQuery(cache, e, aFn)))
.then(passThrough(() => Cache.storeEntity(cache, e, addId(c, undefined, undefined, eValue))))
.then(passThrough(() => Cache.storeEntity(cache, e, addId(c, aFn, undefined, eValue))))
.then(passThrough(() => notify([eValue, ...args], eValue)));
};
}
41 changes: 28 additions & 13 deletions src/plugins/cache/operations/update.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable no-unused-expressions */

import sinon from 'sinon';
import {curry} from 'ladda-fp';
import {decorateUpdate} from './update';
import { curry } from 'ladda-fp';
import { decorateUpdate } from './update';
import * as Cache from '../cache';
import {createApiFunction} from '../test-helper';
import { createApiFunction } from '../test-helper';

const curryNoop = () => () => {};

Expand All @@ -13,9 +13,9 @@ const config = [
name: 'user',
ttl: 300,
api: {
getUsers: (x) => x,
getUsers2: (x) => x,
deleteUser: (x) => x
getUsers: x => x,
getUsers2: x => x,
deleteUser: x => x
},
invalidates: ['user'],
invalidatesOn: ['GET']
Expand All @@ -24,8 +24,8 @@ const config = [
name: 'userPreview',
ttl: 200,
api: {
getPreviews: (x) => x,
updatePreview: (x) => x
getPreviews: x => x,
updatePreview: x => x
},
invalidates: ['fda'],
viewOf: 'user'
Expand All @@ -34,8 +34,8 @@ const config = [
name: 'listUser',
ttl: 200,
api: {
getPreviews: (x) => x,
updatePreview: (x) => x
getPreviews: x => x,
updatePreview: x => x
},
invalidates: ['fda'],
viewOf: 'user'
Expand All @@ -47,13 +47,13 @@ describe('Update', () => {
it('Updates cache based on argument', () => {
const cache = Cache.createCache(config);
const e = config[0];
const xOrg = {id: 1, name: 'Kalle'};
const xOrg = { id: 1, name: 'Kalle' };
const aFnWithoutSpy = createApiFunction(() => Promise.resolve(xOrg));
const aFn = sinon.spy(aFnWithoutSpy);

const res = decorateUpdate({}, cache, curryNoop, e, aFn);
return res(xOrg, 'other args').then(() => {
expect(Cache.getEntity(cache, e, 1).value).to.deep.equal({...xOrg, __ladda__id: 1});
expect(Cache.getEntity(cache, e, 1).value).to.deep.equal({ ...xOrg, __ladda__id: 1 });
});
});

Expand All @@ -63,7 +63,7 @@ describe('Update', () => {

const cache = Cache.createCache(config);
const e = config[0];
const xOrg = {id: 1, name: 'Kalle'};
const xOrg = { id: 1, name: 'Kalle' };
const aFnWithoutSpy = createApiFunction(() => Promise.resolve(xOrg));
const aFn = sinon.spy(aFnWithoutSpy);

Expand All @@ -74,4 +74,19 @@ describe('Update', () => {
});
});
});
describe('with idFrom set', () => {
it('Updates the cache based on idFrom', () => {
const cache = Cache.createCache(config);
const e = config[0];
const xOrg = { objectId: 1, name: 'Kalle' };
const aFnWithoutSpy = createApiFunction(() => Promise.resolve(xOrg),
{ idFrom: o => o.objectId });
const aFn = sinon.spy(aFnWithoutSpy);

const res = decorateUpdate({}, cache, curryNoop, e, aFn);
return res(xOrg, 'other args').then(() => {
expect(Cache.getEntity(cache, e, 1).value).to.deep.equal({ ...xOrg, __ladda__id: 1 });
});
});
});
});