import {async, ComponentFixture, fakeAsync, TestBed} from '@angular/core/testing'; import {JobspecComponent} from './jobspec.component'; import {Component, ViewChild} from "@angular/core"; import {Job} from "../../model/job"; import {JobsService} from "../../services/jobs.service"; import {ConfigurationService} from "../../env/configuration.service"; import {of} from "rxjs"; @Component({ template: '<app-job [job]="job"></app-job>' }) class HostComponent { job: Job = { job_arch_status: "ARCHIVED", job_endtime: 1565212305634, job_endtime_formatted: "08/07/2019, 02:11:45 PM", job_id: 123, job_name: "jobName", job_starttime: 1564785856832, job_starttime_formatted: "08/02/2019, 03:44:16 PM", job_status: "QA_ACCEPTED", jobspec_creation_date: 1563346880361, jobspec_creation_date_formatted: "07/17/2019, 12:01:20 AM", jobspec_id: 321, jobspec_name: "JobSpecName", jobspec_sdm_id: "JobSpecNameSDMid", jobspec_status: "QA_ACCEPTED" }; @ViewChild(JobspecComponent, /* TODO: add static flag */ {}) jobComponent: JobspecComponent; } export class fakeConfigService { static get config() { return {url: "http://localhost:4200/VlassMngr"}; }; } describe('JobComponent', () => { let component: HostComponent; let fixture: ComponentFixture<HostComponent>; let jobServiceSpy: { getJobSpec: jasmine.Spy }; beforeEach(async(() => { jobServiceSpy = jasmine.createSpyObj('JobsService', ['getJobSpec']); TestBed.configureTestingModule({ declarations: [HostComponent, JobspecComponent], providers: [ {provide: JobsService, useValue: jobServiceSpy}, {provide: ConfigurationService, useValue: fakeConfigService} ] }) .compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(HostComponent); component = fixture.componentInstance; jobServiceSpy = TestBed.get(JobsService); }); it('should create', () => { expect(component).toBeTruthy(); }); it('showDetails() should set detailsExposed to true and call job service for specs', fakeAsync(() => { jobServiceSpy.getJobSpec.and.returnValue(of({})); fixture.detectChanges(); // ngOnInit expect(component.jobComponent.detailsExposed).toBeFalsy('detailsExposed starts false'); component.jobComponent.showDetails(); expect(component.jobComponent.detailsExposed).toBeTruthy('detailsExposed set to true'); expect(jobServiceSpy.getJobSpec).toHaveBeenCalledTimes(1); })); it('hideDetails() should set detailsExposed to false', () => { component.jobComponent.detailsExposed = true; component.jobComponent.hideDetails(); expect(component.jobComponent.detailsExposed).toBeFalsy('detailsExposed set to false'); }); it('getJobStatusClass() should return the right class by status', () => { expect(component.jobComponent.getJobStatusClass('ERROR')).toEqual('badge-danger', 'badge-danger returned for ERROR'); expect(component.jobComponent.getJobStatusClass('QA_REJECTED')).toEqual('badge-warning', 'badge-warning returned for QA_REJECTED'); expect(component.jobComponent.getJobStatusClass('PROCESSING')).toEqual('badge-primary', 'badge-primary returned for PROCESSING'); expect(component.jobComponent.getJobStatusClass('QA_ACCEPTED')).toEqual('badge-info', 'badge-info returned for anything else'); }); it('getConfigUrl() should return the url from the config service', () => { fixture.detectChanges(); //ngOnInit expect(component.jobComponent.getConfigUrl()).toEqual('http://localhost:4200/VlassMngr', 'returned url from config service'); }); });