Skip to content

Commit 0a3b616

Browse files
09-effects-testing complete
1 parent 19caf8a commit 0a3b616

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

src/app/books/books.effects.spec.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { TestBed } from "@angular/core/testing";
2+
import { provideMockActions } from "@ngrx/effects/testing";
3+
import { Action } from "@ngrx/store";
4+
import { Observable } from "rxjs";
5+
import { hot, cold } from "jasmine-marbles";
6+
import { BooksService } from "../shared/services/book.service";
7+
import { Book } from "../shared/models/book.model";
8+
import { BooksPageActions, BooksApiActions } from "./actions";
9+
import { BooksEffects } from "./books.effects";
10+
11+
describe("Book API Effects", () => {
12+
let effects: BooksEffects;
13+
let actions$: Observable<Action>;
14+
let mockBookService: {
15+
create: jest.Mock;
16+
update: jest.Mock;
17+
delete: jest.Mock;
18+
};
19+
20+
const mockBook: Book = {
21+
id: "test",
22+
name: "Mock Book",
23+
earnings: 25,
24+
};
25+
26+
beforeEach(() => {
27+
TestBed.configureTestingModule({
28+
providers: [
29+
BooksEffects,
30+
provideMockActions(() => actions$),
31+
{
32+
provide: BooksService,
33+
useFactory() {
34+
mockBookService = {
35+
create: jest.fn(),
36+
update: jest.fn(),
37+
delete: jest.fn()
38+
};
39+
40+
return mockBookService;
41+
}
42+
}
43+
]
44+
});
45+
46+
effects = TestBed.get(BooksEffects);
47+
});
48+
49+
it("should use the API to create a book", () => {
50+
const inputAction = BooksPageActions.createBook({
51+
book: {
52+
name: mockBook.name,
53+
earnings: 25,
54+
}
55+
});
56+
const outputAction = BooksApiActions.bookCreated({
57+
book: mockBook
58+
});
59+
60+
actions$ = hot("--a---", { a: inputAction });
61+
const response$ = cold("--b|", { b: mockBook });
62+
const expected$ = cold("----c--", { c: outputAction });
63+
mockBookService.create.mockReturnValue(response$);
64+
65+
expect(effects.createBook$).toBeObservable(expected$);
66+
});
67+
});

0 commit comments

Comments
 (0)