Skip to content

Commit ba66122

Browse files
committed
test(book-data): add tests for http-Based service
1 parent cb8a90c commit ba66122

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

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

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,57 @@ describe('BookStaticDataService', () => {
2323

2424
it('should return all books', inject([BookDataService, HttpTestingController],
2525
(service: BookDataService, backend: HttpTestingController) => {
26+
// call service method and test IN the subscription. no need to use async anymore!!
27+
service.getBooks()
28+
.subscribe(books => {
29+
expect(books).toEqual(staticBookData)
30+
});
31+
// Wait for the call and response with mockdata `.flush()`
32+
backend.expectOne('http://localhost:4730/books').flush(staticBookData, { status: 200, statusText: 'Ok' });
33+
}));
34+
35+
36+
it('should return a single book', inject([BookDataService, HttpTestingController],
37+
(service: BookDataService, backend: HttpTestingController) => {
38+
// call service method and test IN the subscription. no need to use async anymore!!
39+
service.getBookByIsbn(staticBookData[0].isbn)
40+
.subscribe(books => {
41+
expect(books).toEqual(staticBookData[0])
42+
});
43+
// Wait for the call and response with mockdata `.flush()`
44+
backend.expectOne(`http://localhost:4730/books/${staticBookData[0].isbn}`).flush(staticBookData[0], { status: 200, statusText: 'Ok' });
45+
}));
2646

27-
// TODO: Implement Test s
2847

48+
it('should create a book with a post', inject([BookDataService, HttpTestingController],
49+
(service: BookDataService, backend: HttpTestingController) => {
50+
// call service method and test IN the subscription. no need to use async anymore!!
51+
service.createBook(staticBookData[0])
52+
.subscribe(books => {
53+
expect(books).toEqual(staticBookData[0])
54+
});
55+
// Wait for the call and response with mockdata `.flush()`
56+
backend
57+
.expectOne({ method: "POST", url: 'http://localhost:4730/books' })
58+
.flush(staticBookData[0], { status: 201, statusText: 'Created' });
2959
}));
3060

61+
it('should update a book with a patch', inject([BookDataService, HttpTestingController],
62+
(service: BookDataService, backend: HttpTestingController) => {
63+
// call service method and test IN the subscription. no need to use async anymore!!
64+
service.updateBook(staticBookData[0].isbn, staticBookData[0])
65+
.subscribe(books => {
66+
expect(books).toEqual(staticBookData[0])
67+
});
68+
// Wait for the call and response with mockdata `.flush()`
69+
backend
70+
.expectOne({ method: "PATCH", url: `http://localhost:4730/books/${staticBookData[0].isbn}` })
71+
.flush(staticBookData[0]);
72+
}));
3173
});
3274

3375

34-
const staticBookData : Book[] = [
76+
const staticBookData: Book[] = [
3577
{
3678
'title': 'Design Patterns',
3779
'subtitle': 'Elements of Reusable Object-Oriented Software',

0 commit comments

Comments
 (0)