|
1 |
| -// import { BooksApiActions, BooksPageActions } from "src/app/books/actions"; |
2 |
| -// import { Book } from "../models/book.model"; |
3 |
| -// import { reducer, initialState } from "./book.reducer"; |
| 1 | +import { BooksApiActions, BooksPageActions } from "src/app/books/actions"; |
| 2 | +import { Book } from "../models/book.model"; |
| 3 | +import { |
| 4 | + reducer, |
| 5 | + initialState, |
| 6 | + selectActiveBook, |
| 7 | + adapter, |
| 8 | + selectAll |
| 9 | +} from "./books.reducer"; |
4 | 10 |
|
5 | 11 | describe("Books Reducer", () => {
|
6 |
| - it("should return the initial state when initialized", () => {}); |
| 12 | + it("should return the initial state when initialized", () => { |
| 13 | + const state = reducer(undefined, { type: "@@init" } as any); |
| 14 | + |
| 15 | + expect(state).toBe(initialState); |
| 16 | + }); |
| 17 | + |
| 18 | + it("should load all books when the API loads them all successfully", () => { |
| 19 | + const books: Book[] = [{ id: "1", name: "Castaway", earnings: 1000000 }]; |
| 20 | + const action = BooksApiActions.booksLoaded({ books }); |
| 21 | + |
| 22 | + const state = reducer(initialState, action); |
| 23 | + |
| 24 | + expect(state).toMatchSnapshot(); |
| 25 | + }); |
| 26 | + |
| 27 | + it("should add a newly created book to the state", () => { |
| 28 | + const book: Book = { id: "1", name: "Forrest Gump", earnings: 200000000 }; |
| 29 | + const action = BooksApiActions.bookCreated({ book }); |
| 30 | + |
| 31 | + const state = reducer(initialState, action); |
| 32 | + |
| 33 | + expect(state).toMatchSnapshot(); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should remove books from the state when they are deleted", () => { |
| 37 | + const book: Book = { id: "1", name: "Apollo 13", earnings: 1000 }; |
| 38 | + const firstAction = BooksApiActions.bookCreated({ book }); |
| 39 | + const secondAction = BooksApiActions.bookDeleted({ book }); |
| 40 | + |
| 41 | + const state = [firstAction, secondAction].reduce(reducer, initialState); |
| 42 | + |
| 43 | + expect(state).toMatchSnapshot(); |
| 44 | + }); |
| 45 | + |
| 46 | + describe("Selectors", () => { |
| 47 | + const initialState = { activeBookId: null, ids: [], entities: {} }; |
| 48 | + |
| 49 | + describe("selectActiveBook", () => { |
| 50 | + it("should return null if there is no active book", () => { |
| 51 | + const result = selectActiveBook(initialState); |
| 52 | + |
| 53 | + expect(result).toBe(null); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should return the active book if there is one", () => { |
| 57 | + const book: Book = { id: "1", name: "Castaway", earnings: 1000000 }; |
| 58 | + const state = adapter.addAll([book], { |
| 59 | + ...initialState, |
| 60 | + activeBookId: "1" |
| 61 | + }); |
| 62 | + const result = selectActiveBook(state); |
| 63 | + |
| 64 | + expect(result).toBe(book); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe("selectAll", () => { |
| 69 | + it("should return all the loaded books", () => { |
| 70 | + const books: Book[] = [ |
| 71 | + { id: "1", name: "Castaway", earnings: 1000000 } |
| 72 | + ]; |
| 73 | + const state = adapter.addAll(books, initialState); |
| 74 | + const result = selectAll(state); |
| 75 | + |
| 76 | + expect(result.length).toBe(1); |
| 77 | + }); |
| 78 | + }); |
| 79 | + }); |
7 | 80 | });
|
0 commit comments