import {EntitiesManager as EM} from '../../src/index'; import configureEntitiesManager from '../utils/configureEntitiesManager'; import configureStore from '../utils/configureStore'; import deleteCollection from '../utils/deleteCollection'; const em = configureEntitiesManager({ parent: { createdAt: EM.createdAt(), childrenCount: EM.count('child', [['value', '<=', 3]]), childrenSum: EM.sum('child', 'value', [['value', '>=', 4]]) }, child: { parentId: EM.parentId('parent'), createdAt: EM.createdAt(), updatedAt: EM.updatedAt(), value: EM.required() } }); const store = configureStore({}, { entities: em.getEntitiesReducer() }); describe('conditionalAggregation: ', () => { let parent = null; let children = null; describe('adding parent', () => { it('should resolve with item id(s)', () => store.dispatch(em.entity('parent').addItem({})) .then((result) => { const allParents = em.entity('parent').getAllItemList()(store.getState()); parent = allParents[0]; expect(result).toEqual(jasmine.any(String)); })); it('should have 0 as initial sum, count', () => { expect(parent).toEqual(jasmine.objectContaining({ childrenCount: 0, childrenSum: 0 })); }); }); describe('adding children', () => { it('should resolved with id(s)', () => { const addingChildrenPromises = []; for (let i = 1; i <= 5; i += 1) { addingChildrenPromises.push( store.dispatch(em.entity('child').addItem({ parentId: parent.id, value: i })) ); } return Promise.all(addingChildrenPromises) .then((results) => { expect(results).toEqual(jasmine.arrayContaining([ jasmine.any(String), jasmine.any(String), jasmine.any(String), jasmine.any(String), jasmine.any(String) ])); }) .then(() => { children = em.entity('child').getAllItemList()(store.getState()); }); }, 999999999); }); describe('aggregation result of parent', () => { let aggregatedParent = null; beforeAll( // ------ after ------ // count | sum // 1 2 3 | 4 5 // should be count=3, sum=9 () => store.dispatch(em.entity('parent').fetchItem(parent.id)) .then(() => { aggregatedParent = em.entity('parent').getAllItemList()(store.getState())[0]; }) ); it('count must be 3', () => { expect(aggregatedParent.childrenCount).toEqual(3); }); it('sum must be 9', () => { expect(aggregatedParent.childrenSum).toEqual(9); }); }); describe('updating child to change count and sum', () => { // ------ before ------- // count | sum // 1 2 3 | 4 5 // *(update to 5) // ------ after ------ // count | sum // 1 2 | 4 5 5 // should be count=2, sum=14 let updateChildResult: any = {}; let aggregatedParent: any = {}; beforeAll(() => { const childWithThree = children.find(child => child.value === 3); return store.dispatch(em.entity('child').updateItem(childWithThree.id, { value: 5 })) .then((result) => { updateChildResult = result; return store.dispatch(em.entity('parent').fetchItem(parent.id)); }) .then(() => { aggregatedParent = em.entity('parent').getAllItemList()(store.getState())[0]; children = em.entity('child').getAllItemList()(store.getState()); }); }); it('update child result must be "DONE"', () => { expect(updateChildResult).toEqual('DONE'); }); it('must update aggregations(sum, count), of parent', () => { expect(aggregatedParent.childrenCount).toEqual(2); expect(aggregatedParent.childrenSum).toEqual(14); }); }); describe('deleting child to change count and sum', () => { let aggregatedParent = null; beforeAll(() => { // ------ before ------- // count | sum // 1 2 | 4 5 5 // *(delete) // ------ after ------ // count | sum // 1 2 | 5 5 // should be count=2, sum=10 const childWithFour = children.find(child => child.value === 4); return store.dispatch(em.entity('child').deleteItem(childWithFour.id)) .then(() => store.dispatch(em.entity('parent').fetchItem(parent.id))) .then(() => { aggregatedParent = em.entity('parent').getAllItemList()(store.getState())[0]; children = em.entity('child').getAllItemList()(store.getState()); }); }); it('should update sum of parent to 10, count of parent to 2', () => { expect(aggregatedParent.childrenCount).toEqual(2); expect(aggregatedParent.childrenSum).toEqual(10); }); }); afterAll(() => Promise.all([ deleteCollection('parent'), deleteCollection('child') ]).then((results) => { console.log('deleted results:', results); }), 1000000); });