UNPKG

8.86 kBJavaScriptView Raw
1/* eslint no-unused-vars: 0 */ // since fetch is needed but not used
2import configureStore from 'redux-mock-store';
3import thunk from 'redux-thunk';
4import md5 from 'spark-md5';
5import { polyfill } from 'es6-promise';
6import expect from 'expect';
7import * as actions from '../../actions/topics';
8import * as types from '../../types';
9import createVoteServiceStub from '../../tests/helpers/createVoteServiceStub';
10
11polyfill();
12
13const middlewares = [thunk];
14const mockStore = configureStore(middlewares);
15
16describe('Topic Actions', () => {
17 describe('Asynchronous actions', () => {
18 let sandbox;
19
20 const index = 0;
21 const topic = 'A time machine';
22 const id = md5.hash(topic);
23 const data = {
24 id,
25 count: 1,
26 text: topic
27 };
28
29 const initialState = {
30 topic: {
31 topics: [],
32 newtopic: ''
33 }
34 };
35
36 it('dispatches a duplicate action for a duplicate topic', () => {
37 initialState.topic.topics.push(data);
38
39 const expectedActions = [
40 {
41 type: types.CREATE_TOPIC_DUPLICATE
42 }
43 ];
44
45 const store = mockStore(initialState);
46 store.dispatch(actions.createTopic(topic));
47 expect(store.getActions()).toEqual(expectedActions);
48 initialState.topic.topics.pop();
49 });
50
51 describe('createTopic', () => {
52 let store;
53 let stub;
54
55 describe('on success', () => {
56
57 beforeEach(() => {
58 stub = createVoteServiceStub().replace('createTopic').with(() => Promise.resolve({ status: 200 }));
59 store = mockStore(initialState);
60 });
61
62 afterEach(() => {
63 stub.restore();
64 });
65
66 it('should dispatch a CREATE_TOPIC request and success actions', done => {
67 const expectedActions = [
68 {
69 type: types.CREATE_TOPIC_REQUEST,
70 id,
71 count: 1,
72 text: data.text
73 }, {
74 type: types.CREATE_TOPIC_SUCCESS
75 }
76 ];
77
78 store.dispatch(actions.createTopic(topic))
79 .then(() => {
80 expect(store.getActions()).toEqual(expectedActions);
81 done();
82 })
83 .catch(done);
84 });
85
86 });
87
88 describe('with an existing topic', () => {
89 const topicsWithData = initialState.topic.topics.concat(data);
90 const initialStateWithTopic = { ...initialState, topic: { topics: topicsWithData }};
91
92 beforeEach(() => {
93 stub = createVoteServiceStub().replace('createTopic').with(() => Promise.resolve({ status: 200 }));
94 store = mockStore(initialStateWithTopic);
95 });
96
97 afterEach(() => {
98 stub.restore();
99 });
100
101 it('should dispatch a CREATE_TOPIC_DUPLICATE action', () => {
102 const expectedActions = [
103 {
104 type: types.CREATE_TOPIC_DUPLICATE
105 }
106 ];
107 store.dispatch(actions.createTopic(topic));
108 expect(store.getActions()).toEqual(expectedActions);
109 });
110
111 });
112
113 describe('on failure', () => {
114 beforeEach(() => {
115 stub = createVoteServiceStub().replace('createTopic').with(() => Promise.reject({ status: 400 }));
116 store = mockStore(initialState);
117 });
118
119 afterEach(() => {
120 stub.restore();
121 });
122
123 it('should dispatch a CREATE_TOPIC_FAILURE action', done => {
124 const expectedActions = [
125 {
126 type: types.CREATE_TOPIC_REQUEST,
127 id,
128 count: 1,
129 text: data.text
130 }, {
131 type: types.CREATE_TOPIC_FAILURE,
132 id,
133 error: 'Oops! Something went wrong and we couldn\'t create your topic'
134 }
135 ];
136
137 store.dispatch(actions.createTopic(topic))
138 .then(() => {
139 expect(store.getActions()).toEqual(expectedActions);
140 done();
141 })
142 .catch(done);
143 });
144 });
145 });
146
147
148 describe('incrementCount', () => {
149 let store;
150 let stub;
151
152 describe('on success', () => {
153
154 beforeEach(() => {
155 stub = createVoteServiceStub().replace('updateTopic').with(() => Promise.resolve({ status: 200 }));
156 store = mockStore();
157 });
158
159 afterEach(() => {
160 stub.restore();
161 });
162
163 it('should dispatch a INCREMENT_COUNT action', done => {
164 const expectedActions = [
165 {
166 type: types.INCREMENT_COUNT,
167 id
168 }
169 ];
170
171 store.dispatch(actions.incrementCount(id))
172 .then(() => {
173 expect(store.getActions()).toEqual(expectedActions);
174 done();
175 })
176 .catch(done);
177 });
178 });
179
180 describe('on failure', () => {
181 beforeEach(() => {
182 stub = createVoteServiceStub().replace('updateTopic').with(() => Promise.reject({ status: 400 }));
183 store = mockStore();
184 });
185
186 afterEach(() => {
187 stub.restore();
188 });
189
190 it('should dispatch a CREATE_TOPIC_FAILURE action', done => {
191 const expectedActions = [
192 {
193 type: types.CREATE_TOPIC_FAILURE,
194 id: id,
195 error: 'Oops! Something went wrong and we couldn\'t add your vote'
196 }
197 ];
198
199 store.dispatch(actions.incrementCount(id))
200 .then(() => {
201 expect(store.getActions()).toEqual(expectedActions);
202 done();
203 })
204 .catch(done);
205 });
206 });
207 });
208
209 describe('decrementCount', () => {
210 let store;
211 let stub;
212
213 describe('on success', () => {
214
215 beforeEach(() => {
216 stub = createVoteServiceStub().replace('updateTopic').with(() => Promise.resolve({ status: 200 }));
217 store = mockStore();
218 });
219
220 afterEach(() => {
221 stub.restore();
222 });
223
224 it('should dispatch a DECREMENT_COUNT action', done => {
225 const expectedActions = [
226 {
227 type: types.DECREMENT_COUNT,
228 id
229 }
230 ];
231
232 store.dispatch(actions.decrementCount(id))
233 .then(() => {
234 expect(store.getActions()).toEqual(expectedActions);
235 done();
236 })
237 .catch(done);
238 });
239 });
240
241 describe('on failure', () => {
242 beforeEach(() => {
243 stub = createVoteServiceStub().replace('updateTopic').with(() => Promise.reject({ status: 400 }));
244 store = mockStore();
245 });
246
247 afterEach(() => {
248 stub.restore();
249 });
250
251 it('should dispatch a CREATE_TOPIC_FAILURE action', done => {
252 const expectedActions = [
253 {
254 type: types.CREATE_TOPIC_FAILURE,
255 id: id,
256 error: 'Oops! Something went wrong and we couldn\'t add your vote'
257 }
258 ];
259
260 store.dispatch(actions.decrementCount(id))
261 .then(() => {
262 expect(store.getActions()).toEqual(expectedActions);
263 done();
264 })
265 .catch(done);
266 });
267 });
268 });
269
270 describe('destroyTopic', () => {
271 let store;
272 let stub;
273
274 describe('on success', () => {
275
276 beforeEach(() => {
277 stub = createVoteServiceStub().replace('deleteTopic').with(() => Promise.resolve({ status: 200 }));
278 store = mockStore();
279 });
280
281 afterEach(() => {
282 stub.restore();
283 });
284
285 it('should dispatch a DESTROY_TOPIC action', done => {
286 const expectedActions = [
287 {
288 type: types.DESTROY_TOPIC,
289 id
290 }
291 ];
292
293 store.dispatch(actions.destroyTopic(id))
294 .then(() => {
295 expect(store.getActions()).toEqual(expectedActions);
296 done();
297 })
298 .catch(done);
299 });
300 });
301
302 describe('on failure', () => {
303 beforeEach(() => {
304 stub = createVoteServiceStub().replace('deleteTopic').with(() => Promise.reject({ status: 400 }));
305 store = mockStore();
306 });
307
308 afterEach(() => {
309 stub.restore();
310 });
311
312 it('should dispatch a CREATE_TOPIC_FAILURE action', done => {
313 const expectedActions = [
314 {
315 type: types.CREATE_TOPIC_FAILURE,
316 id: id,
317 error: 'Oops! Something went wrong and we couldn\'t add your vote'
318 }
319 ];
320
321 store.dispatch(actions.destroyTopic(id))
322 .then(() => {
323 expect(store.getActions()).toEqual(expectedActions);
324 done();
325 })
326 .catch(done);
327 });
328 });
329 });
330
331 });
332});
333