Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80517 views
1
'use strict';
2
3
var _interopRequire = function (obj) { return obj && obj.__esModule ? obj['default'] : obj; };
4
5
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } };
6
7
var _inherits = function (subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; };
8
9
var _Flux$Actions = require('../Flux');
10
11
var _sinon = require('sinon');
12
13
var sinon = _interopRequire(_sinon);
14
15
describe('Actions', function () {
16
var TestActions = (function (_Actions) {
17
function TestActions() {
18
_classCallCheck(this, TestActions);
19
20
if (_Actions != null) {
21
_Actions.apply(this, arguments);
22
}
23
}
24
25
_inherits(TestActions, _Actions);
26
27
TestActions.prototype.getFoo = function getFoo() {
28
return { foo: 'bar' };
29
};
30
31
TestActions.prototype.getBar = function getBar() {
32
return { bar: 'baz' };
33
};
34
35
TestActions.prototype.getBaz = function getBaz() {
36
return;
37
};
38
39
TestActions.prototype.asyncAction = function asyncAction(returnValue) {
40
return regeneratorRuntime.async(function asyncAction$(context$3$0) {
41
while (1) switch (context$3$0.prev = context$3$0.next) {
42
case 0:
43
return context$3$0.abrupt('return', returnValue);
44
45
case 1:
46
case 'end':
47
return context$3$0.stop();
48
}
49
}, null, this);
50
};
51
52
TestActions.prototype.badAsyncAction = function badAsyncAction() {
53
return Promise.reject(new Error('some error'));
54
};
55
56
return TestActions;
57
})(_Flux$Actions.Actions);
58
59
describe('#getActionIds / #getConstants', function () {
60
it('returns strings corresponding to action method names', function () {
61
var actions = new TestActions();
62
63
var actionIds = actions.getActionIds();
64
65
expect(actionIds.getFoo).to.be.a('string');
66
expect(actionIds.getBar).to.be.a('string');
67
68
expect(actionIds.getFoo).to.be.a('string');
69
expect(actionIds.getBar).to.be.a('string');
70
});
71
});
72
73
describe('#[methodName]', function () {
74
it('calls Flux dispatcher', function () {
75
var actions = new TestActions();
76
77
// Attach mock flux instance
78
var dispatch = sinon.spy();
79
actions.dispatch = dispatch;
80
81
actions.getFoo();
82
expect(dispatch.firstCall.args[1]).to.deep.equal({ foo: 'bar' });
83
});
84
85
it('warns if actions have not been added to a Flux instance', function () {
86
var actions = new TestActions();
87
var warn = sinon.spy(console, 'warn');
88
89
actions.getFoo();
90
91
expect(warn.firstCall.args[0]).to.equal('You\'ve attempted to perform the action TestActions#getFoo, but it ' + 'hasn\'t been added to a Flux instance.');
92
93
actions.asyncAction();
94
95
expect(warn.secondCall.args[0]).to.equal('You\'ve attempted to perform the asynchronous action ' + 'TestActions#asyncAction, but it hasn\'t been added ' + 'to a Flux instance.');
96
97
console.warn.restore();
98
});
99
100
it('sends return value to Flux dispatch', function () {
101
var actions = new TestActions();
102
var actionId = actions.getActionIds().getFoo;
103
var dispatch = sinon.spy();
104
actions.dispatch = dispatch;
105
106
actions.getFoo();
107
108
expect(dispatch.firstCall.args[0]).to.equal(actionId);
109
expect(dispatch.firstCall.args[1]).to.deep.equal({ foo: 'bar' });
110
});
111
112
it('send async return value to Flux#dispatchAsync', function callee$2$0() {
113
var actions, actionId, dispatch, response;
114
return regeneratorRuntime.async(function callee$2$0$(context$3$0) {
115
while (1) switch (context$3$0.prev = context$3$0.next) {
116
case 0:
117
actions = new TestActions();
118
actionId = actions.getActionIds().asyncAction;
119
dispatch = sinon.stub().returns(Promise.resolve());
120
121
actions.dispatchAsync = dispatch;
122
123
response = actions.asyncAction('foobar');
124
125
expect(response.then).to.be.a('function');
126
127
context$3$0.next = 8;
128
return response;
129
130
case 8:
131
132
expect(dispatch.firstCall.args[0]).to.equal(actionId);
133
expect(dispatch.firstCall.args[1]).to.be.an.instanceOf(Promise);
134
135
case 10:
136
case 'end':
137
return context$3$0.stop();
138
}
139
}, null, this);
140
});
141
142
it('skips disptach if return value is undefined', function () {
143
var actions = new TestActions();
144
var dispatch = sinon.spy();
145
actions.dispatch = dispatch;
146
147
actions.getBaz();
148
149
expect(dispatch.called).to.be['false'];
150
});
151
152
it('does not skip async dispatch, even if resolved value is undefined', function () {
153
var actions = new TestActions();
154
var dispatch = sinon.stub().returns(Promise.resolve(undefined));
155
actions.dispatchAsync = dispatch;
156
157
actions.asyncAction();
158
159
expect(dispatch.called).to.be['true'];
160
});
161
162
it('returns value from wrapped action', function callee$2$1() {
163
var flux, actions;
164
return regeneratorRuntime.async(function callee$2$1$(context$3$0) {
165
while (1) switch (context$3$0.prev = context$3$0.next) {
166
case 0:
167
flux = new _Flux$Actions.Flux();
168
actions = flux.createActions('test', TestActions);
169
170
expect(actions.getFoo()).to.deep.equal({ foo: 'bar' });
171
context$3$0.next = 5;
172
return expect(actions.asyncAction('async result')).to.eventually.equal('async result');
173
174
case 5:
175
case 'end':
176
return context$3$0.stop();
177
}
178
}, null, this);
179
});
180
});
181
});
182