Skip to content

Commit a2cb3d5

Browse files
08-reducer-testing complete
1 parent 50e0071 commit a2cb3d5

File tree

2 files changed

+118
-4
lines changed

2 files changed

+118
-4
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Books Reducer should add a newly created book to the state 1`] = `
4+
Object {
5+
"activeBookId": "1",
6+
"entities": Object {
7+
"1": Object {
8+
"earnings": 200000000,
9+
"id": "1",
10+
"name": "Forrest Gump",
11+
},
12+
},
13+
"ids": Array [
14+
"1",
15+
],
16+
}
17+
`;
18+
19+
exports[`Books Reducer should load all books when the API loads them all successfully 1`] = `
20+
Object {
21+
"activeBookId": null,
22+
"entities": Object {
23+
"1": Object {
24+
"earnings": 1000000,
25+
"id": "1",
26+
"name": "Castaway",
27+
},
28+
},
29+
"ids": Array [
30+
"1",
31+
],
32+
}
33+
`;
34+
35+
exports[`Books Reducer should remove books from the state when they are deleted 1`] = `
36+
Object {
37+
"activeBookId": null,
38+
"entities": Object {},
39+
"ids": Array [],
40+
}
41+
`;
Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,80 @@
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";
410

511
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+
});
780
});

0 commit comments

Comments
 (0)