@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
5 lines • 6.56 kB
JavaScript
function _typeof(obj){if(typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"){_typeof=function _typeof(obj){return typeof obj;};}else{_typeof=function _typeof(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj;};}return _typeof(obj);}function _extends(){_extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};return _extends.apply(this,arguments);}import{RECEIVE_REVIEWS,REQUEST_REVIEWS,ERROR_REVIEWS,REQUEST_PRODUCT_REVIEWS,RECEIVE_PRODUCT_REVIEWS,ERROR_PRODUCT_REVIEWS,REQUEST_USER_REVIEW,RECEIVE_USER_REVIEW,ERROR_USER_REVIEW,REQUEST_SUBMIT_REVIEW,RECEIVE_SUBMIT_REVIEW,ERROR_SUBMIT_REVIEW,RESET_SUBMIT_REVIEW}from"../constants";import{mockedReviews,moreMockedReviews,totalReviewCount}from"./mock";import reducers from"./index";describe('Reviews reducers',function(){describe('ReviewsByHash',function(){var hash='foo';var state={};/**
* Helper function for comparing changed state against expected shape.
* @param {Object} receivedState State.
* @param {number} expectedReviewsLength How many reviews should be stored.
*/var analyzeReceivedState=function analyzeReceivedState(receivedState){var expectedReviewsLength=arguments.length>1&&arguments[1]!==undefined?arguments[1]:totalReviewCount;expect(receivedState.reviewsByHash[hash].totalReviewCount).toBe(totalReviewCount);expect(receivedState.reviewsByHash[hash].reviews).toBeInstanceOf(Array);expect(receivedState.reviewsByHash[hash].reviews).toBeInstanceOf(Array);expect(receivedState.reviewsByHash[hash].reviews).toHaveLength(expectedReviewsLength);};describe(REQUEST_REVIEWS,function(){it('should manipulate state when REQUEST_REVIEWS',function(){state=reducers(state,{type:REQUEST_REVIEWS,hash:hash});expect(state.reviewsByHash[hash]).toEqual({expires:0,isFetching:true});});});describe(RECEIVE_REVIEWS,function(){it('should manipulate state when receive reviews',function(){state=reducers(state,{type:RECEIVE_REVIEWS,hash:hash,reviews:mockedReviews,totalReviewCount:totalReviewCount});analyzeReceivedState(state,mockedReviews.length);expect(state.reviewsByHash[hash].expires).toBeGreaterThan(Date.now());});it('should append more reviews when received more',function(){state=reducers(state,{type:RECEIVE_REVIEWS,hash:hash,reviews:moreMockedReviews,totalReviewCount:totalReviewCount});analyzeReceivedState(state);expect(state.reviewsByHash[hash].expires).toBeGreaterThan(Date.now());});});describe(ERROR_REVIEWS,function(){it('should handle error state',function(){state.reviewsByHash[hash].isFetching=true;state=reducers(state,{type:ERROR_REVIEWS,hash:hash});analyzeReceivedState(state);expect(state.reviewsByHash[hash].expires).toBe(0);});});});describe('ReviewsByProductId',function(){var state={};describe(REQUEST_PRODUCT_REVIEWS,function(){it('should manipulate state when REQUEST_REVIEWS',function(){state=reducers(state,{type:REQUEST_PRODUCT_REVIEWS,productId:'foo'});expect(state.reviewsByProductId.foo).toEqual({expires:0,isFetching:true});});});describe(RECEIVE_PRODUCT_REVIEWS,function(){it('should manipulate state when receive reviews',function(){state=reducers(state,{type:RECEIVE_PRODUCT_REVIEWS,productId:'foo',reviews:mockedReviews,totalReviewCount:totalReviewCount});expect(state.reviewsByProductId.foo.reviews.length).toBe(mockedReviews.length);});it('should replace reviews on another call when received more',function(){state=reducers(state,{type:RECEIVE_PRODUCT_REVIEWS,productId:'foo',reviews:moreMockedReviews,totalReviewCount:totalReviewCount});expect(state.reviewsByProductId.foo.reviews.length).toBe(moreMockedReviews.length);});});describe(ERROR_PRODUCT_REVIEWS,function(){it('should handle error state',function(){state.reviewsByProductId.foo.isFetching=true;state=reducers(state,{type:ERROR_PRODUCT_REVIEWS,productId:'foo'});expect(state.reviewsByProductId.foo.isFetching).toBe(false);});});});describe('userReviewsByProductId',function(){var state={};var review={id:'id',one:1,two:{}};var reviewWithProductId=_extends({},review,{productId:'foo'});describe(REQUEST_USER_REVIEW,function(){it('should handle request state',function(){state=reducers(state,{type:REQUEST_USER_REVIEW,productId:'foo'});expect(state.userReviewsByProductId.foo.isFetching).toBe(true);expect(state.userReviewsByProductId.foo.review).toEqual('');});});describe(RECEIVE_USER_REVIEW,function(){it('should handle receive state',function(){state=reducers(state,{type:RECEIVE_USER_REVIEW,productId:'foo',review:review});expect(state.userReviewsByProductId.foo.isFetching).toBe(false);expect(state.userReviewsByProductId.foo.review).toEqual('id');});});describe(ERROR_USER_REVIEW,function(){it('should handle receive state',function(){state=reducers(state,{type:ERROR_USER_REVIEW,productId:'foo'});expect(_typeof(state.userReviewsByProductId.foo)).toBe('undefined');});});describe(RECEIVE_USER_REVIEW,function(){it('should handle receive state',function(){state=reducers(state,{type:RECEIVE_USER_REVIEW,productId:'foo',review:reviewWithProductId});expect(state.userReviewsByProductId.foo.isFetching).toBe(false);expect(state.userReviewsByProductId.foo.review).toEqual('id');});});describe(REQUEST_SUBMIT_REVIEW,function(){it('should handle request state',function(){state=reducers(state,{type:REQUEST_SUBMIT_REVIEW,review:_extends({},review,{productId:'foo'})});expect(state.userReviewsByProductId.foo.isFetching).toBe(true);expect(state.userReviewsByProductId.foo.review).toEqual('id');});});describe(RECEIVE_SUBMIT_REVIEW,function(){it('should handle receive state',function(){reviewWithProductId.one='one';state=reducers(state,{type:RECEIVE_SUBMIT_REVIEW,review:reviewWithProductId});expect(state.userReviewsByProductId.foo.isFetching).toBe(false);expect(state.userReviewsByProductId.foo.review).toEqual('id');expect(state.reviewsById[reviewWithProductId.id]).toEqual(reviewWithProductId);});});describe(ERROR_SUBMIT_REVIEW,function(){it('should handle receive state',function(){state=reducers(state,{type:ERROR_SUBMIT_REVIEW,productId:'foo'});expect(_typeof(state.userReviewsByProductId.foo)).toBe('undefined');});});describe(RESET_SUBMIT_REVIEW,function(){it('should handle receive state',function(){state=reducers(state,{type:RESET_SUBMIT_REVIEW,productId:'foo',review:{productId:'foo',one:'1'}});expect(state.userReviewsByProductId.foo.isFetching).toBe(false);expect(state.userReviewsByProductId.foo).toEqual({isFetching:false});});});});});