@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
18 lines • 9.9 kB
JavaScript
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{logger}from'@shopgate/pwa-core/helpers';import{mockedPipelineRequestFactory}from'@shopgate/pwa-core/classes/PipelineRequest/mock';import{EUNKNOWN,EACCESS}from'@shopgate/pwa-core/constants/Pipeline';import fetchReviews from"./fetchReviews";import fetchProductReviews from"./fetchProductReviews";import fetchUserReview from"./fetchUserReview";import submitReview from"./submitReview";import{finalState}from"../selectors/mock";import*as pipelines from"../constants/Pipelines";var mockedResolver;jest.mock('@shopgate/pwa-core/classes/PipelineRequest',function(){return mockedPipelineRequestFactory(function(mockInstance,resolve,reject){mockedResolver(mockInstance,resolve,reject);});});jest.mock('@shopgate/pwa-core/helpers',function(){return{logger:{error:jest.fn()}};});describe.skip('Reviews actions',function(){describe('fetchReviews',function(){beforeEach(function(){jest.clearAllMocks();});it('should resolve and call appropriate actions',/*#__PURE__*/function(){var _ref=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(done){var mockedDispatch,result;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:mockedResolver=function mockedResolver(mockInstance,resolve){resolve({reviews:[],mockInstance:mockInstance});};mockedDispatch=jest.fn();_context.prev=2;_context.next=5;return fetchReviews('foo',2,1)(mockedDispatch);case 5:result=_context.sent;expect(result.mockInstance.name).toBe(pipelines.SHOPGATE_CATALOG_GET_PRODUCT_REVIEWS);expect(result.mockInstance.input).toEqual({productId:'foo',limit:2,offset:1,sort:'dateDesc'});expect(logger.error).toHaveBeenCalledTimes(0);expect(mockedDispatch).toHaveBeenCalledTimes(2);done();_context.next=16;break;case 13:_context.prev=13;_context.t0=_context["catch"](2);done(_context.t0);case 16:case"end":return _context.stop();}},_callee,null,[[2,13]]);}));return function(_x){return _ref.apply(this,arguments);};}());it('should fail and call appropriate actions',/*#__PURE__*/function(){var _ref2=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(done){var mockedDispatch,result;return _regeneratorRuntime.wrap(function _callee2$(_context2){while(1)switch(_context2.prev=_context2.next){case 0:mockedResolver=function mockedResolver(mockInstance,resolve,reject){reject({mockInstance:mockInstance});};mockedDispatch=jest.fn();_context2.prev=2;_context2.next=5;return fetchReviews('foo',2,1)(mockedDispatch);case 5:result=_context2.sent;expect(result.mockInstance.name).toBe(pipelines.SHOPGATE_CATALOG_GET_PRODUCT_REVIEWS);expect(result.mockInstance.input).toEqual({productId:'foo',limit:2,offset:1,sort:'dateDesc'});expect(logger.error).toHaveBeenCalledTimes(1);expect(mockedDispatch).toHaveBeenCalledTimes(2);done();_context2.next=16;break;case 13:_context2.prev=13;_context2.t0=_context2["catch"](2);done(_context2.t0);case 16:case"end":return _context2.stop();}},_callee2,null,[[2,13]]);}));return function(_x2){return _ref2.apply(this,arguments);};}());});describe('fetchProductReviews',function(){/**
* Assertion helper function
* @param {string} variant ('then' or 'catch')
* @param {Function} done Async test case done callback function.
* @param {Object} state React state.
*/var testFetchProductReviews=function testFetchProductReviews(variant,done,state){var mockedDispatch=jest.fn();var promise=fetchProductReviews('foo',10,'invalidSort')(mockedDispatch,function(){return state;});setTimeout(function(){promise[variant](function(result){expect(result.mockInstance.name).toBe(pipelines.SHOPGATE_CATALOG_GET_PRODUCT_REVIEWS);expect(result.mockInstance.input).toEqual({productId:'foo',limit:10,sort:'invalidSort'});expect(mockedDispatch).toHaveBeenCalledTimes(2);done();});},0);};it('should resolve and call appropriate actions',/*#__PURE__*/function(){var _ref3=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(done){var mockedDispatch,result;return _regeneratorRuntime.wrap(function _callee3$(_context3){while(1)switch(_context3.prev=_context3.next){case 0:mockedResolver=function mockedResolver(mockInstance,resolve){resolve({reviews:[],mockInstance:mockInstance});};mockedDispatch=jest.fn();_context3.prev=2;_context3.next=5;return fetchProductReviews('foo',10,'invalidSort')(mockedDispatch,function(){return finalState;});case 5:result=_context3.sent;expect(result.mockInstance.name).toBe(pipelines.SHOPGATE_CATALOG_GET_PRODUCT_REVIEWS);expect(result.mockInstance.input).toEqual({productId:'foo',limit:10,sort:'invalidSort'});expect(mockedDispatch).toHaveBeenCalledTimes(2);done();_context3.next=15;break;case 12:_context3.prev=12;_context3.t0=_context3["catch"](2);done(_context3.t0);case 15:case"end":return _context3.stop();}},_callee3,null,[[2,12]]);}));return function(_x3){return _ref3.apply(this,arguments);};}());it('should reject and call appropriate actions',/*#__PURE__*/function(){var _ref4=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee4(done){var mockedDispatch,result;return _regeneratorRuntime.wrap(function _callee4$(_context4){while(1)switch(_context4.prev=_context4.next){case 0:mockedResolver=function mockedResolver(mockInstance,resolve,reject){reject({mockInstance:mockInstance});};mockedDispatch=jest.fn();_context4.prev=2;_context4.next=5;return fetchProductReviews('foo',10,'invalidSort')(mockedDispatch,function(){return finalState;});case 5:result=_context4.sent;expect(result.mockInstance.name).toBe(pipelines.SHOPGATE_CATALOG_GET_PRODUCT_REVIEWS);expect(result.mockInstance.input).toEqual({productId:'foo',limit:10,sort:'invalidSort'});expect(mockedDispatch).toHaveBeenCalledTimes(2);done();_context4.next=15;break;case 12:_context4.prev=12;_context4.t0=_context4["catch"](2);done(_context4.t0);case 15:testFetchProductReviews('catch',done,finalState);case 16:case"end":return _context4.stop();}},_callee4,null,[[2,12]]);}));return function(_x4){return _ref4.apply(this,arguments);};}());});describe('fetchUserReview',function(){/**
* Assertion helper function
* @param {string} variant ('then' or 'catch')
* @param {Function} done Async test case done callback function.
* @param {Object} state React state.
*/var testFetchUserReview=function testFetchUserReview(variant,done,state){var mockedDispatch=jest.fn();var promise=fetchUserReview('foo')(mockedDispatch,function(){return state;});setTimeout(function(){promise[variant](function(result){expect(result.mockInstance.name).toBe(pipelines.SHOPGATE_USER_GET_REVIEW);expect(result.mockInstance.errorBlacklist).toEqual([EUNKNOWN,EACCESS]);expect(result.mockInstance.input).toEqual({productId:'foo'});expect(mockedDispatch).toHaveBeenCalledTimes(2);done();});},0);};it('should resolve and call appropriate actions',function(done){mockedResolver=function mockedResolver(mockInstance,resolve){resolve({reviews:[],mockInstance:mockInstance});};testFetchUserReview('then',done,finalState);});it('should reject and call appropriate actions',function(done){mockedResolver=function mockedResolver(mockInstance,resolve,reject){reject({mockInstance:mockInstance});};testFetchUserReview('catch',done,finalState);});});describe('submitReview',function(){var testReviewInput={productId:'foo',rate:10,title:' Title ',author:' Author ',review:'RRRR '};var testReviewSanitized={productId:'foo',rate:10,title:'Title',author:'Author',review:'RRRR'};/**
* Assertion helper function
* @param {string} variant ('then' or 'catch')
* @param {Object} review Review.
* @param {boolean} update Update.
* @param {Object} state React state.
* @param {Function} done Async test case done callback function.
*/var testSubmitReview=function testSubmitReview(variant,review,update,state,done){var expectedDispatches=variant==='then'?3:2;var mockedDispatch=jest.fn();var promise=submitReview(review,update)(mockedDispatch,function(){return state;});setTimeout(function(){promise[variant](function(result){expect(result.mockInstance.name).toBe(update?pipelines.SHOPGATE_CATALOG_UPDATE_PRODUCT_REVIEW:pipelines.SHOPGATE_CATALOG_ADD_PRODUCT_REVIEW);expect(result.mockInstance.input).toEqual(testReviewSanitized);expect(mockedDispatch).toHaveBeenCalledTimes(expectedDispatches);done();});},0);};it('should resolve and call appropriate actions on update',function(done){mockedResolver=function mockedResolver(mockInstance,resolve){resolve({reviews:[],mockInstance:mockInstance});};testSubmitReview('then',testReviewInput,true,finalState,done);});it('should resolve and call appropriate actions on add',function(done){mockedResolver=function mockedResolver(mockInstance,resolve){resolve({reviews:[],mockInstance:mockInstance});};testSubmitReview('then',testReviewInput,false,finalState,done);});it('should reject and call appropriate actions on add',function(done){mockedResolver=function mockedResolver(mockInstance,resolve,reject){reject({reviews:[],mockInstance:mockInstance});};testSubmitReview('catch',testReviewInput,false,finalState,done);});it('should reject and call appropriate actions on update',function(done){mockedResolver=function mockedResolver(mockInstance,resolve,reject){reject({reviews:[],mockInstance:mockInstance});};testSubmitReview('catch',testReviewInput,true,finalState,done);});});});