Skip to content

Commit

Permalink
09-effects-testing complete
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts authored and MikeRyanDev committed Apr 27, 2019
1 parent 92f56dc commit 3616999
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/app/books/books-api.effects.spec.ts
Original file line number Diff line number Diff line change
@@ -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<Action>;
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$);
});
});

0 comments on commit 3616999

Please sign in to comment.