Skip to content

Commit 22306e9

Browse files
08-reducer-testing complete
1 parent e61d37c commit 22306e9

File tree

2 files changed

+104
-3
lines changed

2 files changed

+104
-3
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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": "1",
38+
"entities": Object {
39+
"1": Object {
40+
"earnings": 1000,
41+
"id": "1",
42+
"name": "Apollo 13",
43+
},
44+
},
45+
"ids": Array [
46+
"1",
47+
],
48+
}
49+
`;
Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,61 @@
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 { reducer, initialState, activeBookId, adapter, selectAll } from "./books.reducer";
44

55
describe("Books Reducer", () => {
66
it("should return the initial state when initialized", () => {
7+
const state = reducer(undefined, { type: "@@init" } as any);
78

9+
expect(state).toBe(initialState);
10+
});
11+
12+
it("should load all books when the API loads them all successfully", () => {
13+
const books: Book[] = [{ id: "1", name: "Castaway", earnings: 1000000 }];
14+
const action = BooksApiActions.booksLoaded({ books });
15+
16+
const state = reducer(initialState, action);
17+
18+
expect(state).toMatchSnapshot();
19+
});
20+
21+
it("should add a newly created book to the state", () => {
22+
const book: Book = { id: "1", name: "Forrest Gump", earnings: 200000000 };
23+
const action = BooksApiActions.bookCreated({ book });
24+
25+
const state = reducer(initialState, action);
26+
27+
expect(state).toMatchSnapshot();
28+
});
29+
30+
it("should remove books from the state when they are deleted", () => {
31+
const book: Book = { id: "1", name: "Apollo 13", earnings: 1000 };
32+
const firstAction = BooksApiActions.bookCreated({ book });
33+
const secondAction = BooksPageActions.deleteBook({ book });
34+
35+
const state = [firstAction, secondAction].reduce(reducer, initialState);
36+
37+
expect(state).toMatchSnapshot();
38+
});
39+
40+
describe("Selectors", () => {
41+
const initialState = { activeBookId: "1", ids: [], entities: {} };
42+
43+
describe("activeBookId", () => {
44+
it("should return the active book id", () => {
45+
const result = activeBookId(initialState);
46+
47+
expect(result).toBe("1");
48+
});
49+
});
50+
51+
describe("selectAll", () => {
52+
it("should return all the loaded books", () => {
53+
const books: Book[] = [{ id: "1", name: "Castaway", earnings: 1000000 }];
54+
const state = adapter.addAll(books, initialState);
55+
const result = selectAll(state);
56+
57+
expect(result.length).toBe(1);
58+
});
59+
});
860
});
961
});

0 commit comments

Comments
 (0)