import { clickTarget, simulateTimeline, startTimeline } from "@jspsych/test-utils"; import htmlSliderResponse from "."; jest.useFakeTimers(); describe("html-slider-response", () => { test("displays html stimulus", async () => { const { getHTML } = await startTimeline([ { type: htmlSliderResponse, stimulus: "this is html", labels: ["left", "right"], button_label: "button", }, ]); expect(getHTML()).toContain( '
this is html
' ); }); test("displays labels", async () => { const { getHTML } = await startTimeline([ { type: htmlSliderResponse, stimulus: "this is html", labels: ["left", "right"], button_label: "button", }, ]); expect(getHTML()).toContain('left'); expect(getHTML()).toContain('right'); }); test("displays button label", async () => { const { getHTML } = await startTimeline([ { type: htmlSliderResponse, stimulus: "this is html", labels: ["left", "right"], button_label: "button", }, ]); expect(getHTML()).toContain( '' ); }); test("should set min, max and step", async () => { const { displayElement } = await startTimeline([ { type: htmlSliderResponse, stimulus: "this is html", labels: ["left", "right"], min: 2, max: 10, step: 2, button_label: "button", }, ]); const responseElement = displayElement.querySelector( "#jspsych-html-slider-response-response" ); expect(responseElement.min).toBe("2"); expect(responseElement.max).toBe("10"); expect(responseElement.step).toBe("2"); }); test("should append to bottom on stimulus", async () => { const { getHTML } = await startTimeline([ { type: htmlSliderResponse, stimulus: "this is html", labels: ["left", "right"], button_label: "button", prompt: "

This is a prompt

", }, ]); expect(getHTML()).toContain("

This is a prompt

"); }); test("should hide stimulus if stimulus_duration is set", async () => { const { displayElement } = await startTimeline([ { type: htmlSliderResponse, stimulus: "this is html", labels: ["left", "right"], button_label: "button", stimulus_duration: 500, }, ]); const stimulusElement = displayElement.querySelector( "#jspsych-html-slider-response-stimulus" ); expect(stimulusElement.style.visibility).toBe(""); jest.advanceTimersByTime(500); expect(stimulusElement.style.visibility).toBe("hidden"); }); test("should end trial when trial duration is reached", async () => { const { getHTML, expectFinished } = await startTimeline([ { type: htmlSliderResponse, stimulus: "this is html", labels: ["left", "right"], button_label: "button", trial_duration: 500, }, ]); expect(getHTML()).toContain( '
this is html
' ); jest.advanceTimersByTime(500); await expectFinished(); }); test("should end trial when button is clicked", async () => { const { getHTML, expectFinished } = await startTimeline([ { type: htmlSliderResponse, stimulus: "this is html", labels: ["left", "right"], button_label: "button", response_ends_trial: true, }, ]); expect(getHTML()).toContain( '
this is html
' ); clickTarget(document.querySelector("#jspsych-html-slider-response-next")); await expectFinished(); }); }); describe("html-slider-response simulation", () => { test("data-only mode works", async () => { const { getData, expectFinished } = await simulateTimeline([ { type: htmlSliderResponse, stimulus: "this is html", labels: ["left", "right"], button_label: "button", }, ]); await expectFinished(); const data = getData().values()[0]; expect(data.response).toBeGreaterThanOrEqual(0); expect(data.response).toBeLessThanOrEqual(100); expect(data.rt).toBeGreaterThan(0); }); test("data-only mode works", async () => { const { getData, expectRunning, expectFinished } = await simulateTimeline( [ { type: htmlSliderResponse, stimulus: "this is html", labels: ["left", "right"], button_label: "button", }, ], "visual" ); await expectRunning(); jest.runAllTimers(); await expectFinished(); const data = getData().values()[0]; expect(data.response).toBeGreaterThanOrEqual(0); expect(data.response).toBeLessThanOrEqual(100); expect(data.rt).toBeGreaterThan(0); }); });