jsPsych/packages/jspsych/tests/core/run.test.ts
bjoluc c119650471 Move experiment execution into jsPsych.run()
The `run()` method takes a timeline and returns a promise that is
resolved when the experiment finishes.
Hence, jsPsych can now be initialized without starting an experiment.
This re-enables usage of `jsPsych.timelineVariable()` in timeline
definitions and repairs exclusion checks and extension loading.
2021-07-19 17:10:37 +02:00

48 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import htmlKeyboardResponse from "@jspsych/plugin-html-keyboard-response";
import { flushPromises, startTimeline } from "../utils";
describe("jsPsych.run()", () => {
beforeEach(() => {
document.body.innerHTML = "";
});
function setReadyState(targetState: "loading" | "complete") {
jest.spyOn(document, "readyState", "get").mockImplementation(() => targetState);
}
function getBodyHTML() {
return document.body.innerHTML;
}
async function init() {
await startTimeline([
{
type: htmlKeyboardResponse,
stimulus: "foo",
},
]);
}
// Currently not implemented we need a way to await promises
it("should delay execution until the document is ready", async () => {
expect(getBodyHTML()).toBe("");
setReadyState("loading");
await init();
expect(getBodyHTML()).toBe("");
// Simulate the document getting ready
setReadyState("complete");
window.dispatchEvent(new Event("load"));
await flushPromises();
expect(getBodyHTML()).not.toBe("");
});
it("should execute immediately when the document is ready", async () => {
// The document is ready by default in jsdom
await init();
expect(getBodyHTML()).not.toBe("");
});
});