import {async, ComponentFixture, TestBed} from '@angular/core/testing'; import {SettingsComponent} from './settings.component'; import {SettingsService} from "../services/settings.service"; import {RouterTestingModule} from "@angular/router/testing"; import {Configuration} from "../env/model/configuration"; import {ConfigurationService} from "../env/configuration.service"; export class FakeConfigurationService { get config() { return { dryrun: "false", rootDataDirectory: "/lustre/aoc/cluster/pipeline/vlass_test/spool", url: "http://localhost:4200/VlassMngr", weblogbaseurl: "http://webtest.aoc.nrao.edu/", projectcode: "VLASS1.1", wsurl: "ws://localhost:8081/VlassMngr/changes", currentepoch: "1" } as Configuration; }; } describe('SettingsComponent', () => { let component: SettingsComponent; let fixture: ComponentFixture<SettingsComponent>; let settingServiceSpy: { getSettings: jasmine.Spy, getQueueSettings: jasmine.Spy, getWorkflowSettings: jasmine.Spy, setSettings: jasmine.Spy, deleteFile: jasmine.Spy }; // we'll use a spy in place of the real service let fakeConfigService: FakeConfigurationService; beforeEach(async(() => { // create the spy to inject as a substitute for the real TileService settingServiceSpy = jasmine.createSpyObj('SettingService', ['getSettings', 'getQueueSettings', 'getWorkflowSettings', 'setSettings', 'deleteFile']); // set up the test env with our mocks and spies TestBed.configureTestingModule({ declarations: [SettingsComponent], imports: [RouterTestingModule], providers: [ {provide: SettingsService, useValue: settingServiceSpy}, {provide: ConfigurationService, useClass: FakeConfigurationService} ] }) .compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(SettingsComponent); component = fixture.componentInstance; settingServiceSpy = TestBed.get(SettingsService); // we want to get the 'real' instance from our env fakeConfigService = TestBed.get(ConfigurationService); }); it('should create', () => { expect(component).toBeTruthy(); }); it('should get settings and config url on init', () => { expect(component.configUrl).toBeUndefined('configUrl starts empty'); fixture.detectChanges(); //ngOnInit expect(settingServiceSpy.getSettings).toHaveBeenCalledTimes(1); expect(settingServiceSpy.getQueueSettings).toHaveBeenCalledTimes(1); expect(settingServiceSpy.getWorkflowSettings).toHaveBeenCalledTimes(1); expect(component.configUrl).toEqual('http://localhost:4200/VlassMngr', 'configUrl updated from service'); }); it('saveSettings() should call service', () => { component.saveSettings(); expect(settingServiceSpy.setSettings).toHaveBeenCalledTimes(1); }); it('deleteFile() should call service', () => { component.deleteFile('calibration', 'filename.xml'); expect(settingServiceSpy.deleteFile).toHaveBeenCalledWith('calibration', 'filename.xml'); expect(settingServiceSpy.deleteFile).toHaveBeenCalledTimes(1); }); });