UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

2 lines 4.22 kB
/* eslint-disable extra-rules/no-single-line-objects */import{mockedState as mockedProductState,mockedProductMetadata,mockedVariantProductMetadata,mockedVariantMetadata}from"../../product/selectors/product.mock";import{getFlags,getAddToCartMetadata,hasCouponSupport,getIsFetching,getSubTotal,getDiscounts,getDiscountsAmount,getTax,getGrandTotal}from"./index";import{CART_TOTALS_TYPE_DISCOUNT,CART_TOTALS_TYPE_GRAND,CART_TOTALS_TYPE_SUB,CART_TOTALS_TYPE_TAX}from"../constants";describe('Cart selectors',function(){describe('getFlags()',function(){it('should return an empty object if no flags are available',function(){var result=getFlags({cart:{data:{}}});expect(result).toEqual({});});it('should return the flags if available',function(){var flags={coupons:true,orderable:true};var result=getFlags({cart:{data:{flags:flags}}});expect(result).toEqual(flags);});});describe('getAddToCartMetadata()',function(){var mockedState;beforeEach(function(){// Create a deep copy of the state to avoid unintended selector caching. mockedState=JSON.parse(JSON.stringify(mockedProductState));});it('should return the metadata of the variant product',function(){var productId='product_1';var variantId='product_2';var result=getAddToCartMetadata(mockedState,{productId:productId,variantId:variantId});expect(result).toEqual(mockedVariantProductMetadata);});it('should return the metadata of the base product',function(){var productId='product_1';var variantId='product_2';delete mockedState.product.productsById[variantId].productData.metadata;var result=getAddToCartMetadata(mockedState,{productId:productId,variantId:variantId});expect(result).toEqual(mockedProductMetadata);});it('should return the metadata of the variant data assigned to the variant product',function(){var productId='product_1';var variantId='product_3';var result=getAddToCartMetadata(mockedState,{productId:productId,variantId:variantId});expect(result).toEqual(mockedVariantMetadata);});it('should return null when no metadata can be determined',function(){var productId='product_10';var variantId='product_11';var result=getAddToCartMetadata(mockedState,{productId:productId,variantId:variantId});expect(result).toBeNull();});});describe('hasCouponSupport()',function(){it('should return true if no flags are available',function(){var result=hasCouponSupport({cart:{data:{}}});expect(result).toEqual(true);});it('should return true if the coupons flag is not available',function(){var result=hasCouponSupport({cart:{data:{flags:{}}}});expect(result).toEqual(true);});it('should return true if the flag is true',function(){var result=hasCouponSupport({cart:{data:{flags:{coupons:true}}}});expect(result).toEqual(true);});it('should return false if the flag is false',function(){var result=hasCouponSupport({cart:{data:{flags:{coupons:false}}}});expect(result).toEqual(false);});});describe('isFetching()',function(){it('should return true if cart is fetching',function(){var result=getIsFetching({cart:{data:{isFetching:true}}});expect(result).toEqual(true);});it('should return false when no store information is given',function(){var result=getIsFetching({cart:{data:{}}});expect(result).toEqual(false);});it('should return false if cart is not fetching',function(){var result=getIsFetching({cart:{data:{isFetching:false}}});expect(result).toEqual(false);});});describe('getTotals',function(){var mockedState={cart:{data:{totals:[{type:CART_TOTALS_TYPE_SUB,amount:100},{type:CART_TOTALS_TYPE_DISCOUNT,amount:5},{type:CART_TOTALS_TYPE_DISCOUNT,amount:2},{type:CART_TOTALS_TYPE_TAX,label:'inkl. 26% MwSt.',amount:5},{type:CART_TOTALS_TYPE_GRAND,amount:105}]}}};it('should return subtotal',function(){expect(getSubTotal(mockedState)).toEqual(100);});it('should return discount',function(){expect(getDiscounts(mockedState)).toEqual([mockedState.cart.data.totals[1],mockedState.cart.data.totals[2]]);});it('should return discount amount',function(){expect(getDiscountsAmount(mockedState)).toEqual(7);});it('should return tax',function(){expect(getTax(mockedState)).toEqual(mockedState.cart.data.totals[3]);});it('should return grand total',function(){expect(getGrandTotal(mockedState)).toEqual(105);});});});/* eslint-enable extra-rules/no-single-line-objects */