jsPsych/packages/jspsych/tests/core/css-classes-parameter.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

88 lines
2.5 KiB
TypeScript

import htmlKeyboardResponse from "@jspsych/plugin-html-keyboard-response";
import { initJsPsych } from "../../src";
import { pressKey, startTimeline } from "../utils";
describe("The css_classes parameter for trials", () => {
test("Adds a single CSS class to the root jsPsych element", async () => {
const { displayElement } = await startTimeline([
{ type: htmlKeyboardResponse, stimulus: "<p>foo</p>", css_classes: ["foo"] },
]);
expect(displayElement.classList).toContain("foo");
});
test("Gracefully handles single class when not in array", async () => {
const { displayElement } = await startTimeline([
{ type: htmlKeyboardResponse, stimulus: "<p>foo</p>", css_classes: "foo" },
]);
expect(displayElement.classList).toContain("foo");
});
test("Removes the added classes at the end of the trial", async () => {
const { displayElement } = await startTimeline([
{ type: htmlKeyboardResponse, stimulus: "<p>foo</p>", css_classes: ["foo"] },
]);
expect(displayElement.classList).toContain("foo");
pressKey("a");
expect(displayElement.classList).not.toContain("foo");
});
test("Class inherits in nested timelines", async () => {
const { displayElement } = await startTimeline([
{
timeline: [
{
type: htmlKeyboardResponse,
stimulus: "<p>foo</p>",
},
],
css_classes: ["foo"],
},
]);
expect(displayElement.classList).toContain("foo");
pressKey("a");
expect(displayElement.classList).not.toContain("foo");
});
test("Parameter works when defined as a function", async () => {
const { displayElement } = await startTimeline([
{
type: htmlKeyboardResponse,
stimulus: "<p>foo</p>",
css_classes: () => ["foo"],
},
]);
expect(displayElement.classList).toContain("foo");
pressKey("a");
expect(displayElement.classList).not.toContain("foo");
});
test("Parameter works when defined as a timeline variable", async () => {
const jsPsych = initJsPsych();
const { displayElement } = await startTimeline(
[
{
timeline: [
{
type: htmlKeyboardResponse,
stimulus: "<p>foo</p>",
css_classes: jsPsych.timelineVariable("css"),
},
],
timeline_variables: [{ css: ["foo"] }],
},
],
jsPsych
);
expect(displayElement.classList).toContain("foo");
pressKey("a");
expect(displayElement.classList).not.toContain("foo");
});
});