import { TestBed } from '@angular/core/testing' ; import { DaffStateError } from '@daffodil/core/state' ; import { DaffProduct } from '@daffodil/product' ; import { DaffBestSellersLoad , DaffBestSellersLoadSuccess , DaffBestSellersLoadFailure , DaffBestSellersReset , DaffBestSellersReducerState , } from '@daffodil/product/state' ; import { DaffProductFactory } from '@daffodil/product/testing' ; import { initialState , daffBestSellersReducer , } from './best-sellers.reducer' ; describe ( 'Product | Best Sellers Reducer' , ( ) => { let productFactory ; let product ; beforeEach ( ( ) => { productFactory = TestBed . inject ( DaffProductFactory ) ; product = productFactory . create ( ) ; } ) ; describe ( 'when an unknown action is triggered' , ( ) => { it ( 'should return the current state' , ( ) => { const action = < any > { } ; const result = daffBestSellersReducer ( initialState , action ) ; expect ( result ) . toBe ( initialState ) ; } ) ; } ) ; describe ( 'when BestSellersLoadAction is triggered' , ( ) => { it ( 'sets loading state to true' , ( ) => { const productsLoadAction = new DaffBestSellersLoad ( ) ; const result = daffBestSellersReducer ( initialState , productsLoadAction ) ; expect ( result . loading ) . toEqual ( true ) ; } ) ; } ) ; describe ( 'when BestSellersLoadSuccessAction is triggered' , ( ) => { let products ; let result ; let state ; beforeEach ( ( ) => { state = { ... initialState , loading : true , } ; products = new Array ( product ) ; const productsLoadSuccess = new DaffBestSellersLoadSuccess ( products ) ; result = daffBestSellersReducer ( state , productsLoadSuccess ) ; } ) ; it ( 'sets loading to false' , ( ) => { expect ( result . loading ) . toEqual ( false ) ; } ) ; it ( 'should set productIds to ids of payload products' , ( ) => { expect ( result . productIds ) . toEqual ( [ product . id ] ) ; } ) ; } ) ; describe ( 'when BestSellersLoadFailureAction is triggered' , ( ) => { let error ; let result ; let state ; beforeEach ( ( ) => { state = { ... initialState , loading : true , errors : [ { code : 'firstErrorCode' , message : 'firstErrorMessage' } ] , } ; error = { code : 'error code' , message : 'error message' , } ; const productsLoadFailure = new DaffBestSellersLoadFailure ( error ) ; result = daffBestSellersReducer ( state , productsLoadFailure ) ; } ) ; it ( 'sets loading to false' , ( ) => { expect ( result . loading ) . toEqual ( false ) ; } ) ; it ( 'adds an error to state.errors' , ( ) => { expect ( result . errors . length ) . toEqual ( 2 ) ; } ) ; } ) ; describe ( 'BestSellersReset' , ( ) => { it ( 'resets state to initialState' , ( ) => { const expectedState = { loading : false , productIds : [ ] , errors : [ ] , } ; const bestSellersReset = new DaffBestSellersReset ( ) ; const result = daffBestSellersReducer ( initialState , bestSellersReset ) ; expect ( result ) . toEqual ( expectedState ) ; } ) ; } ) ; } ) ;