This repository has been archived by the owner on Apr 3, 2022. It is now read-only.
forked from ngrx/example-app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
book.spec.ts
165 lines (141 loc) · 4.7 KB
/
book.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import * as fromBooks from './books';
import {BooksReducerEnum} from './books';
import {Book} from '../models/book';
import {BookAction, BookActionEnum} from '../actions/book';
describe('BooksReducer', () => {
describe('undefined action', () => {
it('should return the default state', () => {
const action = {} as any;
const result = BooksReducerEnum.reducer()(undefined, action);
expect(result).toEqual(fromBooks.initialState);
});
});
describe('SEARCH_COMPLETE & LOAD_SUCCESS', () => {
function noExistingBooks(actionEnum: BookAction<Book[]>) {
const book1 = {id: '111'} as Book;
const book2 = {id: '222'} as Book;
const createAction = actionEnum.toAction([book1, book2]);
const expectedResult = {
ids: ['111', '222'],
entities: {
'111': book1,
'222': book2
},
selectedBookId: null,
} as fromBooks.State;
const result = BooksReducerEnum.reducer()(fromBooks.initialState, createAction);
expect(result).toEqual(expectedResult);
}
function existingBooks(actionEnum: BookAction<Book[]>) {
const book1 = {id: '111'} as Book;
const book2 = {id: '222'} as Book;
const initialState = {
ids: ['111', '222'],
entities: {
'111': book1,
'222': book2
},
selectedBookId: null,
} as any;
// should not replace existing books
const differentBook2 = {id: '222', foo: 'bar'} as any;
const book3 = {id: '333'} as Book;
const createAction = actionEnum.toAction([book3, differentBook2]);
const expectedResult = {
ids: ['111', '222', '333'],
entities: {
'111': book1,
'222': book2,
'333': book3
},
selectedBookId: null,
} as fromBooks.State;
const result = BooksReducerEnum.reducer()(initialState, createAction);
expect(result).toEqual(expectedResult);
}
it('should add all books in the payload when none exist', () => {
noExistingBooks(BookActionEnum.SEARCH_COMPLETE);
noExistingBooks(BookActionEnum.LOAD);
});
it('should add only new books when books already exist', () => {
existingBooks(BookActionEnum.SEARCH_COMPLETE);
existingBooks(BookActionEnum.LOAD);
});
});
describe('LOAD', () => {
it('should add a single book, if the book does not exist', () => {
const book = {id: '888'} as Book;
const action = BookActionEnum.LOAD.toAction(book);
const expectedResult = {
ids: ['888'],
entities: {
'888': book
},
selectedBookId: null
} as fromBooks.State;
const result = BooksReducerEnum.reducer()(fromBooks.initialState, action);
expect(result).toEqual(expectedResult);
});
it('should return the existing state if the book exists', () => {
const initialState = {
ids: ['999'],
entities: {
'999': {id: '999'}
}
} as any;
const book = {id: '999', foo: 'baz'} as any;
const action = BookActionEnum.LOAD.toAction(book);
const result = BooksReducerEnum.reducer()(initialState, action);
expect(result).toEqual(initialState);
});
});
describe('SELECT', () => {
it('should set the selected book id on the state', () => {
const action = BookActionEnum.SELECT.toAction('1');
const result = BooksReducerEnum.reducer()(fromBooks.initialState, action);
expect(result.selectedBookId).toBe('1');
});
});
describe('Selections', () => {
const book1 = {id: '111'} as Book;
const book2 = {id: '222'} as Book;
const state: fromBooks.State = {
ids: ['111', '222'],
entities: {
'111': book1,
'222': book2,
},
selectedBookId: '111'
};
describe('getEntities', () => {
it('should return entities', () => {
const result = fromBooks.getEntities(state);
expect(result).toBe(state.entities);
});
});
describe('getIds', () => {
it('should return ids', () => {
const result = fromBooks.getIds(state);
expect(result).toBe(state.ids);
});
});
describe('getSelectedId', () => {
it('should return the selected id', () => {
const result = fromBooks.getSelectedId(state);
expect(result).toBe('111');
});
});
describe('getSelected', () => {
it('should return the selected book', () => {
const result = fromBooks.getSelected(state);
expect(result).toBe(book1);
});
});
describe('getAll', () => {
it('should return all books as an array ', () => {
const result = fromBooks.getAll(state);
expect(result).toEqual([book1, book2]);
});
});
});
});