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'), childrenSum: EM.sum('child', 'value') }, child: { parentIds: EM.parentIds('parent'), createdAt: EM.createdAt(), updatedAt: EM.updatedAt(), value: EM.required() } }); const store = configureStore({}, { entities: em.getEntitiesReducer() }); describe('aggregation', () => { 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({ parentIds: { [parent.id]: true }, 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( () => store.dispatch(em.entity('parent').fetchItem(parent.id)) .then(() => { aggregatedParent = em.entity('parent').getAllItemList()(store.getState())[0]; }) ); it('count must be 5', () => { expect(aggregatedParent.childrenCount).toEqual(5); }); it('sum must be 15', () => { expect(aggregatedParent.childrenSum).toEqual(15); }); }); describe('updating child to change count and sum', () => { let updateChildResult = null; let aggregatedParent = null; beforeAll(() => { const childWithThree = children.find(child => child.value === 3); return store.dispatch(em.entity('child').updateItem(childWithThree.id, { parentIds: {} })) .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(4); expect(aggregatedParent.childrenSum).toEqual(12); }); }); // 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); });