Implement TextInputFieldComponent spec

This commit is contained in:
Manuel Friedli 2018-09-07 11:25:08 +02:00
parent e6b38fabcb
commit 4bfce46f70

View file

@ -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: '<app-text-input-field [step]="step"></app-text-input-field>'
})
class TestHostComponent {
public step: Step = new Step(42);
@ViewChild(TextInputFieldComponent)
public sutComponent: TextInputFieldComponent;
}
describe('TextInputFieldComponent', () => { describe('TextInputFieldComponent', () => {
let component: TextInputFieldComponent; let testHostComponent: TestHostComponent;
let fixture: ComponentFixture<TextInputFieldComponent>; let testHostFixture: ComponentFixture<TestHostComponent>;
const inputComponentManagerServiceStub = createSpyObj(['getNext']);
inputComponentManagerServiceStub.getNext.and.returnValue(undefined);
beforeEach(async(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
declarations: [ TextInputFieldComponent ] declarations: [
TestHostComponent,
TextInputFieldComponent
],
imports: [FormsModule],
providers: [{provide: InputComponentManagerService, useValue: inputComponentManagerServiceStub}]
}) })
.compileComponents(); .compileComponents();
})); }));
beforeEach(() => { beforeEach(() => {
fixture = TestBed.createComponent(TextInputFieldComponent); testHostFixture = TestBed.createComponent(TestHostComponent);
component = fixture.componentInstance; testHostComponent = testHostFixture.componentInstance;
fixture.detectChanges(); testHostFixture.detectChanges();
}); });
it('should create', () => { 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');
}); });
}); });