UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

8 lines • 14.6 kB
/* eslint-disable extra-rules/no-single-line-objects */import{mainSubject}from'@shopgate/pwa-common/store/middelwares/streams';import{APP_DID_START,ROUTE_WILL_ENTER,SUCCESS_LOGIN,SUCCESS_LOGOUT}from'@shopgate/pwa-common/constants/ActionTypes';import{favoritesWillEnter$,addProductToFavoritesDebounced$,removeProductFromFavoritesDebounced$,favoritesError$,errorFavoritesLimit$,shouldFetchFavorites$,shouldFetchFreshFavorites$,favoritesDidUpdate$,favoritesWillAddItem$,favoritesDidAddItem$,favoritesWillRemoveItem$,favoritesDidRemoveItem$,receiveFavorites$,favoritesSyncIdle$,refreshFavorites$,didRequestChangeFavorites$,didRequestFlushFavoritesBuffer$,didReceiveFlushFavoritesBuffer$}from"./index";import{FAVORITES_PATH,RECEIVE_FAVORITES,ERROR_FAVORITES,ERROR_FETCH_FAVORITES,REQUEST_ADD_FAVORITES,ERROR_ADD_FAVORITES,REQUEST_REMOVE_FAVORITES,ERROR_REMOVE_FAVORITES,FAVORITES_LIMIT_ERROR,FAVORITE_ACTION_BUFFER_TIME,FAVORITE_BUTTON_DEBOUNCE_TIME}from"../constants";import{addProductToFavorites,removeProductFromFavorites,requestAddFavorites,requestRemoveFavorites,successAddFavorites,successRemoveFavorites,receiveFavorites,idleSyncFavorites,requestFlushFavoritesBuffer}from"../action-creators";// Required for custom runner without env-setup jest.mock('@shopgate/pwa-core',function(){return{UIEvents:{emit:jest.fn(),on:jest.fn(),addListener:jest.fn(),removeListener:jest.fn()}};});describe('Favorites streams',function(){var DUMMY_ACTION='DUMMY_ACTION';var subscriber;beforeEach(function(){subscriber=jest.fn();});describe('favoritesWillEnter$',function(){it('should call subscribers when the favorites page will open',function(){favoritesWillEnter$.subscribe(subscriber);mainSubject.next({action:{type:ROUTE_WILL_ENTER,route:{pattern:FAVORITES_PATH}}});expect(subscriber).toHaveBeenCalledTimes(1);});it('should not call subscribers when the path does not match',function(){favoritesWillEnter$.subscribe(subscriber);mainSubject.next({action:{type:ROUTE_WILL_ENTER,route:{pattern:'/other_path'}}});expect(subscriber).toHaveBeenCalledTimes(0);});it('should not call subscribers when the action does not match',function(){favoritesWillEnter$.subscribe(subscriber);mainSubject.next({action:{type:DUMMY_ACTION,route:{pattern:FAVORITES_PATH}}});expect(subscriber).toHaveBeenCalledTimes(0);});});describe('addProductToFavoritesDebounced$',function(){afterEach(function(){jest.useRealTimers();});it('should call subscribers only once, when the action is triggered multiple times',function(){jest.useFakeTimers();addProductToFavoritesDebounced$.subscribe(subscriber);var action=addProductToFavorites('product1');mainSubject.next({action:action});mainSubject.next({action:action});jest.advanceTimersByTime(FAVORITE_BUTTON_DEBOUNCE_TIME);expect(subscriber).toHaveBeenCalledTimes(1);});it('should call subscribers twice, when debounce time has passed',function(){jest.useFakeTimers();addProductToFavoritesDebounced$.subscribe(subscriber);var action=addProductToFavorites('product1');mainSubject.next({action:action});jest.advanceTimersByTime(FAVORITE_BUTTON_DEBOUNCE_TIME);mainSubject.next({action:action});jest.advanceTimersByTime(FAVORITE_BUTTON_DEBOUNCE_TIME);expect(subscriber).toHaveBeenCalledTimes(2);});it('should not call subscribers when the action does not match',function(){addProductToFavoritesDebounced$.subscribe(subscriber);mainSubject.next({action:{type:DUMMY_ACTION}});expect(subscriber).toHaveBeenCalledTimes(0);});});describe('removeProductFromFavoritesDebounced$',function(){afterEach(function(){jest.useRealTimers();});it('should call subscribers only once, when the action is triggered multiple times',function(){jest.useFakeTimers();removeProductFromFavoritesDebounced$.subscribe(subscriber);var action=removeProductFromFavorites('product1',true);mainSubject.next({action:action});mainSubject.next({action:action});jest.advanceTimersByTime(FAVORITE_BUTTON_DEBOUNCE_TIME);expect(subscriber).toHaveBeenCalledTimes(1);});it('should call subscribers twice, when debounce time has passed',function(){jest.useFakeTimers();removeProductFromFavoritesDebounced$.subscribe(subscriber);var action=removeProductFromFavorites('product1',true);mainSubject.next({action:action});jest.advanceTimersByTime(FAVORITE_BUTTON_DEBOUNCE_TIME);mainSubject.next({action:action});jest.advanceTimersByTime(FAVORITE_BUTTON_DEBOUNCE_TIME);expect(subscriber).toHaveBeenCalledTimes(2);});it('should not call subscribers when the action does not match',function(){removeProductFromFavoritesDebounced$.subscribe(subscriber);mainSubject.next({action:{type:DUMMY_ACTION}});expect(subscriber).toHaveBeenCalledTimes(0);});});describe('favoritesError$',function(){var actionTypes=[ERROR_FETCH_FAVORITES,ERROR_ADD_FAVORITES,ERROR_REMOVE_FAVORITES,ERROR_FAVORITES,DUMMY_ACTION];it('should call subscribers for every dispatched favorites error and no others',function(){favoritesError$.subscribe(subscriber);actionTypes.forEach(function(type){mainSubject.next({action:{type:type}});});expect(subscriber).toHaveBeenCalledTimes(4);});});describe('errorFavoritesLimit$',function(){it('should call subscribers only for the internal favorites limit error and no others',function(){errorFavoritesLimit$.subscribe(subscriber);mainSubject.next({action:{type:ERROR_FAVORITES,error:{code:FAVORITES_LIMIT_ERROR}}});expect(subscriber).toHaveBeenCalledTimes(1);});it('should not call subscribers when the action does not match',function(){errorFavoritesLimit$.subscribe(subscriber);mainSubject.next({action:{type:DUMMY_ACTION}});expect(subscriber).toHaveBeenCalledTimes(0);});it('should not call subscribers on any other internal favorites error',function(){errorFavoritesLimit$.subscribe(subscriber);mainSubject.next({action:{type:ERROR_FAVORITES,error:{code:'SOME_OTHER_CODE'}}});expect(subscriber).toHaveBeenCalledTimes(0);});});describe('shouldFetchFavorites$',function(){it('should call subscribers to fetch favorites on every app start and on route enter',function(){shouldFetchFavorites$.subscribe(subscriber);mainSubject.next({action:{type:APP_DID_START}});mainSubject.next({action:{type:ROUTE_WILL_ENTER,route:{pattern:FAVORITES_PATH}}});expect(subscriber).toHaveBeenCalledTimes(2);});it('should call subscribers on any other action',function(){shouldFetchFavorites$.subscribe(subscriber);mainSubject.next({action:{type:DUMMY_ACTION}});expect(subscriber).toHaveBeenCalledTimes(0);});});describe('shouldFetchFreshFavorites$',function(){it('should call subscribers to fetch fresh favorites on every login and logout',function(){shouldFetchFreshFavorites$.subscribe(subscriber);mainSubject.next({action:{type:SUCCESS_LOGIN}});mainSubject.next({action:{type:SUCCESS_LOGOUT}});expect(subscriber).toHaveBeenCalledTimes(2);});it('should call subscribers on any other action',function(){shouldFetchFreshFavorites$.subscribe(subscriber);mainSubject.next({action:{type:DUMMY_ACTION}});expect(subscriber).toHaveBeenCalledTimes(0);});});describe('favoritesDidUpdate$',function(){var actionTypes=[REQUEST_ADD_FAVORITES,ERROR_ADD_FAVORITES,REQUEST_REMOVE_FAVORITES,ERROR_REMOVE_FAVORITES,RECEIVE_FAVORITES,ERROR_FETCH_FAVORITES,DUMMY_ACTION];it('should call subscribers for every dispatched favorites update and no others',function(){favoritesDidUpdate$.subscribe(subscriber);actionTypes.forEach(function(type){mainSubject.next({action:{type:type}});});expect(subscriber).toHaveBeenCalledTimes(6);});});describe('favoritesWillAddItem$',function(){it('should call subscribers for every favorite to be added',function(){favoritesWillAddItem$.subscribe(subscriber);mainSubject.next({action:requestAddFavorites('product1')});expect(subscriber).toHaveBeenCalledTimes(1);});it('should not call subscribers when the action does not match',function(){favoritesWillAddItem$.subscribe(subscriber);mainSubject.next({action:{type:DUMMY_ACTION}});expect(subscriber).toHaveBeenCalledTimes(0);});});describe('favoritesDidAddItem$',function(){it('should call subscribers for every favorite that was added',function(){favoritesDidAddItem$.subscribe(subscriber);mainSubject.next({action:successAddFavorites('product1')});expect(subscriber).toHaveBeenCalledTimes(1);});it('should not call subscribers when the action does not match',function(){favoritesDidAddItem$.subscribe(subscriber);mainSubject.next({action:{type:DUMMY_ACTION}});expect(subscriber).toHaveBeenCalledTimes(0);});});describe('favoritesWillRemoveItem$',function(){it('should call subscribers for every favorite to be removed',function(){favoritesWillRemoveItem$.subscribe(subscriber);mainSubject.next({action:requestRemoveFavorites('product1')(function(action){return action;},function(){return{};})});expect(subscriber).toHaveBeenCalledTimes(1);});it('should not call subscribers when the action does not match',function(){favoritesWillRemoveItem$.subscribe(subscriber);mainSubject.next({action:{type:DUMMY_ACTION}});expect(subscriber).toHaveBeenCalledTimes(0);});});describe('favoritesDidRemoveItem$',function(){it('should call subscribers for every favorite that was removed',function(){favoritesDidRemoveItem$.subscribe(subscriber);mainSubject.next({action:successRemoveFavorites('product1')});expect(subscriber).toHaveBeenCalledTimes(1);});it('should not call subscribers when the action does not match',function(){favoritesDidRemoveItem$.subscribe(subscriber);mainSubject.next({action:{type:DUMMY_ACTION}});expect(subscriber).toHaveBeenCalledTimes(0);});});describe('receiveFavorites$',function(){it('should call subscribers for every receive favorites action',function(){receiveFavorites$.subscribe(subscriber);mainSubject.next({action:receiveFavorites()});expect(subscriber).toHaveBeenCalledTimes(1);});it('should not call subscribers when the action does not match',function(){receiveFavorites$.subscribe(subscriber);mainSubject.next({action:{type:DUMMY_ACTION}});expect(subscriber).toHaveBeenCalledTimes(0);});});describe('favoritesSyncIdle$',function(){it('should call subscribers for every sync idle call',function(){favoritesSyncIdle$.subscribe(subscriber);mainSubject.next({action:idleSyncFavorites()});expect(subscriber).toHaveBeenCalledTimes(1);});it('should not call subscribers when the action does not match',function(){favoritesSyncIdle$.subscribe(subscriber);mainSubject.next({action:{type:DUMMY_ACTION}});expect(subscriber).toHaveBeenCalledTimes(0);});});describe('refreshFavorites$',function(){afterEach(function(){jest.useRealTimers();});it('should call subscribers only once, when the action is triggered multiple times',function(){jest.useFakeTimers();refreshFavorites$.subscribe(subscriber);var action=idleSyncFavorites();mainSubject.next({action:action});mainSubject.next({action:action});jest.advanceTimersByTime(FAVORITE_BUTTON_DEBOUNCE_TIME);expect(subscriber).toHaveBeenCalledTimes(1);});it('should call subscribers twice, when debounce time has passed',function(){jest.useFakeTimers();refreshFavorites$.subscribe(subscriber);var action=idleSyncFavorites();mainSubject.next({action:action});jest.advanceTimersByTime(FAVORITE_BUTTON_DEBOUNCE_TIME);mainSubject.next({action:action});jest.advanceTimersByTime(FAVORITE_BUTTON_DEBOUNCE_TIME);expect(subscriber).toHaveBeenCalledTimes(2);});it('should not call subscribers when the action does not match',function(){refreshFavorites$.subscribe(subscriber);mainSubject.next({action:{type:DUMMY_ACTION}});expect(subscriber).toHaveBeenCalledTimes(0);});});describe('didRequestChangeFavorites$',function(){it('should call subscribers for every actual add or remove request',function(){didRequestChangeFavorites$.subscribe(subscriber);mainSubject.next({action:requestAddFavorites('product1')});mainSubject.next({action:requestRemoveFavorites('product2',false)(function(actionInner){return actionInner;},function(){return{};})});expect(subscriber).toHaveBeenCalledTimes(2);});it('should not call subscribers when the action does not match',function(){didRequestChangeFavorites$.subscribe(subscriber);mainSubject.next({action:{type:DUMMY_ACTION}});expect(subscriber).toHaveBeenCalledTimes(0);});});describe('didRequestFlushFavoritesBuffer$',function(){it('should call subscribers for every buffer flush request',function(){didRequestFlushFavoritesBuffer$.subscribe(subscriber);mainSubject.next({action:requestFlushFavoritesBuffer()});expect(subscriber).toHaveBeenCalledTimes(1);});it('should not call subscribers when the action does not match',function(){didRequestFlushFavoritesBuffer$.subscribe(subscriber);mainSubject.next({action:{type:DUMMY_ACTION}});expect(subscriber).toHaveBeenCalledTimes(0);});});describe('didReceiveFlushFavoritesBuffer$',function(){it('should call subscribers with the full action buffer when the buffer period has passed',function(){jest.useFakeTimers();var bufferedActions=[{action:requestAddFavorites('product1')},{action:requestAddFavorites('product2')},{action:requestRemoveFavorites('product2',false)},{action:requestAddFavorites('product2')}];didReceiveFlushFavoritesBuffer$.subscribe(subscriber);// Pump all actions into the buffer bufferedActions.forEach(function(action){if(typeof(action===null||action===void 0?void 0:action.action)==='function'){// eslint-disable-next-line no-param-reassign action.action=action.action(function(actionInner){return actionInner;},function(){return{};});}mainSubject.next(action);});// Trigger flush by timeout jest.advanceTimersByTime(FAVORITE_ACTION_BUFFER_TIME);expect(subscriber).toHaveBeenCalledTimes(1);expect(subscriber).toHaveBeenCalledWith(bufferedActions);jest.useRealTimers();});it('should call subscribers with the full action buffer when the buffer is cleared by manual request',function(){var bufferedActions=[{action:requestAddFavorites('product1')},{action:requestAddFavorites('product2')},{action:requestRemoveFavorites('product2',false)},{action:requestAddFavorites('product2')}];didReceiveFlushFavoritesBuffer$.subscribe(subscriber);// Pump all actions into the buffer bufferedActions.forEach(function(action){if(typeof(action===null||action===void 0?void 0:action.action)==='function'){// eslint-disable-next-line no-param-reassign action.action=action.action(function(actionInner){return actionInner;},function(){return{};});}mainSubject.next(action);});// Trigger flush via manual request action mainSubject.next({action:requestFlushFavoritesBuffer()});expect(subscriber).toHaveBeenCalledTimes(1);expect(subscriber).toHaveBeenCalledWith(bufferedActions);});it('should not call subscribers when the action does not match',function(){didReceiveFlushFavoritesBuffer$.subscribe(subscriber);mainSubject.next({action:{type:DUMMY_ACTION}});expect(subscriber).toHaveBeenCalledTimes(0);});});});/* eslint-enable extra-rules/no-single-line-objects */