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" ;
4
4
5
5
describe ( "Books Reducer" , ( ) => {
6
6
it ( "should return the initial state when initialized" , ( ) => {
7
+ const state = reducer ( undefined , { type : "@@init" } as any ) ;
7
8
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
+ } ) ;
8
60
} ) ;
9
61
} ) ;
0 commit comments