Add additional Blog Component tests

unit-test
Florian Hartwich 2017-04-15 10:06:40 +02:00
parent 5eb77db89f
commit 704f426eea
2 changed files with 52 additions and 3 deletions

View File

@ -3,6 +3,7 @@ import {BlogEntryComponent} from "./blog-entry.component";
import {BlogEntry} from "./blog-entry"; import {BlogEntry} from "./blog-entry";
import {BlogComponent} from "../blog.component"; import {BlogComponent} from "../blog.component";
import {RouterTestingModule} from "@angular/router/testing"; import {RouterTestingModule} from "@angular/router/testing";
import {Component} from "@angular/core";
describe('Blog Entry Isolated Test', () => { describe('Blog Entry Isolated Test', () => {
@ -61,7 +62,36 @@ describe('Blog Entry Isolated Test', () => {
describe('BlogEntryComp -> BlogComp Dependent Test', () => { describe('BlogEntryComp -> BlogComp Dependent Test', () => {
it('should remove entry from BlogComponent on "delete" button click', () => { it('should remove entry from BlogComponent on "delete" button click', () => {
TestBed.configureTestingModule({
declarations: [BlogEntryComponent, MockBlogComponent]
});
const entryFixture = TestBed.createComponent(BlogEntryComponent);
const entryInstance: BlogEntryComponent = entryFixture.componentInstance;
const blogFixture = TestBed.createComponent(MockBlogComponent);
const blogInstance: any = blogFixture.componentInstance;
const blogEntry: BlogEntry = blogInstance.entries[0];
entryInstance.entry = blogEntry;
entryInstance.blogComponent = blogInstance;
const oldLength = blogInstance.entries.length;
entryFixture.nativeElement.querySelector('div /deep/ .blog-delete > button').click();
expect(blogInstance.entries.length).toBe(oldLength - 1);
});
it('should remove entry from BlogComponent on "delete" button click : OVERRIDE BLOG TEMPLATE', () => {
TestBed.overrideComponent(BlogComponent, {
set: {
template: '<blog-entry *ngFor="let entry of entries" [entry]="entry" [blogComponent]="this"></blog-entry>'
}
});
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [RouterTestingModule.withRoutes([])], imports: [RouterTestingModule.withRoutes([])],
declarations: [BlogEntryComponent, BlogComponent] declarations: [BlogEntryComponent, BlogComponent]
@ -87,3 +117,21 @@ describe('Blog Entry Isolated Test', () => {
}); });
// Mocked Blog Component
@Component({
selector: 'blog',
template: '<blog-entry></blog-entry>'
})
export class MockBlogComponent {
entries = [{}, {}];
deleteBlogEntry(id) {
this.entries.splice(0, 1);
}
}

View File

@ -1,7 +1,7 @@
import {BlogComponent} from './blog.component' import {BlogComponent} from './blog.component'
import {TestBed} from "@angular/core/testing"; import {TestBed} from "@angular/core/testing";
import {RouterTestingModule} from "@angular/router/testing"; import {RouterTestingModule} from "@angular/router/testing";
import {BlogEntryComponent} from "./blog-entry/blog-entry.component"; import {CUSTOM_ELEMENTS_SCHEMA} from "@angular/core";
describe('Blog Component', () => { describe('Blog Component', () => {
@ -50,7 +50,6 @@ describe('Blog Component', () => {
}); });
}); });
describe('Template Driven Form Integration Test', () => { describe('Template Driven Form Integration Test', () => {
let fixture; let fixture;
@ -60,7 +59,9 @@ describe('Blog Component', () => {
beforeEach(() => { beforeEach(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [RouterTestingModule.withRoutes([])], imports: [RouterTestingModule.withRoutes([])],
declarations: [BlogEntryComponent, BlogComponent] declarations: [BlogComponent],
// CUSTOM_ELEMENTS_SCHEMA - verhindert das Laden von Sub-Komponenten
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}); });
fixture = TestBed.createComponent(BlogComponent); fixture = TestBed.createComponent(BlogComponent);
instance = fixture.componentInstance; instance = fixture.componentInstance;