Add tests for model driven form
parent
67def504ef
commit
7ed157625b
|
@ -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;
|
||||||
|
|
|
@ -1,9 +1,15 @@
|
||||||
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";
|
||||||
|
|
||||||
|
describe('Model Driven Form', () => {
|
||||||
|
|
||||||
|
let fixture ;
|
||||||
|
let form ;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
|
@ -11,9 +17,51 @@ beforeEach(() => {
|
||||||
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
Loading…
Reference in New Issue