import { TestBed } from '@angular/core/testing'; import { provideMockActions } from '@ngrx/effects/testing'; import { Observable, of } from 'rxjs'; import { hot, cold } from 'jasmine-marbles'; import { BomEffects } from './bom.effects'; import { Actions } from '@ngrx/effects'; import { BomService, DialogService, UserService2 } from '@arrow/bom/core'; import { Bom, DeleteBom, DeleteBomSuccess, LoadBoms, LoadBomsSuccess, Error as BomError, NoOp } from '@arrow/bom/store'; describe('BomEffects', () => { let actions$: Observable; let effects: BomEffects; let us2: jasmine.SpyObj; let ds: jasmine.SpyObj; let bs: jasmine.SpyObj; beforeEach(() => { const us2Spy = jasmine.createSpyObj('UserService2', ['getBoms']); const dsSpy = jasmine.createSpyObj('DialogService', ['alert', 'confirm']); const bsSpy = jasmine.createSpyObj('BomService', ['deleteBom']); TestBed.configureTestingModule({ providers: [ BomEffects, provideMockActions(() => actions$), { provide: UserService2, useValue: us2Spy }, { provide: DialogService, useValue: dsSpy }, { provide: BomService, useValue: bsSpy } ] }); actions$ = TestBed.inject(Actions); effects = TestBed.inject(BomEffects); us2 = TestBed.inject(UserService2) as jasmine.SpyObj; ds = TestBed.inject(DialogService) as jasmine.SpyObj; bs = TestBed.inject(BomService) as jasmine.SpyObj; }); it('should be created', () => { expect(effects).toBeTruthy(); }); it('should return a stream with BOMs loaded action', () => { const bomList: Bom[] = [ { id: 'ABC123', name: 'Test1', partCount: 5, created: '' } ]; const action = new LoadBoms(); const outcome = new LoadBomsSuccess({ boms: bomList }); actions$ = hot('-a', { a: action }); const response$ = cold('-a|', { a: bomList }); us2.getBoms.and.returnValue(response$); const expected$ = cold('--b', { b: outcome }); expect(effects.loadBoms$).toBeObservable(expected$); }); it('should return a stream with BOM error action', () => { const error = 'Test Error x'; const action = new LoadBoms(); const outcome = new BomError({ error }); actions$ = hot('-a', { a: action }); const response$ = cold('-#|', {}, { error_description: error }); us2.getBoms.and.returnValue(response$); const expected$ = cold('--b', { b: outcome }); expect(effects.loadBoms$).toBeObservable(expected$); }); it('should call delete bom', () => { const bomId = '123ABC'; const action = new DeleteBom({ id: bomId }); const outcome = new DeleteBomSuccess({ id: bomId }); actions$ = hot('-a', { a: action }); const response$ = cold('-a|', { a: bomId }); ds.confirm.and.returnValue(of(true)); bs.deleteBom.and.returnValue(response$); const expected$ = cold('--b', { b: outcome }); expect(effects.deleteBom$).toBeObservable(expected$); }); it('should not call delete bom', () => { const bomId = '123ABC'; const action = new DeleteBom({ id: bomId }); const outcome = new NoOp(); actions$ = hot('-a', { a: action }); const response$ = cold('-a|', { a: bomId }); ds.confirm.and.returnValue(of(false)); bs.deleteBom.and.returnValue(response$); const expected$ = cold('-b', { b: outcome }); expect(effects.deleteBom$).toBeObservable(expected$); }); it('should alert the user of an error', () => { const error = 'Test Error'; const action = new BomError({ error }); actions$ = cold('a', { a: action }); effects.bomError$.subscribe(() => { expect(ds.alert).toHaveBeenCalled(); }); }); });