import {EntitiesManager as EM} from '../../src/index'; import configureEntitiesManager from '../utils/configureEntitiesManager'; import configureStore from '../utils/configureStore'; import deleteCollection from '../utils/deleteCollection'; // clas101에서 user들이 관심있는 category를 선택하는 상황을 mimic const em = configureEntitiesManager({ categories: { createdAt: EM.createdAt(), title: EM.required(), userCount: EM.count('users'), userScoreSum: EM.count('users') }, users: { categoryIds: EM.parentIds('categories'), score: EM.required(), createdAt: EM.createdAt(), updatedAt: EM.updatedAt(), } }); const store = configureStore({}, { entities: em.getEntitiesReducer() }); describe('aggregation performance (mimic categories, users in class101)', () => { let allCategories = null; let allUsers = null; describe('adding categories', () => { it('should resolve with item id(s)', () => { const categories = [ { id: 'humanities', title: '인문학' }, { id: 'travel', title: '여행' }, { id: 'cooking', title: '요리' }, { id: 'design', title: '디자인' } ]; const addCategories$ = Promise.all( categories.map(category => store.dispatch(em.entity('categories').addItem(category))) ); return addCategories$.then(results => { expect(results).toEqual(['humanities', 'travel', 'cooking', 'design']); allCategories = em.entity('categories').getAllItemList()(store.getState()); }); }); it('should have 0 as initial userCount', () => { expect(allCategories).toEqual(jasmine.arrayContaining([ jasmine.objectContaining({ userCount: 0 }), jasmine.objectContaining({ userCount: 0 }), jasmine.objectContaining({ userCount: 0 }), jasmine.objectContaining({ userCount: 0 }) ])); }); }); describe('adding a user', () => { it('should resolve with item id(s)', () => { const user = { id: 'foo', categoryIds: { travel: true, cooking: true }, score: 3, address: 'Ulsan Uljugun' }; return store.dispatch(em.entity('users').addItem(user)) .then(result => { expect(result).toEqual('foo'); }); }); }); describe('change a user', () => { it('should not update category', () => { return store.dispatch(em.entity('users').updateItem('foo', { address: 'Ulsan Uljugun Bancheon-Ree' })) .then(result => { expect(result).toEqual('DONE'); }); }); }); xdescribe('change a user', () => { it('should update category', () => { return store.dispatch(em.entity('users').updateItem('foo', { categoryIds: { travel: true } })) .then(result => { expect(result).toEqual('DONE'); allCategories = em.entity('categories').getAllItemList()(store.getState()); expect(allCategories.find(c => c.id === 'cooking').userCount).toEqual(0); expect(allCategories.find(c => c.id === 'cooking').userScoreSum).toEqual(0); }); }); }); // 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); // }); // }); afterAll(() => Promise.all([ deleteCollection('categories'), deleteCollection('users') ]).then((results) => { console.log('deleted results:', results); }), 1000000); });