Skip to content

Commit 5202950

Browse files
committed
test(book-static-async-data): add test for async static bookdata service
1 parent c6daa96 commit 5202950

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

src/app/book/shared/book-static-async-data.service.spec.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { TestBed, inject } from '@angular/core/testing';
22

33
import { BookStaticAsyncDataService } from './book-static-async-data.service';
4+
import { Book } from './book';
45

56
describe('BookStaticAsyncDataService', () => {
67
beforeEach(() => {
@@ -12,4 +13,76 @@ describe('BookStaticAsyncDataService', () => {
1213
it('should be created', inject([BookStaticAsyncDataService], (service: BookStaticAsyncDataService) => {
1314
expect(service).toBeTruthy();
1415
}));
16+
17+
describe('getBooks()', () => {
18+
it('should return the whole list of books with an Observable', inject([BookStaticAsyncDataService], (service: BookStaticAsyncDataService) => {
19+
let result;
20+
service.getBooks().subscribe((books) => result = books)
21+
22+
expect(result).toBe(service.staticBookData)
23+
}));
24+
});
25+
26+
describe('getBook(isbn)', () => {
27+
it('should return the first elemnt of data', inject([BookStaticAsyncDataService], (service: BookStaticAsyncDataService) => {
28+
let result;
29+
service.getBook(123).subscribe((book) => result = book)
30+
31+
expect(result).toBe(service.staticBookData[0]);
32+
}));
33+
});
34+
35+
describe('getBook(book)', () => {
36+
it('should return the book argument itself', inject([BookStaticAsyncDataService], (service: BookStaticAsyncDataService) => {
37+
let result;
38+
const book: Book = service.staticBookData[0];
39+
service.updateBook(book).subscribe((book) => result = book)
40+
41+
expect(result).toBe(service.staticBookData[0]);
42+
}));
43+
});
44+
45+
46+
describe('createBook(book)', () => {
47+
it('should return the book argument itself', inject([BookStaticAsyncDataService], (service: BookStaticAsyncDataService) => {
48+
let result;
49+
const book: Book = {
50+
'title': 'The New Design Patterns',
51+
'subtitle': 'Elements of Reusable Object-Oriented Software',
52+
'isbn': '978-0-20163-361-0',
53+
'abstract': 'Capturing a wealth of experience about the design of object-oriented software, four top-notch designers present a catalog of simple and succinct solutions to commonly occurring design problems. Previously undocumented, these 23 patterns allow designers to create more flexible, elegant, and ultimately reusable designs without having to rediscover the design solutions themselves.',
54+
'numPages': 395,
55+
'author': 'Erich Gamma / Richard Helm / Ralph E. Johnson / John Vlissides',
56+
'publisher': {
57+
'name': 'Addison-Wesley',
58+
'url': 'http://www.addison-wesley.de/'
59+
}
60+
};
61+
62+
service.createBook(book).subscribe((book) => result = book)
63+
expect(result).toBe(book);
64+
}));
65+
66+
it('should add the new book to the dataset', inject([BookStaticAsyncDataService], (service: BookStaticAsyncDataService) => {
67+
let result;
68+
const book: Book = {
69+
'title': 'The New Design Patterns',
70+
'subtitle': 'Elements of Reusable Object-Oriented Software',
71+
'isbn': '978-0-20163-361-0',
72+
'abstract': 'Capturing a wealth of experience about the design of object-oriented software, four top-notch designers present a catalog of simple and succinct solutions to commonly occurring design problems. Previously undocumented, these 23 patterns allow designers to create more flexible, elegant, and ultimately reusable designs without having to rediscover the design solutions themselves.',
73+
'numPages': 395,
74+
'author': 'Erich Gamma / Richard Helm / Ralph E. Johnson / John Vlissides',
75+
'publisher': {
76+
'name': 'Addison-Wesley',
77+
'url': 'http://www.addison-wesley.de/'
78+
}
79+
};
80+
81+
service.createBook(book).subscribe((book) => result = book)
82+
expect(service.staticBookData.length).toBe(4);
83+
}));
84+
});
85+
86+
87+
1588
});

0 commit comments

Comments
 (0)