@jspsych/test-utils 1.1.2 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +97 -179
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +82 -78
- package/dist/index.js +97 -177
- package/dist/index.js.map +1 -1
- package/package.json +5 -4
- package/src/index.ts +51 -49
package/dist/index.cjs
CHANGED
|
@@ -1,187 +1,105 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var timers = require('timers');
|
|
3
|
+
var node_timers = require('node:timers');
|
|
6
4
|
var jspsych = require('jspsych');
|
|
7
5
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
12
|
-
purpose with or without fee is hereby granted.
|
|
13
|
-
|
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
15
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
16
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
17
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
18
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
19
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
20
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
21
|
-
***************************************************************************** */
|
|
22
|
-
|
|
23
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
24
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
25
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
26
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
27
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
28
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
29
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
30
|
-
});
|
|
6
|
+
function flushPromises() {
|
|
7
|
+
return new Promise((resolve) => node_timers.setImmediate(resolve));
|
|
31
8
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
function keyDown(key) {
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
function keyUp(key) {
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
function pressKey(key) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
function mouseDownMouseUpTarget(target) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
function clickTarget(target) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
function
|
|
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
|
-
/** Shorthand for `jsPsych.getDisplayElement().innerHTML` */
|
|
127
|
-
getHTML: () => displayElement.innerHTML,
|
|
128
|
-
/** Shorthand for `jsPsych.data.get()` */
|
|
129
|
-
getData: () => jsPsychInstance.data.get(),
|
|
130
|
-
expectFinished: () => __awaiter(this, void 0, void 0, function* () {
|
|
131
|
-
yield flushPromises();
|
|
132
|
-
expect(hasFinished).toBe(true);
|
|
133
|
-
}),
|
|
134
|
-
expectRunning: () => __awaiter(this, void 0, void 0, function* () {
|
|
135
|
-
yield flushPromises();
|
|
136
|
-
expect(hasFinished).toBe(false);
|
|
137
|
-
}),
|
|
138
|
-
/** A promise that is resolved when `jsPsych.run()` is done. */
|
|
139
|
-
finished,
|
|
140
|
-
};
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Runs the given timeline by calling `jsPsych.simulate()` on the provided JsPsych object.
|
|
145
|
-
*
|
|
146
|
-
* @param timeline The timeline that is passed to `jsPsych.run()`
|
|
147
|
-
* @param simulation_mode Either 'data-only' mode or 'visual' mode.
|
|
148
|
-
* @param simulation_options Options to pass to `jsPsych.simulate()`
|
|
149
|
-
* @param jsPsych The jsPsych instance to be used. If left empty, a new instance will be created. If
|
|
150
|
-
* a settings object is passed instead, the settings will be used to create the jsPsych instance.
|
|
151
|
-
*
|
|
152
|
-
* @returns An object containing test helper functions, the jsPsych instance, and the jsPsych
|
|
153
|
-
* display element
|
|
154
|
-
*/
|
|
155
|
-
function simulateTimeline(timeline, simulation_mode, simulation_options = {}, jsPsych = {}) {
|
|
156
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
157
|
-
const jsPsychInstance = jsPsych instanceof jspsych.JsPsych ? jsPsych : new jspsych.JsPsych(jsPsych);
|
|
158
|
-
let hasFinished = false;
|
|
159
|
-
const finished = jsPsychInstance
|
|
160
|
-
.simulate(timeline, simulation_mode, simulation_options)
|
|
161
|
-
.then(() => {
|
|
162
|
-
hasFinished = true;
|
|
163
|
-
});
|
|
164
|
-
yield flushPromises();
|
|
165
|
-
const displayElement = jsPsychInstance.getDisplayElement();
|
|
166
|
-
return {
|
|
167
|
-
jsPsych: jsPsychInstance,
|
|
168
|
-
displayElement,
|
|
169
|
-
/** Shorthand for `jsPsych.getDisplayElement().innerHTML` */
|
|
170
|
-
getHTML: () => displayElement.innerHTML,
|
|
171
|
-
/** Shorthand for `jsPsych.data.get()` */
|
|
172
|
-
getData: () => jsPsychInstance.data.get(),
|
|
173
|
-
expectFinished: () => __awaiter(this, void 0, void 0, function* () {
|
|
174
|
-
yield flushPromises();
|
|
175
|
-
expect(hasFinished).toBe(true);
|
|
176
|
-
}),
|
|
177
|
-
expectRunning: () => __awaiter(this, void 0, void 0, function* () {
|
|
178
|
-
yield flushPromises();
|
|
179
|
-
expect(hasFinished).toBe(false);
|
|
180
|
-
}),
|
|
181
|
-
/** A promise that is resolved when `jsPsych.simulate()` is done. */
|
|
182
|
-
finished,
|
|
183
|
-
};
|
|
184
|
-
});
|
|
9
|
+
function dispatchEvent(event, target = document.body) {
|
|
10
|
+
target.dispatchEvent(event);
|
|
11
|
+
return flushPromises();
|
|
12
|
+
}
|
|
13
|
+
async function keyDown(key) {
|
|
14
|
+
await dispatchEvent(new KeyboardEvent("keydown", { key }));
|
|
15
|
+
}
|
|
16
|
+
async function keyUp(key) {
|
|
17
|
+
await dispatchEvent(new KeyboardEvent("keyup", { key }));
|
|
18
|
+
}
|
|
19
|
+
async function pressKey(key) {
|
|
20
|
+
await keyDown(key);
|
|
21
|
+
await keyUp(key);
|
|
22
|
+
}
|
|
23
|
+
async function mouseDownMouseUpTarget(target) {
|
|
24
|
+
await dispatchEvent(new MouseEvent("mousedown", { bubbles: true }), target);
|
|
25
|
+
await dispatchEvent(new MouseEvent("mouseup", { bubbles: true }), target);
|
|
26
|
+
}
|
|
27
|
+
async function clickTarget(target) {
|
|
28
|
+
if (target instanceof HTMLButtonElement || target instanceof HTMLInputElement) {
|
|
29
|
+
if (target.disabled) {
|
|
30
|
+
console.log("Target is disabled, not dispatching click event.");
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
await dispatchEvent(new MouseEvent("click", { bubbles: true }), target);
|
|
35
|
+
}
|
|
36
|
+
async function dispatchMouseEvent(eventType, x, y, container) {
|
|
37
|
+
const containerRect = container.getBoundingClientRect();
|
|
38
|
+
await dispatchEvent(
|
|
39
|
+
new MouseEvent(eventType, {
|
|
40
|
+
clientX: containerRect.x + x,
|
|
41
|
+
clientY: containerRect.y + y,
|
|
42
|
+
bubbles: true
|
|
43
|
+
}),
|
|
44
|
+
container
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
async function mouseMove(x, y, container) {
|
|
48
|
+
await dispatchMouseEvent("mousemove", x, y, container);
|
|
49
|
+
}
|
|
50
|
+
async function mouseUp(x, y, container) {
|
|
51
|
+
await dispatchMouseEvent("mouseup", x, y, container);
|
|
52
|
+
}
|
|
53
|
+
async function mouseDown(x, y, container) {
|
|
54
|
+
await dispatchMouseEvent("mousedown", x, y, container);
|
|
55
|
+
}
|
|
56
|
+
async function startTimeline(timeline, jsPsych = {}) {
|
|
57
|
+
const jsPsychInstance = jsPsych instanceof jspsych.JsPsych ? jsPsych : new jspsych.JsPsych(jsPsych);
|
|
58
|
+
let hasFinished = false;
|
|
59
|
+
const finished = jsPsychInstance.run(timeline).then(() => {
|
|
60
|
+
hasFinished = true;
|
|
61
|
+
});
|
|
62
|
+
await flushPromises();
|
|
63
|
+
const displayElement = jsPsychInstance.getDisplayElement();
|
|
64
|
+
return {
|
|
65
|
+
jsPsych: jsPsychInstance,
|
|
66
|
+
displayElement,
|
|
67
|
+
getHTML: () => displayElement.innerHTML,
|
|
68
|
+
getData: () => jsPsychInstance.data.get(),
|
|
69
|
+
expectFinished: async () => {
|
|
70
|
+
await flushPromises();
|
|
71
|
+
expect(hasFinished).toBe(true);
|
|
72
|
+
},
|
|
73
|
+
expectRunning: async () => {
|
|
74
|
+
await flushPromises();
|
|
75
|
+
expect(hasFinished).toBe(false);
|
|
76
|
+
},
|
|
77
|
+
finished
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
async function simulateTimeline(timeline, simulation_mode, simulation_options = {}, jsPsych = {}) {
|
|
81
|
+
const jsPsychInstance = jsPsych instanceof jspsych.JsPsych ? jsPsych : new jspsych.JsPsych(jsPsych);
|
|
82
|
+
let hasFinished = false;
|
|
83
|
+
const finished = jsPsychInstance.simulate(timeline, simulation_mode, simulation_options).then(() => {
|
|
84
|
+
hasFinished = true;
|
|
85
|
+
});
|
|
86
|
+
await flushPromises();
|
|
87
|
+
const displayElement = jsPsychInstance.getDisplayElement();
|
|
88
|
+
return {
|
|
89
|
+
jsPsych: jsPsychInstance,
|
|
90
|
+
displayElement,
|
|
91
|
+
getHTML: () => displayElement.innerHTML,
|
|
92
|
+
getData: () => jsPsychInstance.data.get(),
|
|
93
|
+
expectFinished: async () => {
|
|
94
|
+
await flushPromises();
|
|
95
|
+
expect(hasFinished).toBe(true);
|
|
96
|
+
},
|
|
97
|
+
expectRunning: async () => {
|
|
98
|
+
await flushPromises();
|
|
99
|
+
expect(hasFinished).toBe(false);
|
|
100
|
+
},
|
|
101
|
+
finished
|
|
102
|
+
};
|
|
185
103
|
}
|
|
186
104
|
|
|
187
105
|
exports.clickTarget = clickTarget;
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../../node_modules/tslib/tslib.es6.js","../src/index.ts"],"sourcesContent":["/******************************************************************************\r\nCopyright (c) Microsoft Corporation.\r\n\r\nPermission to use, copy, modify, and/or distribute this software for any\r\npurpose with or without fee is hereby granted.\r\n\r\nTHE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\r\nREGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\r\nAND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\r\nINDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\r\nLOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR\r\nOTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\r\nPERFORMANCE OF THIS SOFTWARE.\r\n***************************************************************************** */\r\n/* global Reflect, Promise */\r\n\r\nvar extendStatics = function(d, b) {\r\n extendStatics = Object.setPrototypeOf ||\r\n ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||\r\n function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };\r\n return extendStatics(d, b);\r\n};\r\n\r\nexport function __extends(d, b) {\r\n if (typeof b !== \"function\" && b !== null)\r\n throw new TypeError(\"Class extends value \" + String(b) + \" is not a constructor or null\");\r\n extendStatics(d, b);\r\n function __() { this.constructor = d; }\r\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\r\n}\r\n\r\nexport var __assign = function() {\r\n __assign = Object.assign || function __assign(t) {\r\n for (var s, i = 1, n = arguments.length; i < n; i++) {\r\n s = arguments[i];\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];\r\n }\r\n return t;\r\n }\r\n return __assign.apply(this, arguments);\r\n}\r\n\r\nexport function __rest(s, e) {\r\n var t = {};\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)\r\n t[p] = s[p];\r\n if (s != null && typeof Object.getOwnPropertySymbols === \"function\")\r\n for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {\r\n if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))\r\n t[p[i]] = s[p[i]];\r\n }\r\n return t;\r\n}\r\n\r\nexport function __decorate(decorators, target, key, desc) {\r\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\r\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\r\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\r\n return c > 3 && r && Object.defineProperty(target, key, r), r;\r\n}\r\n\r\nexport function __param(paramIndex, decorator) {\r\n return function (target, key) { decorator(target, key, paramIndex); }\r\n}\r\n\r\nexport function __metadata(metadataKey, metadataValue) {\r\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(metadataKey, metadataValue);\r\n}\r\n\r\nexport function __awaiter(thisArg, _arguments, P, generator) {\r\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n}\r\n\r\nexport function __generator(thisArg, body) {\r\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;\r\n return g = { next: verb(0), \"throw\": verb(1), \"return\": verb(2) }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\r\n function verb(n) { return function (v) { return step([n, v]); }; }\r\n function step(op) {\r\n if (f) throw new TypeError(\"Generator is already executing.\");\r\n while (_) try {\r\n if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\r\n if (y = 0, t) op = [op[0] & 2, t.value];\r\n switch (op[0]) {\r\n case 0: case 1: t = op; break;\r\n case 4: _.label++; return { value: op[1], done: false };\r\n case 5: _.label++; y = op[1]; op = [0]; continue;\r\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\r\n default:\r\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\r\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\r\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\r\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\r\n if (t[2]) _.ops.pop();\r\n _.trys.pop(); continue;\r\n }\r\n op = body.call(thisArg, _);\r\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\r\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\r\n }\r\n}\r\n\r\nexport var __createBinding = Object.create ? (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n var desc = Object.getOwnPropertyDescriptor(m, k);\r\n if (!desc || (\"get\" in desc ? !m.__esModule : desc.writable || desc.configurable)) {\r\n desc = { enumerable: true, get: function() { return m[k]; } };\r\n }\r\n Object.defineProperty(o, k2, desc);\r\n}) : (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n o[k2] = m[k];\r\n});\r\n\r\nexport function __exportStar(m, o) {\r\n for (var p in m) if (p !== \"default\" && !Object.prototype.hasOwnProperty.call(o, p)) __createBinding(o, m, p);\r\n}\r\n\r\nexport function __values(o) {\r\n var s = typeof Symbol === \"function\" && Symbol.iterator, m = s && o[s], i = 0;\r\n if (m) return m.call(o);\r\n if (o && typeof o.length === \"number\") return {\r\n next: function () {\r\n if (o && i >= o.length) o = void 0;\r\n return { value: o && o[i++], done: !o };\r\n }\r\n };\r\n throw new TypeError(s ? \"Object is not iterable.\" : \"Symbol.iterator is not defined.\");\r\n}\r\n\r\nexport function __read(o, n) {\r\n var m = typeof Symbol === \"function\" && o[Symbol.iterator];\r\n if (!m) return o;\r\n var i = m.call(o), r, ar = [], e;\r\n try {\r\n while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);\r\n }\r\n catch (error) { e = { error: error }; }\r\n finally {\r\n try {\r\n if (r && !r.done && (m = i[\"return\"])) m.call(i);\r\n }\r\n finally { if (e) throw e.error; }\r\n }\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spread() {\r\n for (var ar = [], i = 0; i < arguments.length; i++)\r\n ar = ar.concat(__read(arguments[i]));\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spreadArrays() {\r\n for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;\r\n for (var r = Array(s), k = 0, i = 0; i < il; i++)\r\n for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)\r\n r[k] = a[j];\r\n return r;\r\n}\r\n\r\nexport function __spreadArray(to, from, pack) {\r\n if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {\r\n if (ar || !(i in from)) {\r\n if (!ar) ar = Array.prototype.slice.call(from, 0, i);\r\n ar[i] = from[i];\r\n }\r\n }\r\n return to.concat(ar || Array.prototype.slice.call(from));\r\n}\r\n\r\nexport function __await(v) {\r\n return this instanceof __await ? (this.v = v, this) : new __await(v);\r\n}\r\n\r\nexport function __asyncGenerator(thisArg, _arguments, generator) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var g = generator.apply(thisArg, _arguments || []), i, q = [];\r\n return i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i;\r\n function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }\r\n function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }\r\n function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }\r\n function fulfill(value) { resume(\"next\", value); }\r\n function reject(value) { resume(\"throw\", value); }\r\n function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }\r\n}\r\n\r\nexport function __asyncDelegator(o) {\r\n var i, p;\r\n return i = {}, verb(\"next\"), verb(\"throw\", function (e) { throw e; }), verb(\"return\"), i[Symbol.iterator] = function () { return this; }, i;\r\n function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === \"return\" } : f ? f(v) : v; } : f; }\r\n}\r\n\r\nexport function __asyncValues(o) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var m = o[Symbol.asyncIterator], i;\r\n return m ? m.call(o) : (o = typeof __values === \"function\" ? __values(o) : o[Symbol.iterator](), i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i);\r\n function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }\r\n function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }\r\n}\r\n\r\nexport function __makeTemplateObject(cooked, raw) {\r\n if (Object.defineProperty) { Object.defineProperty(cooked, \"raw\", { value: raw }); } else { cooked.raw = raw; }\r\n return cooked;\r\n};\r\n\r\nvar __setModuleDefault = Object.create ? (function(o, v) {\r\n Object.defineProperty(o, \"default\", { enumerable: true, value: v });\r\n}) : function(o, v) {\r\n o[\"default\"] = v;\r\n};\r\n\r\nexport function __importStar(mod) {\r\n if (mod && mod.__esModule) return mod;\r\n var result = {};\r\n if (mod != null) for (var k in mod) if (k !== \"default\" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);\r\n __setModuleDefault(result, mod);\r\n return result;\r\n}\r\n\r\nexport function __importDefault(mod) {\r\n return (mod && mod.__esModule) ? mod : { default: mod };\r\n}\r\n\r\nexport function __classPrivateFieldGet(receiver, state, kind, f) {\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a getter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot read private member from an object whose class did not declare it\");\r\n return kind === \"m\" ? f : kind === \"a\" ? f.call(receiver) : f ? f.value : state.get(receiver);\r\n}\r\n\r\nexport function __classPrivateFieldSet(receiver, state, value, kind, f) {\r\n if (kind === \"m\") throw new TypeError(\"Private method is not writable\");\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a setter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot write private member to an object whose class did not declare it\");\r\n return (kind === \"a\" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;\r\n}\r\n\r\nexport function __classPrivateFieldIn(state, receiver) {\r\n if (receiver === null || (typeof receiver !== \"object\" && typeof receiver !== \"function\")) throw new TypeError(\"Cannot use 'in' operator on non-object\");\r\n return typeof state === \"function\" ? receiver === state : state.has(receiver);\r\n}\r\n","import { setImmediate as flushMicroTasks } from \"timers\";\n\nimport { JsPsych } from \"jspsych\";\n\nexport function dispatchEvent(event: Event) {\n document.body.dispatchEvent(event);\n}\n\nexport function keyDown(key: string) {\n dispatchEvent(new KeyboardEvent(\"keydown\", { key }));\n}\n\nexport function keyUp(key: string) {\n dispatchEvent(new KeyboardEvent(\"keyup\", { key }));\n}\n\nexport function pressKey(key: string) {\n keyDown(key);\n keyUp(key);\n}\n\nexport function mouseDownMouseUpTarget(target: Element) {\n target.dispatchEvent(new MouseEvent(\"mousedown\", { bubbles: true }));\n target.dispatchEvent(new MouseEvent(\"mouseup\", { bubbles: true }));\n}\n\nexport function clickTarget(target: Element) {\n target.dispatchEvent(new MouseEvent(\"click\", { bubbles: true }));\n}\n\n/**\n * Dispatch a `mousemove` event, with x and y defined relative to the container element.\n * @param x The x location of the event, relative to the x location of `container`.\n * @param y The y location of the event, relative to the y location of `container`.\n * @param container The DOM element for relative location of the event.\n */\nexport function mouseMove(x: number, y: number, container: Element) {\n const containerRect = container.getBoundingClientRect();\n\n const eventInit = {\n clientX: containerRect.x + x,\n clientY: containerRect.y + y,\n bubbles: true,\n };\n\n container.dispatchEvent(new MouseEvent(\"mousemove\", eventInit));\n}\n\n/**\n * Dispatch a `mouseup` event, with x and y defined relative to the container element.\n * @param x The x location of the event, relative to the x location of `container`.\n * @param y The y location of the event, relative to the y location of `container`.\n * @param container The DOM element for relative location of the event.\n */\nexport function mouseUp(x: number, y: number, container: Element) {\n const containerRect = container.getBoundingClientRect();\n\n const eventInit = {\n clientX: containerRect.x + x,\n clientY: containerRect.y + y,\n bubbles: true,\n };\n\n container.dispatchEvent(new MouseEvent(\"mouseup\", eventInit));\n}\n\n/**\n * Dispatch a `mousemove` event, with x and y defined relative to the container element.\n * @param x The x location of the event, relative to the x location of `container`.\n * @param y The y location of the event, relative to the y location of `container`.\n * @param container The DOM element for relative location of the event.\n */\nexport function mouseDown(x: number, y: number, container: Element) {\n const containerRect = container.getBoundingClientRect();\n\n const eventInit = {\n clientX: containerRect.x + x,\n clientY: containerRect.y + y,\n bubbles: true,\n };\n\n container.dispatchEvent(new MouseEvent(\"mousedown\", eventInit));\n}\n\n/**\n * https://github.com/facebook/jest/issues/2157#issuecomment-279171856\n */\nexport function flushPromises() {\n return new Promise((resolve) => flushMicroTasks(resolve));\n}\n\n/**\n * Runs the given timeline by calling `jsPsych.run()` on the provided JsPsych object.\n *\n * @param timeline The timeline that is passed to `jsPsych.run()`\n * @param jsPsych The jsPsych instance to be used. If left empty, a new instance will be created. If\n * a settings object is passed instead, the settings will be used to create the jsPsych instance.\n *\n * @returns An object containing test helper functions, the jsPsych instance, and the jsPsych\n * display element\n */\nexport async function startTimeline(timeline: any[], jsPsych: JsPsych | any = {}) {\n const jsPsychInstance = jsPsych instanceof JsPsych ? jsPsych : new JsPsych(jsPsych);\n\n let hasFinished = false;\n const finished = jsPsychInstance.run(timeline).then(() => {\n hasFinished = true;\n });\n await flushPromises();\n\n const displayElement = jsPsychInstance.getDisplayElement();\n\n return {\n jsPsych: jsPsychInstance,\n displayElement,\n /** Shorthand for `jsPsych.getDisplayElement().innerHTML` */\n getHTML: () => displayElement.innerHTML,\n /** Shorthand for `jsPsych.data.get()` */\n getData: () => jsPsychInstance.data.get(),\n expectFinished: async () => {\n await flushPromises();\n expect(hasFinished).toBe(true);\n },\n expectRunning: async () => {\n await flushPromises();\n expect(hasFinished).toBe(false);\n },\n /** A promise that is resolved when `jsPsych.run()` is done. */\n finished,\n };\n}\n\n/**\n * Runs the given timeline by calling `jsPsych.simulate()` on the provided JsPsych object.\n *\n * @param timeline The timeline that is passed to `jsPsych.run()`\n * @param simulation_mode Either 'data-only' mode or 'visual' mode.\n * @param simulation_options Options to pass to `jsPsych.simulate()`\n * @param jsPsych The jsPsych instance to be used. If left empty, a new instance will be created. If\n * a settings object is passed instead, the settings will be used to create the jsPsych instance.\n *\n * @returns An object containing test helper functions, the jsPsych instance, and the jsPsych\n * display element\n */\nexport async function simulateTimeline(\n timeline: any[],\n simulation_mode?: \"data-only\" | \"visual\",\n simulation_options: any = {},\n jsPsych: JsPsych | any = {}\n) {\n const jsPsychInstance = jsPsych instanceof JsPsych ? jsPsych : new JsPsych(jsPsych);\n\n let hasFinished = false;\n const finished = jsPsychInstance\n .simulate(timeline, simulation_mode, simulation_options)\n .then(() => {\n hasFinished = true;\n });\n await flushPromises();\n\n const displayElement = jsPsychInstance.getDisplayElement();\n\n return {\n jsPsych: jsPsychInstance,\n displayElement,\n /** Shorthand for `jsPsych.getDisplayElement().innerHTML` */\n getHTML: () => displayElement.innerHTML,\n /** Shorthand for `jsPsych.data.get()` */\n getData: () => jsPsychInstance.data.get(),\n expectFinished: async () => {\n await flushPromises();\n expect(hasFinished).toBe(true);\n },\n expectRunning: async () => {\n await flushPromises();\n expect(hasFinished).toBe(false);\n },\n /** A promise that is resolved when `jsPsych.simulate()` is done. */\n finished,\n };\n}\n"],"names":["flushMicroTasks","JsPsych"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAuDA;AACO,SAAS,SAAS,CAAC,OAAO,EAAE,UAAU,EAAE,CAAC,EAAE,SAAS,EAAE;AAC7D,IAAI,SAAS,KAAK,CAAC,KAAK,EAAE,EAAE,OAAO,KAAK,YAAY,CAAC,GAAG,KAAK,GAAG,IAAI,CAAC,CAAC,UAAU,OAAO,EAAE,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE;AAChH,IAAI,OAAO,KAAK,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,EAAE,UAAU,OAAO,EAAE,MAAM,EAAE;AAC/D,QAAQ,SAAS,SAAS,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE;AACnG,QAAQ,SAAS,QAAQ,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE;AACtG,QAAQ,SAAS,IAA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|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["import { setImmediate as flushMicroTasks } from \"timers\";\n\nimport { JsPsych } from \"jspsych\";\n\n/**\n * https://github.com/facebook/jest/issues/2157#issuecomment-279171856\n */\nexport function flushPromises() {\n return new Promise((resolve) => flushMicroTasks(resolve));\n}\n\nexport function dispatchEvent(event: Event, target: Element = document.body) {\n target.dispatchEvent(event);\n return flushPromises();\n}\n\nexport async function keyDown(key: string) {\n await dispatchEvent(new KeyboardEvent(\"keydown\", { key }));\n}\n\nexport async function keyUp(key: string) {\n await dispatchEvent(new KeyboardEvent(\"keyup\", { key }));\n}\n\nexport async function pressKey(key: string) {\n await keyDown(key);\n await keyUp(key);\n}\n\nexport async function mouseDownMouseUpTarget(target: Element) {\n await dispatchEvent(new MouseEvent(\"mousedown\", { bubbles: true }), target);\n await dispatchEvent(new MouseEvent(\"mouseup\", { bubbles: true }), target);\n}\n\nexport async function clickTarget(target: Element) {\n // Check if the target is a form element and if it's disabled\n if (target instanceof HTMLButtonElement || target instanceof HTMLInputElement) {\n if (target.disabled) {\n console.log(\"Target is disabled, not dispatching click event.\");\n return; // Exit the function if the target is disabled\n }\n }\n await dispatchEvent(new MouseEvent(\"click\", { bubbles: true }), target);\n}\n\n/**\n * Dispatch a `MouseEvent` of type `eventType`, with x and y defined relative to the container element.\n * @param x The x location of the event, relative to the x location of `container`.\n * @param y The y location of the event, relative to the y location of `container`.\n * @param container The DOM element for relative location of the event.\n */\nasync function dispatchMouseEvent(eventType: string, x: number, y: number, container: Element) {\n const containerRect = container.getBoundingClientRect();\n await dispatchEvent(\n new MouseEvent(eventType, {\n clientX: containerRect.x + x,\n clientY: containerRect.y + y,\n bubbles: true,\n }),\n container\n );\n}\n\n/**\n * Dispatch a `mousemove` event, with x and y defined relative to the container element.\n * @param x The x location of the event, relative to the x location of `container`.\n * @param y The y location of the event, relative to the y location of `container`.\n * @param container The DOM element for relative location of the event.\n */\nexport async function mouseMove(x: number, y: number, container: Element) {\n await dispatchMouseEvent(\"mousemove\", x, y, container);\n}\n\n/**\n * Dispatch a `mouseup` event, with x and y defined relative to the container element.\n * @param x The x location of the event, relative to the x location of `container`.\n * @param y The y location of the event, relative to the y location of `container`.\n * @param container The DOM element for relative location of the event.\n */\nexport async function mouseUp(x: number, y: number, container: Element) {\n await dispatchMouseEvent(\"mouseup\", x, y, container);\n}\n\n/**\n * Dispatch a `mousemove` event, with x and y defined relative to the container element.\n * @param x The x location of the event, relative to the x location of `container`.\n * @param y The y location of the event, relative to the y location of `container`.\n * @param container The DOM element for relative location of the event.\n */\nexport async function mouseDown(x: number, y: number, container: Element) {\n await dispatchMouseEvent(\"mousedown\", x, y, container);\n}\n\n/**\n * Runs the given timeline by calling `jsPsych.run()` on the provided JsPsych object.\n *\n * @param timeline The timeline that is passed to `jsPsych.run()`\n * @param jsPsych The jsPsych instance to be used. If left empty, a new instance will be created. If\n * a settings object is passed instead, the settings will be used to create the jsPsych instance.\n *\n * @returns An object containing test helper functions, the jsPsych instance, and the jsPsych\n * display element\n */\nexport async function startTimeline(timeline: any[], jsPsych: JsPsych | any = {}) {\n const jsPsychInstance = jsPsych instanceof JsPsych ? jsPsych : new JsPsych(jsPsych);\n\n let hasFinished = false;\n const finished = jsPsychInstance.run(timeline).then(() => {\n hasFinished = true;\n });\n await flushPromises();\n\n const displayElement = jsPsychInstance.getDisplayElement();\n\n return {\n jsPsych: jsPsychInstance,\n displayElement,\n /** Shorthand for `jsPsych.getDisplayElement().innerHTML` */\n getHTML: () => displayElement.innerHTML,\n /** Shorthand for `jsPsych.data.get()` */\n getData: () => jsPsychInstance.data.get(),\n expectFinished: async () => {\n await flushPromises();\n expect(hasFinished).toBe(true);\n },\n expectRunning: async () => {\n await flushPromises();\n expect(hasFinished).toBe(false);\n },\n /** A promise that is resolved when `jsPsych.run()` is done. */\n finished,\n };\n}\n\n/**\n * Runs the given timeline by calling `jsPsych.simulate()` on the provided JsPsych object.\n *\n * @param timeline The timeline that is passed to `jsPsych.run()`\n * @param simulation_mode Either 'data-only' mode or 'visual' mode.\n * @param simulation_options Options to pass to `jsPsych.simulate()`\n * @param jsPsych The jsPsych instance to be used. If left empty, a new instance will be created. If\n * a settings object is passed instead, the settings will be used to create the jsPsych instance.\n *\n * @returns An object containing test helper functions, the jsPsych instance, and the jsPsych\n * display element\n */\nexport async function simulateTimeline(\n timeline: any[],\n simulation_mode?: \"data-only\" | \"visual\",\n simulation_options: any = {},\n jsPsych: JsPsych | any = {}\n) {\n const jsPsychInstance = jsPsych instanceof JsPsych ? jsPsych : new JsPsych(jsPsych);\n\n let hasFinished = false;\n const finished = jsPsychInstance\n .simulate(timeline, simulation_mode, simulation_options)\n .then(() => {\n hasFinished = true;\n });\n await flushPromises();\n\n const displayElement = jsPsychInstance.getDisplayElement();\n\n return {\n jsPsych: jsPsychInstance,\n displayElement,\n /** Shorthand for `jsPsych.getDisplayElement().innerHTML` */\n getHTML: () => displayElement.innerHTML,\n /** Shorthand for `jsPsych.data.get()` */\n getData: () => jsPsychInstance.data.get(),\n expectFinished: async () => {\n await flushPromises();\n expect(hasFinished).toBe(true);\n },\n expectRunning: async () => {\n await flushPromises();\n expect(hasFinished).toBe(false);\n },\n /** A promise that is resolved when `jsPsych.simulate()` is done. */\n finished,\n };\n}\n"],"names":["flushMicroTasks","JsPsych"],"mappings":";;;;;AAOO,SAAS,aAAgB,GAAA;AAC9B,EAAA,OAAO,IAAI,OAAQ,CAAA,CAAC,OAAY,KAAAA,wBAAA,CAAgB,OAAO,CAAC,CAAA,CAAA;AAC1D,CAAA;AAEO,SAAS,aAAc,CAAA,KAAA,EAAc,MAAkB,GAAA,QAAA,CAAS,IAAM,EAAA;AAC3E,EAAA,MAAA,CAAO,cAAc,KAAK,CAAA,CAAA;AAC1B,EAAA,OAAO,aAAc,EAAA,CAAA;AACvB,CAAA;AAEA,eAAsB,QAAQ,GAAa,EAAA;AACzC,EAAA,MAAM,cAAc,IAAI,aAAA,CAAc,WAAW,EA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A,WAAW,CAAE,CAAA,IAAA,CAAK,KAAK,CAAA,CAAA;AAAA,KAChC;AAAA,IAEA,QAAA;AAAA,GACF,CAAA;AACF;;;;;;;;;;;;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,78 +1,82 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
*
|
|
17
|
-
* @param
|
|
18
|
-
* @param
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
*
|
|
24
|
-
* @param
|
|
25
|
-
* @param
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
*
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
/** Shorthand for `jsPsych.
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
* @param
|
|
61
|
-
* @param
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
/** Shorthand for `jsPsych.
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
1
|
+
import * as jspsych from 'jspsych';
|
|
2
|
+
import { JsPsych } from 'jspsych';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* https://github.com/facebook/jest/issues/2157#issuecomment-279171856
|
|
6
|
+
*/
|
|
7
|
+
declare function flushPromises(): Promise<unknown>;
|
|
8
|
+
declare function dispatchEvent(event: Event, target?: Element): Promise<unknown>;
|
|
9
|
+
declare function keyDown(key: string): Promise<void>;
|
|
10
|
+
declare function keyUp(key: string): Promise<void>;
|
|
11
|
+
declare function pressKey(key: string): Promise<void>;
|
|
12
|
+
declare function mouseDownMouseUpTarget(target: Element): Promise<void>;
|
|
13
|
+
declare function clickTarget(target: Element): Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* Dispatch a `mousemove` event, with x and y defined relative to the container element.
|
|
16
|
+
* @param x The x location of the event, relative to the x location of `container`.
|
|
17
|
+
* @param y The y location of the event, relative to the y location of `container`.
|
|
18
|
+
* @param container The DOM element for relative location of the event.
|
|
19
|
+
*/
|
|
20
|
+
declare function mouseMove(x: number, y: number, container: Element): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Dispatch a `mouseup` event, with x and y defined relative to the container element.
|
|
23
|
+
* @param x The x location of the event, relative to the x location of `container`.
|
|
24
|
+
* @param y The y location of the event, relative to the y location of `container`.
|
|
25
|
+
* @param container The DOM element for relative location of the event.
|
|
26
|
+
*/
|
|
27
|
+
declare function mouseUp(x: number, y: number, container: Element): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Dispatch a `mousemove` event, with x and y defined relative to the container element.
|
|
30
|
+
* @param x The x location of the event, relative to the x location of `container`.
|
|
31
|
+
* @param y The y location of the event, relative to the y location of `container`.
|
|
32
|
+
* @param container The DOM element for relative location of the event.
|
|
33
|
+
*/
|
|
34
|
+
declare function mouseDown(x: number, y: number, container: Element): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Runs the given timeline by calling `jsPsych.run()` on the provided JsPsych object.
|
|
37
|
+
*
|
|
38
|
+
* @param timeline The timeline that is passed to `jsPsych.run()`
|
|
39
|
+
* @param jsPsych The jsPsych instance to be used. If left empty, a new instance will be created. If
|
|
40
|
+
* a settings object is passed instead, the settings will be used to create the jsPsych instance.
|
|
41
|
+
*
|
|
42
|
+
* @returns An object containing test helper functions, the jsPsych instance, and the jsPsych
|
|
43
|
+
* display element
|
|
44
|
+
*/
|
|
45
|
+
declare function startTimeline(timeline: any[], jsPsych?: JsPsych | any): Promise<{
|
|
46
|
+
jsPsych: JsPsych;
|
|
47
|
+
displayElement: HTMLElement;
|
|
48
|
+
/** Shorthand for `jsPsych.getDisplayElement().innerHTML` */
|
|
49
|
+
getHTML: () => string;
|
|
50
|
+
/** Shorthand for `jsPsych.data.get()` */
|
|
51
|
+
getData: () => jspsych.DataCollection;
|
|
52
|
+
expectFinished: () => Promise<void>;
|
|
53
|
+
expectRunning: () => Promise<void>;
|
|
54
|
+
/** A promise that is resolved when `jsPsych.run()` is done. */
|
|
55
|
+
finished: Promise<void>;
|
|
56
|
+
}>;
|
|
57
|
+
/**
|
|
58
|
+
* Runs the given timeline by calling `jsPsych.simulate()` on the provided JsPsych object.
|
|
59
|
+
*
|
|
60
|
+
* @param timeline The timeline that is passed to `jsPsych.run()`
|
|
61
|
+
* @param simulation_mode Either 'data-only' mode or 'visual' mode.
|
|
62
|
+
* @param simulation_options Options to pass to `jsPsych.simulate()`
|
|
63
|
+
* @param jsPsych The jsPsych instance to be used. If left empty, a new instance will be created. If
|
|
64
|
+
* a settings object is passed instead, the settings will be used to create the jsPsych instance.
|
|
65
|
+
*
|
|
66
|
+
* @returns An object containing test helper functions, the jsPsych instance, and the jsPsych
|
|
67
|
+
* display element
|
|
68
|
+
*/
|
|
69
|
+
declare function simulateTimeline(timeline: any[], simulation_mode?: "data-only" | "visual", simulation_options?: any, jsPsych?: JsPsych | any): Promise<{
|
|
70
|
+
jsPsych: JsPsych;
|
|
71
|
+
displayElement: HTMLElement;
|
|
72
|
+
/** Shorthand for `jsPsych.getDisplayElement().innerHTML` */
|
|
73
|
+
getHTML: () => string;
|
|
74
|
+
/** Shorthand for `jsPsych.data.get()` */
|
|
75
|
+
getData: () => jspsych.DataCollection;
|
|
76
|
+
expectFinished: () => Promise<void>;
|
|
77
|
+
expectRunning: () => Promise<void>;
|
|
78
|
+
/** A promise that is resolved when `jsPsych.simulate()` is done. */
|
|
79
|
+
finished: Promise<void>;
|
|
80
|
+
}>;
|
|
81
|
+
|
|
82
|
+
export { clickTarget, dispatchEvent, flushPromises, keyDown, keyUp, mouseDown, mouseDownMouseUpTarget, mouseMove, mouseUp, pressKey, simulateTimeline, startTimeline };
|
package/dist/index.js
CHANGED
|
@@ -1,183 +1,103 @@
|
|
|
1
|
-
import { setImmediate } from 'timers';
|
|
1
|
+
import { setImmediate } from 'node:timers';
|
|
2
2
|
import { JsPsych } from 'jspsych';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
8
|
-
purpose with or without fee is hereby granted.
|
|
9
|
-
|
|
10
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
11
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
12
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
13
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
14
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
15
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
16
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
17
|
-
***************************************************************************** */
|
|
18
|
-
|
|
19
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
20
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
21
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
22
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
23
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
24
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
25
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
26
|
-
});
|
|
4
|
+
function flushPromises() {
|
|
5
|
+
return new Promise((resolve) => setImmediate(resolve));
|
|
27
6
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
function keyDown(key) {
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
function keyUp(key) {
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
function pressKey(key) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
function mouseDownMouseUpTarget(target) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
function clickTarget(target) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
function
|
|
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
|
-
/** Shorthand for `jsPsych.getDisplayElement().innerHTML` */
|
|
123
|
-
getHTML: () => displayElement.innerHTML,
|
|
124
|
-
/** Shorthand for `jsPsych.data.get()` */
|
|
125
|
-
getData: () => jsPsychInstance.data.get(),
|
|
126
|
-
expectFinished: () => __awaiter(this, void 0, void 0, function* () {
|
|
127
|
-
yield flushPromises();
|
|
128
|
-
expect(hasFinished).toBe(true);
|
|
129
|
-
}),
|
|
130
|
-
expectRunning: () => __awaiter(this, void 0, void 0, function* () {
|
|
131
|
-
yield flushPromises();
|
|
132
|
-
expect(hasFinished).toBe(false);
|
|
133
|
-
}),
|
|
134
|
-
/** A promise that is resolved when `jsPsych.run()` is done. */
|
|
135
|
-
finished,
|
|
136
|
-
};
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Runs the given timeline by calling `jsPsych.simulate()` on the provided JsPsych object.
|
|
141
|
-
*
|
|
142
|
-
* @param timeline The timeline that is passed to `jsPsych.run()`
|
|
143
|
-
* @param simulation_mode Either 'data-only' mode or 'visual' mode.
|
|
144
|
-
* @param simulation_options Options to pass to `jsPsych.simulate()`
|
|
145
|
-
* @param jsPsych The jsPsych instance to be used. If left empty, a new instance will be created. If
|
|
146
|
-
* a settings object is passed instead, the settings will be used to create the jsPsych instance.
|
|
147
|
-
*
|
|
148
|
-
* @returns An object containing test helper functions, the jsPsych instance, and the jsPsych
|
|
149
|
-
* display element
|
|
150
|
-
*/
|
|
151
|
-
function simulateTimeline(timeline, simulation_mode, simulation_options = {}, jsPsych = {}) {
|
|
152
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
153
|
-
const jsPsychInstance = jsPsych instanceof JsPsych ? jsPsych : new JsPsych(jsPsych);
|
|
154
|
-
let hasFinished = false;
|
|
155
|
-
const finished = jsPsychInstance
|
|
156
|
-
.simulate(timeline, simulation_mode, simulation_options)
|
|
157
|
-
.then(() => {
|
|
158
|
-
hasFinished = true;
|
|
159
|
-
});
|
|
160
|
-
yield flushPromises();
|
|
161
|
-
const displayElement = jsPsychInstance.getDisplayElement();
|
|
162
|
-
return {
|
|
163
|
-
jsPsych: jsPsychInstance,
|
|
164
|
-
displayElement,
|
|
165
|
-
/** Shorthand for `jsPsych.getDisplayElement().innerHTML` */
|
|
166
|
-
getHTML: () => displayElement.innerHTML,
|
|
167
|
-
/** Shorthand for `jsPsych.data.get()` */
|
|
168
|
-
getData: () => jsPsychInstance.data.get(),
|
|
169
|
-
expectFinished: () => __awaiter(this, void 0, void 0, function* () {
|
|
170
|
-
yield flushPromises();
|
|
171
|
-
expect(hasFinished).toBe(true);
|
|
172
|
-
}),
|
|
173
|
-
expectRunning: () => __awaiter(this, void 0, void 0, function* () {
|
|
174
|
-
yield flushPromises();
|
|
175
|
-
expect(hasFinished).toBe(false);
|
|
176
|
-
}),
|
|
177
|
-
/** A promise that is resolved when `jsPsych.simulate()` is done. */
|
|
178
|
-
finished,
|
|
179
|
-
};
|
|
180
|
-
});
|
|
7
|
+
function dispatchEvent(event, target = document.body) {
|
|
8
|
+
target.dispatchEvent(event);
|
|
9
|
+
return flushPromises();
|
|
10
|
+
}
|
|
11
|
+
async function keyDown(key) {
|
|
12
|
+
await dispatchEvent(new KeyboardEvent("keydown", { key }));
|
|
13
|
+
}
|
|
14
|
+
async function keyUp(key) {
|
|
15
|
+
await dispatchEvent(new KeyboardEvent("keyup", { key }));
|
|
16
|
+
}
|
|
17
|
+
async function pressKey(key) {
|
|
18
|
+
await keyDown(key);
|
|
19
|
+
await keyUp(key);
|
|
20
|
+
}
|
|
21
|
+
async function mouseDownMouseUpTarget(target) {
|
|
22
|
+
await dispatchEvent(new MouseEvent("mousedown", { bubbles: true }), target);
|
|
23
|
+
await dispatchEvent(new MouseEvent("mouseup", { bubbles: true }), target);
|
|
24
|
+
}
|
|
25
|
+
async function clickTarget(target) {
|
|
26
|
+
if (target instanceof HTMLButtonElement || target instanceof HTMLInputElement) {
|
|
27
|
+
if (target.disabled) {
|
|
28
|
+
console.log("Target is disabled, not dispatching click event.");
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
await dispatchEvent(new MouseEvent("click", { bubbles: true }), target);
|
|
33
|
+
}
|
|
34
|
+
async function dispatchMouseEvent(eventType, x, y, container) {
|
|
35
|
+
const containerRect = container.getBoundingClientRect();
|
|
36
|
+
await dispatchEvent(
|
|
37
|
+
new MouseEvent(eventType, {
|
|
38
|
+
clientX: containerRect.x + x,
|
|
39
|
+
clientY: containerRect.y + y,
|
|
40
|
+
bubbles: true
|
|
41
|
+
}),
|
|
42
|
+
container
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
async function mouseMove(x, y, container) {
|
|
46
|
+
await dispatchMouseEvent("mousemove", x, y, container);
|
|
47
|
+
}
|
|
48
|
+
async function mouseUp(x, y, container) {
|
|
49
|
+
await dispatchMouseEvent("mouseup", x, y, container);
|
|
50
|
+
}
|
|
51
|
+
async function mouseDown(x, y, container) {
|
|
52
|
+
await dispatchMouseEvent("mousedown", x, y, container);
|
|
53
|
+
}
|
|
54
|
+
async function startTimeline(timeline, jsPsych = {}) {
|
|
55
|
+
const jsPsychInstance = jsPsych instanceof JsPsych ? jsPsych : new JsPsych(jsPsych);
|
|
56
|
+
let hasFinished = false;
|
|
57
|
+
const finished = jsPsychInstance.run(timeline).then(() => {
|
|
58
|
+
hasFinished = true;
|
|
59
|
+
});
|
|
60
|
+
await flushPromises();
|
|
61
|
+
const displayElement = jsPsychInstance.getDisplayElement();
|
|
62
|
+
return {
|
|
63
|
+
jsPsych: jsPsychInstance,
|
|
64
|
+
displayElement,
|
|
65
|
+
getHTML: () => displayElement.innerHTML,
|
|
66
|
+
getData: () => jsPsychInstance.data.get(),
|
|
67
|
+
expectFinished: async () => {
|
|
68
|
+
await flushPromises();
|
|
69
|
+
expect(hasFinished).toBe(true);
|
|
70
|
+
},
|
|
71
|
+
expectRunning: async () => {
|
|
72
|
+
await flushPromises();
|
|
73
|
+
expect(hasFinished).toBe(false);
|
|
74
|
+
},
|
|
75
|
+
finished
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
async function simulateTimeline(timeline, simulation_mode, simulation_options = {}, jsPsych = {}) {
|
|
79
|
+
const jsPsychInstance = jsPsych instanceof JsPsych ? jsPsych : new JsPsych(jsPsych);
|
|
80
|
+
let hasFinished = false;
|
|
81
|
+
const finished = jsPsychInstance.simulate(timeline, simulation_mode, simulation_options).then(() => {
|
|
82
|
+
hasFinished = true;
|
|
83
|
+
});
|
|
84
|
+
await flushPromises();
|
|
85
|
+
const displayElement = jsPsychInstance.getDisplayElement();
|
|
86
|
+
return {
|
|
87
|
+
jsPsych: jsPsychInstance,
|
|
88
|
+
displayElement,
|
|
89
|
+
getHTML: () => displayElement.innerHTML,
|
|
90
|
+
getData: () => jsPsychInstance.data.get(),
|
|
91
|
+
expectFinished: async () => {
|
|
92
|
+
await flushPromises();
|
|
93
|
+
expect(hasFinished).toBe(true);
|
|
94
|
+
},
|
|
95
|
+
expectRunning: async () => {
|
|
96
|
+
await flushPromises();
|
|
97
|
+
expect(hasFinished).toBe(false);
|
|
98
|
+
},
|
|
99
|
+
finished
|
|
100
|
+
};
|
|
181
101
|
}
|
|
182
102
|
|
|
183
103
|
export { clickTarget, dispatchEvent, flushPromises, keyDown, keyUp, mouseDown, mouseDownMouseUpTarget, mouseMove, mouseUp, pressKey, simulateTimeline, startTimeline };
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../node_modules/tslib/tslib.es6.js","../src/index.ts"],"sourcesContent":["/******************************************************************************\r\nCopyright (c) Microsoft Corporation.\r\n\r\nPermission to use, copy, modify, and/or distribute this software for any\r\npurpose with or without fee is hereby granted.\r\n\r\nTHE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\r\nREGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\r\nAND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\r\nINDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\r\nLOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR\r\nOTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\r\nPERFORMANCE OF THIS SOFTWARE.\r\n***************************************************************************** */\r\n/* global Reflect, Promise */\r\n\r\nvar extendStatics = function(d, b) {\r\n extendStatics = Object.setPrototypeOf ||\r\n ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||\r\n function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };\r\n return extendStatics(d, b);\r\n};\r\n\r\nexport function __extends(d, b) {\r\n if (typeof b !== \"function\" && b !== null)\r\n throw new TypeError(\"Class extends value \" + String(b) + \" is not a constructor or null\");\r\n extendStatics(d, b);\r\n function __() { this.constructor = d; }\r\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\r\n}\r\n\r\nexport var __assign = function() {\r\n __assign = Object.assign || function __assign(t) {\r\n for (var s, i = 1, n = arguments.length; i < n; i++) {\r\n s = arguments[i];\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];\r\n }\r\n return t;\r\n }\r\n return __assign.apply(this, arguments);\r\n}\r\n\r\nexport function __rest(s, e) {\r\n var t = {};\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)\r\n t[p] = s[p];\r\n if (s != null && typeof Object.getOwnPropertySymbols === \"function\")\r\n for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {\r\n if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))\r\n t[p[i]] = s[p[i]];\r\n }\r\n return t;\r\n}\r\n\r\nexport function __decorate(decorators, target, key, desc) {\r\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\r\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\r\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\r\n return c > 3 && r && Object.defineProperty(target, key, r), r;\r\n}\r\n\r\nexport function __param(paramIndex, decorator) {\r\n return function (target, key) { decorator(target, key, paramIndex); }\r\n}\r\n\r\nexport function __metadata(metadataKey, metadataValue) {\r\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(metadataKey, metadataValue);\r\n}\r\n\r\nexport function __awaiter(thisArg, _arguments, P, generator) {\r\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n}\r\n\r\nexport function __generator(thisArg, body) {\r\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;\r\n return g = { next: verb(0), \"throw\": verb(1), \"return\": verb(2) }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\r\n function verb(n) { return function (v) { return step([n, v]); }; }\r\n function step(op) {\r\n if (f) throw new TypeError(\"Generator is already executing.\");\r\n while (_) try {\r\n if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\r\n if (y = 0, t) op = [op[0] & 2, t.value];\r\n switch (op[0]) {\r\n case 0: case 1: t = op; break;\r\n case 4: _.label++; return { value: op[1], done: false };\r\n case 5: _.label++; y = op[1]; op = [0]; continue;\r\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\r\n default:\r\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\r\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\r\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\r\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\r\n if (t[2]) _.ops.pop();\r\n _.trys.pop(); continue;\r\n }\r\n op = body.call(thisArg, _);\r\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\r\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\r\n }\r\n}\r\n\r\nexport var __createBinding = Object.create ? (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n var desc = Object.getOwnPropertyDescriptor(m, k);\r\n if (!desc || (\"get\" in desc ? !m.__esModule : desc.writable || desc.configurable)) {\r\n desc = { enumerable: true, get: function() { return m[k]; } };\r\n }\r\n Object.defineProperty(o, k2, desc);\r\n}) : (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n o[k2] = m[k];\r\n});\r\n\r\nexport function __exportStar(m, o) {\r\n for (var p in m) if (p !== \"default\" && !Object.prototype.hasOwnProperty.call(o, p)) __createBinding(o, m, p);\r\n}\r\n\r\nexport function __values(o) {\r\n var s = typeof Symbol === \"function\" && Symbol.iterator, m = s && o[s], i = 0;\r\n if (m) return m.call(o);\r\n if (o && typeof o.length === \"number\") return {\r\n next: function () {\r\n if (o && i >= o.length) o = void 0;\r\n return { value: o && o[i++], done: !o };\r\n }\r\n };\r\n throw new TypeError(s ? \"Object is not iterable.\" : \"Symbol.iterator is not defined.\");\r\n}\r\n\r\nexport function __read(o, n) {\r\n var m = typeof Symbol === \"function\" && o[Symbol.iterator];\r\n if (!m) return o;\r\n var i = m.call(o), r, ar = [], e;\r\n try {\r\n while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);\r\n }\r\n catch (error) { e = { error: error }; }\r\n finally {\r\n try {\r\n if (r && !r.done && (m = i[\"return\"])) m.call(i);\r\n }\r\n finally { if (e) throw e.error; }\r\n }\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spread() {\r\n for (var ar = [], i = 0; i < arguments.length; i++)\r\n ar = ar.concat(__read(arguments[i]));\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spreadArrays() {\r\n for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;\r\n for (var r = Array(s), k = 0, i = 0; i < il; i++)\r\n for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)\r\n r[k] = a[j];\r\n return r;\r\n}\r\n\r\nexport function __spreadArray(to, from, pack) {\r\n if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {\r\n if (ar || !(i in from)) {\r\n if (!ar) ar = Array.prototype.slice.call(from, 0, i);\r\n ar[i] = from[i];\r\n }\r\n }\r\n return to.concat(ar || Array.prototype.slice.call(from));\r\n}\r\n\r\nexport function __await(v) {\r\n return this instanceof __await ? (this.v = v, this) : new __await(v);\r\n}\r\n\r\nexport function __asyncGenerator(thisArg, _arguments, generator) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var g = generator.apply(thisArg, _arguments || []), i, q = [];\r\n return i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i;\r\n function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }\r\n function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }\r\n function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }\r\n function fulfill(value) { resume(\"next\", value); }\r\n function reject(value) { resume(\"throw\", value); }\r\n function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }\r\n}\r\n\r\nexport function __asyncDelegator(o) {\r\n var i, p;\r\n return i = {}, verb(\"next\"), verb(\"throw\", function (e) { throw e; }), verb(\"return\"), i[Symbol.iterator] = function () { return this; }, i;\r\n function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === \"return\" } : f ? f(v) : v; } : f; }\r\n}\r\n\r\nexport function __asyncValues(o) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var m = o[Symbol.asyncIterator], i;\r\n return m ? m.call(o) : (o = typeof __values === \"function\" ? __values(o) : o[Symbol.iterator](), i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i);\r\n function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }\r\n function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }\r\n}\r\n\r\nexport function __makeTemplateObject(cooked, raw) {\r\n if (Object.defineProperty) { Object.defineProperty(cooked, \"raw\", { value: raw }); } else { cooked.raw = raw; }\r\n return cooked;\r\n};\r\n\r\nvar __setModuleDefault = Object.create ? (function(o, v) {\r\n Object.defineProperty(o, \"default\", { enumerable: true, value: v });\r\n}) : function(o, v) {\r\n o[\"default\"] = v;\r\n};\r\n\r\nexport function __importStar(mod) {\r\n if (mod && mod.__esModule) return mod;\r\n var result = {};\r\n if (mod != null) for (var k in mod) if (k !== \"default\" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);\r\n __setModuleDefault(result, mod);\r\n return result;\r\n}\r\n\r\nexport function __importDefault(mod) {\r\n return (mod && mod.__esModule) ? mod : { default: mod };\r\n}\r\n\r\nexport function __classPrivateFieldGet(receiver, state, kind, f) {\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a getter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot read private member from an object whose class did not declare it\");\r\n return kind === \"m\" ? f : kind === \"a\" ? f.call(receiver) : f ? f.value : state.get(receiver);\r\n}\r\n\r\nexport function __classPrivateFieldSet(receiver, state, value, kind, f) {\r\n if (kind === \"m\") throw new TypeError(\"Private method is not writable\");\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a setter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot write private member to an object whose class did not declare it\");\r\n return (kind === \"a\" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;\r\n}\r\n\r\nexport function __classPrivateFieldIn(state, receiver) {\r\n if (receiver === null || (typeof receiver !== \"object\" && typeof receiver !== \"function\")) throw new TypeError(\"Cannot use 'in' operator on non-object\");\r\n return typeof state === \"function\" ? receiver === state : state.has(receiver);\r\n}\r\n","import { setImmediate as flushMicroTasks } from \"timers\";\n\nimport { JsPsych } from \"jspsych\";\n\nexport function dispatchEvent(event: Event) {\n document.body.dispatchEvent(event);\n}\n\nexport function keyDown(key: string) {\n dispatchEvent(new KeyboardEvent(\"keydown\", { key }));\n}\n\nexport function keyUp(key: string) {\n dispatchEvent(new KeyboardEvent(\"keyup\", { key }));\n}\n\nexport function pressKey(key: string) {\n keyDown(key);\n keyUp(key);\n}\n\nexport function mouseDownMouseUpTarget(target: Element) {\n target.dispatchEvent(new MouseEvent(\"mousedown\", { bubbles: true }));\n target.dispatchEvent(new MouseEvent(\"mouseup\", { bubbles: true }));\n}\n\nexport function clickTarget(target: Element) {\n target.dispatchEvent(new MouseEvent(\"click\", { bubbles: true }));\n}\n\n/**\n * Dispatch a `mousemove` event, with x and y defined relative to the container element.\n * @param x The x location of the event, relative to the x location of `container`.\n * @param y The y location of the event, relative to the y location of `container`.\n * @param container The DOM element for relative location of the event.\n */\nexport function mouseMove(x: number, y: number, container: Element) {\n const containerRect = container.getBoundingClientRect();\n\n const eventInit = {\n clientX: containerRect.x + x,\n clientY: containerRect.y + y,\n bubbles: true,\n };\n\n container.dispatchEvent(new MouseEvent(\"mousemove\", eventInit));\n}\n\n/**\n * Dispatch a `mouseup` event, with x and y defined relative to the container element.\n * @param x The x location of the event, relative to the x location of `container`.\n * @param y The y location of the event, relative to the y location of `container`.\n * @param container The DOM element for relative location of the event.\n */\nexport function mouseUp(x: number, y: number, container: Element) {\n const containerRect = container.getBoundingClientRect();\n\n const eventInit = {\n clientX: containerRect.x + x,\n clientY: containerRect.y + y,\n bubbles: true,\n };\n\n container.dispatchEvent(new MouseEvent(\"mouseup\", eventInit));\n}\n\n/**\n * Dispatch a `mousemove` event, with x and y defined relative to the container element.\n * @param x The x location of the event, relative to the x location of `container`.\n * @param y The y location of the event, relative to the y location of `container`.\n * @param container The DOM element for relative location of the event.\n */\nexport function mouseDown(x: number, y: number, container: Element) {\n const containerRect = container.getBoundingClientRect();\n\n const eventInit = {\n clientX: containerRect.x + x,\n clientY: containerRect.y + y,\n bubbles: true,\n };\n\n container.dispatchEvent(new MouseEvent(\"mousedown\", eventInit));\n}\n\n/**\n * https://github.com/facebook/jest/issues/2157#issuecomment-279171856\n */\nexport function flushPromises() {\n return new Promise((resolve) => flushMicroTasks(resolve));\n}\n\n/**\n * Runs the given timeline by calling `jsPsych.run()` on the provided JsPsych object.\n *\n * @param timeline The timeline that is passed to `jsPsych.run()`\n * @param jsPsych The jsPsych instance to be used. If left empty, a new instance will be created. If\n * a settings object is passed instead, the settings will be used to create the jsPsych instance.\n *\n * @returns An object containing test helper functions, the jsPsych instance, and the jsPsych\n * display element\n */\nexport async function startTimeline(timeline: any[], jsPsych: JsPsych | any = {}) {\n const jsPsychInstance = jsPsych instanceof JsPsych ? jsPsych : new JsPsych(jsPsych);\n\n let hasFinished = false;\n const finished = jsPsychInstance.run(timeline).then(() => {\n hasFinished = true;\n });\n await flushPromises();\n\n const displayElement = jsPsychInstance.getDisplayElement();\n\n return {\n jsPsych: jsPsychInstance,\n displayElement,\n /** Shorthand for `jsPsych.getDisplayElement().innerHTML` */\n getHTML: () => displayElement.innerHTML,\n /** Shorthand for `jsPsych.data.get()` */\n getData: () => jsPsychInstance.data.get(),\n expectFinished: async () => {\n await flushPromises();\n expect(hasFinished).toBe(true);\n },\n expectRunning: async () => {\n await flushPromises();\n expect(hasFinished).toBe(false);\n },\n /** A promise that is resolved when `jsPsych.run()` is done. */\n finished,\n };\n}\n\n/**\n * Runs the given timeline by calling `jsPsych.simulate()` on the provided JsPsych object.\n *\n * @param timeline The timeline that is passed to `jsPsych.run()`\n * @param simulation_mode Either 'data-only' mode or 'visual' mode.\n * @param simulation_options Options to pass to `jsPsych.simulate()`\n * @param jsPsych The jsPsych instance to be used. If left empty, a new instance will be created. If\n * a settings object is passed instead, the settings will be used to create the jsPsych instance.\n *\n * @returns An object containing test helper functions, the jsPsych instance, and the jsPsych\n * display element\n */\nexport async function simulateTimeline(\n timeline: any[],\n simulation_mode?: \"data-only\" | \"visual\",\n simulation_options: any = {},\n jsPsych: JsPsych | any = {}\n) {\n const jsPsychInstance = jsPsych instanceof JsPsych ? jsPsych : new JsPsych(jsPsych);\n\n let hasFinished = false;\n const finished = jsPsychInstance\n .simulate(timeline, simulation_mode, simulation_options)\n .then(() => {\n hasFinished = true;\n });\n await flushPromises();\n\n const displayElement = jsPsychInstance.getDisplayElement();\n\n return {\n jsPsych: jsPsychInstance,\n displayElement,\n /** Shorthand for `jsPsych.getDisplayElement().innerHTML` */\n getHTML: () => displayElement.innerHTML,\n /** Shorthand for `jsPsych.data.get()` */\n getData: () => jsPsychInstance.data.get(),\n expectFinished: async () => {\n await flushPromises();\n expect(hasFinished).toBe(true);\n },\n expectRunning: async () => {\n await flushPromises();\n expect(hasFinished).toBe(false);\n },\n /** A promise that is resolved when `jsPsych.simulate()` is done. */\n finished,\n };\n}\n"],"names":["flushMicroTasks"],"mappings":";;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAuDA;AACO,SAAS,SAAS,CAAC,OAAO,EAAE,UAAU,EAAE,CAAC,EAAE,SAAS,EAAE;AAC7D,IAAI,SAAS,KAAK,CAAC,KAAK,EAAE,EAAE,OAAO,KAAK,YAAY,CAAC,GAAG,KAAK,GAAG,IAAI,CAAC,CAAC,UAAU,OAAO,EAAE,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE;AAChH,IAAI,OAAO,KAAK,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,EAAE,UAAU,OAAO,EAAE,MAAM,EAAE;AAC/D,QAAQ,SAAS,SAAS,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE;AACnG,QAAQ,SAAS,QAAQ,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,KAAK,C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I,CAAC,KAAK,CAAC,CAAC;AAClC,aAAC,CAAA;;YAED,QAAQ;SACT,CAAC;KACH,CAAA,CAAA;AAAA;;;;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import { setImmediate as flushMicroTasks } from \"timers\";\n\nimport { JsPsych } from \"jspsych\";\n\n/**\n * https://github.com/facebook/jest/issues/2157#issuecomment-279171856\n */\nexport function flushPromises() {\n return new Promise((resolve) => flushMicroTasks(resolve));\n}\n\nexport function dispatchEvent(event: Event, target: Element = document.body) {\n target.dispatchEvent(event);\n return flushPromises();\n}\n\nexport async function keyDown(key: string) {\n await dispatchEvent(new KeyboardEvent(\"keydown\", { key }));\n}\n\nexport async function keyUp(key: string) {\n await dispatchEvent(new KeyboardEvent(\"keyup\", { key }));\n}\n\nexport async function pressKey(key: string) {\n await keyDown(key);\n await keyUp(key);\n}\n\nexport async function mouseDownMouseUpTarget(target: Element) {\n await dispatchEvent(new MouseEvent(\"mousedown\", { bubbles: true }), target);\n await dispatchEvent(new MouseEvent(\"mouseup\", { bubbles: true }), target);\n}\n\nexport async function clickTarget(target: Element) {\n // Check if the target is a form element and if it's disabled\n if (target instanceof HTMLButtonElement || target instanceof HTMLInputElement) {\n if (target.disabled) {\n console.log(\"Target is disabled, not dispatching click event.\");\n return; // Exit the function if the target is disabled\n }\n }\n await dispatchEvent(new MouseEvent(\"click\", { bubbles: true }), target);\n}\n\n/**\n * Dispatch a `MouseEvent` of type `eventType`, with x and y defined relative to the container element.\n * @param x The x location of the event, relative to the x location of `container`.\n * @param y The y location of the event, relative to the y location of `container`.\n * @param container The DOM element for relative location of the event.\n */\nasync function dispatchMouseEvent(eventType: string, x: number, y: number, container: Element) {\n const containerRect = container.getBoundingClientRect();\n await dispatchEvent(\n new MouseEvent(eventType, {\n clientX: containerRect.x + x,\n clientY: containerRect.y + y,\n bubbles: true,\n }),\n container\n );\n}\n\n/**\n * Dispatch a `mousemove` event, with x and y defined relative to the container element.\n * @param x The x location of the event, relative to the x location of `container`.\n * @param y The y location of the event, relative to the y location of `container`.\n * @param container The DOM element for relative location of the event.\n */\nexport async function mouseMove(x: number, y: number, container: Element) {\n await dispatchMouseEvent(\"mousemove\", x, y, container);\n}\n\n/**\n * Dispatch a `mouseup` event, with x and y defined relative to the container element.\n * @param x The x location of the event, relative to the x location of `container`.\n * @param y The y location of the event, relative to the y location of `container`.\n * @param container The DOM element for relative location of the event.\n */\nexport async function mouseUp(x: number, y: number, container: Element) {\n await dispatchMouseEvent(\"mouseup\", x, y, container);\n}\n\n/**\n * Dispatch a `mousemove` event, with x and y defined relative to the container element.\n * @param x The x location of the event, relative to the x location of `container`.\n * @param y The y location of the event, relative to the y location of `container`.\n * @param container The DOM element for relative location of the event.\n */\nexport async function mouseDown(x: number, y: number, container: Element) {\n await dispatchMouseEvent(\"mousedown\", x, y, container);\n}\n\n/**\n * Runs the given timeline by calling `jsPsych.run()` on the provided JsPsych object.\n *\n * @param timeline The timeline that is passed to `jsPsych.run()`\n * @param jsPsych The jsPsych instance to be used. If left empty, a new instance will be created. If\n * a settings object is passed instead, the settings will be used to create the jsPsych instance.\n *\n * @returns An object containing test helper functions, the jsPsych instance, and the jsPsych\n * display element\n */\nexport async function startTimeline(timeline: any[], jsPsych: JsPsych | any = {}) {\n const jsPsychInstance = jsPsych instanceof JsPsych ? jsPsych : new JsPsych(jsPsych);\n\n let hasFinished = false;\n const finished = jsPsychInstance.run(timeline).then(() => {\n hasFinished = true;\n });\n await flushPromises();\n\n const displayElement = jsPsychInstance.getDisplayElement();\n\n return {\n jsPsych: jsPsychInstance,\n displayElement,\n /** Shorthand for `jsPsych.getDisplayElement().innerHTML` */\n getHTML: () => displayElement.innerHTML,\n /** Shorthand for `jsPsych.data.get()` */\n getData: () => jsPsychInstance.data.get(),\n expectFinished: async () => {\n await flushPromises();\n expect(hasFinished).toBe(true);\n },\n expectRunning: async () => {\n await flushPromises();\n expect(hasFinished).toBe(false);\n },\n /** A promise that is resolved when `jsPsych.run()` is done. */\n finished,\n };\n}\n\n/**\n * Runs the given timeline by calling `jsPsych.simulate()` on the provided JsPsych object.\n *\n * @param timeline The timeline that is passed to `jsPsych.run()`\n * @param simulation_mode Either 'data-only' mode or 'visual' mode.\n * @param simulation_options Options to pass to `jsPsych.simulate()`\n * @param jsPsych The jsPsych instance to be used. If left empty, a new instance will be created. If\n * a settings object is passed instead, the settings will be used to create the jsPsych instance.\n *\n * @returns An object containing test helper functions, the jsPsych instance, and the jsPsych\n * display element\n */\nexport async function simulateTimeline(\n timeline: any[],\n simulation_mode?: \"data-only\" | \"visual\",\n simulation_options: any = {},\n jsPsych: JsPsych | any = {}\n) {\n const jsPsychInstance = jsPsych instanceof JsPsych ? jsPsych : new JsPsych(jsPsych);\n\n let hasFinished = false;\n const finished = jsPsychInstance\n .simulate(timeline, simulation_mode, simulation_options)\n .then(() => {\n hasFinished = true;\n });\n await flushPromises();\n\n const displayElement = jsPsychInstance.getDisplayElement();\n\n return {\n jsPsych: jsPsychInstance,\n displayElement,\n /** Shorthand for `jsPsych.getDisplayElement().innerHTML` */\n getHTML: () => displayElement.innerHTML,\n /** Shorthand for `jsPsych.data.get()` */\n getData: () => jsPsychInstance.data.get(),\n expectFinished: async () => {\n await flushPromises();\n expect(hasFinished).toBe(true);\n },\n expectRunning: async () => {\n await flushPromises();\n expect(hasFinished).toBe(false);\n },\n /** A promise that is resolved when `jsPsych.simulate()` is done. */\n finished,\n };\n}\n"],"names":["flushMicroTasks"],"mappings":";;;AAOO,SAAS,aAAgB,GAAA;AAC9B,EAAA,OAAO,IAAI,OAAQ,CAAA,CAAC,OAAY,KAAAA,YAAA,CAAgB,OAAO,CAAC,CAAA,CAAA;AAC1D,CAAA;AAEO,SAAS,aAAc,CAAA,KAAA,EAAc,MAAkB,GAAA,QAAA,CAAS,IAAM,EAAA;AAC3E,EAAA,MAAA,CAAO,cAAc,KAAK,CAAA,CAAA;AAC1B,EAAA,OAAO,aAAc,EAAA,CAAA;AACvB,CAA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AA,IACA,eAAe,YAAY;AACzB,MAAA,MAAM,aAAc,EAAA,CAAA;AACpB,MAAO,MAAA,CAAA,WAAW,CAAE,CAAA,IAAA,CAAK,KAAK,CAAA,CAAA;AAAA,KAChC;AAAA,IAEA,QAAA;AAAA,GACF,CAAA;AACF;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jspsych/test-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Test utility functions for jsPsych-related test cases",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -30,10 +30,11 @@
|
|
|
30
30
|
"url": "https://github.com/jspsych/jsPsych/issues"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"jspsych": ">=7.0.0"
|
|
33
|
+
"jspsych": ">=7.0.0",
|
|
34
|
+
"@types/jest": "*"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
36
|
-
"@jspsych/config": "^
|
|
37
|
-
"jspsych": "^
|
|
37
|
+
"@jspsych/config": "^3.0.0",
|
|
38
|
+
"jspsych": "^8.0.0"
|
|
38
39
|
}
|
|
39
40
|
}
|
package/src/index.ts
CHANGED
|
@@ -2,91 +2,93 @@ import { setImmediate as flushMicroTasks } from "timers";
|
|
|
2
2
|
|
|
3
3
|
import { JsPsych } from "jspsych";
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
/**
|
|
6
|
+
* https://github.com/facebook/jest/issues/2157#issuecomment-279171856
|
|
7
|
+
*/
|
|
8
|
+
export function flushPromises() {
|
|
9
|
+
return new Promise((resolve) => flushMicroTasks(resolve));
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function dispatchEvent(event: Event, target: Element = document.body) {
|
|
13
|
+
target.dispatchEvent(event);
|
|
14
|
+
return flushPromises();
|
|
7
15
|
}
|
|
8
16
|
|
|
9
|
-
export function keyDown(key: string) {
|
|
10
|
-
dispatchEvent(new KeyboardEvent("keydown", { key }));
|
|
17
|
+
export async function keyDown(key: string) {
|
|
18
|
+
await dispatchEvent(new KeyboardEvent("keydown", { key }));
|
|
11
19
|
}
|
|
12
20
|
|
|
13
|
-
export function keyUp(key: string) {
|
|
14
|
-
dispatchEvent(new KeyboardEvent("keyup", { key }));
|
|
21
|
+
export async function keyUp(key: string) {
|
|
22
|
+
await dispatchEvent(new KeyboardEvent("keyup", { key }));
|
|
15
23
|
}
|
|
16
24
|
|
|
17
|
-
export function pressKey(key: string) {
|
|
18
|
-
keyDown(key);
|
|
19
|
-
keyUp(key);
|
|
25
|
+
export async function pressKey(key: string) {
|
|
26
|
+
await keyDown(key);
|
|
27
|
+
await keyUp(key);
|
|
20
28
|
}
|
|
21
29
|
|
|
22
|
-
export function mouseDownMouseUpTarget(target: Element) {
|
|
23
|
-
|
|
24
|
-
|
|
30
|
+
export async function mouseDownMouseUpTarget(target: Element) {
|
|
31
|
+
await dispatchEvent(new MouseEvent("mousedown", { bubbles: true }), target);
|
|
32
|
+
await dispatchEvent(new MouseEvent("mouseup", { bubbles: true }), target);
|
|
25
33
|
}
|
|
26
34
|
|
|
27
|
-
export function clickTarget(target: Element) {
|
|
28
|
-
target
|
|
35
|
+
export async function clickTarget(target: Element) {
|
|
36
|
+
// Check if the target is a form element and if it's disabled
|
|
37
|
+
if (target instanceof HTMLButtonElement || target instanceof HTMLInputElement) {
|
|
38
|
+
if (target.disabled) {
|
|
39
|
+
console.log("Target is disabled, not dispatching click event.");
|
|
40
|
+
return; // Exit the function if the target is disabled
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
await dispatchEvent(new MouseEvent("click", { bubbles: true }), target);
|
|
29
44
|
}
|
|
30
45
|
|
|
31
46
|
/**
|
|
32
|
-
* Dispatch a `
|
|
47
|
+
* Dispatch a `MouseEvent` of type `eventType`, with x and y defined relative to the container element.
|
|
33
48
|
* @param x The x location of the event, relative to the x location of `container`.
|
|
34
49
|
* @param y The y location of the event, relative to the y location of `container`.
|
|
35
50
|
* @param container The DOM element for relative location of the event.
|
|
36
51
|
*/
|
|
37
|
-
|
|
52
|
+
async function dispatchMouseEvent(eventType: string, x: number, y: number, container: Element) {
|
|
38
53
|
const containerRect = container.getBoundingClientRect();
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
54
|
+
await dispatchEvent(
|
|
55
|
+
new MouseEvent(eventType, {
|
|
56
|
+
clientX: containerRect.x + x,
|
|
57
|
+
clientY: containerRect.y + y,
|
|
58
|
+
bubbles: true,
|
|
59
|
+
}),
|
|
60
|
+
container
|
|
61
|
+
);
|
|
47
62
|
}
|
|
48
63
|
|
|
49
64
|
/**
|
|
50
|
-
* Dispatch a `
|
|
65
|
+
* Dispatch a `mousemove` event, with x and y defined relative to the container element.
|
|
51
66
|
* @param x The x location of the event, relative to the x location of `container`.
|
|
52
67
|
* @param y The y location of the event, relative to the y location of `container`.
|
|
53
68
|
* @param container The DOM element for relative location of the event.
|
|
54
69
|
*/
|
|
55
|
-
export function
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const eventInit = {
|
|
59
|
-
clientX: containerRect.x + x,
|
|
60
|
-
clientY: containerRect.y + y,
|
|
61
|
-
bubbles: true,
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
container.dispatchEvent(new MouseEvent("mouseup", eventInit));
|
|
70
|
+
export async function mouseMove(x: number, y: number, container: Element) {
|
|
71
|
+
await dispatchMouseEvent("mousemove", x, y, container);
|
|
65
72
|
}
|
|
66
73
|
|
|
67
74
|
/**
|
|
68
|
-
* Dispatch a `
|
|
75
|
+
* Dispatch a `mouseup` event, with x and y defined relative to the container element.
|
|
69
76
|
* @param x The x location of the event, relative to the x location of `container`.
|
|
70
77
|
* @param y The y location of the event, relative to the y location of `container`.
|
|
71
78
|
* @param container The DOM element for relative location of the event.
|
|
72
79
|
*/
|
|
73
|
-
export function
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const eventInit = {
|
|
77
|
-
clientX: containerRect.x + x,
|
|
78
|
-
clientY: containerRect.y + y,
|
|
79
|
-
bubbles: true,
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
container.dispatchEvent(new MouseEvent("mousedown", eventInit));
|
|
80
|
+
export async function mouseUp(x: number, y: number, container: Element) {
|
|
81
|
+
await dispatchMouseEvent("mouseup", x, y, container);
|
|
83
82
|
}
|
|
84
83
|
|
|
85
84
|
/**
|
|
86
|
-
*
|
|
85
|
+
* Dispatch a `mousemove` event, with x and y defined relative to the container element.
|
|
86
|
+
* @param x The x location of the event, relative to the x location of `container`.
|
|
87
|
+
* @param y The y location of the event, relative to the y location of `container`.
|
|
88
|
+
* @param container The DOM element for relative location of the event.
|
|
87
89
|
*/
|
|
88
|
-
export function
|
|
89
|
-
|
|
90
|
+
export async function mouseDown(x: number, y: number, container: Element) {
|
|
91
|
+
await dispatchMouseEvent("mousedown", x, y, container);
|
|
90
92
|
}
|
|
91
93
|
|
|
92
94
|
/**
|