Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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$);
});
});