Skip to content

Commit

Permalink
Leave a clean DOM after each survey-text plugin test case
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoluc committed Sep 5, 2023
1 parent 3d3299f commit 3ce3bdc
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions packages/plugin-survey-text/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { clickTarget, simulateTimeline, startTimeline } from "@jspsych/test-util

import surveyText from ".";

const selectInput = (selector: string) => document.querySelector<HTMLInputElement>(selector);
const selectInput = (inputId: number) =>
document.querySelector<HTMLInputElement>(`#input-${inputId}`);

jest.useFakeTimers();

Expand All @@ -16,8 +17,8 @@ describe("survey-text plugin", () => {
]);

expect(displayElement.querySelectorAll("p.jspsych-survey-text").length).toBe(2);
expect(selectInput("#input-0").size).toBe(40);
expect(selectInput("#input-1").size).toBe(40);
expect(selectInput(0).size).toBe(40);
expect(selectInput(1).size).toBe(40);

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

Expand All @@ -36,16 +37,16 @@ describe("survey-text plugin", () => {
]);

expect(displayElement.querySelectorAll("p.jspsych-survey-text").length).toBe(2);
expect(selectInput("#input-0").size).toBe(50);
expect(selectInput("#input-1").size).toBe(20);
expect(selectInput(0).size).toBe(50);
expect(selectInput(1).size).toBe(20);

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

await expectFinished();
});

test("required parameter works", async () => {
const { displayElement } = await startTimeline([
const { displayElement, expectFinished } = await startTimeline([
{
type: surveyText,
questions: [
Expand All @@ -56,8 +57,12 @@ describe("survey-text plugin", () => {
]);

expect(displayElement.querySelectorAll("p.jspsych-survey-text").length).toBe(2);
expect(selectInput("#input-0").required).toBe(true);
expect(selectInput("#input-1").required).toBe(false);
expect(selectInput(0).required).toBe(true);
expect(selectInput(1).required).toBe(false);

selectInput(0).value = "42";
await clickTarget(document.querySelector("#jspsych-survey-text-next"));
await expectFinished();
});

test("data are logged with the right question when randomize order is true", async () => {
Expand All @@ -75,11 +80,11 @@ describe("survey-text plugin", () => {
},
]);

selectInput("#input-0").value = "a0";
selectInput("#input-1").value = "a1";
selectInput("#input-2").value = "a2";
selectInput("#input-3").value = "a3";
selectInput("#input-4").value = "a4";
selectInput(0).value = "a0";
selectInput(1).value = "a1";
selectInput(2).value = "a2";
selectInput(3).value = "a3";
selectInput(4).value = "a4";

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

Expand Down

0 comments on commit 3ce3bdc

Please sign in to comment.