jsPsych/packages/jspsych/tests/core/endexperiment.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

50 lines
1.2 KiB
TypeScript

import htmlKeyboardResponse from "@jspsych/plugin-html-keyboard-response";
import { initJsPsych } from "../../src";
import { pressKey, startTimeline } from "../utils";
test("works on basic timeline", async () => {
const jsPsych = initJsPsych();
const { getHTML, expectFinished } = await startTimeline(
[
{
type: htmlKeyboardResponse,
stimulus: "trial 1",
on_finish: () => {
jsPsych.endExperiment("the end");
},
},
{
type: htmlKeyboardResponse,
stimulus: "trial 2",
},
],
jsPsych
);
expect(getHTML()).toMatch("trial 1");
pressKey("a");
expect(getHTML()).toMatch("the end");
await expectFinished();
});
test("works with looping timeline (#541)", async () => {
const jsPsych = initJsPsych();
const { getHTML, expectFinished } = await startTimeline(
[
{
timeline: [{ type: htmlKeyboardResponse, stimulus: "trial 1" }],
loop_function: () => {
jsPsych.endExperiment("the end");
},
},
],
jsPsych
);
expect(getHTML()).toMatch("trial 1");
pressKey("a");
expect(getHTML()).toMatch("the end");
await expectFinished();
});