From 89f5b2d1ab28b33cde0931293c9e075dd4beba00 Mon Sep 17 00:00:00 2001 From: Florian Hartwich Date: Mon, 17 Apr 2017 08:26:39 +0200 Subject: [PATCH] final unittest --- .../blog/blog-entry/blog-entry.component.ts | 3 +- .../src/app/blog/blog.component.html | 4 +- .../src/app/blog/blog.component.spec.ts | 42 +++++++- .../src/app/blog/blog.component.ts | 4 +- .../src/app/login/login.component.spec.ts | 27 +++--- .../src/app/mocks/mock-empty-class.ts | 3 - .../src/app/mocks/mock-login-service.ts | 5 + .../edit-task/edit-task.component.spec.ts | 66 +++++++------ .../src/app/test/basic-test-example.spec.ts | 96 +++++++++++++------ 9 files changed, 169 insertions(+), 81 deletions(-) delete mode 100644 project-manager/src/app/mocks/mock-empty-class.ts create mode 100644 project-manager/src/app/mocks/mock-login-service.ts diff --git a/project-manager/src/app/blog/blog-entry/blog-entry.component.ts b/project-manager/src/app/blog/blog-entry/blog-entry.component.ts index e2a688b..28c1365 100644 --- a/project-manager/src/app/blog/blog-entry/blog-entry.component.ts +++ b/project-manager/src/app/blog/blog-entry/blog-entry.component.ts @@ -8,8 +8,9 @@ import {BlogComponent} from "../blog.component"; styleUrls: ['../blog.component.css'] }) export class BlogEntryComponent { - @Input() blogComponent: BlogComponent; @Input() entry: BlogEntry; + @Input() blogComponent: BlogComponent; + deleteBlogEntry(id: number) { this.blogComponent.deleteBlogEntry(id); diff --git a/project-manager/src/app/blog/blog.component.html b/project-manager/src/app/blog/blog.component.html index 404bcbf..73b713c 100644 --- a/project-manager/src/app/blog/blog.component.html +++ b/project-manager/src/app/blog/blog.component.html @@ -1,5 +1,7 @@

Angular-Blog

- +
+ +

Neuen Blog-Eintrag erstellen

diff --git a/project-manager/src/app/blog/blog.component.spec.ts b/project-manager/src/app/blog/blog.component.spec.ts index 85617ed..e681e40 100644 --- a/project-manager/src/app/blog/blog.component.spec.ts +++ b/project-manager/src/app/blog/blog.component.spec.ts @@ -1,19 +1,27 @@ import {BlogComponent} from './blog.component' -import {TestBed} from "@angular/core/testing"; +import {async, TestBed} from "@angular/core/testing"; import {RouterTestingModule} from "@angular/router/testing"; import {CUSTOM_ELEMENTS_SCHEMA} from "@angular/core"; import {By} from "@angular/platform-browser"; -import {FormsModule} from "@angular/forms"; +import {BlogEntryComponent} from "./blog-entry/blog-entry.component";; describe('Blog Component', () => { - describe('Isolated Class Test', () => { - let blogComponent: BlogComponent; + describe('Isolated Component Test', () => { + let blogComponent; beforeEach(() => { - blogComponent = new BlogComponent(null, null, null); + TestBed.configureTestingModule({ + imports: [RouterTestingModule.withRoutes([])], + declarations: [BlogComponent] + }); }); + beforeEach(async(()=> { + TestBed.overrideComponent(BlogComponent, {set: {template: ''}}); + blogComponent = TestBed.createComponent(BlogComponent).componentInstance; + })); + it('should have initial entries', () => { expect(blogComponent.entries.length).toBe(2); blogComponent.entries.forEach((entry) => { @@ -102,4 +110,28 @@ describe('Blog Component', () => { }); + describe('Basic DOM Form Test including BlogEntryComponent ', () => { + + let fixture; + let instance; + let element; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [RouterTestingModule.withRoutes([])], + declarations: [BlogComponent, BlogEntryComponent], + }); + fixture = TestBed.createComponent(BlogComponent); + instance = fixture.componentInstance; + element = fixture.debugElement; + }); + + it('should find first entry title in DOM', () => { + fixture.detectChanges(); + const firstEntryTitle = instance.entries[0].title; + expect(element.query(By.css('#entry-list > blog-entry > div > div:nth-child(2) > .title')) + .nativeElement.textContent).toBe(firstEntryTitle); + }); + }) + }); diff --git a/project-manager/src/app/blog/blog.component.ts b/project-manager/src/app/blog/blog.component.ts index f4aab30..245ce55 100644 --- a/project-manager/src/app/blog/blog.component.ts +++ b/project-manager/src/app/blog/blog.component.ts @@ -19,9 +19,9 @@ export class BlogComponent { const initialEntries = [ { id: 1, - title: "Angular 2 erschienen", + title: "Angular 4 erschienen", image: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANkAAADoCAMAAABVRrFMAAAAkFBMVEXzVz/////ySi3zVDvzUTf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uvZ7qVJK6EI4a7piR0TSRbaXN3AhHDYfMqJTsoEJ35CN7HDycMqNS8iAqdAfZu/t5YI6ZEfLxzrWoxZtbjLZ0zoyQQdSSkiSRbK1ExA2YEfLZtN+FvnHSVxM3YUbGp7iUkig73GxG4m2YXdrvzKpFprgVMxq8LSyTvpq4HTNqum94LkL+A32x+QfAdXrsAAAAAElFTkSuQmCC", - text: "Endlich ist die finale Version von Angular 2 erschienen. Nach langer Beta-Phase ...", + text: "Endlich ist die finale Version von Angular 2 erschienen.", createdAt: null }, { diff --git a/project-manager/src/app/login/login.component.spec.ts b/project-manager/src/app/login/login.component.spec.ts index b170260..f6ed848 100644 --- a/project-manager/src/app/login/login.component.spec.ts +++ b/project-manager/src/app/login/login.component.spec.ts @@ -3,7 +3,7 @@ import {LoginComponent} from "./login.component"; import {RouterTestingModule} from "@angular/router/testing"; import {LoginService} from "../services/login-service/login-service"; import {FormsModule} from "@angular/forms"; -import {MockEmptyClass} from "../mocks/mock-empty-class"; +import {MockLoginService} from "../mocks/mock-login-service"; import Spy = jasmine.Spy; @@ -14,7 +14,7 @@ describe('Login Component UI Driven Form Test with Spy', () => { imports: [FormsModule, RouterTestingModule.withRoutes([])], declarations: [LoginComponent], providers: [ - {provide: LoginService, useClass: MockEmptyClass} + {provide: LoginService, useClass: MockLoginService} ], }) }); @@ -30,21 +30,20 @@ describe('Login Component UI Driven Form Test with Spy', () => { loginFixture.autoDetectChanges(true); // Login ausfuellen und absenden - loginFixture.whenStable().then(() => { - // Spy anmelden, die eigentliche Methode wird nicht mehr aufgerufen - const spy = spyOn(loginInstance, 'login'); - // Formular Eingabe - loginElement.querySelector('div /deep/ div > #inputEmail').value = inputUser; - loginElement.querySelector('div /deep/ div > #inputPassword').value = inputPass; - // Trigger Submit - nicht möglich über click() oder HTTPNode submit() - // daher in Comp '@ViewChild(NgForm)' benoetigt - loginInstance.form.ngSubmit.emit(); + let service = loginFixture.debugElement.injector.get(LoginService); + // Spy anmelden, die eigentliche Methode wird nicht mehr aufgerufen + const spy = spyOn(service, 'login'); - // Spy auswerten - expect(spy).toHaveBeenCalledWith(inputUser, inputPass); - }); + // Formular Eingabe + loginElement.querySelector('div /deep/ div > #inputEmail').value = inputUser; + loginElement.querySelector('div /deep/ div > #inputPassword').value = inputPass; + // Trigger Submit - nicht möglich über click() oder HTTPNode submit() + // daher in Comp '@ViewChild(NgForm)' benoetigt + loginInstance.form.ngSubmit.emit(); + // Spy auswerten + expect(spy).toHaveBeenCalledWith(inputUser, inputPass); }); }); diff --git a/project-manager/src/app/mocks/mock-empty-class.ts b/project-manager/src/app/mocks/mock-empty-class.ts deleted file mode 100644 index 828d90d..0000000 --- a/project-manager/src/app/mocks/mock-empty-class.ts +++ /dev/null @@ -1,3 +0,0 @@ -export class MockEmptyClass { - -} diff --git a/project-manager/src/app/mocks/mock-login-service.ts b/project-manager/src/app/mocks/mock-login-service.ts new file mode 100644 index 0000000..2e2796b --- /dev/null +++ b/project-manager/src/app/mocks/mock-login-service.ts @@ -0,0 +1,5 @@ +export class MockLoginService { + + login(name, password) {}; + +} diff --git a/project-manager/src/app/tasks/edit-task/edit-task.component.spec.ts b/project-manager/src/app/tasks/edit-task/edit-task.component.spec.ts index da272b7..529cdb1 100644 --- a/project-manager/src/app/tasks/edit-task/edit-task.component.spec.ts +++ b/project-manager/src/app/tasks/edit-task/edit-task.component.spec.ts @@ -18,28 +18,6 @@ import {generateRandomString} from "../../test/test.helper"; describe('EditTask Component', () => { - @Component({ - template: '' - }) - class TestComponent { - } - - beforeEach(() => { - TestBed.configureTestingModule({ - imports: [FormsModule, RouterTestingModule.withRoutes([ - {path: 'new', component: EditTaskComponent}, - {path: 'edit/:id', component: EditTaskComponent} - ], - )], - declarations: [EditTaskComponent, ShowErrorComponent, APPLICATION_VALIDATORS, TestComponent], - providers: [ - Title, - {provide: TaskService, useClass: MockTaskService}, - ] - }); - }); - - describe('Template Driven Form API-based Test', () => { let fixture; @@ -47,11 +25,20 @@ describe('EditTask Component', () => { let form; beforeEach(() => { + TestBed.configureTestingModule({ + imports: [FormsModule, RouterTestingModule.withRoutes([], + )], + declarations: [EditTaskComponent, ShowErrorComponent, APPLICATION_VALIDATORS], + providers: [ + Title, + {provide: TaskService, useClass: MockTaskService}, + ] + }); fixture = TestBed.createComponent(EditTaskComponent); fixture.autoDetectChanges(true); }); - it('should validate the title correctly', (done) => { + it('should validate the title correctly', () => { fixture.whenStable().then(() => { form = fixture.componentInstance.form.form; const titleControl = form.get('title'); @@ -66,7 +53,6 @@ describe('EditTask Component', () => { titleControl.setValue(generateRandomString(101)); expect(titleControl.errors['maxlength']).toEqual({requiredLength: 100, actualLength: 101}); - done(); }); }); @@ -86,7 +72,7 @@ describe('EditTask Component', () => { }); }); - it('should show error on tag name provided with gt 0 and lt 3', (done) => { + it('should show error on tag name provided with gt 0 and lt 3', () => { fixture.componentInstance.addTag(); fixture.whenStable().then(() => { form = fixture.componentInstance.form.form; @@ -99,11 +85,11 @@ describe('EditTask Component', () => { tagControl.setValue('abc'); expect(tagControl.errors).toBeNull(); - done(); + }); }); - it('should validate assignee email correctly', (done) => { + it('should validate assignee email correctly', () => { fixture.whenStable().then(() => { form = fixture.componentInstance.form.form; const invalidEmailObject = {invalidEMail: true}; @@ -121,16 +107,40 @@ describe('EditTask Component', () => { assigneeEmail.setValue('test@web.de'); expect(assigneeEmail.errors).toBeNull(); - done(); }) }); }); + /** + * Hinweis: Die folgende TestSuite entstammt dem ursprünglichen edit-task.component.spec.ts Tests + * dieses Projektes und ist geistiges Eigentum vom Chritoph Höller, dem Ersteller des Ausgangsprojektes + */ describe('Routing', () => { + @Component({ + template: '' + }) + class TestComponent { + } + let taskService: TaskService; + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [FormsModule, RouterTestingModule.withRoutes([ + {path: 'new', component: EditTaskComponent}, + {path: 'edit/:id', component: EditTaskComponent} + ], + )], + declarations: [EditTaskComponent, ShowErrorComponent, APPLICATION_VALIDATORS, TestComponent], + providers: [ + Title, + {provide: TaskService, useClass: MockTaskService}, + ] + }); + }); + beforeEach(inject([TaskService], (_taskService) => { taskService = _taskService; })); diff --git a/project-manager/src/app/test/basic-test-example.spec.ts b/project-manager/src/app/test/basic-test-example.spec.ts index f7debf7..783d9ed 100644 --- a/project-manager/src/app/test/basic-test-example.spec.ts +++ b/project-manager/src/app/test/basic-test-example.spec.ts @@ -1,37 +1,79 @@ // Test Suite describe('Basic Test', () => { - // Variablendeklaration zur Verwendung in mehreren Testfällen - let someInt; + // geschachtelte TestSuite + describe('Matcher example', () => { + // Variablendeklaration zur Verwendung in mehreren Testfällen + let someInt; - // Methode wird vor jedem Testfall ausgeführt - beforeEach(() => { - someInt = 2; + // Methode wird vor jedem Testfall ausgeführt + beforeEach(() => { + someInt = 2; + }); + + // Testfall 1 + it('should succeed on most commonly used matchers', () => { + expect(1 + 1).toBe(2) + expect(null).toEqual(null); + expect('Hello world').toContain('Hello'); + expect(true).toBeTruthy(); + expect(false).toBeFalsy(); + expect(null).toBeNull(); + expect(undefined).toBeUndefined(); + expect('angular.txt').toMatch(/\w+.(txt)/); + expect('World').not.toContain('Hello'); + }); + + //Testfall 2 + it('someInt should have value from beforeEach()', () => { + expect(someInt).toBe(2); + someInt = 20; + expect(someInt).toBe(20); + }); + + // Testfall 3 + it('someInt should have overwritten value from beforeEach()', () => { + expect(someInt).toBe(2); + }); }); - // Testfall 1 - it('should succeed on most commonly used matchers', () => { - expect(1 + 1).toBe(2) - expect(null).toEqual(null); - expect('Hello world').toContain('Hello'); - expect(true).toBeTruthy(); - expect(false).toBeFalsy(); - expect(null).toBeNull(); - expect(undefined).toBeUndefined(); - expect('angular.txt').toMatch(/\w+.(txt)/); - expect('World').not.toContain('Hello'); + + describe('Calculator Class Testing', () => { + class Calculator { + private val1: number = 0; + private val2: number = 0; + + constructor(val1: number, val2: number) { + this.val1 = val1; + this.val2 = val2; + } + + add(): number { + return this.val1 + this.val2 + }; + + sub(): number { + return this.val1 - this.val2 + }; + + mul(): number { + return this.val1 * this.val2 + }; + + div(): number { + return this.val1 / this.val2 + }; + } + + it('should return calculated values for each method', () => { + const calc = new Calculator(4, 12); + expect(calc.add()).toBe(16); + expect(calc.sub()).toBe(-8); + expect(calc.mul()).toBe(48); + expect(calc.div()).toBe(1 / 3); + }); + }); - //Testfall 2 - it('someInt should have value from beforeEach()', () => { - expect(someInt).toBe(2); - someInt = 20; - expect(someInt).toBe(20); - }); - - // Testfall 3 - it('someInt should have overwritten value from beforeEach()', () => { - expect(someInt).toBe(2); - }); });