From 4bfce46f70e74d52a958be601ffb7fa0c6b6c411 Mon Sep 17 00:00:00 2001 From: Manuel Friedli Date: Fri, 7 Sep 2018 11:25:08 +0200 Subject: [PATCH] Implement TextInputFieldComponent spec --- .../text-input-field.component.spec.ts | 105 ++++++++++++++++-- 1 file changed, 95 insertions(+), 10 deletions(-) diff --git a/src/app/text-input-field/text-input-field.component.spec.ts b/src/app/text-input-field/text-input-field.component.spec.ts index 144588c..d71f04a 100644 --- a/src/app/text-input-field/text-input-field.component.spec.ts +++ b/src/app/text-input-field/text-input-field.component.spec.ts @@ -1,25 +1,110 @@ -import { async, ComponentFixture, TestBed } from '@angular/core/testing'; +import {async, ComponentFixture, TestBed} from '@angular/core/testing'; -import { TextInputFieldComponent } from './text-input-field.component'; +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 component: TextInputFieldComponent; - let fixture: ComponentFixture; + let testHostComponent: TestHostComponent; + let testHostFixture: ComponentFixture; + + const inputComponentManagerServiceStub = createSpyObj(['getNext']); + inputComponentManagerServiceStub.getNext.and.returnValue(undefined); beforeEach(async(() => { TestBed.configureTestingModule({ - declarations: [ TextInputFieldComponent ] + declarations: [ + TestHostComponent, + TextInputFieldComponent + ], + imports: [FormsModule], + providers: [{provide: InputComponentManagerService, useValue: inputComponentManagerServiceStub}] }) - .compileComponents(); + .compileComponents(); })); beforeEach(() => { - fixture = TestBed.createComponent(TextInputFieldComponent); - component = fixture.componentInstance; - fixture.detectChanges(); + testHostFixture = TestBed.createComponent(TestHostComponent); + testHostComponent = testHostFixture.componentInstance; + testHostFixture.detectChanges(); }); it('should create', () => { - expect(component).toBeTruthy(); + 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'); }); });