Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Collection parameterBag change detection not working #58
  • Loading branch information
michiel committed Jun 5, 2019
1 parent 9f64849 commit fffee99
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
40 changes: 40 additions & 0 deletions packages/core/__tests__/Resource/Collection.test.js
Expand Up @@ -158,4 +158,44 @@ describe('Collection tests', () => {
expect(collectionNoPaging.isLoaded).toBeTruthy();
});
});

test('that a collection will only load once if the parameterBag does not change', () => {
const repositoryFindOnceMock = jest.fn(() => Promise.resolve(mockResponse));

const productRepositoryOnceMock = {
find: repositoryFindOnceMock,
};

const collectionOnce = Collection.create('product', productRepositoryOnceMock);

return collectionOnce.load().then(() => {
expect(repositoryFindOnceMock).toHaveBeenCalledTimes(1);

collection.load().then(() => {
expect(repositoryFindOnceMock).toHaveBeenCalledTimes(1);
});
});
});

test('that a collection will only load items again if the parameterBag had changed after loading', () => {
const repositoryFindOnceMock = jest.fn(() => Promise.resolve(mockResponse));

const productRepositoryOnceMock = {
find: repositoryFindOnceMock,
};

const collectionOnce = Collection.create('product', productRepositoryOnceMock);

return collectionOnce.load().then(() => {
expect(repositoryFindOnceMock).toHaveBeenCalledTimes(1);

const paramBag = collectionOnce.parameterBag;
paramBag.setParams({ test: 'param' });
collectionOnce.parameterBag = paramBag;

collectionOnce.load().then(() => {
expect(repositoryFindOnceMock).toHaveBeenCalledTimes(2);
});
});
});
});
5 changes: 3 additions & 2 deletions packages/core/src/Resource/Collection.js
@@ -1,4 +1,5 @@
import cloneDeep from 'lodash/cloneDeep';
import isEqual from 'lodash/isEqual';
import ParameterBag from './ParameterBag';
import {
currentState,
Expand All @@ -17,7 +18,7 @@ import collectionParameterBagDecorator
function collectionLoad(collection) {
return () => {
if (collection.isLoaded === true
&& collection.state.metadata.lastParameterBagState === collection.parameterBag.stateId) {
&& isEqual(collection.state.metadata.lastParameterBagState, collection.parameterBag.state)) {
return Promise.resolve();
}

Expand All @@ -33,7 +34,7 @@ function collectionLoad(collection) {
metadata: Object.assign({}, collection.state.metadata, {
loading: false,
loaded: true,
lastParameterBagState: collection.parameterBag.stateId,
lastParameterBagState: collection.parameterBag.state,
paging: response.paging
? response.paging
: { pages: 0, count: response.data.length },
Expand Down

0 comments on commit fffee99

Please sign in to comment.