@shopgate/pwa-common-commerce 7.9.0-beta.6 → 7.9.0-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -34,13 +34,20 @@ function _extends(){_extends=Object.assign||function(target){for(var i=1;i<argum
34
34
  * @param {Function} getListCode Selects the list code.
35
35
  * @returns {Function}
36
36
  */export var makeGetFavorites=function makeGetFavorites(getListCode){return createSelector(getFavoritesProducts,getListCode,getProducts,function(favItems,listId,products){var _favItems$byList$list;var items=(favItems===null||favItems===void 0?void 0:(_favItems$byList$list=favItems.byList[listId])===null||_favItems$byList$list===void 0?void 0:_favItems$byList$list.items)||[];return items.map(function(item){var _products$item$produc;return _extends({},item,{product:(_products$item$produc=products[item.productId])===null||_products$item$produc===void 0?void 0:_products$item$produc.productData});});});};/**
37
+ * Creates a selector that retrieves the amount of items on a specific favorites list
38
+ * to the given favorite list.
39
+ * @param {Function} getListCode Selects the list code.
40
+ * @returns {Function}
41
+ */export var makeGetFavoritesCountByList=function makeGetFavoritesCountByList(getListCode){var getFavoritesIdsByList=makeGetFavoritesIdsByList(getListCode);return createSelector(getFavoritesIdsByList,function(favorites){return favorites.length;});};/**
37
42
  * True when favorites where not yet fetched for the first time.
38
43
  * @param {Object} state The global state.
39
44
  * @returns {boolean}
40
45
  */export var isInitialLoading=createSelector(getFavoritesProducts,function(products){if(!products){return true;}return!Object.values(products===null||products===void 0?void 0:products.byList).every(function(l){return l.ready;});});/**
41
46
  * @param {Object} state The global state.
42
- * @return {number}
43
- */export var getFavoritesCount=createSelector(getFavoritesProducts,function(products){if(!(products===null||products===void 0?void 0:products.byList)){return 0;}return Object.values(products.byList).reduce(function(prev,list){return prev+list.items.reduce(function(acc,_ref4){var _ref4$quantity=_ref4.quantity,quantity=_ref4$quantity===void 0?1:_ref4$quantity;return acc+quantity;},0);},0);});/**
47
+ * @param {Object} props The selector props
48
+ * @param {boolean} [props.useItemQuantity=false] Whether to consider item quantity at calculation
49
+ * @return {Function}
50
+ */export var getFavoritesCount=createSelector(getFavoritesProducts,function(_,props){return(props===null||props===void 0?void 0:props.useItemQuantity)||false;},function(products,useItemQuantity){if(!(products===null||products===void 0?void 0:products.byList)){return 0;}return Object.values(products.byList).reduce(function(prev,list){return prev+list.items.reduce(function(acc,_ref4){var _ref4$quantity=_ref4.quantity,quantity=_ref4$quantity===void 0?1:_ref4$quantity;if(useItemQuantity){return acc+quantity;}return acc+1;},0);},0);});/**
44
51
  * @param {Object} state The global state.
45
52
  * @return {boolean}
46
53
  */export var hasFavorites=createSelector(getFavoritesCount,function(count){return!!count;});/**
@@ -4,22 +4,24 @@ import{routeWillEnter$}from'@shopgate/pwa-common/streams/router';import{main$}fr
4
4
  * Gets triggered when the favorites page is about to be entered.
5
5
  * @type {Observable}
6
6
  */export var favoritesWillEnter$=routeWillEnter$.filter(function(_ref){var action=_ref.action;return action.route.pattern===FAVORITES_PATH||action.route.pathname===WISH_LIST_PATH;});/**
7
+ * Gets triggered when ADD_PRODUCT_TO_FAVORITES action is dispatched
8
+ */export var addProductToFavorites$=main$.filter(function(_ref2){var action=_ref2.action;return action.type===ADD_PRODUCT_TO_FAVORITES;});/**
7
9
  * Gets triggered when the debounce time of `addProductToFavorites` passes.
8
10
  * @type {Observable}
9
- */export var addProductToFavoritesDebounced$=main$.filter(function(_ref2){var action=_ref2.action;return action.type===ADD_PRODUCT_TO_FAVORITES;}).debounceTime(FAVORITE_BUTTON_DEBOUNCE_TIME);/**
11
+ */export var addProductToFavoritesDebounced$=main$.filter(function(_ref3){var action=_ref3.action;return action.type===ADD_PRODUCT_TO_FAVORITES;}).debounceTime(FAVORITE_BUTTON_DEBOUNCE_TIME);/**
10
12
  * Gets triggered when the debounce time of `updateProductInFavorites` passes.
11
13
  * @type {Observable}
12
- */export var updateProductInFavoritesDebounced$=main$.filter(function(_ref3){var action=_ref3.action;return action.type===UPDATE_PRODUCT_IN_FAVORITES;}).debounceTime(FAVORITE_QUANTITY_DEBOUNCE_TIME);/**
14
+ */export var updateProductInFavoritesDebounced$=main$.filter(function(_ref4){var action=_ref4.action;return action.type===UPDATE_PRODUCT_IN_FAVORITES;}).debounceTime(FAVORITE_QUANTITY_DEBOUNCE_TIME);/**
13
15
  * Gets triggered when the debounce time of `removeProductFromFavorites` passes.
14
16
  * @type {Observable}
15
- */export var removeProductFromFavoritesDebounced$=main$.filter(function(_ref4){var action=_ref4.action;return action.type===REMOVE_PRODUCT_FROM_FAVORITES;}).debounceTime(FAVORITE_BUTTON_DEBOUNCE_TIME);/**
17
+ */export var removeProductFromFavoritesDebounced$=main$.filter(function(_ref5){var action=_ref5.action;return action.type===REMOVE_PRODUCT_FROM_FAVORITES;}).debounceTime(FAVORITE_BUTTON_DEBOUNCE_TIME);/**
16
18
  * Gets triggered when any errors related to the favorite list occur.
17
19
  * @type {Observable}
18
- */export var favoritesError$=main$.filter(function(_ref5){var action=_ref5.action;return[ERROR_FETCH_FAVORITES,ERROR_ADD_FAVORITES,ERROR_REMOVE_FAVORITES,ERROR_FAVORITES].includes(action.type);});/**
20
+ */export var favoritesError$=main$.filter(function(_ref6){var action=_ref6.action;return[ERROR_FETCH_FAVORITES,ERROR_ADD_FAVORITES,ERROR_REMOVE_FAVORITES,ERROR_FAVORITES].includes(action.type);});/**
19
21
  * Gets triggered when errors occur, that are only related to the frontend part of the
20
22
  * favorite list.
21
23
  * @type {Observable}
22
- */export var errorFavoritesLimit$=favoritesError$.filter(function(_ref6){var action=_ref6.action;return action.type===ERROR_FAVORITES&&action.error&&action.error.code===FAVORITES_LIMIT_ERROR;});/**
24
+ */export var errorFavoritesLimit$=favoritesError$.filter(function(_ref7){var action=_ref7.action;return action.type===ERROR_FAVORITES&&action.error&&action.error.code===FAVORITES_LIMIT_ERROR;});/**
23
25
  * Gets triggered when the app started or when the favorites page is about to be entered.
24
26
  * @type {Observable}
25
27
  */export var shouldFetchFavorites$=favoritesWillEnter$.merge(appDidStart$);/**
@@ -28,39 +30,39 @@ import{routeWillEnter$}from'@shopgate/pwa-common/streams/router';import{main$}fr
28
30
  */export var shouldFetchFreshFavorites$=userDidLogin$.merge(userDidLogout$);/**
29
31
  * Gets triggered when the favorites updated in any way.
30
32
  * @type {Observable}
31
- */export var favoritesDidUpdate$=main$.filter(function(_ref7){var action=_ref7.action;return[REQUEST_ADD_FAVORITES,ERROR_ADD_FAVORITES,REQUEST_REMOVE_FAVORITES,ERROR_REMOVE_FAVORITES,RECEIVE_FAVORITES,ERROR_FETCH_FAVORITES].includes(action.type);});/**
33
+ */export var favoritesDidUpdate$=main$.filter(function(_ref8){var action=_ref8.action;return[REQUEST_ADD_FAVORITES,ERROR_ADD_FAVORITES,REQUEST_REMOVE_FAVORITES,ERROR_REMOVE_FAVORITES,RECEIVE_FAVORITES,ERROR_FETCH_FAVORITES].includes(action.type);});/**
32
34
  * Gets triggered when an "add to favorites" action is being placed into the favorite list buffer.
33
35
  * @type {Observable}
34
- */export var favoritesWillAddItem$=main$.filter(function(_ref8){var action=_ref8.action;return action.type===REQUEST_ADD_FAVORITES;});/**
36
+ */export var favoritesWillAddItem$=main$.filter(function(_ref9){var action=_ref9.action;return action.type===REQUEST_ADD_FAVORITES;});/**
35
37
  * Gets triggered when a single product has been successfully added to the favorite list.
36
38
  * @type {Observable}
37
- */export var favoritesDidAddItem$=main$.filter(function(_ref9){var action=_ref9.action;return action.type===SUCCESS_ADD_FAVORITES;});/**
39
+ */export var favoritesDidAddItem$=main$.filter(function(_ref10){var action=_ref10.action;return action.type===SUCCESS_ADD_FAVORITES;});/**
38
40
  * Gets triggered when a "remove from favorites" action is being placed into the favorite
39
41
  * list buffer.
40
42
  * @type {Observable}
41
- */export var favoritesWillRemoveItem$=main$.filter(function(_ref10){var action=_ref10.action;return action.type===REQUEST_REMOVE_FAVORITES;});/**
43
+ */export var favoritesWillRemoveItem$=main$.filter(function(_ref11){var action=_ref11.action;return action.type===REQUEST_REMOVE_FAVORITES;});/**
42
44
  * Gets triggered when a single product has been successfully removed from the favorite list.
43
45
  * @type {Observable}
44
- */export var favoritesDidRemoveItem$=main$.filter(function(_ref11){var action=_ref11.action;return action.type===SUCCESS_REMOVE_FAVORITES;});/**
46
+ */export var favoritesDidRemoveItem$=main$.filter(function(_ref12){var action=_ref12.action;return action.type===SUCCESS_REMOVE_FAVORITES;});/**
45
47
  * Gets triggered when the favorite list has successfully been received from the backend.
46
48
  * @type {Observable}
47
- */export var receiveFavorites$=main$.filter(function(_ref12){var action=_ref12.action;return action.type===RECEIVE_FAVORITES;});export var receiveFavoritesWhileVisible$=receiveFavorites$.filter(function(_ref13){var getState=_ref13.getState;var currentPath=getCurrentPathname(getState());return currentPath===FAVORITES_PATH||currentPath===WISH_LIST_PATH;});/**
49
+ */export var receiveFavorites$=main$.filter(function(_ref13){var action=_ref13.action;return action.type===RECEIVE_FAVORITES;});export var receiveFavoritesWhileVisible$=receiveFavorites$.filter(function(_ref14){var getState=_ref14.getState;var currentPath=getCurrentPathname(getState());return currentPath===FAVORITES_PATH||currentPath===WISH_LIST_PATH;});/**
48
50
  * Gets triggered whenever all favorite changes have been successfully processed or once when
49
51
  * any of the buffered change requests fails.
50
52
  * @type {Observable}
51
- */export var favoritesSyncIdle$=main$.filter(function(_ref14){var action=_ref14.action;return action.type===IDLE_SYNC_FAVORITES;});/**
53
+ */export var favoritesSyncIdle$=main$.filter(function(_ref15){var action=_ref15.action;return action.type===IDLE_SYNC_FAVORITES;});/**
52
54
  * Gets triggered after an favorites list has been added.
53
- */export var favoritesListAdded$=main$.filter(function(_ref15){var action=_ref15.action;return action.type===SUCCESS_ADD_FAVORITES_LIST;});/**
55
+ */export var favoritesListAdded$=main$.filter(function(_ref16){var action=_ref16.action;return action.type===SUCCESS_ADD_FAVORITES_LIST;});/**
54
56
  * Gets triggered when the favorites should be refreshed to maintain data consistency.
55
57
  * @type {Observable}
56
58
  */export var refreshFavorites$=favoritesSyncIdle$.debounceTime(FETCH_FAVORITES_THROTTLE);/**
57
59
  * Gets triggered when a product is requested to be added, removed or updated
58
60
  * from the favorite list.
59
61
  * @type {Observable}
60
- */export var didRequestChangeFavorites$=main$.filter(function(_ref16){var action=_ref16.action;return action.type===REQUEST_ADD_FAVORITES||action.type===REQUEST_REMOVE_FAVORITES||action.type===REQUEST_UPDATE_FAVORITES;});/**
62
+ */export var didRequestChangeFavorites$=main$.filter(function(_ref17){var action=_ref17.action;return action.type===REQUEST_ADD_FAVORITES||action.type===REQUEST_REMOVE_FAVORITES||action.type===REQUEST_UPDATE_FAVORITES;});/**
61
63
  * Gets triggered when the favorites action buffer should be flushed immediately.
62
64
  * @type {Observable}
63
- */export var didRequestFlushFavoritesBuffer$=main$.filter(function(_ref17){var action=_ref17.action;return action.type===REQUEST_FLUSH_FAVORITES_BUFFER;});/**
65
+ */export var didRequestFlushFavoritesBuffer$=main$.filter(function(_ref18){var action=_ref18.action;return action.type===REQUEST_FLUSH_FAVORITES_BUFFER;});/**
64
66
  * Gets triggered when the favorites buffer is supposed to be flushed after some delay time or
65
67
  * when the `requestFlushFavoritesBuffer` action was triggered.
66
68
  * @type {Observable}
@@ -1,9 +1,9 @@
1
- import _regeneratorRuntime from"@babel/runtime/regenerator";function _slicedToArray(arr,i){return _arrayWithHoles(arr)||_iterableToArrayLimit(arr,i)||_nonIterableRest();}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance");}function _iterableToArrayLimit(arr,i){var _arr=[];var _n=true;var _d=false;var _e=undefined;try{for(var _i=arr[Symbol.iterator](),_s;!(_n=(_s=_i.next()).done);_n=true){_arr.push(_s.value);if(i&&_arr.length===i)break;}}catch(err){_d=true;_e=err;}finally{try{if(!_n&&_i["return"]!=null)_i["return"]();}finally{if(_d)throw _e;}}return _arr;}function _arrayWithHoles(arr){if(Array.isArray(arr))return arr;}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 pipelineDependencies from'@shopgate/pwa-core/classes/PipelineDependencies';import appConfig from'@shopgate/pwa-common/helpers/config';import showModal from'@shopgate/pwa-common/actions/modal/showModal';import{appDidStart$}from'@shopgate/pwa-common/streams';import groupBy from'lodash/groupBy';import ToastProvider from'@shopgate/pwa-common/providers/toast';import{getLoadWishlistOnAppStartEnabled,getWishlistItemQuantityEnabled}from'@shopgate/engage/core/selectors/shopSettings';import{LoadingProvider}from'@shopgate/pwa-common/providers';import{favoritesWillEnter$,shouldFetchFreshFavorites$,addProductToFavoritesDebounced$,removeProductFromFavoritesDebounced$,errorFavoritesLimit$,refreshFavorites$,didReceiveFlushFavoritesBuffer$,updateProductInFavoritesDebounced$,favoritesDidAddItem$,favoritesSyncIdle$}from"../streams";import{SHOPGATE_USER_ADD_FAVORITES,SHOPGATE_USER_DELETE_FAVORITES}from"../constants/Pipelines";import addFavorites from"../actions/addFavorites";import updateFavorites from"../actions/updateFavorites";import removeFavorites from"../actions/removeFavorites";import fetchFavoritesListsWithItems from"../actions/fetchFavoritesListsWithItems";import fetchFavorites from"../actions/fetchFavorites";import{requestAddFavorites,requestRemoveFavorites,cancelRequestSyncFavorites,errorFavorites,idleSyncFavorites,requestUpdateFavorites}from"../action-creators";import{REQUEST_ADD_FAVORITES,REQUEST_REMOVE_FAVORITES,FAVORITES_LIMIT_ERROR,REQUEST_UPDATE_FAVORITES,FAVORITES_PATH}from"../constants";import{getFavoritesCount,makeGetProductRelativesOnFavorites,getFavoritesProducts}from"../selectors";/**
1
+ import _regeneratorRuntime from"@babel/runtime/regenerator";function _slicedToArray(arr,i){return _arrayWithHoles(arr)||_iterableToArrayLimit(arr,i)||_nonIterableRest();}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance");}function _iterableToArrayLimit(arr,i){var _arr=[];var _n=true;var _d=false;var _e=undefined;try{for(var _i=arr[Symbol.iterator](),_s;!(_n=(_s=_i.next()).done);_n=true){_arr.push(_s.value);if(i&&_arr.length===i)break;}}catch(err){_d=true;_e=err;}finally{try{if(!_n&&_i["return"]!=null)_i["return"]();}finally{if(_d)throw _e;}}return _arr;}function _arrayWithHoles(arr){if(Array.isArray(arr))return arr;}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 pipelineDependencies from'@shopgate/pwa-core/classes/PipelineDependencies';import appConfig from'@shopgate/pwa-common/helpers/config';import showModal from'@shopgate/pwa-common/actions/modal/showModal';import{appDidStart$}from'@shopgate/pwa-common/streams';import groupBy from'lodash/groupBy';import ToastProvider from'@shopgate/pwa-common/providers/toast';import{getLoadWishlistOnAppStartEnabled,getWishlistItemQuantityEnabled}from'@shopgate/engage/core/selectors/shopSettings';import{LoadingProvider}from'@shopgate/pwa-common/providers';import{favoritesWillEnter$,shouldFetchFreshFavorites$,addProductToFavoritesDebounced$,addProductToFavorites$,removeProductFromFavoritesDebounced$,errorFavoritesLimit$,refreshFavorites$,didReceiveFlushFavoritesBuffer$,updateProductInFavoritesDebounced$,favoritesDidAddItem$,favoritesSyncIdle$}from"../streams";import{SHOPGATE_USER_ADD_FAVORITES,SHOPGATE_USER_DELETE_FAVORITES}from"../constants/Pipelines";import addFavorites from"../actions/addFavorites";import updateFavorites from"../actions/updateFavorites";import removeFavorites from"../actions/removeFavorites";import fetchFavoritesListsWithItems from"../actions/fetchFavoritesListsWithItems";import fetchFavorites from"../actions/fetchFavorites";import{requestAddFavorites,requestRemoveFavorites,cancelRequestSyncFavorites,errorFavorites,idleSyncFavorites,requestUpdateFavorites}from"../action-creators";import{REQUEST_ADD_FAVORITES,REQUEST_REMOVE_FAVORITES,FAVORITES_LIMIT_ERROR,REQUEST_UPDATE_FAVORITES,FAVORITES_PATH}from"../constants";import{getFavoritesCount,makeGetFavoritesCountByList,makeGetProductRelativesOnFavorites,getFavoritesProducts}from"../selectors";/**
2
2
  * @param {Function} subscribe Subscribes to an observable.
3
3
  */export default function favorites(subscribe){if(!appConfig.hasFavorites){return;}/** App start */subscribe(appDidStart$,/*#__PURE__*/function(){var _ref2=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(_ref){var dispatch,getState,loadWishlistOnAppStartEnabled;return _regeneratorRuntime.wrap(function _callee$(_context){while(1){switch(_context.prev=_context.next){case 0:dispatch=_ref.dispatch,getState=_ref.getState;// Setup sync pipeline dependencies (concurrency to each other and themselves)
4
4
  pipelineDependencies.set(SHOPGATE_USER_ADD_FAVORITES,[SHOPGATE_USER_ADD_FAVORITES,SHOPGATE_USER_DELETE_FAVORITES]);pipelineDependencies.set(SHOPGATE_USER_DELETE_FAVORITES,[SHOPGATE_USER_ADD_FAVORITES,SHOPGATE_USER_DELETE_FAVORITES]);loadWishlistOnAppStartEnabled=getLoadWishlistOnAppStartEnabled(getState());if(!loadWishlistOnAppStartEnabled){_context.next=7;break;}_context.next=7;return dispatch(fetchFavoritesListsWithItems(false));case 7:case"end":return _context.stop();}}},_callee);}));return function(_x){return _ref2.apply(this,arguments);};}());/** Favorites route enter */subscribe(favoritesWillEnter$,/*#__PURE__*/function(){var _ref4=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(_ref3){var dispatch;return _regeneratorRuntime.wrap(function _callee2$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:dispatch=_ref3.dispatch;_context2.next=3;return dispatch(fetchFavoritesListsWithItems(true));case 3:case"end":return _context2.stop();}}},_callee2);}));return function(_x2){return _ref4.apply(this,arguments);};}());/** User login / logout */subscribe(shouldFetchFreshFavorites$,/*#__PURE__*/function(){var _ref6=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(_ref5){var dispatch;return _regeneratorRuntime.wrap(function _callee3$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:dispatch=_ref5.dispatch;_context3.next=3;return dispatch(fetchFavoritesListsWithItems(true));case 3:case"end":return _context3.stop();}}},_callee3);}));return function(_x3){return _ref6.apply(this,arguments);};}());subscribe(addProductToFavoritesDebounced$,function(_ref7){var _getFavoritesProducts;var action=_ref7.action,dispatch=_ref7.dispatch,getState=_ref7.getState;var state=getState();var wishlistItemQuantityEnabled=getWishlistItemQuantityEnabled(state);// Nothing to do, when the store already contains the item
5
5
  var activeProductInList=(_getFavoritesProducts=getFavoritesProducts(state).byList[action.listId])===null||_getFavoritesProducts===void 0?void 0:_getFavoritesProducts.items.find(function(_ref8){var productId=_ref8.productId;return productId===action.productId;});if(activeProductInList&&!wishlistItemQuantityEnabled){// Call cancel action with "zero" count, because request was even dispatched
6
- dispatch(cancelRequestSyncFavorites(0,action.listId));return;}var _appConfig$favorites=appConfig.favorites;_appConfig$favorites=_appConfig$favorites===void 0?{}:_appConfig$favorites;var _appConfig$favorites$=_appConfig$favorites.limit,limit=_appConfig$favorites$===void 0?100:_appConfig$favorites$;var count=getFavoritesCount(state);if(count>=limit){// Dispatch a local error only, because the request to add is prevented
6
+ dispatch(cancelRequestSyncFavorites(0,action.listId));return;}var _appConfig$favorites=appConfig.favorites;_appConfig$favorites=_appConfig$favorites===void 0?{}:_appConfig$favorites;var _appConfig$favorites$=_appConfig$favorites.limit,limit=_appConfig$favorites$===void 0?100:_appConfig$favorites$;var count=makeGetFavoritesCountByList(function(){return action===null||action===void 0?void 0:action.listId;})(state);if(limit&&count>=limit){// Dispatch a local error only, because the request to add is prevented
7
7
  var error=new Error('Limit exceeded');error.code=FAVORITES_LIMIT_ERROR;dispatch(errorFavorites(action.productId,error));}else{dispatch(requestAddFavorites(action.productId,action.listId,action.quantity,action.notes));}});subscribe(updateProductInFavoritesDebounced$,function(_ref9){var action=_ref9.action,dispatch=_ref9.dispatch;dispatch(requestUpdateFavorites(action.productId,action.listId,action.quantity,action.notes));});subscribe(removeProductFromFavoritesDebounced$,function(_ref10){var _getFavoritesProducts2;var action=_ref10.action,dispatch=_ref10.dispatch,getState=_ref10.getState;var count=getFavoritesCount(getState());if(count>0){if(action.withRelatives){// Will only handle ids which are present in the store, no additional check needed
8
8
  var allOnList=makeGetProductRelativesOnFavorites(function(){return action.listId;})(getState(),{productId:action.productId});allOnList.forEach(function(id){return dispatch(requestRemoveFavorites(id,action.listId));});return;}// Avoids trying to remove something that was already removed (incoming fetch response)
9
9
  var list=getFavoritesProducts(getState()).byList[action.listId];if(!(list===null||list===void 0?void 0:list.items.find(function(_ref11){var productId=_ref11.productId;return productId===action.productId;}))){// Call cancel action with "zero" count, because request was even dispatched
@@ -20,4 +20,10 @@ subscribe(errorFavoritesLimit$,function(_ref12){var dispatch=_ref12.dispatch;dis
20
20
  */subscribe(didReceiveFlushFavoritesBuffer$,/*#__PURE__*/function(){var _ref15=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee6(actionBuffer){var dispatch,actions,actionsByListAndProduct,idleLists;return _regeneratorRuntime.wrap(function _callee6$(_context6){while(1){switch(_context6.prev=_context6.next){case 0:if(!(!Array.isArray(actionBuffer)||!actionBuffer.length)){_context6.next=2;break;}return _context6.abrupt("return");case 2:LoadingProvider.setLoading(FAVORITES_PATH);// All actions provide the same functionality, just take the first entry
21
21
  dispatch=actionBuffer[0].dispatch;// Group all buffered actions by listId and productID
22
22
  actions=actionBuffer.map(function(_ref16){var action=_ref16.action;return action;});actionsByListAndProduct=groupBy(actions,function(_ref17){var listId=_ref17.listId,productId=_ref17.productId;return"".concat(listId,"-").concat(productId,"}");});idleLists=[];_context6.next=9;return Object.values(actionsByListAndProduct).forEach(/*#__PURE__*/function(){var _ref18=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee5(groupedActions){var _groupedActions,_groupedActions$,productId,listId,quantity,notes,updateActions,addActions,removeActions,_updateActions$slice,_updateActions$slice2,lastUpdateAction,addRemoveBalance;return _regeneratorRuntime.wrap(function _callee5$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:_groupedActions=_slicedToArray(groupedActions,1),_groupedActions$=_groupedActions[0];_groupedActions$=_groupedActions$===void 0?{}:_groupedActions$;productId=_groupedActions$.productId,listId=_groupedActions$.listId,quantity=_groupedActions$.quantity,notes=_groupedActions$.notes;updateActions=groupedActions.filter(function(action){return action.type===REQUEST_UPDATE_FAVORITES;});addActions=groupedActions.filter(function(action){return action.type===REQUEST_ADD_FAVORITES;});removeActions=groupedActions.filter(function(action){return action.type===REQUEST_REMOVE_FAVORITES;});_context5.prev=6;if(!(updateActions.length>0)){_context5.next=11;break;}_updateActions$slice=updateActions.slice(-1),_updateActions$slice2=_slicedToArray(_updateActions$slice,1),lastUpdateAction=_updateActions$slice2[0];_context5.next=11;return dispatch(updateFavorites(lastUpdateAction.productId,lastUpdateAction.listId,lastUpdateAction.quantity,lastUpdateAction.notes));case 11:// Sum up all adds and removes, based on sum dispatch add / remove
23
- addRemoveBalance=addActions.length-removeActions.length;if(!(addRemoveBalance>0)){_context5.next=15;break;}_context5.next=15;return dispatch(addFavorites(productId,listId,quantity,notes));case 15:if(!(addRemoveBalance<0)){_context5.next=18;break;}_context5.next=18;return dispatch(removeFavorites(productId,listId,quantity,notes));case 18:if(!(updateActions.length===0&&addRemoveBalance===0)){_context5.next=22;break;}dispatch(cancelRequestSyncFavorites(groupedActions.length,listId));_context5.next=26;break;case 22:if(idleLists.includes(listId)){_context5.next=26;break;}idleLists.push(listId);_context5.next=26;return dispatch(idleSyncFavorites(listId));case 26:_context5.next=34;break;case 28:_context5.prev=28;_context5.t0=_context5["catch"](6);if(idleLists.includes(listId)){_context5.next=34;break;}idleLists.push(listId);_context5.next=34;return dispatch(idleSyncFavorites(listId));case 34:case"end":return _context5.stop();}}},_callee5,null,[[6,28]]);}));return function(_x6){return _ref18.apply(this,arguments);};}());case 9:case"end":return _context6.stop();}}},_callee6);}));return function(_x5){return _ref15.apply(this,arguments);};}());subscribe(favoritesSyncIdle$,function(){LoadingProvider.resetLoading(FAVORITES_PATH);});subscribe(favoritesDidAddItem$,function(_ref19){var events=_ref19.events;var shouldShowToast=true;if(shouldShowToast){events.emit(ToastProvider.ADD,{id:'favorites.added',message:'favorites.added'});}});}
23
+ addRemoveBalance=addActions.length-removeActions.length;if(!(addRemoveBalance>0)){_context5.next=15;break;}_context5.next=15;return dispatch(addFavorites(productId,listId,quantity,notes));case 15:if(!(addRemoveBalance<0)){_context5.next=18;break;}_context5.next=18;return dispatch(removeFavorites(productId,listId,quantity,notes));case 18:if(!(updateActions.length===0&&addRemoveBalance===0)){_context5.next=22;break;}dispatch(cancelRequestSyncFavorites(groupedActions.length,listId));_context5.next=26;break;case 22:if(idleLists.includes(listId)){_context5.next=26;break;}idleLists.push(listId);_context5.next=26;return dispatch(idleSyncFavorites(listId));case 26:_context5.next=34;break;case 28:_context5.prev=28;_context5.t0=_context5["catch"](6);if(idleLists.includes(listId)){_context5.next=34;break;}idleLists.push(listId);_context5.next=34;return dispatch(idleSyncFavorites(listId));case 34:case"end":return _context5.stop();}}},_callee5,null,[[6,28]]);}));return function(_x6){return _ref18.apply(this,arguments);};}());case 9:case"end":return _context6.stop();}}},_callee6);}));return function(_x5){return _ref15.apply(this,arguments);};}());subscribe(favoritesSyncIdle$,function(){LoadingProvider.resetLoading(FAVORITES_PATH);});subscribe(addProductToFavorites$,function(_ref19){var events=_ref19.events,getState=_ref19.getState;var loadWishlistOnAppStartEnabled=getLoadWishlistOnAppStartEnabled(getState());// When wish list loading on app start is disabled, toast is shown instantly after the add
24
+ // action was dispatched. We don't have to wait for the "debounced" action, since
25
+ // removal of wishlist items is not possible.
26
+ if(!loadWishlistOnAppStartEnabled){events.emit(ToastProvider.ADD,{id:'favorites.added',message:'favorites.added'});}});subscribe(favoritesDidAddItem$,function(_ref20){var events=_ref20.events,getState=_ref20.getState;var loadWishlistOnAppStartEnabled=getLoadWishlistOnAppStartEnabled(getState());// When wish list loading on app start is enabled, toast is shown after the add action from
27
+ // the buffer system is dispatched. We wait for that since the item might have been removed
28
+ // again within the debounce time.
29
+ if(loadWishlistOnAppStartEnabled){events.emit(ToastProvider.ADD,{id:'favorites.added',message:'favorites.added'});}});}
@@ -1,6 +1,6 @@
1
1
  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
2
2
  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
3
- var mockedGetFavoritesProductsIdsReturnValue;var mockedGetFavoritesCountReturnValue;var mockedGetFavoritesProductsReturnValue;var mockedGetProductRelativesOnFavoritesReturnValue;var mockedMakeGetFavoritesReturnValue;jest.mock("../selectors",function(){return{getFavoritesProductsIds:jest.fn(function(){return mockedGetFavoritesProductsIdsReturnValue;}),getFavoritesProducts:jest.fn(function(){return mockedGetFavoritesProductsReturnValue;}),getFavoritesCount:jest.fn(function(){return mockedGetFavoritesCountReturnValue;}),makeGetFavorites:jest.fn(function(){return mockedMakeGetFavoritesReturnValue;}),makeGetProductRelativesOnFavorites:jest.fn(function(){return jest.fn().mockReturnValue(mockedGetProductRelativesOnFavoritesReturnValue);})};});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{};}};});/**
3
+ 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);})};});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{};}};});/**
4
4
  * Flushes the promise queue.
5
5
  * @returns {Promise}
6
6
  */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
@@ -11,7 +11,7 @@ resetSubscriptions();// Restore singleton
11
11
  pipelineDependencies.set=pipelineDependenciesSet;});var getState=jest.fn().mockReturnValue(function(){});// Dispatch always resolves it's promise by default
12
12
  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
13
13
  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
14
- 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;mockedGetFavoritesCountReturnValue=2;invoke(addProductToFavoritesDebounced$,{action:addProductToFavorites(productId),dispatch:dispatch,getState:getState});expect(getState).toHaveBeenCalledTimes(1);expect(getFavoritesProducts).toHaveBeenCalledWith(getState());expect(getFavoritesProducts).toHaveBeenCalledTimes(1);expect(getFavoritesCount).toHaveBeenCalledWith(getState());expect(getFavoritesCount).toHaveBeenCalledTimes(1);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(getFavoritesCount).toHaveBeenCalledWith(getState());expect(getFavoritesCount).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
14
+ 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
15
15
  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
16
16
  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
17
17
  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}})).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}});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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shopgate/pwa-common-commerce",
3
- "version": "7.9.0-beta.6",
3
+ "version": "7.9.0-beta.8",
4
4
  "description": "Commerce library for the Shopgate Connect PWA.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Shopgate <support@shopgate.com>",
@@ -19,8 +19,8 @@
19
19
  "reselect": "^3.0.1"
20
20
  },
21
21
  "devDependencies": {
22
- "@shopgate/pwa-common": "7.9.0-beta.6",
23
- "@shopgate/pwa-core": "7.9.0-beta.6",
22
+ "@shopgate/pwa-common": "7.9.0-beta.8",
23
+ "@shopgate/pwa-core": "7.9.0-beta.8",
24
24
  "lodash": "^4.17.4",
25
25
  "react": "~16.12.0",
26
26
  "react-dom": "~16.12.0"