Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
fix(vue): collection vue mixin reactivity on computed value fixed #93
Browse files Browse the repository at this point in the history
  • Loading branch information
michiel committed Aug 1, 2019
1 parent 87184bc commit 7e001f5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/vue/__tests__/Mixin/Collection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ describe('The Collection mixin', () => {

mixin.mounted.call(mixin);
expect(mockCollection.load).toHaveBeenCalled();

mixin.created.call(mixin);
expect(mixin.$store.commit).toHaveBeenCalled();
});

test('that the mixin handles errors on serverPrefetch', () => {
Expand Down
22 changes: 21 additions & 1 deletion packages/vue/__tests__/Module/createStoreModule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('The createStoreModule', () => {
expect(products.load).toHaveBeenCalled();
});

test('that it is possible to a resource in the store', () => {
test('that it is possible to get a non existing resource in the store', () => {
const product = Resource.create('1', 'product', { title: 'A great product' });

const mockRepository = {
Expand All @@ -150,10 +150,30 @@ describe('The createStoreModule', () => {
const mockModule = { state: { resources: {} }, commit: jest.fn() };
return module.actions.LOAD_RESOURCE(mockModule, '1').then(() => {
expect(mockRepository.findById).toHaveBeenCalledWith('1');
expect(mockModule.commit).toHaveBeenCalledTimes(2);
expect(mockModule.commit).toHaveBeenCalledWith('SET_RESOURCE', product);
});
});

test('that it is possible to an existing resource in the store', () => {
const product = Resource.create('1', 'product', { title: 'A great product' });

const mockRepository = {
resourceType: 'product',
findById: jest.fn(() => Promise.resolve(product)),
};

const module = createStoreModule(mockRepository, {
commit: jest.fn(),
});

const mockModule = { state: { resources: { 1: product.state } }, commit: jest.fn() };
return module.actions.LOAD_RESOURCE(mockModule, '1').then(() => {
expect(mockRepository.findById).toHaveBeenCalledWith('1');
expect(mockModule.commit).toHaveBeenCalledTimes(1);
expect(mockModule.commit).toHaveBeenCalledWith('SET_RESOURCE', product);
});
});

test('that the Vuex store is correctly initialized', () => {
const product = Resource.create('1', 'product', { title: 'A great product' });
Expand Down
8 changes: 8 additions & 0 deletions packages/vue/src/Mixin/Collection.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Collection from '@hyral/core/lib/Resource/Collection';

export default {
computed: {
collection() {
Expand All @@ -12,6 +14,9 @@ export default {
return collection;
},
},
created() {
this.initCollection();
},
/**
* Execute server prefetch actions.
*
Expand All @@ -34,6 +39,9 @@ export default {
this.loadCollection();
},
methods: {
initCollection() {
this.$store.commit(`hyral_${this.resourceType}/SET_COLLECTION`, Collection.create(this.collectionName));
},
loadCollection() {
return this.collection.load();
},
Expand Down

0 comments on commit 7e001f5

Please sign in to comment.