import { CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA } from '@angular/core'; import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { AnalyticsService, BomItemService, DialogService, DownloadService, ModalService, SocketsService, UserService, Utils } from '@arrow/bom/core'; import { LoggerService } from '@arrow/bom/logger'; import { SettingsService } from '@arrow/bom/shared'; import { WPT_DIALOG_DATA } from '@arrow/warpaint/dialog'; import { Observable, of } from 'rxjs'; import { DownloadBomComponent } from './download-bom.component'; function provideDialogData() { return { bom: { id: 'some id' }, bomData: { exportableColumns : [] }, userSettings: { includeCrosses: null } }; } const mockAnalyticsService: Partial = { googleTrackEvent: () => {}, track: () => { return of(); } }; const mockModalService: Partial = { hideMessages$: of() }; const mockUtils: Partial = { emailRegex: new RegExp("/^\w+([.-]\w+)*@\w+([.-]\w+)*\.\w{2,25}$/") }; const mockLog: Partial = { info: () => {}, warn: () => {} }; const mockSockets: Partial = { observeAction: subject => { switch (subject) { case 'rowDeleted': return of(0); default: return new Observable(); } }, emitAction: (actionName: string, params?: any) => { return of({}); }, observeApiErrors: () => { return of({}); } }; describe('DownloadBomComponent', () => { let component: DownloadBomComponent; let fixture: ComponentFixture; beforeEach(async(() => { const settingsServiceSpyObj = jasmine.createSpyObj('SettingsService', ['getSettings']); const dialogServiceSpyObj = jasmine.createSpyObj('DialogService', ['alert', 'confirm']); const bomItemServiceSpyObj = jasmine.createSpyObj('BomItemService', { 'getSelection': { rowIds : [] }, }); TestBed.configureTestingModule({ schemas: [NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA], declarations: [DownloadBomComponent], providers : [ { provide: WPT_DIALOG_DATA, useFactory: provideDialogData }, { provide: UserService, useValue: {} }, { provide: AnalyticsService, useValue: mockAnalyticsService }, { provide: ModalService, useValue: mockModalService }, { provide: SettingsService, useValue: settingsServiceSpyObj }, { provide: DialogService, useValue: dialogServiceSpyObj }, { provide: DownloadService, useValue: {} }, { provide: Utils, useValue: mockUtils }, { provide: BomItemService, useValue: bomItemServiceSpyObj }, { provide: LoggerService, useValue: mockLog }, { provide: SocketsService, useValue: mockSockets } ] }).compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(DownloadBomComponent); component = fixture.componentInstance; fixture.detectChanges(); }); it('should create', () => { expect(component).toBeTruthy(); }); it('should have a toggleSelectAll method', () => { expect(component.toggleSelectAll).toBeDefined(); }); it('should toggle selectAll flag for default', () => { const prevFlagState = component.selectToggle; component.toggleSelectAll(true); expect(prevFlagState).not.toBe(component.selectToggle); }); it('should toggle selectToggleCustom flag for default', () => { const prevFlagState = component.selectToggleCustom; component.toggleSelectAll(false); expect(prevFlagState).not.toBe(component.selectToggleCustom); }); it('should have a updateTotalSelected method', () => { expect(component.updateTotalSelected).toBeDefined(); }); it('should have a createExportData method', () => { expect(component.createExportData).toBeDefined(); }); it('should have a toggleShowEmailSelection method', () => { expect(component.toggleShowEmailSection).toBeDefined(); }); it('should toggle show email flag', () => { const prevState = component.openEmail; const event = new Event('test'); component.toggleShowEmailSection(event); expect(prevState).not.toBe(component.openEmail); }); it('should have a confirm method', () => { expect(component.confirm).toBeDefined(); }); it('should toggle downloading flag on confirm', () => { spyOn(component, 'downloadBom').and.callThrough(); component.confirm(); expect(component.downloading).toBeTruthy(); expect(component.downloadBom).toHaveBeenCalled(); }); it('should have a sendEmail method', () => { expect(component.sendEmail).toBeDefined(); }); it('should set emailing flag to false', () => { // spyOn(component, 'saveUserDownloadSettings').and.callThrough(); // expect(component.saveUserDownloadSettings).toHaveBeenCalled(); expect(component.emailing).toBe(false); }); it('should have a downloadBom method', () => { expect(component.downloadBom).toBeDefined(); }); it('should have a saveUserDownloadSettings method', () => { expect(component.saveUserDownloadSettings).toBeDefined(); }); });