forked from GordyD/3ree
-
Notifications
You must be signed in to change notification settings - Fork 1
/
actions.test.js
90 lines (73 loc) · 2.55 KB
/
actions.test.js
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
/* global describe */
/* global it */
/* global afterEach */
import sinon from 'sinon';
import chai from 'chai';
var expect = chai.expect;
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import * as actions from '../universal/actions/PulseActions.js';
import * as types from '../universal/constants/ActionTypes';
describe('Actions', () => {
afterEach(function() {
actions.__ResetDependency__('request');
});
/**
* Example of writing a test on a syncronous action creator
*/
describe('setUserId', () => {
it('should return action with type SET_USER_ID and userId equal to 200', () => {
let action = actions.setUserId(200);
expect(action.type).to.equal(types.SET_USER_ID);
expect(action.userId).to.equal(200);
});
it('should return action with type SET_USER_ID and userId equal to 6700102', () => {
let action = actions.setUserId(6700102);
expect(action.type).to.equal(types.SET_USER_ID);
expect(action.userId).to.equal(6700102);
});
});
/**
* Example of writing a test on an asyncronous action creator
*/
describe('loadEvents', () => {
const mockStore = configureStore([thunk]);
it('should trigger a LOAD_EVENTS_REQUEST and LOAD_EVENTS_SUCCESS action when succesful', (done) => {
let requestMock = {
get: () => ({
set: () => ({
end: (x) => x(null, {
body: [ { name: 'Awesome', value: 54 } ]
})
})
})
};
actions.__Rewire__('request', requestMock);
let expectedActions = [
{ type: 'LOAD_EVENTS_REQUEST' },
{ type: 'LOAD_EVENTS_SUCCESS', events: [ { name: 'Awesome', value: 54 } ] }
];
let initialState = {pulseApp: { events: [], userId: 'baseUser'} };
let store = mockStore(initialState, expectedActions, done);
store.dispatch(actions.loadEvents());
});
it('should trigger a LOAD_EVENTS_REQUEST and LOAD_EVENTS_FAILURE action when unsuccessful', (done) => {
let error = 'An Error Occurred!';
let requestMock = {
get: () => ({
set: () => ({
end: (x) => x(error)
})
})
};
actions.__Rewire__('request', requestMock);
let expectedActions = [
{ type: 'LOAD_EVENTS_REQUEST' },
{ type: 'LOAD_EVENTS_FAILURE', error: error }
];
let initialState = {pulseApp: { events: [], userId: 'baseUser'} };
let store = mockStore(initialState, expectedActions, done);
store.dispatch(actions.loadEvents());
});
});
});