import { clickTarget, simulateTimeline, startTimeline } from "@jspsych/test-utils"; import htmlButtonResponse from "."; jest.useFakeTimers(); describe("html-button-response", () => { test("displays html stimulus", async () => { const { getHTML } = await startTimeline([ { type: htmlButtonResponse, stimulus: "this is html", choices: ["button-choice"], }, ]); expect(getHTML()).toContain( '
this is html
' ); }); test("display button labels", async () => { const { getHTML } = await startTimeline([ { type: htmlButtonResponse, stimulus: "this is html", choices: ["button-choice1", "button-choice2"], }, ]); expect(getHTML()).toContain(''); expect(getHTML()).toContain(''); }); test("display button html", async () => { const { getHTML } = await startTimeline([ { type: htmlButtonResponse, stimulus: "this is html", choices: ["buttonChoice"], button_html: '', }, ]); expect(getHTML()).toContain(''); }); test("display should clear after button click", async () => { const { getHTML, expectFinished } = await startTimeline([ { type: htmlButtonResponse, stimulus: "this is html", choices: ["button-choice"], }, ]); expect(getHTML()).toContain( '
this is html
' ); clickTarget(document.querySelector("#jspsych-html-button-response-button-0")); await expectFinished(); }); test("prompt should append below button", async () => { const { getHTML } = await startTimeline([ { type: htmlButtonResponse, stimulus: "this is html", choices: ["button-choice"], 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: htmlButtonResponse, stimulus: "this is html", choices: ["button-choice"], stimulus_duration: 500, }, ]); const stimulusElement = displayElement.querySelector( "#jspsych-html-button-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: htmlButtonResponse, stimulus: "this is html", choices: ["button-choice"], 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: htmlButtonResponse, stimulus: "this is html", choices: ["button-choice"], response_ends_trial: true, }, ]); expect(getHTML()).toContain( '
this is html
' ); clickTarget(document.querySelector("#jspsych-html-button-response-button-0")); await expectFinished(); }); test("class should have responded when button is clicked", async () => { const { getHTML } = await startTimeline([ { type: htmlButtonResponse, stimulus: "this is html", choices: ["button-choice"], response_ends_trial: false, }, ]); expect(getHTML()).toContain( '
this is html
' ); clickTarget(document.querySelector("#jspsych-html-button-response-button-0")); expect(document.querySelector("#jspsych-html-button-response-stimulus").className).toBe( " responded" ); }); }); describe("html-button-response simulation", () => { test("data mode works", async () => { const timeline = [ { type: htmlButtonResponse, stimulus: "foo", choices: ["a", "b", "c"], }, ]; const { expectFinished, getData } = await simulateTimeline(timeline); await expectFinished(); const response = getData().values()[0].response; expect(getData().values()[0].rt).toBeGreaterThan(0); expect(response).toBeGreaterThanOrEqual(0); expect(response).toBeLessThanOrEqual(2); }); test("visual mode works", async () => { const timeline = [ { type: htmlButtonResponse, stimulus: "foo", choices: ["a", "b", "c"], }, ]; const { expectFinished, expectRunning, getHTML, getData } = await simulateTimeline( timeline, "visual" ); await expectRunning(); expect(getHTML()).toContain("foo"); jest.runAllTimers(); await expectFinished(); const response = getData().values()[0].response; expect(getData().values()[0].rt).toBeGreaterThan(0); expect(response).toBeGreaterThanOrEqual(0); expect(response).toBeLessThanOrEqual(2); }); });