Add tests for model driven form

merge-requests/1/head
Florian Hartwich 2017-04-06 04:01:45 +02:00
parent 67def504ef
commit 7ed157625b
3 changed files with 69 additions and 11 deletions

View File

@ -9,7 +9,7 @@ describe('Blog Entry Isolated Test', () => {
it('should render DOM correctly according to Input', () => { it('should render DOM correctly according to Input', () => {
// Umgebung initialisieren // Umgebung initialisieren
TestBed.configureTestingModule({ TestBed.configureTestingModule({
declarations: [BlogEntryComponent], declarations: [BlogEntryComponent]
}); });
const fixture = TestBed.createComponent(BlogEntryComponent); const fixture = TestBed.createComponent(BlogEntryComponent);
const blogEntryComponent : BlogEntryComponent = fixture.componentInstance; const blogEntryComponent : BlogEntryComponent = fixture.componentInstance;

View File

@ -1,19 +1,67 @@
import {ReactiveFormsModule} from '@angular/forms'; import {FormGroup, ReactiveFormsModule} from '@angular/forms';
import {TestBed} from '@angular/core/testing'; import {TestBed} from '@angular/core/testing';
import {ModelDrivenFormComponent} from './model-driven-form.component'; import {ModelDrivenFormComponent} from './model-driven-form.component';
import {UserService} from '../services/user-service/user.service'; import {UserService} from '../services/user-service/user.service';
import {ShowErrorComponentModelDriven} from "../show-error/show-error-model-driven.component";
import {TaskServiceModelForm} from "../services/task-service/task-model-form.service"; import {TaskServiceModelForm} from "../services/task-service/task-model-form.service";
import {ShowErrorComponentModelDriven} from "../show-error/show-error-model-driven.component";
import {generateRandomString} from "../../test/test.helper";
beforeEach(() => { describe('Model Driven Form', () => {
let fixture ;
let form ;
beforeEach(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ReactiveFormsModule], imports: [ReactiveFormsModule],
providers: [TaskServiceModelForm, UserService], providers: [TaskServiceModelForm, UserService],
declarations: [ModelDrivenFormComponent, ShowErrorComponentModelDriven] declarations: [ModelDrivenFormComponent, ShowErrorComponentModelDriven]
}); });
}); fixture = TestBed.createComponent(ModelDrivenFormComponent);
form = fixture.componentInstance.taskForm;
});
describe('Model Driven Form', () => { it('should show error on state ne BACKLOG and no Assignee given', () => {
const stateControl = form.get('state');
expect(form.errors).toBeNull();
stateControl.setValue('IN_PROGRESS');
expect(form.errors).toEqual({assigneeRequired: true});
});
it('should show error on tag name provided with gt 0 and lt 3', () => {
// "add tag" Aufruf starten -> neue FormGroup wird erstellt
const tagControlGroup : FormGroup = fixture.componentInstance.createTagControl()
const tagLabel = tagControlGroup.get('label');
expectErrorOnInput(tagLabel, '');
expectErrorOnInput(tagLabel, 'xy', {requiredLength: 3, actualLength: 2}, 'minlength');
});
it('should show error in description gt 2000 signs', () => {
const descriptionControl = form.get('description');
expectErrorOnInput(descriptionControl, generateRandomString(2000));
expectErrorOnInput(descriptionControl, generateRandomString(2001), {requiredLength: 2000, actualLength: 2001}, 'maxlength');
});
it('should show error on email not matching email-pattern', () => {
const assigneeFormGroup = form.get('assignee');
const assigneeEMail = assigneeFormGroup.get('email');
const invalidEMailErrorObject = {invalidEMail: true};
expectErrorOnInput(assigneeEMail, 'test', invalidEMailErrorObject);
expectErrorOnInput(assigneeEMail, 'test@web', invalidEMailErrorObject);
expectErrorOnInput(assigneeEMail, 'test@web.d', invalidEMailErrorObject);
expectErrorOnInput(assigneeEMail, 'test@web.de');
});
function expectErrorOnInput(formControl, inputString, expectedErrorObject?, expectedErrorAttribute?){
formControl.setValue(inputString);
if (expectedErrorObject && expectedErrorAttribute) {
expect(formControl.errors[expectedErrorAttribute]).toEqual(expectedErrorObject);
} else if (expectedErrorObject) {
expect(formControl.errors).toEqual(expectedErrorObject);
} else {
expect(formControl.errors).toBeNull();
}
}
}); });

View File

@ -0,0 +1,10 @@
// method to create random string of given length, from:
// http://stackoverflow.com/questions/16106701/how-to-generate-a-random-string-of-letters-and-numbers-in-javascript
export function generateRandomString(len) {
var text = "";
var charset = "abcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < len; i++ )
text += charset.charAt(Math.floor(Math.random() * charset.length));
return text;
}