import { TestBed } from '@angular/core/testing' ; import { provideMockActions } from '@ngrx/effects/testing' ; import { hot , cold , } from 'jasmine-marbles' ; import { Observable , of , } from 'rxjs' ; import { daffTransformErrorToStateError } from '@daffodil/core/state' ; import { DaffOrder } from '@daffodil/order' ; import { DaffOrderServiceInterface , DaffOrderDriver , DaffOrderNotFoundError , } from '@daffodil/order/driver' ; import { DaffOrderTestingDriverModule } from '@daffodil/order/driver/testing' ; import { DaffOrderLoad , DaffOrderLoadSuccess , DaffOrderLoadFailure , DaffOrderList , DaffOrderListSuccess , DaffOrderListFailure , } from '@daffodil/order/state' ; import { DaffOrderFactory } from '@daffodil/order/testing' ; import { DaffOrderEffects } from './order.effects' ; describe ( 'Daffodil | Order | OrderEffects' , ( ) => { let actions$ ; let effects ; let mockOrder ; let orderId ; let orderFactory ; let daffDriver ; let driverGetSpy ; let driverListSpy ; beforeEach ( ( ) => { TestBed . configureTestingModule ( { imports : [ DaffOrderTestingDriverModule . forRoot ( ) , ] , providers : [ DaffOrderEffects , provideMockActions ( ( ) => actions$ ) , ] , } ) ; effects = TestBed . inject < DaffOrderEffects < DaffOrder > > ( DaffOrderEffects ) ; daffDriver = TestBed . inject < DaffOrderServiceInterface > ( DaffOrderDriver ) ; orderFactory = TestBed . inject < DaffOrderFactory > ( DaffOrderFactory ) ; mockOrder = orderFactory . create ( ) ; orderId = mockOrder . id ; driverGetSpy = spyOn ( daffDriver , 'get' ) ; driverListSpy = spyOn ( daffDriver , 'list' ) ; } ) ; it ( 'should be created' , ( ) => { expect ( effects ) . toBeTruthy ( ) ; } ) ; describe ( 'when DaffOrderLoadAction is triggered' , ( ) => { let expected ; const orderLoadAction = new DaffOrderLoad ( orderId , 'cartId' ) ; describe ( 'and the call to OrderService is successful' , ( ) => { beforeEach ( ( ) => { driverGetSpy . and . returnValue ( of ( mockOrder ) ) ; const orderLoadSuccessAction = new DaffOrderLoadSuccess ( mockOrder ) ; actions$ = hot ( '--a' , { a : orderLoadAction } ) ; expected = cold ( '--b' , { b : orderLoadSuccessAction } ) ; } ) ; it ( 'should dispatch a DaffOrderLoadSuccess action' , ( ) => { expect ( effects . get$ ) . toBeObservable ( expected ) ; } ) ; } ) ; describe ( 'and the call to OrderService fails' , ( ) => { beforeEach ( ( ) => { const error = new DaffOrderNotFoundError ( 'Failed to load order' ) ; const response = cold ( '#' , { } , error ) ; driverGetSpy . and . returnValue ( response ) ; const orderLoadFailureAction = new DaffOrderLoadFailure ( daffTransformErrorToStateError ( error ) ) ; actions$ = hot ( '--a' , { a : orderLoadAction } ) ; expected = cold ( '--b' , { b : orderLoadFailureAction } ) ; } ) ; it ( 'should dispatch a DaffOrderLoadFailure action' , ( ) => { expect ( effects . get$ ) . toBeObservable ( expected ) ; } ) ; } ) ; } ) ; describe ( 'when DaffOrderListAction is triggered' , ( ) => { let expected ; const orderListAction = new DaffOrderList ( 'cartId' ) ; describe ( 'and the call to OrderService is successful' , ( ) => { beforeEach ( ( ) => { driverListSpy . and . returnValue ( of ( [ mockOrder ] ) ) ; const orderListSuccessAction = new DaffOrderListSuccess ( [ mockOrder ] ) ; actions$ = hot ( '--a' , { a : orderListAction } ) ; expected = cold ( '--b' , { b : orderListSuccessAction } ) ; } ) ; it ( 'should return a DaffOrderListSucess action' , ( ) => { expect ( effects . list$ ) . toBeObservable ( expected ) ; } ) ; } ) ; describe ( 'and the call to OrderService fails' , ( ) => { beforeEach ( ( ) => { const error = new DaffOrderNotFoundError ( 'Failed to list the orders' ) ; const response = cold ( '#' , { } , error ) ; driverListSpy . and . returnValue ( response ) ; const orderListFailureAction = new DaffOrderListFailure ( daffTransformErrorToStateError ( error ) ) ; actions$ = hot ( '--a' , { a : orderListAction } ) ; expected = cold ( '--b' , { b : orderListFailureAction } ) ; } ) ; it ( 'should return a DaffOrderListFailure action' , ( ) => { expect ( effects . list$ ) . toBeObservable ( expected ) ; } ) ; } ) ; } ) ; } ) ;