Skip to content

Commit

Permalink
Merge pull request #3486 from jspsych/standardize-tests
Browse files Browse the repository at this point in the history
standardize use of `displayElement` in tests
  • Loading branch information
jodeleeuw authored Jan 10, 2025
2 parents f87d104 + 0b78155 commit a757ab8
Show file tree
Hide file tree
Showing 31 changed files with 258 additions and 230 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-ads-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@jspsych/config": patch
---

remove DOM clearing after each individual test, fixes issues with testing in other repositories
3 changes: 1 addition & 2 deletions packages/config/jest.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ module.exports.makePackageConfig = (dirname) => {
displayName: {
name: packageBaseName,
color: packageBaseName === "jspsych" ? "white" : "cyanBright",
},
setupFilesAfterEnv: ["../config/jest.setup.js"],
}
};
};
3 changes: 0 additions & 3 deletions packages/config/jest.setup.js

This file was deleted.

4 changes: 2 additions & 2 deletions packages/jspsych/src/timeline/Timeline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,10 +586,10 @@ describe("Timeline", () => {
const variable = new TimelineVariable("x");

await timeline.run();
expect(() => timeline.evaluateTimelineVariable(variable)).toThrowError("");
expect(() => timeline.evaluateTimelineVariable(variable)).toThrow();
expect(() =>
(timeline.children[0] as Timeline).evaluateTimelineVariable(variable)
).toThrowError("");
).toThrow();
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/jspsych/src/timeline/Trial.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ describe("Trial", () => {
);
await expect(
createTrial({ type: TestPlugin, requiredComplexNested: {} }).run()
).rejects.toThrowError('"requiredComplexNested.requiredChild" parameter');
).rejects.toThrow('"requiredComplexNested.requiredChild" parameter');
});

it("errors on missing parameters nested in `COMPLEX` array parameters", async () => {
Expand Down
26 changes: 13 additions & 13 deletions packages/jspsych/tests/core/functions-as-parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("standard use of function as parameter", () => {
test("parameters can be protected from early evaluation using ParameterType.FUNCTION", async () => {
const mock = jest.fn();

await startTimeline([
const { displayElement } = await startTimeline([
{
type: cloze,
text: "%foo%",
Expand All @@ -31,7 +31,7 @@ describe("standard use of function as parameter", () => {
]);

expect(mock).not.toHaveBeenCalled();
await clickTarget(document.querySelector("#finish_cloze_button"));
await clickTarget(displayElement.querySelector("#finish_cloze_button"));
expect(mock).toHaveBeenCalledTimes(1);
});
});
Expand Down Expand Up @@ -76,12 +76,12 @@ describe("nested parameters as functions", () => {
]);

expect(displayElement.querySelectorAll("p.jspsych-survey-text").length).toBe(2);
await clickTarget(document.querySelector("#jspsych-survey-text-next"));
await clickTarget(displayElement.querySelector("#jspsych-survey-text-next"));
await expectFinished();
});

test("nested parameter can be a function", async () => {
const { expectFinished } = await startTimeline([
const { expectFinished, displayElement } = await startTimeline([
{
type: surveyText,
questions: [
Expand All @@ -95,18 +95,18 @@ describe("nested parameters as functions", () => {
},
]);

expect(document.querySelector("#jspsych-survey-text-0 p.jspsych-survey-text").innerHTML).toBe(
expect(displayElement.querySelector("#jspsych-survey-text-0 p.jspsych-survey-text").innerHTML).toBe(
"foo"
);
expect(document.querySelector("#jspsych-survey-text-1 p.jspsych-survey-text").innerHTML).toBe(
expect(displayElement.querySelector("#jspsych-survey-text-1 p.jspsych-survey-text").innerHTML).toBe(
"bar"
);
await clickTarget(document.querySelector("#jspsych-survey-text-next"));
await clickTarget(displayElement.querySelector("#jspsych-survey-text-next"));
await expectFinished();
});

test("multiple nested parameters can be functions", async () => {
const { expectFinished } = await startTimeline([
const { expectFinished, displayElement } = await startTimeline([
{
type: surveyMultiChoice,
questions: [
Expand All @@ -128,11 +128,11 @@ describe("nested parameters as functions", () => {
},
]);

expect(document.querySelector("#jspsych-survey-multi-choice-0").innerHTML).toMatch("foo");
expect(document.querySelector("#jspsych-survey-multi-choice-0").innerHTML).toMatch("buzz");
expect(document.querySelector("#jspsych-survey-multi-choice-1").innerHTML).toMatch("bar");
expect(document.querySelector("#jspsych-survey-multi-choice-1").innerHTML).toMatch("one");
await clickTarget(document.querySelector("#jspsych-survey-multi-choice-next"));
expect(displayElement.querySelector("#jspsych-survey-multi-choice-0").innerHTML).toMatch("foo");
expect(displayElement.querySelector("#jspsych-survey-multi-choice-0").innerHTML).toMatch("buzz");
expect(displayElement.querySelector("#jspsych-survey-multi-choice-1").innerHTML).toMatch("bar");
expect(displayElement.querySelector("#jspsych-survey-multi-choice-1").innerHTML).toMatch("one");
await clickTarget(displayElement.querySelector("#jspsych-survey-multi-choice-next"));
await expectFinished();
});

Expand Down
36 changes: 22 additions & 14 deletions packages/jspsych/tests/core/progressbar.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import htmlKeyboardResponse from "@jspsych/plugin-html-keyboard-response";
import { pressKey, startTimeline } from "@jspsych/test-utils";

import { initJsPsych } from "../../src";
import { initJsPsych, JsPsych } from "../../src";

describe("automatic progress bar", () => {
test("progress bar does not display by default", async () => {
await startTimeline([
const { jsPsych } = await startTimeline([
{
type: htmlKeyboardResponse,
stimulus: "foo",
},
]);

expect(document.querySelector("#jspsych-progressbar-container")).toBeNull();
expect(jsPsych.getDisplayContainerElement().querySelector("#jspsych-progressbar-container"))
.toBeNull();
await pressKey("a");
});

test("progress bar displays when show_progress_bar is true", async () => {
await startTimeline(
const { jsPsych } = await startTimeline(
[
{
type: htmlKeyboardResponse,
Expand All @@ -27,9 +28,10 @@ describe("automatic progress bar", () => {
{ show_progress_bar: true }
);

expect(document.querySelector("#jspsych-progressbar-container").innerHTML).toMatch(
'<span>Completion Progress</span><div id="jspsych-progressbar-outer"><div id="jspsych-progressbar-inner" style="width: 0%;"></div></div>'
);
expect(jsPsych.getDisplayContainerElement().querySelector("#jspsych-progressbar-container").innerHTML)
.toMatch(
'<span>Completion Progress</span><div id="jspsych-progressbar-outer"><div id="jspsych-progressbar-inner" style="width: 0%;"></div></div>'
);
});

test("progress bar automatically updates by default", async () => {
Expand All @@ -38,9 +40,13 @@ describe("automatic progress bar", () => {
stimulus: "foo",
};

await startTimeline([trial, trial, trial, trial], { show_progress_bar: true });
const { jsPsych } = await startTimeline(
[trial, trial, trial, trial],
{ show_progress_bar: true }
);

const progressbarElement = document.querySelector<HTMLElement>("#jspsych-progressbar-inner");
const progressbarElement =
jsPsych.getDisplayContainerElement().querySelector<HTMLElement>("#jspsych-progressbar-inner");

expect(progressbarElement.style.width).toEqual("0%");
await pressKey("a");
Expand All @@ -59,12 +65,13 @@ describe("automatic progress bar", () => {
stimulus: "foo",
};

await startTimeline([trial, trial, trial, trial], {
const { jsPsych } = await startTimeline([trial, trial, trial, trial], {
show_progress_bar: true,
auto_update_progress_bar: false,
});

const progressbarElement = document.querySelector<HTMLElement>("#jspsych-progressbar-inner");
const progressbarElement =
jsPsych.getDisplayContainerElement().querySelector<HTMLElement>("#jspsych-progressbar-inner");

for (let i = 0; i < 4; i++) {
expect(progressbarElement.style.width).toEqual("0%");
Expand All @@ -74,7 +81,7 @@ describe("automatic progress bar", () => {
});

test("set `progressBar.progress` manually", async () => {
const jsPsych = initJsPsych({
const jsPsychObject = initJsPsych({
show_progress_bar: true,
auto_update_progress_bar: false,
});
Expand All @@ -96,9 +103,10 @@ describe("automatic progress bar", () => {
},
];

await startTimeline(timeline, jsPsych);
const { jsPsych } = await startTimeline(timeline, jsPsychObject);

const progressbarElement = document.querySelector<HTMLElement>("#jspsych-progressbar-inner");
const progressbarElement =
jsPsych.getDisplayContainerElement().querySelector<HTMLElement>("#jspsych-progressbar-inner");

expect(progressbarElement.style.width).toEqual("0%");
await pressKey("a");
Expand Down
9 changes: 4 additions & 5 deletions packages/jspsych/tests/core/timeline-variables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,7 @@ describe("timeline variables are correctly evaluated", () => {
});
});

// using console.warn instead of error for now. plan is to enable this test with version 8.
test.skip("timelineVariable() throws an error when variable doesn't exist", async () => {
test("throws an error when variable doesn't exist", async () => {
const jsPsych = initJsPsych();
const { expectFinished } = await startTimeline(
[
Expand All @@ -411,7 +410,7 @@ test.skip("timelineVariable() throws an error when variable doesn't exist", asyn
type: htmlKeyboardResponse,
stimulus: "foo",
on_start: () => {
expect(() => jsPsych.evaluateTimelineVariable("c")).toThrowError();
expect(() => jsPsych.evaluateTimelineVariable("c")).toThrow();
},
},
],
Expand All @@ -430,7 +429,7 @@ test.skip("timelineVariable() throws an error when variable doesn't exist", asyn
await expectFinished();
});

test("timelineVariable() can fetch a variable called 'data'", async () => {
test("can fetch a variable called 'data'", async () => {
const jsPsych = initJsPsych();
const { expectFinished } = await startTimeline(
[
Expand All @@ -440,7 +439,7 @@ test("timelineVariable() can fetch a variable called 'data'", async () => {
type: htmlKeyboardResponse,
stimulus: "foo",
on_start: () => {
expect(() => jsPsych.evaluateTimelineVariable("data")).not.toThrowError();
expect(() => jsPsych.evaluateTimelineVariable("data")).not.toThrow();
},
},
],
Expand Down
16 changes: 8 additions & 8 deletions packages/jspsych/tests/data/data-csv-conversion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ jest.useFakeTimers();

describe("data conversion to csv", () => {
test("survey-text data response object is correctly converted", async () => {
const { getData } = await startTimeline([
const { getData, displayElement } = await startTimeline([
{
type: surveyText,
questions: [{ prompt: "Q1" }, { prompt: "Q2" }],
},
]);

document.querySelector<HTMLInputElement>("#input-0").value = "Response 1";
document.querySelector<HTMLInputElement>("#input-1").value = "Response 2";
displayElement.querySelector<HTMLInputElement>("#input-0").value = "Response 1";
displayElement.querySelector<HTMLInputElement>("#input-1").value = "Response 2";

await clickTarget(document.querySelector("#jspsych-survey-text-next"));
await clickTarget(displayElement.querySelector("#jspsych-survey-text-next"));

expect(
getData()
Expand Down Expand Up @@ -62,17 +62,17 @@ describe("data conversion to csv", () => {
});

test("survey-multi-select response array is correctly converted", async () => {
const { getHTML, getData } = await startTimeline([
const { getHTML, getData, displayElement } = await startTimeline([
{
type: surveyMultiSelect,
questions: [{ prompt: "foo", options: ["fuzz", "bizz", "bar"], name: "q" }],
},
]);

expect(getHTML()).toMatch("foo");
await clickTarget(document.querySelector("#jspsych-survey-multi-select-response-0-0"));
await clickTarget(document.querySelector("#jspsych-survey-multi-select-response-0-1"));
await clickTarget(document.querySelector("#jspsych-survey-multi-select-next"));
await clickTarget(displayElement.querySelector("#jspsych-survey-multi-select-response-0-0"));
await clickTarget(displayElement.querySelector("#jspsych-survey-multi-select-response-0-1"));
await clickTarget(displayElement.querySelector("#jspsych-survey-multi-select-next"));
expect(getHTML()).toBe("");

expect(
Expand Down
16 changes: 8 additions & 8 deletions packages/jspsych/tests/data/data-json-conversion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ jest.useFakeTimers();

describe("data conversion to json", () => {
test("survey-text data response object is correctly converted", async () => {
const { getData } = await startTimeline([
const { getData, displayElement } = await startTimeline([
{
type: surveyText,
questions: [{ prompt: "Q1" }, { prompt: "Q2" }],
},
]);

document.querySelector<HTMLInputElement>("#input-0").value = "Response 1";
document.querySelector<HTMLInputElement>("#input-1").value = "Response 2";
displayElement.querySelector<HTMLInputElement>("#input-0").value = "Response 1";
displayElement.querySelector<HTMLInputElement>("#input-1").value = "Response 2";

await clickTarget(document.querySelector("#jspsych-survey-text-next"));
await clickTarget(displayElement.querySelector("#jspsych-survey-text-next"));

expect(
getData()
Expand Down Expand Up @@ -71,17 +71,17 @@ describe("data conversion to json", () => {
});

test("survey-multi-select response array is correctly converted", async () => {
const { getHTML, getData } = await startTimeline([
const { getHTML, getData, displayElement } = await startTimeline([
{
type: surveyMultiSelect,
questions: [{ prompt: "foo", options: ["fuzz", "bizz", "bar"], name: "q" }],
},
]);

expect(getHTML()).toMatch("foo");
await clickTarget(document.querySelector("#jspsych-survey-multi-select-response-0-0"));
await clickTarget(document.querySelector("#jspsych-survey-multi-select-response-0-1"));
await clickTarget(document.querySelector("#jspsych-survey-multi-select-next"));
await clickTarget(displayElement.querySelector("#jspsych-survey-multi-select-response-0-0"));
await clickTarget(displayElement.querySelector("#jspsych-survey-multi-select-response-0-1"));
await clickTarget(displayElement.querySelector("#jspsych-survey-multi-select-next"));
expect(getHTML()).toBe("");

expect(
Expand Down
8 changes: 4 additions & 4 deletions packages/jspsych/tests/data/trialparameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe("Trial parameters in the data", () => {
test("Arrayed objects work with save_trial_parameters ", async () => {
const questions = [{ prompt: "foo" }, { prompt: "bar" }];

const { getData } = await startTimeline([
const { getData, displayElement } = await startTimeline([
{
type: surveyText,
questions,
Expand All @@ -75,7 +75,7 @@ describe("Trial parameters in the data", () => {
},
]);

await clickTarget(document.querySelector("#jspsych-survey-text-next"));
await clickTarget(displayElement.querySelector("#jspsych-survey-text-next"));

const data = getData().values()[0];
expect(data.questions[0].prompt).toBe(questions[0].prompt);
Expand All @@ -96,7 +96,7 @@ describe("Trial parameters in the data", () => {
return html;
};

const { getData } = await startTimeline([
const { getData, displayElement } = await startTimeline([
{
type: reconstruction,
stim_function: sample_function,
Expand All @@ -107,7 +107,7 @@ describe("Trial parameters in the data", () => {
},
]);

await clickTarget(document.querySelector("button"));
await clickTarget(displayElement.querySelector("button"));

expect(getData().values()[0].stim_function).toBe(sample_function.toString());
});
Expand Down
2 changes: 1 addition & 1 deletion packages/jspsych/tests/randomization/randomization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ describe("randomInt", () => {
test("setting upper < lower throws an error", () => {
expect(() => {
randomInt(1, 0);
}).toThrowError();
}).toThrow();
});
});

Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-audio-button-response/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,9 @@ describe("audio-button-response", () => {
use_webaudio: false,
});

await startTimeline(timeline, jsPsych);
const { displayElement } = await startTimeline(timeline, jsPsych);

const btns = document.querySelectorAll(".jspsych-html-button-response-button button");
const btns = displayElement.querySelectorAll(".jspsych-html-button-response-button button");

for (let i = 0; i < btns.length; i++) {
expect(btns[i].getAttribute("disabled")).toBe(true);
Expand Down
Loading

0 comments on commit a757ab8

Please sign in to comment.