import {async, ComponentFixture, TestBed} from '@angular/core/testing'; import {TextInputFieldComponent} from './text-input-field.component'; import {Step} from '../step'; import {Component, ViewChild} from '@angular/core'; import {FormsModule} from '@angular/forms'; import {InputComponentManagerService} from '../input-component-manager.service'; import createSpyObj = jasmine.createSpyObj; @Component({ template: '' }) class TestHostComponent { public step: Step = new Step(42); @ViewChild(TextInputFieldComponent) public sutComponent: TextInputFieldComponent; } describe('TextInputFieldComponent', () => { let testHostComponent: TestHostComponent; let testHostFixture: ComponentFixture; const inputComponentManagerServiceStub = createSpyObj(['getNext']); inputComponentManagerServiceStub.getNext.and.returnValue(undefined); beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ TestHostComponent, TextInputFieldComponent ], imports: [FormsModule], providers: [{provide: InputComponentManagerService, useValue: inputComponentManagerServiceStub}] }) .compileComponents(); })); beforeEach(() => { testHostFixture = TestBed.createComponent(TestHostComponent); testHostComponent = testHostFixture.componentInstance; testHostFixture.detectChanges(); }); it('should create', () => { expect(testHostComponent.sutComponent).toBeTruthy(); }); it('should not perform an update if no converter is selected', () => { // arrange const step: Step = testHostComponent.step; step.content = 'Don\'t you change me!'; step.error = false; step.message = 'There be no message.'; // act testHostComponent.sutComponent.update(step); // assert expect(step.content).toEqual('Don\'t you change me!'); expect(step.error).toBeFalsy(); expect(step.message).toEqual('There be no message.'); }); it('should set the error flag if the conversion fails', () => { // arrange const step: Step = testHostComponent.step; step.content = 'Don\'t you change me!'; step.error = false; step.message = 'There be no message.'; step.selectedConverter = { getId: () => 'testConverter', convert: () => { throw new Error('Test error'); }, getDisplayname: () => 'Testing converter' }; // act testHostComponent.sutComponent.update(step); // assert expect(step.content).toEqual('Don\'t you change me!'); expect(step.error).toBeTruthy(); expect(step.message).toEqual('Test error'); }); it('should correctly convert valid input', () => { // arrange const nextStep: Step = new Step(82); inputComponentManagerServiceStub.getNext.and.returnValue(nextStep); const step: Step = testHostComponent.step; step.content = 'Don\'t you change me!'; step.error = true; step.message = 'There be no message.'; step.selectedConverter = { getId: () => 'testConverter', convert: () => 'The Converted Result', getDisplayname: () => 'Testing converter' }; // act testHostComponent.sutComponent.update(step); // assert expect(step.content).toEqual('Don\'t you change me!'); expect(step.error).toBeFalsy(); expect(step.message).toEqual(''); expect(nextStep.content).toEqual('The Converted Result'); }); });