UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

30 lines • 32 kB
import _regeneratorRuntime from"@babel/runtime/regenerator";function asyncGeneratorStep(gen,resolve,reject,_next,_throw,key,arg){try{var info=gen[key](arg);var value=info.value;}catch(error){reject(error);return;}if(info.done){resolve(value);}else{Promise.resolve(value).then(_next,_throw);}}function _asyncToGenerator(fn){return function(){var self=this,args=arguments;return new Promise(function(resolve,reject){var gen=fn.apply(self,args);function _next(value){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"next",value);}function _throw(err){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"throw",err);}_next(undefined);});};}import{subscribe,invoke,getSubscriptionCount,resetSubscriptions}from'@shopgate/pwa-common/subscriptions/mock';import pipelineDependencies from'@shopgate/pwa-core/classes/PipelineDependencies';import showModal from'@shopgate/pwa-common/actions/modal/showModal';import{appDidStart$}from'@shopgate/pwa-common/streams';import{favoritesWillEnter$,shouldFetchFreshFavorites$,addProductToFavoritesDebounced$,removeProductFromFavoritesDebounced$,didReceiveFlushFavoritesBuffer$,errorFavoritesLimit$,refreshFavorites$,updateProductInFavoritesDebounced$}from"../streams";import favorites from"./index";import fetchFavorites from"../actions/fetchFavorites";import addFavorites from"../actions/addFavorites";import removeFavorites from"../actions/removeFavorites";import updateFavorites from"../actions/updateFavorites";import{FAVORITES_LIMIT_ERROR}from"../constants";import{SHOPGATE_USER_ADD_FAVORITES,SHOPGATE_USER_DELETE_FAVORITES}from"../constants/Pipelines";import{addProductToFavorites,removeProductFromFavorites,requestAddFavorites,requestRemoveFavorites,cancelRequestSyncFavorites,errorFavorites,idleSyncFavorites,updateProductInFavorites,requestUpdateFavorites}from"../action-creators";import{getFavoritesProducts,getFavoritesCount,makeGetProductRelativesOnFavorites}from"../selectors";import fetchFavoritesListsWithItems from"../actions/fetchFavoritesListsWithItems";// Required for custom runner without env-setup jest.mock('@shopgate/pwa-core',function(){});jest.mock('@shopgate/pwa-common/actions/modal/showModal',function(){return jest.fn().mockReturnValue('showModal');});jest.mock('@shopgate/pwa-common-commerce/product',function(){return{fetchProductsById:jest.fn()};});jest.mock('@shopgate/pwa-common/providers',function(){return{LoadingProvider:{setLoading:jest.fn(),unsetLoading:jest.fn()}};});var mockedDefaultListId='DEFAULT';jest.mock("../actions/fetchFavorites",function(){return jest.fn().mockReturnValue('fetchFavoritesResult');});jest.mock("../actions/fetchFavoritesList",function(){return jest.fn().mockResolvedValue([{id:'DEFAULT',name:'Wish List'}]);});jest.mock("../actions/fetchFavoritesListsWithItems",function(){var original=jest.requireActual('../actions/fetchFavoritesListsWithItems');return{__esModule:true,"default":jest.fn(original["default"])};});// Mock all used selectors to avoid mocking the store var mockedGetFavoritesProductsIdsReturnValue;var mockedGetFavoritesCountReturnValue;var mockedGetFavoritesCountByListReturnValue;var mockedGetFavoritesProductsReturnValue;var mockedGetProductRelativesOnFavoritesReturnValue;var mockedMakeGetFavoritesReturnValue;var mockedMakeGetFavoritesCountByList=jest.fn(function(){return mockedGetFavoritesCountByListReturnValue||0;});jest.mock("../selectors",function(){return{getFavoritesProductsIds:jest.fn(function(){return mockedGetFavoritesProductsIdsReturnValue;}),getFavoritesProducts:jest.fn(function(){return mockedGetFavoritesProductsReturnValue;}),getFavoritesCount:jest.fn(function(){return mockedGetFavoritesCountReturnValue;}),makeGetFavoritesCountByList:jest.fn(function(){return mockedMakeGetFavoritesCountByList;}),makeGetFavorites:jest.fn(function(){return mockedMakeGetFavoritesReturnValue;}),makeGetProductRelativesOnFavorites:jest.fn(function(){return jest.fn().mockReturnValue(mockedGetProductRelativesOnFavoritesReturnValue);}),getUseGetFavoriteIdsPipeline:jest.fn().mockReturnValue(false)};});jest.mock("../actions/addFavorites",function(){return jest.fn().mockReturnValue('addFavoritesResult');});jest.mock("../actions/removeFavorites",function(){return jest.fn().mockReturnValue('removeFavoritesResult');});jest.mock("../actions/updateFavorites",function(){return jest.fn().mockReturnValue('updateFavoritesResult');});var mockedHasFavorites=true;var mockedFavoritesLimit=100;jest.mock('@shopgate/pwa-common/helpers/config',function(){return{get hasFavorites(){return mockedHasFavorites;},get favorites(){return{limit:mockedFavoritesLimit};},get themeConfig(){return{};}};});/** * Flushes the promise queue. * @returns {Promise} */var flushPromises=function flushPromises(){return new Promise(function(resolve){return setImmediate(resolve);});};describe('Favorites - subscriptions',function(){describe('Favorites enabled',function(){var pipelineDependenciesSet=pipelineDependencies.set;beforeAll(function(){// Make sure no conflicting subscriptions exist resetSubscriptions();// Replace singleton object property with a mock pipelineDependencies.set=jest.fn();// Subscribe all streams favorites(subscribe);});afterAll(function(){// Clean up subscriptions resetSubscriptions();// Restore singleton pipelineDependencies.set=pipelineDependenciesSet;});var getState=jest.fn().mockReturnValue(function(){});// Dispatch always resolves it's promise by default var dispatch=jest.fn(function(result){if(typeof result==='function'){return result(dispatch,getState);}return result;});var productIds=['prod1','prod2'];var defaultListItems=[{quantity:1,notes:null,productId:productIds[0]},{quantity:1,notes:null,productId:productIds[1]}];beforeEach(function(){jest.clearAllMocks();// Reset selector return values mockedGetFavoritesProductsIdsReturnValue=productIds;mockedGetFavoritesCountReturnValue=productIds.length;mockedGetProductRelativesOnFavoritesReturnValue=[];mockedGetFavoritesProductsReturnValue={byList:{DEFAULT:{items:defaultListItems,isFetching:false}}};mockedMakeGetFavoritesReturnValue=jest.fn().mockReturnValue(defaultListItems);});describe('appDidStart$',function(){it('should subscribe to the stream',function(){expect(getSubscriptionCount(appDidStart$)).toBe(1);});it('should not return any value',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(){return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:_context.next=2;return expect(invoke(appDidStart$,{dispatch:dispatch,getState:getState})).resolves.toBeUndefined();case 2:case"end":return _context.stop();}},_callee);})));it('should set up pipeline dependencies correctly',function(){invoke(appDidStart$,{dispatch:dispatch,getState:getState});// Expect pipeline dependencies to be set correctly expect(pipelineDependencies.set).toHaveBeenCalledTimes(2);expect(pipelineDependencies.set.mock.calls[0][0]).toBe(SHOPGATE_USER_ADD_FAVORITES);expect(pipelineDependencies.set.mock.calls[0][1]).toEqual([SHOPGATE_USER_ADD_FAVORITES,SHOPGATE_USER_DELETE_FAVORITES]);expect(pipelineDependencies.set.mock.calls[1][0]).toBe(SHOPGATE_USER_DELETE_FAVORITES);expect(pipelineDependencies.set.mock.calls[1][1]).toEqual([SHOPGATE_USER_ADD_FAVORITES,SHOPGATE_USER_DELETE_FAVORITES]);});it('should fetch favorites',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(){return _regeneratorRuntime.wrap(function _callee2$(_context2){while(1)switch(_context2.prev=_context2.next){case 0:_context2.next=2;return invoke(appDidStart$,{dispatch:dispatch,getState:getState});case 2:expect(fetchFavoritesListsWithItems).toHaveBeenCalledTimes(1);expect(fetchFavoritesListsWithItems).toHaveBeenCalledWith(false);expect(dispatch).toHaveBeenCalledTimes(3);case 5:case"end":return _context2.stop();}},_callee2);})));});describe('favoritesWillEnter$',function(){it('should subscribe to the stream',function(){expect(getSubscriptionCount(favoritesWillEnter$)).toBe(1);});it('should not return any value',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(){return _regeneratorRuntime.wrap(function _callee3$(_context3){while(1)switch(_context3.prev=_context3.next){case 0:_context3.t0=expect;_context3.next=3;return invoke(favoritesWillEnter$,{dispatch:dispatch,getState:getState});case 3:_context3.t1=_context3.sent;(0,_context3.t0)(_context3.t1).toBeUndefined();case 5:case"end":return _context3.stop();}},_callee3);})));it('should handle fetch and map favorites',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee4(){return _regeneratorRuntime.wrap(function _callee4$(_context4){while(1)switch(_context4.prev=_context4.next){case 0:_context4.next=2;return invoke(favoritesWillEnter$,{dispatch:dispatch,getState:getState});case 2:expect(fetchFavoritesListsWithItems).toHaveBeenCalledTimes(1);expect(fetchFavoritesListsWithItems).toHaveBeenCalledWith(true);expect(dispatch).toHaveBeenCalledTimes(3);case 5:case"end":return _context4.stop();}},_callee4);})));});describe('shouldFetchFreshFavorites$',function(){it('should subscribe to the stream',function(){expect(getSubscriptionCount(shouldFetchFreshFavorites$)).toBe(1);});it('should not return any value',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee5(){return _regeneratorRuntime.wrap(function _callee5$(_context5){while(1)switch(_context5.prev=_context5.next){case 0:_context5.next=2;return expect(invoke(shouldFetchFreshFavorites$,{dispatch:dispatch})).resolves.toBeUndefined();case 2:case"end":return _context5.stop();}},_callee5);})));it('should fetch fresh favorites without using the cache',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee6(){return _regeneratorRuntime.wrap(function _callee6$(_context6){while(1)switch(_context6.prev=_context6.next){case 0:_context6.next=2;return invoke(shouldFetchFreshFavorites$,{dispatch:dispatch,getState:getState});case 2:expect(fetchFavoritesListsWithItems).toHaveBeenCalledTimes(1);expect(fetchFavoritesListsWithItems).toHaveBeenCalledWith(true);expect(dispatch).toHaveBeenCalledTimes(3);case 5:case"end":return _context6.stop();}},_callee6);})));});describe('addProductToFavoritesDebounced$',function(){it('should subscribe to the stream',function(){expect(getSubscriptionCount(addProductToFavoritesDebounced$)).toBe(1);});it('should not return any value',function(){expect(invoke(addProductToFavoritesDebounced$,{action:addProductToFavorites('prod3'),dispatch:dispatch,getState:getState})).toBeUndefined();});it('should cancel the request when the product is already there',function(){invoke(addProductToFavoritesDebounced$,{action:addProductToFavorites('prod1',mockedDefaultListId),dispatch:dispatch,getState:getState});expect(getState).toHaveBeenCalledTimes(1);expect(getFavoritesProducts).toHaveBeenCalledWith(getState());expect(getFavoritesProducts).toHaveBeenCalledTimes(1);expect(dispatch).toHaveBeenCalledWith(cancelRequestSyncFavorites(0,mockedDefaultListId));expect(dispatch).toHaveBeenCalledTimes(1);});it('should proceed and dispatch an error if the product is not yet on the list and the limit is reached',function(){var favoritesLimit=mockedFavoritesLimit;var productId='prod3';mockedFavoritesLimit=2;mockedGetFavoritesCountByListReturnValue=2;invoke(addProductToFavoritesDebounced$,{action:addProductToFavorites(productId,'DEFAULT'),dispatch:dispatch,getState:getState});expect(getState).toHaveBeenCalledTimes(1);expect(getFavoritesProducts).toHaveBeenCalledWith(getState());expect(getFavoritesProducts).toHaveBeenCalledTimes(1);expect(mockedMakeGetFavoritesCountByList).toHaveBeenCalledWith(getState());expect(mockedMakeGetFavoritesCountByList).toHaveBeenCalledTimes(1);expect(mockedMakeGetFavoritesCountByList).toHaveReturnedWith(mockedGetFavoritesCountByListReturnValue);var error=new Error('Limit exceeded');error.code=FAVORITES_LIMIT_ERROR;expect(dispatch).toHaveBeenCalledWith(errorFavorites(productId,error));expect(dispatch).toHaveBeenCalledTimes(1);mockedFavoritesLimit=favoritesLimit;});it('should proceed and dispatch an add request if all is good',function(){var productId='prod3';invoke(addProductToFavoritesDebounced$,{action:addProductToFavorites(productId),dispatch:dispatch,getState:getState});expect(getState).toHaveBeenCalledTimes(1);expect(getFavoritesProducts).toHaveBeenCalledWith(getState());expect(getFavoritesProducts).toHaveBeenCalledTimes(1);expect(mockedMakeGetFavoritesCountByList).toHaveBeenCalledWith(getState());expect(mockedMakeGetFavoritesCountByList).toHaveBeenCalledTimes(1);expect(dispatch).toHaveBeenCalledWith(requestAddFavorites(productId));expect(dispatch).toHaveBeenCalledTimes(1);});});describe('updateProductInFavoritesDebounced$',function(){it('should subscribe to the stream',function(){expect(getSubscriptionCount(updateProductInFavoritesDebounced$)).toBe(1);});it('should not return any value',function(){expect(invoke(updateProductInFavoritesDebounced$,{action:updateProductInFavorites('prod3'),dispatch:dispatch,getState:getState})).toBeUndefined();});it('should proceed and dispatch an add request if all is good',function(){var productId='prod3';invoke(updateProductInFavoritesDebounced$,{action:updateProductInFavorites(productId),dispatch:dispatch,getState:getState});expect(dispatch).toHaveBeenCalledWith(requestUpdateFavorites(productId));expect(dispatch).toHaveBeenCalledTimes(1);});});describe('removeProductFromFavoritesDebounced$',function(){it('should subscribe to the stream',function(){expect(getSubscriptionCount(removeProductFromFavoritesDebounced$)).toBe(1);});it('should not return any value',function(){expect(invoke(removeProductFromFavoritesDebounced$,{action:removeProductFromFavorites('prod1',true,mockedDefaultListId),dispatch:dispatch,getState:getState})).toBeUndefined();expect(invoke(removeProductFromFavoritesDebounced$,{action:removeProductFromFavorites('prod1',false,mockedDefaultListId),dispatch:dispatch,getState:getState})).toBeUndefined();});it('should not do anything if no products are on the list and currently fetching',function(){mockedGetFavoritesCountReturnValue=0;mockedGetFavoritesProductsReturnValue.isFetching=true;invoke(removeProductFromFavoritesDebounced$,{action:removeProductFromFavorites('prod1',false,mockedDefaultListId),dispatch:dispatch,getState:getState});expect(getFavoritesCount).toHaveBeenCalledTimes(1);expect(makeGetProductRelativesOnFavorites).toHaveBeenCalledTimes(0);expect(getFavoritesProducts).toHaveBeenCalledTimes(1);expect(getState).toHaveBeenCalledTimes(2);expect(dispatch).toHaveBeenCalledTimes(1);});it('should idle-sync when no products are on the list and not fetching',function(){mockedGetFavoritesCountReturnValue=0;invoke(removeProductFromFavoritesDebounced$,{action:removeProductFromFavorites('prod1',false,mockedDefaultListId),dispatch:dispatch,getState:getState});expect(getFavoritesCount).toHaveBeenCalledTimes(1);expect(makeGetProductRelativesOnFavorites).toHaveBeenCalledTimes(0);expect(getFavoritesProducts).toHaveBeenCalledTimes(1);expect(getState).toHaveBeenCalledTimes(2);expect(dispatch).toHaveBeenCalledTimes(1);expect(dispatch).toHaveBeenCalledWith(idleSyncFavorites(mockedDefaultListId));});it('should dispatch a remove request for each relative',function(){var productId='prod0';var mock=jest.fn(function(){return productIds;});makeGetProductRelativesOnFavorites.mockReturnValueOnce(mock);invoke(removeProductFromFavoritesDebounced$,{action:removeProductFromFavorites(productId,true,mockedDefaultListId),dispatch:dispatch,getState:getState});// Expect multiple remove requests to be dispatched expect(makeGetProductRelativesOnFavorites).toHaveBeenCalledTimes(1);expect(dispatch).toHaveBeenCalledTimes(4);expect(dispatch.mock.calls[1][0]).toEqual(requestRemoveFavorites(productIds[0],mockedDefaultListId)(dispatch,getState));expect(dispatch.mock.calls[3][0]).toEqual(requestRemoveFavorites(productIds[1],mockedDefaultListId)(dispatch,getState));});it('should cancel the remove request if the product is not on the list',function(){var productId='prod0';mockedGetProductRelativesOnFavoritesReturnValue=productIds;invoke(removeProductFromFavoritesDebounced$,{action:removeProductFromFavorites(productId,false,mockedDefaultListId),dispatch:dispatch,getState:getState});// Don't expect any remove requests to be triggered expect(makeGetProductRelativesOnFavorites).toHaveBeenCalledTimes(0);expect(dispatch).toHaveBeenCalledTimes(1);expect(dispatch).toHaveBeenCalledWith(cancelRequestSyncFavorites(0,mockedDefaultListId));});it('should request the removal of the single favorite item',function(){var productId='prod0';mockedGetFavoritesProductsReturnValue={byList:{DEFAULT:{items:[].concat(mockedGetFavoritesProductsReturnValue.byList.DEFAULT.items,[{productId:productId,quantity:1,notes:null}])}}};var mock=jest.fn(function(){return productIds;});makeGetProductRelativesOnFavorites.mockReturnValueOnce(mock);invoke(removeProductFromFavoritesDebounced$,{action:removeProductFromFavorites(productId,false,mockedDefaultListId),dispatch:dispatch,getState:getState});// Only one removal request is expected expect(makeGetProductRelativesOnFavorites).toHaveBeenCalledTimes(0);expect(dispatch).toHaveBeenCalledTimes(2);expect(dispatch.mock.calls[1][0]).toEqual(requestRemoveFavorites(productId,mockedDefaultListId)(dispatch,getState));});});describe('errorFavoritesLimit$',function(){it('should subscribe to the stream',function(){expect(getSubscriptionCount(errorFavoritesLimit$)).toBe(1);});it('should not return any value',function(){expect(invoke(errorFavoritesLimit$,{dispatch:dispatch})).toBeUndefined();});it('should dispatch an action to show a modal',function(){invoke(errorFavoritesLimit$,{dispatch:dispatch});expect(showModal).toHaveBeenCalledTimes(1);expect(dispatch).toHaveBeenCalledTimes(1);expect(dispatch).toHaveBeenCalledWith(showModal({confirm:null,dismiss:'modal.ok',title:'modal.title_error',message:'favorites.error_limit'}));});});describe('refreshFavorites$',function(){it('should subscribe to the stream',function(){expect(getSubscriptionCount(refreshFavorites$)).toBe(1);});it('should not return any value',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee7(){return _regeneratorRuntime.wrap(function _callee7$(_context7){while(1)switch(_context7.prev=_context7.next){case 0:_context7.next=2;return expect(invoke(refreshFavorites$,{dispatch:dispatch,action:{listId:mockedDefaultListId},getState:getState})).resolves.toBeUndefined();case 2:case"end":return _context7.stop();}},_callee7);})));it('should refresh favorites by fetching them without using the cache',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee8(){return _regeneratorRuntime.wrap(function _callee8$(_context8){while(1)switch(_context8.prev=_context8.next){case 0:_context8.next=2;return invoke(refreshFavorites$,{dispatch:dispatch,action:{listId:mockedDefaultListId},getState:getState});case 2:expect(fetchFavorites).toHaveBeenCalledTimes(1);expect(dispatch).toHaveBeenCalledTimes(1);expect(dispatch).toHaveBeenCalledWith(fetchFavorites(true));case 5:case"end":return _context8.stop();}},_callee8);})));});describe('didReceiveFlushFavoritesBuffer$',function(){it('should subscribe to the stream',function(){expect(getSubscriptionCount(didReceiveFlushFavoritesBuffer$)).toBe(1);});it('should not return any value',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee9(){var productId;return _regeneratorRuntime.wrap(function _callee9$(_context9){while(1)switch(_context9.prev=_context9.next){case 0:productId='prod1';_context9.t0=expect;_context9.next=4;return invoke(didReceiveFlushFavoritesBuffer$,null);case 4:_context9.t1=_context9.sent;(0,_context9.t0)(_context9.t1).toBeUndefined();_context9.t2=expect;_context9.next=9;return invoke(didReceiveFlushFavoritesBuffer$,[]);case 9:_context9.t3=_context9.sent;(0,_context9.t2)(_context9.t3).toBeUndefined();_context9.t4=expect;_context9.next=14;return invoke(didReceiveFlushFavoritesBuffer$,[{action:requestAddFavorites(productId,mockedDefaultListId),dispatch:dispatch,getState:getState}]);case 14:_context9.t5=_context9.sent;(0,_context9.t4)(_context9.t5).toBeUndefined();_context9.t6=expect;_context9.next=19;return invoke(didReceiveFlushFavoritesBuffer$,[{action:requestAddFavorites(productId,mockedDefaultListId),dispatch:dispatch,getState:getState},{action:requestUpdateFavorites(productId,mockedDefaultListId),dispatch:dispatch,getState:getState},{action:requestRemoveFavorites(productId,mockedDefaultListId)(dispatch,getState),dispatch:dispatch,getState:getState}]);case 19:_context9.t7=_context9.sent;(0,_context9.t6)(_context9.t7).toBeUndefined();case 21:case"end":return _context9.stop();}},_callee9);})));it('should cancel all redundant requests',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee10(){var productId;return _regeneratorRuntime.wrap(function _callee10$(_context10){while(1)switch(_context10.prev=_context10.next){case 0:productId='prod1';// Add and remove the same product to make both redundant _context10.next=3;return invoke(didReceiveFlushFavoritesBuffer$,[{action:requestAddFavorites(productId,mockedDefaultListId),dispatch:dispatch,getState:getState},{action:requestRemoveFavorites(productId,mockedDefaultListId)(dispatch,getState),dispatch:dispatch,getState:getState}]);case 3:expect(dispatch).toHaveBeenCalledTimes(2);expect(dispatch).toHaveBeenCalledWith(cancelRequestSyncFavorites(2,mockedDefaultListId));case 5:case"end":return _context10.stop();}},_callee10);})));it('should filter out duplicated add requests',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee11(){var productId;return _regeneratorRuntime.wrap(function _callee11$(_context11){while(1)switch(_context11.prev=_context11.next){case 0:productId='prod1';// Add the same product twice _context11.next=3;return invoke(didReceiveFlushFavoritesBuffer$,[{action:requestAddFavorites(productId,mockedDefaultListId),dispatch:dispatch,getState:getState},{action:requestAddFavorites(productId,mockedDefaultListId),dispatch:dispatch,getState:getState}]);case 3:_context11.next=5;return flushPromises();case 5:// One cancel and one add action call as well as one idle-sync afterwards expect(addFavorites).toHaveBeenCalledTimes(1);expect(addFavorites.mock.calls[0][0]).toBe(productId);expect(dispatch).toHaveBeenCalledTimes(2);expect(dispatch.mock.calls[0][0]).toEqual(addFavorites());expect(dispatch.mock.calls[1][0]).toEqual(idleSyncFavorites(mockedDefaultListId));case 10:case"end":return _context11.stop();}},_callee11);})));it('should filter out duplicated remove requests',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee12(){var productId;return _regeneratorRuntime.wrap(function _callee12$(_context12){while(1)switch(_context12.prev=_context12.next){case 0:productId='prod1';// Remove the same product twice _context12.next=3;return invoke(didReceiveFlushFavoritesBuffer$,[{action:requestRemoveFavorites(productId,mockedDefaultListId)(dispatch,getState),dispatch:dispatch,getState:getState},{action:requestRemoveFavorites(productId,mockedDefaultListId)(dispatch,getState),dispatch:dispatch,getState:getState}]);case 3:_context12.next=5;return flushPromises();case 5:// One cancel and one add action call as well as one idle-sync afterwards expect(removeFavorites).toHaveBeenCalledTimes(1);expect(removeFavorites.mock.calls[0][0]).toBe(productId);expect(dispatch).toHaveBeenCalledTimes(4);expect(dispatch.mock.calls[2][0]).toEqual(removeFavorites());expect(dispatch.mock.calls[3][0]).toEqual(idleSyncFavorites(mockedDefaultListId));case 10:case"end":return _context12.stop();}},_callee12);})));it('should not filter out unique add or remove requests',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee13(){return _regeneratorRuntime.wrap(function _callee13$(_context13){while(1)switch(_context13.prev=_context13.next){case 0:_context13.next=2;return invoke(didReceiveFlushFavoritesBuffer$,[{action:requestAddFavorites('prod3',mockedDefaultListId),dispatch:dispatch,getState:getState},{action:requestAddFavorites('prod4',mockedDefaultListId),dispatch:dispatch,getState:getState},{action:requestRemoveFavorites('prod1',mockedDefaultListId)(dispatch,getState),dispatch:dispatch,getState:getState},{action:requestRemoveFavorites('prod2',mockedDefaultListId)(dispatch,getState),dispatch:dispatch,getState:getState}]);case 2:_context13.next=4;return flushPromises();case 4:// Only dispatch add and remove actions, no cancellation expect(addFavorites).toHaveBeenCalledTimes(2);expect(addFavorites.mock.calls[0][0]).toBe('prod3');expect(addFavorites.mock.calls[1][0]).toBe('prod4');expect(removeFavorites).toHaveBeenCalledTimes(2);expect(removeFavorites.mock.calls[0][0]).toBe('prod1');expect(removeFavorites.mock.calls[1][0]).toBe('prod2');expect(dispatch).toHaveBeenCalledTimes(7);expect(dispatch.mock.calls[2][0]).toEqual(addFavorites());expect(dispatch.mock.calls[3][0]).toEqual(addFavorites());expect(dispatch.mock.calls[4][0]).toEqual(removeFavorites());expect(dispatch.mock.calls[5][0]).toEqual(removeFavorites());expect(dispatch.mock.calls[6][0]).toEqual(idleSyncFavorites(mockedDefaultListId));case 16:case"end":return _context13.stop();}},_callee13);})));it('should filter out duplicated update requests',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee14(){var productId;return _regeneratorRuntime.wrap(function _callee14$(_context14){while(1)switch(_context14.prev=_context14.next){case 0:productId='prod1';// Add the same product twice _context14.next=3;return invoke(didReceiveFlushFavoritesBuffer$,[{action:requestUpdateFavorites(productId,mockedDefaultListId,2),dispatch:dispatch,getState:getState},{action:requestUpdateFavorites(productId,mockedDefaultListId,3),dispatch:dispatch,getState:getState}]);case 3:_context14.next=5;return flushPromises();case 5:// One cancel and one add action call as well as one idle-sync afterwards expect(updateFavorites).toHaveBeenCalledTimes(1);expect(updateFavorites.mock.calls[0][0]).toBe(productId);expect(updateFavorites.mock.calls[0][1]).toBe(mockedDefaultListId);expect(updateFavorites.mock.calls[0][2]).toBe(3);expect(dispatch).toHaveBeenCalledTimes(2);expect(dispatch.mock.calls[0][0]).toEqual(updateFavorites());expect(dispatch.mock.calls[1][0]).toEqual(idleSyncFavorites(mockedDefaultListId));case 12:case"end":return _context14.stop();}},_callee14);})));it('should properly handle conflicts and duplicates when they occur at once',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee15(){return _regeneratorRuntime.wrap(function _callee15$(_context15){while(1)switch(_context15.prev=_context15.next){case 0:_context15.next=2;return invoke(didReceiveFlushFavoritesBuffer$,[{action:requestAddFavorites('prod3',mockedDefaultListId),dispatch:dispatch,getState:getState},{action:requestAddFavorites('prod4',mockedDefaultListId),dispatch:dispatch,getState:getState},{action:requestAddFavorites('prod4',mockedDefaultListId),// duplicated dispatch:dispatch,getState:getState},{action:requestRemoveFavorites('prod1',mockedDefaultListId)(dispatch,getState),dispatch:dispatch,getState:getState},{action:requestAddFavorites('prod1',mockedDefaultListId),// conflict dispatch:dispatch,getState:getState},{action:requestRemoveFavorites('prod2',mockedDefaultListId)(dispatch,getState),dispatch:dispatch,getState:getState}]);case 2:_context15.next=4;return flushPromises();case 4:// Cancel three calls (conflict -> 2 + duplicate -> 1 => 3), handle the rest expect(addFavorites).toHaveBeenCalledTimes(2);expect(addFavorites.mock.calls[0][0]).toBe('prod3');expect(addFavorites.mock.calls[1][0]).toBe('prod4');expect(removeFavorites).toHaveBeenCalledTimes(1);expect(removeFavorites.mock.calls[0][0]).toBe('prod2');expect(dispatch).toHaveBeenCalledTimes(7);expect(dispatch.mock.calls[2][0]).toEqual(addFavorites());expect(dispatch.mock.calls[3][0]).toEqual(addFavorites());expect(dispatch.mock.calls[4][0]).toEqual(cancelRequestSyncFavorites(2,mockedDefaultListId));expect(dispatch.mock.calls[5][0]).toEqual(removeFavorites());expect(dispatch.mock.calls[6][0]).toEqual(idleSyncFavorites(mockedDefaultListId));case 15:case"end":return _context15.stop();}},_callee15);})));it('should not break on any failing request',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee17(){return _regeneratorRuntime.wrap(function _callee17$(_context17){while(1)switch(_context17.prev=_context17.next){case 0:dispatch.mockImplementation(/*#__PURE__*/function(){var _ref17=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee16(action){return _regeneratorRuntime.wrap(function _callee16$(_context16){while(1)switch(_context16.prev=_context16.next){case 0:if(!(action===addFavorites()||action===removeFavorites()||action===updateFavorites())){_context16.next=2;break;}throw new Error('Failed to add/remove favorite');case 2:case"end":return _context16.stop();}},_callee16);}));return function(_x){return _ref17.apply(this,arguments);};}());// Remove the same product twice _context17.next=3;return expect(invoke(didReceiveFlushFavoritesBuffer$,[{action:requestAddFavorites('prod3'),dispatch:dispatch,getState:getState},{action:requestUpdateFavorites('prod3'),dispatch:dispatch,getState:getState},{action:requestRemoveFavorites('prod1')(dispatch,getState),dispatch:dispatch,getState:getState}])).resolves.toBeUndefined();case 3:case"end":return _context17.stop();}},_callee17);})));it('should dispatch only the requests and one idle-sync if any error occurs',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee19(){return _regeneratorRuntime.wrap(function _callee19$(_context19){while(1)switch(_context19.prev=_context19.next){case 0:dispatch.mockImplementation(/*#__PURE__*/function(){var _ref19=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee18(action){return _regeneratorRuntime.wrap(function _callee18$(_context18){while(1)switch(_context18.prev=_context18.next){case 0:if(!(action===addFavorites()||action===updateFavorites()||action===removeFavorites())){_context18.next=2;break;}throw new Error('Failed to add/remove favorite');case 2:if(!(typeof action==='function')){_context18.next=4;break;}return _context18.abrupt("return",action(dispatch,getState));case 4:return _context18.abrupt("return",action);case 5:case"end":return _context18.stop();}},_callee18);}));return function(_x2){return _ref19.apply(this,arguments);};}());// Remove the same product twice _context19.t0=invoke;_context19.t1=didReceiveFlushFavoritesBuffer$;_context19.t2={action:requestAddFavorites('prod3',mockedDefaultListId),dispatch:dispatch,getState:getState};_context19.t3={action:requestUpdateFavorites('prod2',mockedDefaultListId,3),dispatch:dispatch,getState:getState};_context19.next=7;return requestRemoveFavorites('prod1',mockedDefaultListId)(dispatch,getState);case 7:_context19.t4=_context19.sent;_context19.t5=dispatch;_context19.t6=getState;_context19.t7={action:_context19.t4,dispatch:_context19.t5,getState:_context19.t6};_context19.t8=[_context19.t2,_context19.t3,_context19.t7];_context19.next=14;return(0,_context19.t0)(_context19.t1,_context19.t8);case 14:_context19.next=16;return flushPromises();case 16:expect(dispatch).toHaveBeenCalledTimes(5);expect(dispatch.mock.calls[1][0]).toEqual(addFavorites());expect(dispatch.mock.calls[2][0]).toEqual(updateFavorites());expect(dispatch.mock.calls[3][0]).toEqual(removeFavorites());expect(dispatch.mock.calls[4][0]).toEqual(idleSyncFavorites(mockedDefaultListId));case 21:case"end":return _context19.stop();}},_callee19);})));});});describe('Favorites disabled',function(){it('should not subscribe to anything',function(){var hasFavorites=mockedHasFavorites;mockedHasFavorites=false;favorites(subscribe);expect(getSubscriptionCount()).toBe(0);mockedHasFavorites=hasFavorites;});});});