import { mock } from 'jest-mock-extended' ; import { ApiProduct } from '@api' ; import { CartProduct } from '@core' ; import reducer , { CartState , initialState } from './reducer' ; import { addToCartAction , removeFromCartAction , increaseCartInventoryAction , decreaseCartInventoryAction , emptyCartAction , } from './actions' ; import { AddToCartActionPayload , RemoveFromCartActionPayload , IncreaseCartInventoryActionPayload , DecreaseCartInventoryActionPayload , } from './types' ; import { mockState } from '@testUtils' ; describe ( 'Cart reducer' , ( ) => { describe ( 'handles addToCartAction' , ( ) => { it ( 'when the product is not already in the cart' , ( ) => { const previousState = initialState ; const actionPayload = { product : mock < ApiProduct > ( ) , } ; const expectedState = { products : [ { product : actionPayload . product , quantity : 1 } ] , } ; expect ( reducer ( previousState , addToCartAction ( actionPayload ) ) ) . toEqual ( expectedState ) ; } ) ; it ( 'when the product is already in the cart' , ( ) => { const previousState = { ... mockState . cart , } ; const actionPayload = { product : mockState . cart . products [ 0 ] . product , } ; const expectedState = { products : [ { product : actionPayload . product , quantity : 2 } ] , } ; expect ( reducer ( previousState , addToCartAction ( actionPayload ) ) ) . toEqual ( expectedState ) ; } ) ; } ) ; it ( 'handles removeFromCartAction' , ( ) => { const previousState = { products : [ { product : mock < ApiProduct > ( { id : 'mockId' } ) , quantity : 1 } , ] , } ; const actionPayload = { productId : 'mockId' , } ; const expectedState = { products : [ ] } ; expect ( reducer ( previousState , removeFromCartAction ( actionPayload ) ) ) . toEqual ( expectedState ) ; } ) ; it ( 'handles increaseCartInventoryAction' , ( ) => { const previousState = { products : [ { product : mock < ApiProduct > ( { id : 'mockId' } ) , quantity : 1 , } , ] , } ; const actionPayload = { productId : 'mockId' , } ; const expectedState = { products : [ { product : previousState . products [ 0 ] . product , quantity : 2 } , ] , } ; expect ( reducer ( previousState , increaseCartInventoryAction ( actionPayload ) ) ) . toEqual ( expectedState ) ; } ) ; describe ( 'handles decreaseCartInventoryAction' , ( ) => { it ( 'when quantity > 1' , ( ) => { const previousState = { products : [ { product : mock < ApiProduct > ( { id : 'mockId' } ) , quantity : 2 , } , ] , } ; const actionPayload = { productId : 'mockId' , } ; const expectedState = { products : [ { product : previousState . products [ 0 ] . product , quantity : 1 } , ] , } ; expect ( reducer ( previousState , decreaseCartInventoryAction ( actionPayload ) ) ) . toEqual ( expectedState ) ; } ) ; it ( 'when quantity === 1' , ( ) => { const previousState = { products : [ { product : mock < ApiProduct > ( { id : 'mockId' } ) , quantity : 1 , } , ] , } ; const actionPayload = { productId : 'mockId' , } ; const expectedState = { products : [ ] , } ; expect ( reducer ( previousState , decreaseCartInventoryAction ( actionPayload ) ) ) . toEqual ( expectedState ) ; } ) ; } ) ; it ( 'handles emptyCartAction' , ( ) => { const previousState = { products : [ mock < CartProduct > ( ) ] , } ; expect ( reducer ( previousState , emptyCartAction ( ) ) ) . toEqual ( initialState ) ; } ) ; } ) ;