From eded4af2b8a4b3f7d23104ee260163e4879edab1 Mon Sep 17 00:00:00 2001 From: Brandon Roberts Date: Fri, 26 Apr 2019 14:51:48 -0500 Subject: [PATCH] 10-effects-testing complete --- src/app/books/books-api.effects.spec.ts | 67 +++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/app/books/books-api.effects.spec.ts diff --git a/src/app/books/books-api.effects.spec.ts b/src/app/books/books-api.effects.spec.ts new file mode 100644 index 0000000..8495d69 --- /dev/null +++ b/src/app/books/books-api.effects.spec.ts @@ -0,0 +1,67 @@ +import { TestBed } from "@angular/core/testing"; +import { provideMockActions } from "@ngrx/effects/testing"; +import { Action } from "@ngrx/store"; +import { Observable } from "rxjs"; +import { hot, cold } from "jasmine-marbles"; +import { BooksService } from "../shared/services/book.service"; +import { Book } from "../shared/models/book.model"; +import { BooksPageActions, BooksApiActions } from "./actions"; +import { BooksApiEffects } from "./books-api.effects"; + +describe("Book API Effects", () => { + let effects: BooksApiEffects; + let actions$: Observable; + let mockBookService: { + create: jest.Mock; + update: jest.Mock; + delete: jest.Mock; + }; + + const mockBook: Book = { + id: "test", + name: "Mock Book", + earnings: 25 + }; + + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [ + BooksApiEffects, + provideMockActions(() => actions$), + { + provide: BooksService, + useFactory() { + mockBookService = { + create: jest.fn(), + update: jest.fn(), + delete: jest.fn() + }; + + return mockBookService; + } + } + ] + }); + + effects = TestBed.get(BooksApiEffects); + }); + + it("should use the API to create a book", () => { + const inputAction = BooksPageActions.createBook({ + book: { + name: mockBook.name, + earnings: 25 + } + }); + const outputAction = BooksApiActions.bookCreated({ + book: mockBook + }); + + actions$ = hot("--a---", { a: inputAction }); + const response$ = cold("--b|", { b: mockBook }); + const expected$ = cold("----c--", { c: outputAction }); + mockBookService.create.mockReturnValue(response$); + + expect(effects.createBook$).toBeObservable(expected$); + }); +});