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({ item: { createdAt: EM.createdAt(), updatedAt: EM.updatedAt() } }); const store = configureStore({}, { entities: em.getEntitiesReducer() }); describe('basicUsages', () => { let targetItem = null; describe('adding an item', () => { let addItemResult = null; beforeAll( () => store.dispatch(em.entity('item').addItem({ value: 'foo', description: 'foo description' })) .then((result) => { addItemResult = result; }) .then(() => { console.log('dispatched queryList'); return store.dispatch(em.entity('item').queryList('queriedItems', [['value', '==', 'foo']], 'createdAt', 'asc'))(5, 'FROM_FIRST'); }) ); it('should be done with id of added item', () => { expect(addItemResult).toEqual(jasmine.any(String)); // item.id }); it('should save item into the store', () => { console.log(store.getState()); targetItem = em.entity('item').getAllItemList()(store.getState())[0]; expect(targetItem).toEqual({ id: jasmine.any(String), createdAt: jasmine.any(Number), updatedAt: jasmine.any(Number), value: 'foo', description: 'foo description' }); }); it( 'should not fetch not existing item', () => store.dispatch(em.entity('item').fetchItem('notexisting')) .then((result) => { console.log('result:', result); const notExistingItem = em.entity('item').getItem('notexisting')(store.getState()); console.log('notExistingItem:', notExistingItem); expect(notExistingItem).not.toEqual(jasmine.anything()); }), 100000 ); }); describe('update the item', () => { let updateItemResult = null; let fetchItemResult = null; beforeAll( () => store.dispatch(em.entity('item').updateItem(targetItem.id, { value: 'bar', description: 'DELETE_FIELD' })) .then((result) => { updateItemResult = result; }) ); it('should be done with "DONE"', () => { expect(updateItemResult).toEqual('DONE'); }); it('should update item in store', () => { targetItem = em.entity('item').getAllItemList()(store.getState())[0]; expect(targetItem).toEqual({ id: jasmine.any(String), createdAt: jasmine.any(Number), updatedAt: jasmine.any(Number), value: 'bar', }); }); it( 'should update remote item in firestore', () => store.dispatch(em.entity('item').fetchItem(targetItem.id)) .then((result) => { fetchItemResult = result; }) .then(() => { expect(fetchItemResult).toEqual('DONE'); }) .then(() => { targetItem = em.entity('item').getAllItemList()(store.getState())[0]; expect(targetItem).toEqual({ id: jasmine.any(String), createdAt: jasmine.any(Number), updatedAt: jasmine.any(Number), value: 'bar' // description field is removed }); }) ); it( 'should not update item that does satisfy condition', async () => store.dispatch(em.entity('item').updateItem(targetItem.id, { value: 'baz' }, (beforeItem) => (beforeItem.value === 'wrong'))) .catch((error) => { expect(error.message).toEqual('NOT_PASSED_CONDITION_FUNC_ON_UPDATE'); expect(error.beforeItem).toEqual(jasmine.any(Object)); expect(error.afterItem).toEqual(jasmine.any(Object)); return 'DONE'; }) ) }); afterAll(() => Promise.all([ deleteCollection('item') ]).then((results) => { console.log('deleted results:', results); }), 1000000); });