import faker from 'faker' ; import { mockState } from '@testUtils' ; import reducer , { CheckoutState , initialState } from './reducer' ; import { updateBillingFullNameAction , updateBillingAddressAction , updateCardNumberAction , updateCardNameAction , updateCardExpiryAction , updateCardCvcAction , clearCheckoutInfoAction , } from './actions' ; import { UpdateBillingFullNameActionPayload , UpdateBillingAddressActionPayload , UpdateCardNumberActionPayload , UpdateCardNameActionPayload , UpdateCardExpiryActionPayload , UpdateCardCvcActionPayload , } from './types' ; describe ( 'Checkout reducer' , ( ) => { describe ( 'billing info' , ( ) => { it ( 'updates billing full name' , ( ) => { const previousState = initialState ; const rndFullName = `${ faker . name . firstName ( ) } ${ faker . name . lastName ( ) }` ; const actionPayload = { fullName : rndFullName , } ; const expectedState = { ... initialState , billingInfo : { ... previousState . billingInfo , fullName : rndFullName , } , } ; expect ( reducer ( previousState , updateBillingFullNameAction ( actionPayload ) ) ) . toEqual ( expectedState ) ; } ) ; it ( 'updates billing address' , ( ) => { const previousState = initialState ; const rndAddress = faker . address . streetName ( ) ; const actionPayload = { address : rndAddress , } ; const expectedState = { ... initialState , billingInfo : { ... previousState . billingInfo , address : rndAddress , } , } ; expect ( reducer ( previousState , updateBillingAddressAction ( actionPayload ) ) ) . toEqual ( expectedState ) ; } ) ; } ) ; describe ( 'payment method' , ( ) => { it ( 'updates card number' , ( ) => { const previousState = initialState ; const cardNumberDigits = 16 ; const mockCardNumber = faker . datatype . string ( cardNumberDigits ) ; const actionPayload = { number : mockCardNumber , } ; const expectedState = { ... initialState , paymentInfo : { card : { ... initialState . paymentInfo . card , number : mockCardNumber , } , } , } ; expect ( reducer ( previousState , updateCardNumberAction ( actionPayload ) ) ) . toEqual ( expectedState ) ; } ) ; it ( 'updates card name' , ( ) => { const previousState = initialState ; const mockCardName = faker . datatype . string ( ) ; const actionPayload = { name : mockCardName , } ; const expectedState = { ... initialState , paymentInfo : { card : { ... initialState . paymentInfo . card , name : mockCardName , } , } , } ; expect ( reducer ( previousState , updateCardNameAction ( actionPayload ) ) ) . toEqual ( expectedState ) ; } ) ; it ( 'updates card expiry' , ( ) => { const previousState = initialState ; const cardExpiryDigits = 4 ; const mockCardExpiry = faker . datatype . string ( cardExpiryDigits ) ; const actionPayload = { expiry : mockCardExpiry , } ; const expectedState = { ... initialState , paymentInfo : { card : { ... initialState . paymentInfo . card , expiry : mockCardExpiry , } , } , } ; expect ( reducer ( previousState , updateCardExpiryAction ( actionPayload ) ) ) . toEqual ( expectedState ) ; } ) ; it ( 'updates card cvc' , ( ) => { const previousState = initialState ; const cardCvcDigits = 3 ; const mockCardCvc = faker . datatype . string ( cardCvcDigits ) ; const actionPayload = { cvc : mockCardCvc , } ; const expectedState = { ... initialState , paymentInfo : { card : { ... initialState . paymentInfo . card , cvc : mockCardCvc , } , } , } ; expect ( reducer ( previousState , updateCardCvcAction ( actionPayload ) ) ) . toEqual ( expectedState ) ; } ) ; it ( 'clearCheckoutInfoAction' , ( ) => { const previousState = { ... mockState . checkout } ; expect ( reducer ( previousState , clearCheckoutInfoAction ( ) ) ) . toEqual ( initialState ) ; } ) ; } ) ; } ) ;