UNPKG

6.39 kBJavaScriptView Raw
1'use strict';
2import {reducers} from '../src/reducers';
3import actions from '../src/actions';
4import expect, {createSpy, spyOn, isSpy} from 'expect'
5import freeze from 'deep-freeze';
6
7describe('Reducers', () => {
8
9 it('sets initial items', () => {
10
11 const items = {
12 item1: 'blah',
13 item2: 'foo',
14 item3: 'zork'
15 };
16
17 const oldState = {
18 items: {},
19 visibleItems: {}
20 };
21
22 const newState =
23 {
24 items: {
25 item1: 'blah',
26 item2: 'foo',
27 item3: 'zork'
28 },
29 visibleItems: {
30 item1: 'blah',
31 item2: 'foo',
32 item3: 'zork'
33 }
34 };
35
36 freeze(oldState);
37 freeze(items);
38
39 expect(reducers(oldState, {
40 type: actions.SET_ITEMS, payload: items
41 })).toEqual(newState);
42
43 });
44
45 it('sets filter and visible items', () => {
46
47 const filter = 'bla';
48
49 const oldState = {
50 visibilityFilter: 'bla',
51 items: {
52 item1: 'blah',
53 item2: 'foo',
54 item3: 'zork',
55 item4: 'blahheh'
56 },
57 open: true,
58 visibleItems: {}
59 };
60
61 const newState =
62 {
63 visibilityFilter: 'bla',
64 items: {
65 item1: 'blah',
66 item2: 'foo',
67 item3: 'zork',
68 item4: 'blahheh'
69 },
70 visibleItems: {
71 item1: 'blah',
72 item4: 'blahheh'
73 },
74 open: true,
75 currentlyHighlighted: 'item1'
76 };
77
78 freeze(oldState);
79 freeze(filter);
80
81 expect(reducers(oldState, {
82 type: actions.SET_FILTER,
83 payload: filter
84 })).toEqual(newState);
85
86 });
87 it('changes state of opened', () => {
88
89 const trueState = {
90 items: {foo: 'barr'},
91 visibleItems:{foo: 'barr'},
92 open: true,
93 currentlyHighlighted: 'foo'
94 };
95
96 const falseState = {
97 items: {foo: 'barr'},
98 visibleItems:{foo: 'barr'},
99 open: false,
100 currentlyHighlighted: ''
101 };
102
103 freeze(trueState);
104 freeze(falseState);
105
106 expect(reducers(trueState, {type: actions.TOGGLE_OPEN})).toEqual(falseState);
107 expect(reducers(trueState, {type: actions.SET_OPEN, payload: false})).toEqual(falseState);
108 expect(reducers(falseState, {type: actions.SET_OPEN, payload: true})).toEqual(trueState);
109 }
110 );
111
112
113 it('selects next item', () => {
114
115 const oldState = {
116 visibleItems: {
117 item1: 'blah',
118 item2: 'foo',
119 item3: 'zork'
120 },
121 currentlyHighlighted: 'item2'
122 };
123 const newState = {
124 visibleItems: {
125 item1: 'blah',
126 item2: 'foo',
127 item3: 'zork'
128 },
129 currentlyHighlighted: 'item3'
130 };
131
132 freeze(oldState);
133 freeze(newState);
134
135 expect(reducers(oldState, {type: actions.SET_NEXT_HIGHLIGHTED})).toEqual(newState);
136 });
137
138 it('selects previous item', () => {
139
140 const oldState = {
141 visibleItems: {
142 item1: 'blah',
143 item2: 'foo',
144 item3: 'zork'
145 },
146 currentlyHighlighted: 'item2'
147 };
148 const newState = {
149 visibleItems: {
150 item1: 'blah',
151 item2: 'foo',
152 item3: 'zork'
153 },
154 currentlyHighlighted: 'item1'
155 };
156
157 freeze(oldState);
158 freeze(newState);
159
160 expect(reducers(oldState, {type: actions.SET_PREV_HIGHLIGHTED})).toEqual(newState);
161 });
162 it('sets currently highlighted', () => {
163
164 const oldState = {
165 visibleItems: {
166 item1: 'blah',
167 item2: 'foo',
168 item3: 'zork'
169 },
170 currentlyHighlighted: 'lkjasdflkj'
171 };
172 const newState = {
173 visibleItems: {
174 item1: 'blah',
175 item2: 'foo',
176 item3: 'zork'
177 },
178 currentlyHighlighted: 'item3'
179 };
180
181 freeze(oldState);
182 freeze(newState);
183
184 expect(reducers(oldState, {type: actions.SET_HIGHLIGHTED, payload: 'item3'})).toEqual(newState);
185 });
186
187 it('sets selected item and the currently highlighted', () => {
188 const oldState = {
189 visibleItems: {
190 item1: 'blah',
191 item2: 'foo',
192 item3: 'zork',
193 itemffff: 'lkjsdflkjslkj'
194 },
195 selected: '',
196 selectedItemLabel: '',
197 currentlyHighlighted: ''
198 };
199 const newState = {
200 visibleItems: {
201 item1: 'blah',
202 item2: 'foo',
203 item3: 'zork',
204 itemffff: 'lkjsdflkjslkj'
205 },
206 selected: 'itemffff',
207 selectedItemLabel: 'lkjsdflkjslkj',
208 currentlyHighlighted: 'itemffff'
209 };
210 const key = {selected: 'itemffff', selectedItemLabel: 'lkjsdflkjslkj'};
211
212 freeze(oldState);
213 freeze(newState);
214
215 expect(reducers(oldState, {type: actions.SET_SELECTED, payload: key})).toEqual(newState);
216 });
217
218 it('sets the tab index value', () => {
219 const oldState = {
220 tabIndex: null
221 };
222 const newState = {
223 tabIndex: 2
224 };
225 freeze(oldState);
226 freeze(newState);
227
228 expect(reducers(oldState, {type: actions.SET_TABINDEX, payload: 2})).toEqual(newState);
229 });
230
231 it('sets initial render to false', () => {
232
233 const oldState = {
234 initialRender: true
235 };
236 const newState = {
237 initialRender: false
238 };
239
240 freeze(oldState);
241 freeze(newState);
242 expect(reducers(oldState, {type: actions.SET_INITIAL_RENDER_FALSE})).toEqual(newState);
243 })
244
245});
\No newline at end of file