@jspsych/test-utils 1.1.1 → 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/README.md CHANGED
@@ -1 +1,3 @@
1
1
  # Test utility functions for jsPsych-related test cases
2
+
3
+ These functions are used to assist in testing jsPsych plugins.
package/dist/index.cjs CHANGED
@@ -1,187 +1,105 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var timers = require('timers');
3
+ var node_timers = require('node:timers');
6
4
  var jspsych = require('jspsych');
7
5
 
8
- /*! *****************************************************************************
9
- Copyright (c) Microsoft Corporation.
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
- function dispatchEvent(event) {
34
- document.body.dispatchEvent(event);
35
- }
36
- function keyDown(key) {
37
- dispatchEvent(new KeyboardEvent("keydown", { key }));
38
- }
39
- function keyUp(key) {
40
- dispatchEvent(new KeyboardEvent("keyup", { key }));
41
- }
42
- function pressKey(key) {
43
- keyDown(key);
44
- keyUp(key);
45
- }
46
- function mouseDownMouseUpTarget(target) {
47
- target.dispatchEvent(new MouseEvent("mousedown", { bubbles: true }));
48
- target.dispatchEvent(new MouseEvent("mouseup", { bubbles: true }));
49
- }
50
- function clickTarget(target) {
51
- target.dispatchEvent(new MouseEvent("click", { bubbles: true }));
52
- }
53
- /**
54
- * Dispatch a `mousemove` event, with x and y defined relative to the container element.
55
- * @param x The x location of the event, relative to the x location of `container`.
56
- * @param y The y location of the event, relative to the y location of `container`.
57
- * @param container The DOM element for relative location of the event.
58
- */
59
- function mouseMove(x, y, container) {
60
- const containerRect = container.getBoundingClientRect();
61
- const eventInit = {
62
- clientX: containerRect.x + x,
63
- clientY: containerRect.y + y,
64
- bubbles: true,
65
- };
66
- container.dispatchEvent(new MouseEvent("mousemove", eventInit));
67
- }
68
- /**
69
- * Dispatch a `mouseup` event, with x and y defined relative to the container element.
70
- * @param x The x location of the event, relative to the x location of `container`.
71
- * @param y The y location of the event, relative to the y location of `container`.
72
- * @param container The DOM element for relative location of the event.
73
- */
74
- function mouseUp(x, y, container) {
75
- const containerRect = container.getBoundingClientRect();
76
- const eventInit = {
77
- clientX: containerRect.x + x,
78
- clientY: containerRect.y + y,
79
- bubbles: true,
80
- };
81
- container.dispatchEvent(new MouseEvent("mouseup", eventInit));
82
- }
83
- /**
84
- * Dispatch a `mousemove` event, with x and y defined relative to the container element.
85
- * @param x The x location of the event, relative to the x location of `container`.
86
- * @param y The y location of the event, relative to the y location of `container`.
87
- * @param container The DOM element for relative location of the event.
88
- */
89
- function mouseDown(x, y, container) {
90
- const containerRect = container.getBoundingClientRect();
91
- const eventInit = {
92
- clientX: containerRect.x + x,
93
- clientY: containerRect.y + y,
94
- bubbles: true,
95
- };
96
- container.dispatchEvent(new MouseEvent("mousedown", eventInit));
97
- }
98
- /**
99
- * https://github.com/facebook/jest/issues/2157#issuecomment-279171856
100
- */
101
- function flushPromises() {
102
- return new Promise((resolve) => timers.setImmediate(resolve));
103
- }
104
- /**
105
- * Runs the given timeline by calling `jsPsych.run()` on the provided JsPsych object.
106
- *
107
- * @param timeline The timeline that is passed to `jsPsych.run()`
108
- * @param jsPsych The jsPsych instance to be used. If left empty, a new instance will be created. If
109
- * a settings object is passed instead, the settings will be used to create the jsPsych instance.
110
- *
111
- * @returns An object containing test helper functions, the jsPsych instance, and the jsPsych
112
- * display element
113
- */
114
- function startTimeline(timeline, jsPsych = {}) {
115
- return __awaiter(this, void 0, void 0, function* () {
116
- const jsPsychInstance = jsPsych instanceof jspsych.JsPsych ? jsPsych : new jspsych.JsPsych(jsPsych);
117
- let hasFinished = false;
118
- const finished = jsPsychInstance.run(timeline).then(() => {
119
- hasFinished = true;
120
- });
121
- yield flushPromises();
122
- const displayElement = jsPsychInstance.getDisplayElement();
123
- return {
124
- jsPsych: jsPsychInstance,
125
- displayElement,
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;
@@ -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 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });\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","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,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CAAC,CAAC;QAEpF,IAAI,WAAW,GAAG,KAAK,CAAC;QACxB,MAAM,QAAQ,GAAG,eAAe;aAC7B,QAAQ,CAAC,QAAQ,EAAE,eAAe,EAAE,kBAAkB,CAAC;aACvD,IAAI,CAAC;YACJ,WAAW,GAAG,IAAI,CAAC;SACpB,CAAC,CAAC;QACL,MAAM,aAAa,EAAE,CAAC;QAEtB,MAAM,cAAc,GAAG,eAAe,CAAC,iBAAiB,EAAE,CAAC;QAE3D,OAAO;YACL,OAAO,EAAE,eAAe;YACxB,cAAc;;YAEd,OAAO,EAAE,MAAM,cAAc,CAAC,SAAS;;YAEvC,OAAO,EAAE,MAAM,eAAe,CAAC,IAAI,CAAC,GAAG,EAAE;YACzC,cAAc,EAAE;gBACd,MAAM,aAAa,EAAE,CAAC;gBACtB,MAAM,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;aAChC,CAAA;YACD,aAAa,EAAE;gBACb,MAAM,aAAa,EAAE,CAAC;gBACtB,MAAM,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;aACjC,CAAA;;YAED,QAAQ;SACT,CAAC;KACH;;;;;;;;;;;;;;;;"}
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,EAAE,GAAA,EAAK,CAAC,CAAA,CAAA;AAC3D,CAAA;AAEA,eAAsB,MAAM,GAAa,EAAA;AACvC,EAAA,MAAM,cAAc,IAAI,aAAA,CAAc,SAAS,EAAE,GAAA,EAAK,CAAC,CAAA,CAAA;AACzD,CAAA;AAEA,eAAsB,SAAS,GAAa,EAAA;AAC1C,EAAA,MAAM,QAAQ,GAAG,CAAA,CAAA;AACjB,EAAA,MAAM,MAAM,GAAG,CAAA,CAAA;AACjB,CAAA;AAEA,eAAsB,uBAAuB,MAAiB,EAAA;AAC5D,EAAM,MAAA,aAAA,CAAc,IAAI,UAAW,CAAA,WAAA,EAAa,EAAE,OAAS,EAAA,IAAA,EAAM,CAAA,EAAG,MAAM,CAAA,CAAA;AAC1E,EAAM,MAAA,aAAA,C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A,IAAIA,gBAAQ,OAAO,CAAA,CAAA;AAElF,EAAA,IAAI,WAAc,GAAA,KAAA,CAAA;AAClB,EAAM,MAAA,QAAA,GAAW,gBACd,QAAS,CAAA,QAAA,EAAU,iBAAiB,kBAAkB,CAAA,CACtD,KAAK,MAAM;AACV,IAAc,WAAA,GAAA,IAAA,CAAA;AAAA,GACf,CAAA,CAAA;AACH,EAAA,MAAM,aAAc,EAAA,CAAA;AAEpB,EAAM,MAAA,cAAA,GAAiB,gBAAgB,iBAAkB,EAAA,CAAA;AAEzD,EAAO,OAAA;AAAA,IACL,OAAS,EAAA,eAAA;AAAA,IACT,cAAA;AAAA,IAEA,OAAA,EAAS,MAAM,cAAe,CAAA,SAAA;AAAA,IAE9B,OAAS,EAAA,MAAM,eAAgB,CAAA,IAAA,CAAK,GAAI,EAAA;AAAA,IACxC,gBAAgB,YAAY;AAC1B,MAAA,MAAM,aAAc,EAAA,CAAA;AACpB,MAAO,MAAA,CAAA,WAAW,CAAE,CAAA,IAAA,CAAK,IAAI,CAAA,CAAA;AAAA,KAC/B;AAAA,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/dist/index.d.ts CHANGED
@@ -1,78 +1,82 @@
1
- import { JsPsych } from "jspsych";
2
- export declare function dispatchEvent(event: Event): void;
3
- export declare function keyDown(key: string): void;
4
- export declare function keyUp(key: string): void;
5
- export declare function pressKey(key: string): void;
6
- export declare function mouseDownMouseUpTarget(target: Element): void;
7
- export declare function clickTarget(target: Element): void;
8
- /**
9
- * Dispatch a `mousemove` event, with x and y defined relative to the container element.
10
- * @param x The x location of the event, relative to the x location of `container`.
11
- * @param y The y location of the event, relative to the y location of `container`.
12
- * @param container The DOM element for relative location of the event.
13
- */
14
- export declare function mouseMove(x: number, y: number, container: Element): void;
15
- /**
16
- * Dispatch a `mouseup` event, with x and y defined relative to the container element.
17
- * @param x The x location of the event, relative to the x location of `container`.
18
- * @param y The y location of the event, relative to the y location of `container`.
19
- * @param container The DOM element for relative location of the event.
20
- */
21
- export declare function mouseUp(x: number, y: number, container: Element): void;
22
- /**
23
- * Dispatch a `mousemove` event, with x and y defined relative to the container element.
24
- * @param x The x location of the event, relative to the x location of `container`.
25
- * @param y The y location of the event, relative to the y location of `container`.
26
- * @param container The DOM element for relative location of the event.
27
- */
28
- export declare function mouseDown(x: number, y: number, container: Element): void;
29
- /**
30
- * https://github.com/facebook/jest/issues/2157#issuecomment-279171856
31
- */
32
- export declare function flushPromises(): Promise<unknown>;
33
- /**
34
- * Runs the given timeline by calling `jsPsych.run()` on the provided JsPsych object.
35
- *
36
- * @param timeline The timeline that is passed to `jsPsych.run()`
37
- * @param jsPsych The jsPsych instance to be used. If left empty, a new instance will be created. If
38
- * a settings object is passed instead, the settings will be used to create the jsPsych instance.
39
- *
40
- * @returns An object containing test helper functions, the jsPsych instance, and the jsPsych
41
- * display element
42
- */
43
- export declare function startTimeline(timeline: any[], jsPsych?: JsPsych | any): Promise<{
44
- jsPsych: JsPsych;
45
- displayElement: HTMLElement;
46
- /** Shorthand for `jsPsych.getDisplayElement().innerHTML` */
47
- getHTML: () => string;
48
- /** Shorthand for `jsPsych.data.get()` */
49
- getData: () => import("jspsych/dist/modules/data/DataCollection").DataCollection;
50
- expectFinished: () => Promise<void>;
51
- expectRunning: () => Promise<void>;
52
- /** A promise that is resolved when `jsPsych.run()` is done. */
53
- finished: Promise<void>;
54
- }>;
55
- /**
56
- * Runs the given timeline by calling `jsPsych.simulate()` on the provided JsPsych object.
57
- *
58
- * @param timeline The timeline that is passed to `jsPsych.run()`
59
- * @param simulation_mode Either 'data-only' mode or 'visual' mode.
60
- * @param simulation_options Options to pass to `jsPsych.simulate()`
61
- * @param jsPsych The jsPsych instance to be used. If left empty, a new instance will be created. If
62
- * a settings object is passed instead, the settings will be used to create the jsPsych instance.
63
- *
64
- * @returns An object containing test helper functions, the jsPsych instance, and the jsPsych
65
- * display element
66
- */
67
- export declare function simulateTimeline(timeline: any[], simulation_mode?: "data-only" | "visual", simulation_options?: any, jsPsych?: JsPsych | any): Promise<{
68
- jsPsych: JsPsych;
69
- displayElement: HTMLElement;
70
- /** Shorthand for `jsPsych.getDisplayElement().innerHTML` */
71
- getHTML: () => string;
72
- /** Shorthand for `jsPsych.data.get()` */
73
- getData: () => import("jspsych/dist/modules/data/DataCollection").DataCollection;
74
- expectFinished: () => Promise<void>;
75
- expectRunning: () => Promise<void>;
76
- /** A promise that is resolved when `jsPsych.simulate()` is done. */
77
- finished: Promise<void>;
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
- Copyright (c) Microsoft Corporation.
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
- function dispatchEvent(event) {
30
- document.body.dispatchEvent(event);
31
- }
32
- function keyDown(key) {
33
- dispatchEvent(new KeyboardEvent("keydown", { key }));
34
- }
35
- function keyUp(key) {
36
- dispatchEvent(new KeyboardEvent("keyup", { key }));
37
- }
38
- function pressKey(key) {
39
- keyDown(key);
40
- keyUp(key);
41
- }
42
- function mouseDownMouseUpTarget(target) {
43
- target.dispatchEvent(new MouseEvent("mousedown", { bubbles: true }));
44
- target.dispatchEvent(new MouseEvent("mouseup", { bubbles: true }));
45
- }
46
- function clickTarget(target) {
47
- target.dispatchEvent(new MouseEvent("click", { bubbles: true }));
48
- }
49
- /**
50
- * Dispatch a `mousemove` event, with x and y defined relative to the container element.
51
- * @param x The x location of the event, relative to the x location of `container`.
52
- * @param y The y location of the event, relative to the y location of `container`.
53
- * @param container The DOM element for relative location of the event.
54
- */
55
- function mouseMove(x, y, container) {
56
- const containerRect = container.getBoundingClientRect();
57
- const eventInit = {
58
- clientX: containerRect.x + x,
59
- clientY: containerRect.y + y,
60
- bubbles: true,
61
- };
62
- container.dispatchEvent(new MouseEvent("mousemove", eventInit));
63
- }
64
- /**
65
- * Dispatch a `mouseup` event, with x and y defined relative to the container element.
66
- * @param x The x location of the event, relative to the x location of `container`.
67
- * @param y The y location of the event, relative to the y location of `container`.
68
- * @param container The DOM element for relative location of the event.
69
- */
70
- function mouseUp(x, y, container) {
71
- const containerRect = container.getBoundingClientRect();
72
- const eventInit = {
73
- clientX: containerRect.x + x,
74
- clientY: containerRect.y + y,
75
- bubbles: true,
76
- };
77
- container.dispatchEvent(new MouseEvent("mouseup", eventInit));
78
- }
79
- /**
80
- * Dispatch a `mousemove` event, with x and y defined relative to the container element.
81
- * @param x The x location of the event, relative to the x location of `container`.
82
- * @param y The y location of the event, relative to the y location of `container`.
83
- * @param container The DOM element for relative location of the event.
84
- */
85
- function mouseDown(x, y, container) {
86
- const containerRect = container.getBoundingClientRect();
87
- const eventInit = {
88
- clientX: containerRect.x + x,
89
- clientY: containerRect.y + y,
90
- bubbles: true,
91
- };
92
- container.dispatchEvent(new MouseEvent("mousedown", eventInit));
93
- }
94
- /**
95
- * https://github.com/facebook/jest/issues/2157#issuecomment-279171856
96
- */
97
- function flushPromises() {
98
- return new Promise((resolve) => setImmediate(resolve));
99
- }
100
- /**
101
- * Runs the given timeline by calling `jsPsych.run()` on the provided JsPsych object.
102
- *
103
- * @param timeline The timeline that is passed to `jsPsych.run()`
104
- * @param jsPsych The jsPsych instance to be used. If left empty, a new instance will be created. If
105
- * a settings object is passed instead, the settings will be used to create the jsPsych instance.
106
- *
107
- * @returns An object containing test helper functions, the jsPsych instance, and the jsPsych
108
- * display element
109
- */
110
- function startTimeline(timeline, jsPsych = {}) {
111
- return __awaiter(this, void 0, void 0, function* () {
112
- const jsPsychInstance = jsPsych instanceof JsPsych ? jsPsych : new JsPsych(jsPsych);
113
- let hasFinished = false;
114
- const finished = jsPsychInstance.run(timeline).then(() => {
115
- hasFinished = true;
116
- });
117
- yield flushPromises();
118
- const displayElement = jsPsychInstance.getDisplayElement();
119
- return {
120
- jsPsych: jsPsychInstance,
121
- displayElement,
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 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });\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","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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G,OAAO,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;QAEpF,IAAI,WAAW,GAAG,KAAK,CAAC;QACxB,MAAM,QAAQ,GAAG,eAAe;aAC7B,QAAQ,CAAC,QAAQ,EAAE,eAAe,EAAE,kBAAkB,CAAC;aACvD,IAAI,CAAC;YACJ,WAAW,GAAG,IAAI,CAAC;SACpB,CAAC,CAAC;QACL,MAAM,aAAa,EAAE,CAAC;QAEtB,MAAM,cAAc,GAAG,eAAe,CAAC,iBAAiB,EAAE,CAAC;QAE3D,OAAO;YACL,OAAO,EAAE,eAAe;YACxB,cAAc;;YAEd,OAAO,EAAE,MAAM,cAAc,CAAC,SAAS;;YAEvC,OAAO,EAAE,MAAM,eAAe,CAAC,IAAI,CAAC,GAAG,EAAE;YACzC,cAAc,EAAE;gBACd,MAAM,aAAa,EAAE,CAAC;gBACtB,MAAM,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;aAChC,CAAA;YACD,aAAa,EAAE;gBACb,MAAM,aAAa,EAAE,CAAC;gBACtB,MAAM,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;aACjC,CAAA;;YAED,QAAQ;SACT,CAAC;KACH;;;;;"}
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,CAAA;AAEA,eAAsB,QAAQ,GAAa,EAAA;AACzC,EAAA,MAAM,cAAc,IAAI,aAAA,CAAc,WAAW,EAAE,GAAA,EAAK,CAAC,CAAA,CAAA;AAC3D,CAAA;AAEA,eAAsB,MAAM,GAAa,EAAA;AACvC,EAAA,MAAM,cAAc,IAAI,aAAA,CAAc,SAAS,EAAE,GAAA,EAAK,CAAC,CAAA,CAAA;AACzD,CAAA;AAEA,eAAsB,SAAS,GAAa,EAAA;AAC1C,EAAA,MAAM,QAAQ,GAAG,CAAA,CAAA;AACjB,EAAA,MAAM,MAAM,GAAG,CAAA,CAAA;AACjB,CAAA;AAEA,eAAsB,uBAAuB,MAAiB,EAAA;AAC5D,EAAM,MAAA,aAAA,CAAc,IAAI,UAAW,CAAA,WAAA,EAAa,EAAE,OAAS,EAAA,IAAA,EAAM,CAAA,EAAG,MAAM,CAAA,CAAA;AAC1E,EAAM,M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AU,GAAA,IAAI,QAAQ,OAAO,CAAA,CAAA;AAElF,EAAA,IAAI,WAAc,GAAA,KAAA,CAAA;AAClB,EAAM,MAAA,QAAA,GAAW,gBACd,QAAS,CAAA,QAAA,EAAU,iBAAiB,kBAAkB,CAAA,CACtD,KAAK,MAAM;AACV,IAAc,WAAA,GAAA,IAAA,CAAA;AAAA,GACf,CAAA,CAAA;AACH,EAAA,MAAM,aAAc,EAAA,CAAA;AAEpB,EAAM,MAAA,cAAA,GAAiB,gBAAgB,iBAAkB,EAAA,CAAA;AAEzD,EAAO,OAAA;AAAA,IACL,OAAS,EAAA,eAAA;AAAA,IACT,cAAA;AAAA,IAEA,OAAA,EAAS,MAAM,cAAe,CAAA,SAAA;AAAA,IAE9B,OAAS,EAAA,MAAM,eAAgB,CAAA,IAAA,CAAK,GAAI,EAAA;AAAA,IACxC,gBAAgB,YAAY;AAC1B,MAAA,MAAM,aAAc,EAAA,CAAA;AACpB,MAAO,MAAA,CAAA,WAAW,CAAE,CAAA,IAAA,CAAK,IAAI,CAAA,CAAA;AAAA,KAC/B;AAAA,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.1.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,9 +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": "^1.1.0"
37
+ "@jspsych/config": "^3.0.0",
38
+ "jspsych": "^8.0.0"
37
39
  }
38
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
- export function dispatchEvent(event: Event) {
6
- document.body.dispatchEvent(event);
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
- target.dispatchEvent(new MouseEvent("mousedown", { bubbles: true }));
24
- target.dispatchEvent(new MouseEvent("mouseup", { bubbles: true }));
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.dispatchEvent(new MouseEvent("click", { bubbles: true }));
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 `mousemove` event, with x and y defined relative to the container element.
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
- export function mouseMove(x: number, y: number, container: Element) {
52
+ async function dispatchMouseEvent(eventType: string, x: number, y: number, container: Element) {
38
53
  const containerRect = container.getBoundingClientRect();
39
-
40
- const eventInit = {
41
- clientX: containerRect.x + x,
42
- clientY: containerRect.y + y,
43
- bubbles: true,
44
- };
45
-
46
- container.dispatchEvent(new MouseEvent("mousemove", eventInit));
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 `mouseup` event, with x and y defined relative to the container element.
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 mouseUp(x: number, y: number, container: Element) {
56
- const containerRect = container.getBoundingClientRect();
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 `mousemove` event, with x and y defined relative to the container element.
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 mouseDown(x: number, y: number, container: Element) {
74
- const containerRect = container.getBoundingClientRect();
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
- * https://github.com/facebook/jest/issues/2157#issuecomment-279171856
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 flushPromises() {
89
- return new Promise((resolve) => flushMicroTasks(resolve));
90
+ export async function mouseDown(x: number, y: number, container: Element) {
91
+ await dispatchMouseEvent("mousedown", x, y, container);
90
92
  }
91
93
 
92
94
  /**