diff --git a/project-manager/src/app/blog/blog-entry/blog-entry.component.spec.ts b/project-manager/src/app/blog/blog-entry/blog-entry.component.spec.ts index b48ab04..ff2f6b7 100644 --- a/project-manager/src/app/blog/blog-entry/blog-entry.component.spec.ts +++ b/project-manager/src/app/blog/blog-entry/blog-entry.component.spec.ts @@ -9,7 +9,7 @@ describe('Blog Entry Isolated Test', () => { it('should render DOM correctly according to Input', () => { // Umgebung initialisieren TestBed.configureTestingModule({ - declarations: [BlogEntryComponent], + declarations: [BlogEntryComponent] }); const fixture = TestBed.createComponent(BlogEntryComponent); const blogEntryComponent : BlogEntryComponent = fixture.componentInstance; diff --git a/project-manager/src/app/model-driven-form/model-driven-form.component.spec.ts b/project-manager/src/app/model-driven-form/model-driven-form.component.spec.ts index cbc9c1e..1fd905a 100644 --- a/project-manager/src/app/model-driven-form/model-driven-form.component.spec.ts +++ b/project-manager/src/app/model-driven-form/model-driven-form.component.spec.ts @@ -1,19 +1,67 @@ -import {ReactiveFormsModule} from '@angular/forms'; +import {FormGroup, ReactiveFormsModule} from '@angular/forms'; import {TestBed} from '@angular/core/testing'; import {ModelDrivenFormComponent} from './model-driven-form.component'; 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"; - -beforeEach(() => { - TestBed.configureTestingModule({ - imports: [ReactiveFormsModule], - providers: [TaskServiceModelForm, UserService], - declarations: [ModelDrivenFormComponent, ShowErrorComponentModelDriven] - }); -}); +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(() => { + TestBed.configureTestingModule({ + imports: [ReactiveFormsModule], + providers: [TaskServiceModelForm, UserService], + declarations: [ModelDrivenFormComponent, ShowErrorComponentModelDriven] + }); + fixture = TestBed.createComponent(ModelDrivenFormComponent); + form = fixture.componentInstance.taskForm; + }); + + 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(); + } + } + }); diff --git a/project-manager/src/test/test.helper.ts b/project-manager/src/test/test.helper.ts new file mode 100644 index 0000000..0f0743b --- /dev/null +++ b/project-manager/src/test/test.helper.ts @@ -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; +}