-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcreatingActions.spec.js
More file actions
360 lines (289 loc) · 12 KB
/
creatingActions.spec.js
File metadata and controls
360 lines (289 loc) · 12 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import Q from 'q';
import chai, { assert } from 'chai';
import asPromised from 'chai-as-promised';
import sinon from 'sinon';
import Reflux from '../src';
chai.use(asPromised);
describe('Creating action', function() {
it("should implement the publisher API",function(){
var action = Reflux.createAction();
for(var apimethod in Reflux.PublisherMethods){
assert.equal(Reflux.PublisherMethods[apimethod],action[apimethod]);
}
});
it("should copy properties from the definition into the action",function(){
var def = {preEmit:"PRE",shouldEmit:"SHO",random:"RAN"},
action = Reflux.createAction(def);
assert.equal(action.preEmit, def.preEmit);
assert.equal(action.shouldEmit, def.shouldEmit);
assert.equal(action.random, def.random);
});
it("should create specified child actions",function(){
var def = {children: ["foo","BAR"]},
action = Reflux.createAction(def);
assert.deepEqual(action.children, ["foo", "BAR"]);
assert.equal(action.foo._isAction, true);
assert.deepEqual(action.foo.children, []);
assert.equal(action.BAR._isAction, true);
});
it("should create completed and failed child actions for async actions",function(){
var def = {asyncResult: true, sync: true},
action = Reflux.createAction(def);
assert.equal(action.asyncResult, true);
assert.deepEqual(action.children, ["completed", "failed"]);
assert.equal(action.completed._isAction, true);
assert.equal(action.failed._isAction, true);
});
it("should throw an error if you overwrite any API other than preEmit and shouldEmit",function(){
assert.throws(function(){
Reflux.createAction({listen:"FOO"});
});
});
it.only("should create action with the parameter asyncCall",function(done){
let doLogin = () => {
done();
};
let actions = Reflux.createActions({login: {asyncCall: doLogin }});
actions.login();
});
describe('Reflux.ActionMethods', function() {
afterEach(function(){
for (var prop in Reflux.ActionMethods) {
if (Reflux.ActionMethods.hasOwnProperty(prop)) {
delete Reflux.ActionMethods[prop];
}
}
});
it("should copy properties from Reflux.ActionMethods into the action",function(){
Reflux.ActionMethods.preEmit = function() {};
Reflux.ActionMethods.exampleFn = function() {};
var action = Reflux.createAction();
assert.equal(action.preEmit, Reflux.ActionMethods.preEmit);
assert.equal(action.exampleFn, Reflux.ActionMethods.exampleFn);
});
it("should throw an error if you overwrite any API other than preEmit and shouldEmit in Reflux.ActionMethods",function(){
Reflux.ActionMethods.listen = "FOO";
assert.throws(function(){
Reflux.createAction({});
});
});
});
var action,
testArgs;
beforeEach(function () {
action = Reflux.createAction();
testArgs = [1337, 'test'];
});
it('should be a callable functor', function() {
assert.isFunction(action);
});
describe("the synchronisity",function(){
var syncaction = Reflux.createAction({sync:true}),
asyncaction = Reflux.createAction(),
synccalled = false,
asynccalled = false,
store = Reflux.createStore({
sync: function(){synccalled=true;},
async: function(){asynccalled=true;}
});
store.listenTo(syncaction,"sync");
store.listenTo(asyncaction,"async");
it("should be asynchronous when not specified",function(){
asyncaction();
assert.equal(false,asynccalled);
});
it("should be synchronous if requested",function(){
syncaction();
assert.equal(true,synccalled);
});
describe("when changed during lifetime",function(){
var syncaction = Reflux.createAction({sync:true}),
asyncaction = Reflux.createAction(),
synccalled = false,
asynccalled = false,
store = Reflux.createStore({
sync: function(){synccalled=true;},
async: function(){asynccalled=true;}
});
store.listenTo(syncaction,"sync");
store.listenTo(asyncaction,"async");
it("should be asynchronous if initial sync was overridden",function(){
syncaction.sync = false;
syncaction();
assert.equal(false,synccalled);
});
it("should be synchronous if set during lifetime",function(){
asyncaction.sync = true;
asyncaction();
assert.equal(true,asynccalled);
});
});
});
describe('when listening to action', function() {
var promise;
beforeEach(function() {
promise = Q.promise(function(resolve) {
action.listen(function() {
resolve(Array.prototype.slice.call(arguments, 0));
});
});
});
it('should receive the correct arguments', function() {
action(testArgs[0], testArgs[1]);
return assert.eventually.deepEqual(promise, testArgs);
});
describe('when adding preEmit hook', function() {
var preEmit = sinon.spy(),
action = Reflux.createAction({preEmit:preEmit});
action(1337,'test');
it('should receive arguments from action functor', function() {
assert.deepEqual(preEmit.firstCall.args,[1337,'test']);
});
});
describe('when adding shouldEmit hook',function(){
var context = {
validateListening:function(){},
fetchInitialState:function(){}
};
describe("when hook returns true",function(){
var shouldEmit = sinon.stub().returns(true),
action = Reflux.createAction({shouldEmit:shouldEmit}),
callback = sinon.spy();
Reflux.ListenerMethods.listenTo.call(context,action,callback);
action(1337,'test');
it('should receive arguments from action functor', function() {
assert.deepEqual(shouldEmit.firstCall.args,[1337,'test']);
});
it('should still trigger to listeners',function(){
assert.equal(callback.callCount,1);
assert.deepEqual(callback.firstCall.args,[1337,'test']);
});
});
describe("when hook returns false",function(){
var shouldEmit = sinon.stub().returns(false),
action = Reflux.createAction({shouldEmit:shouldEmit}),
callback = sinon.spy();
Reflux.ListenerMethods.listenTo.call(context,action,callback);
action(1337,'test');
it('should receive arguments from action functor', function() {
assert.deepEqual(shouldEmit.firstCall.args,[1337,'test']);
});
it('should not trigger to listeners',function(){
assert.equal(callback.callCount,0);
});
});
});
});
});
describe('Creating actions with children to an action definition object', function() {
var actionNames, actions;
beforeEach(function () {
actionNames = {'foo': {asyncResult: true}, 'bar': {children: ['baz']}};
actions = Reflux.createActions(actionNames);
});
it('should contain foo and bar properties', function() {
assert.property(actions, 'foo');
assert.property(actions, 'bar');
});
it('should contain action functor on foo and bar properties with children', function() {
assert.isFunction(actions.foo);
assert.isFunction(actions.foo.completed);
assert.isFunction(actions.foo.failed);
assert.isFunction(actions.bar);
assert.isFunction(actions.bar.baz);
});
describe('when listening to the child action created this way', function() {
var promise;
beforeEach(function() {
promise = Q.promise(function(resolve) {
actions.bar.baz.listen(function() {
resolve(Array.prototype.slice.call(arguments, 0));
}, {}); // pass empty context
});
});
it('should receive the correct arguments', function() {
var testArgs = [1337, 'test'];
actions.bar.baz(testArgs[0], testArgs[1]);
return assert.eventually.deepEqual(promise, testArgs);
});
});
});
describe('Creating multiple actions to an action definition object', function() {
var actionNames, actions;
beforeEach(function () {
actionNames = ['foo', 'bar'];
actions = Reflux.createActions(actionNames);
});
it('should contain foo and bar properties', function() {
assert.property(actions, 'foo');
assert.property(actions, 'bar');
});
it('should contain action functor on foo and bar properties', function() {
assert.isFunction(actions.foo);
assert.isFunction(actions.bar);
});
describe('when listening to any of the actions created this way', function() {
var promise;
beforeEach(function() {
promise = Q.promise(function(resolve) {
actions.foo.listen(function() {
assert.equal(this, actions.foo);
resolve(Array.prototype.slice.call(arguments, 0));
}); // not passing context, should default to action
});
});
it('should receive the correct arguments', function() {
var testArgs = [1337, 'test'];
actions.foo(testArgs[0], testArgs[1]);
return assert.eventually.deepEqual(promise, testArgs);
});
});
});
describe('Creating multiple actions from an mixed array of strings and object definitions', function() {
var actionNames, actions;
beforeEach(function () {
actionNames = [
'foo',
'bar',
{ baz: { asyncResult: true, children: ['woo'] }},
{
anotherFoo: { asyncResult: true },
anotherBar: { children: ['wee'] }
}];
actions = Reflux.createActions(actionNames);
});
it('should contain foo, bar and baz properties', function() {
assert.property(actions, 'foo');
assert.property(actions, 'bar');
assert.property(actions, 'baz');
assert.property(actions, 'anotherFoo');
assert.property(actions, 'anotherBar');
});
it('should contain action functor on foo, bar and baz properties with children', function() {
assert.isFunction(actions.foo);
assert.isFunction(actions.bar);
assert.isFunction(actions.baz);
assert.isFunction(actions.baz.completed);
assert.isFunction(actions.baz.failed);
assert.isFunction(actions.baz.woo);
assert.isFunction(actions.anotherFoo.completed);
assert.isFunction(actions.anotherFoo.failed);
assert.isFunction(actions.anotherBar.wee);
});
describe('when listening to any of the actions created this way', function() {
var promise;
beforeEach(function() {
promise = Q.promise(function(resolve) {
actions.foo.listen(function() {
assert.equal(this, actions.foo);
resolve(Array.prototype.slice.call(arguments, 0));
}); // not passing context, should default to action
});
});
it('should receive the correct arguments', function() {
var testArgs = [1337, 'test'];
actions.foo(testArgs[0], testArgs[1]);
return assert.eventually.deepEqual(promise, testArgs);
});
});
});