Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80522 views
1
'use strict';
2
3
exports.__esModule = true;
4
/**
5
* Used for simulating actions on stores when testing.
6
*
7
*/
8
exports.simulateAction = simulateAction;
9
10
/**
11
* Used for simulating asynchronous actions on stores when testing.
12
*
13
* asyncAction must be one of the following: begin, success or failure.
14
*
15
* When simulating the 'begin' action, all arguments after 'begin' will
16
* be passed to the action handler in the store.
17
*
18
* @example
19
*
20
* TestUtils.simulateActionAsync(store, 'actionId', 'begin', 'arg1', 'arg2');
21
* TestUtils.simulateActionAsync(store, 'actionId', 'success', { foo: 'bar' });
22
* TestUtils.simulateActionAsync(store, 'actionId', 'failure', new Error('action failed'));
23
*/
24
exports.simulateActionAsync = simulateActionAsync;
25
26
function simulateAction(store, action, body) {
27
var actionId = ensureActionId(action);
28
store.handler({ actionId: actionId, body: body });
29
}
30
31
function simulateActionAsync(store, action, asyncAction) {
32
for (var _len = arguments.length, args = Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) {
33
args[_key - 3] = arguments[_key];
34
}
35
36
var actionId = ensureActionId(action);
37
var payload = {
38
actionId: actionId, async: asyncAction
39
};
40
41
switch (asyncAction) {
42
case 'begin':
43
if (args.length) {
44
payload.actionArgs = args;
45
}
46
break;
47
case 'success':
48
payload.body = args[0];
49
break;
50
case 'failure':
51
payload.error = args[0];
52
break;
53
default:
54
throw new Error('asyncAction must be one of: begin, success or failure');
55
}
56
57
store.handler(payload);
58
}
59
60
function ensureActionId(actionOrActionId) {
61
return typeof actionOrActionId === 'function' ? actionOrActionId._id : actionOrActionId;
62
}
63