91 lines
3.1 KiB
TypeScript
91 lines
3.1 KiB
TypeScript
import {BlogComponent} from './blog.component'
|
|
import {TestBed} from "@angular/core/testing";
|
|
import {RouterTestingModule} from "@angular/router/testing";
|
|
import {CUSTOM_ELEMENTS_SCHEMA} from "@angular/core";
|
|
|
|
describe('Blog Component', () => {
|
|
|
|
describe('Isolated Class Test', () => {
|
|
let blogComponent: BlogComponent;
|
|
|
|
beforeEach(() => {
|
|
blogComponent = new BlogComponent(null, null, null);
|
|
});
|
|
|
|
it('should have initial entries', () => {
|
|
expect(blogComponent.entries.length).toBe(2);
|
|
blogComponent.entries.forEach((entry) => {
|
|
expect(entry.id).toBeLessThanOrEqual(blogComponent.id);
|
|
expect(entry.createdAt.getDate()).toBe(new Date().getDate());
|
|
})
|
|
});
|
|
|
|
it('should create new list entry and increment id-pointer', () => {
|
|
let preCreationId = blogComponent.id;
|
|
let entryTitle = "some fancy title";
|
|
let entryImage = "https://avatars1.githubusercontent.com/u/3284117";
|
|
let entryText = "some important text";
|
|
blogComponent.createBlogEntry(entryTitle, entryImage, entryText);
|
|
|
|
let newEntry = blogComponent.entries[blogComponent.entries.length - 1];
|
|
expect(newEntry.id - 1).toBe(preCreationId);
|
|
expect(newEntry.image).toBe(entryImage);
|
|
expect(newEntry.text).toBe(entryText);
|
|
expect(newEntry.createdAt.getDate()).toBe(new Date().getDate());
|
|
});
|
|
|
|
it('should delete entry by given id - and not change global max-id', () => {
|
|
let preDeletionId = blogComponent.id;
|
|
let latestId = blogComponent.entries[blogComponent.entries.length - 1].id;
|
|
blogComponent.deleteBlogEntry(latestId);
|
|
|
|
expect(blogComponent.id).toBe(preDeletionId);
|
|
expect(() => {
|
|
if (blogComponent.entries.length > 0) {
|
|
return blogComponent.entries[blogComponent.entries.length - 1];
|
|
} else {
|
|
return 0;
|
|
}
|
|
}).not.toBe(latestId);
|
|
});
|
|
});
|
|
|
|
describe('Template Driven Form Integration Test', () => {
|
|
|
|
let fixture;
|
|
let instance;
|
|
let element;
|
|
|
|
beforeEach(() => {
|
|
TestBed.configureTestingModule({
|
|
imports: [RouterTestingModule.withRoutes([])],
|
|
declarations: [BlogComponent],
|
|
// CUSTOM_ELEMENTS_SCHEMA - verhindert das Laden von Sub-Komponenten
|
|
schemas: [CUSTOM_ELEMENTS_SCHEMA]
|
|
});
|
|
fixture = TestBed.createComponent(BlogComponent);
|
|
instance = fixture.componentInstance;
|
|
element = fixture.nativeElement;
|
|
});
|
|
|
|
it('should call create method with provided field input', () => {
|
|
const spy = spyOn(instance, 'createBlogEntry');
|
|
|
|
const testTitle = 'testTitle';
|
|
const testImage = 'imageUrl';
|
|
const testText = 'testText';
|
|
|
|
// beachten : value würde immer funktionieren um Werte als Variable
|
|
// übertragbar einzutragen, textContent hingegen nur bei textarea
|
|
element.querySelector('div /deep/ div > #title').value = testTitle;
|
|
element.querySelector('div /deep/ div > #image').value = testImage;
|
|
element.querySelector('div /deep/ div > #text').textContent = testText;
|
|
element.querySelector('div /deep/ div > button').click();
|
|
|
|
expect(spy).toHaveBeenCalledWith(testTitle, testImage, testText);
|
|
})
|
|
|
|
});
|
|
|
|
});
|