Add component tests #8
					 1 changed files with 95 additions and 10 deletions
				
			
		|  | @ -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', () => { | ||||
|   let component: TextInputFieldComponent; | ||||
|   let fixture: ComponentFixture<TextInputFieldComponent>; | ||||
|   let testHostComponent: TestHostComponent; | ||||
|   let testHostFixture: ComponentFixture<TestHostComponent>; | ||||
| 
 | ||||
|   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(); | ||||
|   })); | ||||
| 
 | ||||
|   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'); | ||||
|   }); | ||||
| }); | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue