import { clickTarget, simulateTimeline, startTimeline } from "@jspsych/test-utils";
import imageButtonResponse from ".";
jest.useFakeTimers();
describe("image-button-response", () => {
test("displays image stimulus", async () => {
const { getHTML } = await startTimeline([
{
type: imageButtonResponse,
stimulus: "../media/blue.png",
choices: ["button-choice"],
render_on_canvas: false,
},
]);
expect(getHTML()).toContain('
{
const { getHTML } = await startTimeline([
{
type: imageButtonResponse,
stimulus: "../media/blue.png",
choices: ["button-choice1", "button-choice2"],
render_on_canvas: false,
},
]);
expect(getHTML()).toContain('');
expect(getHTML()).toContain('');
});
test("display button html", async () => {
const { getHTML } = await startTimeline([
{
type: imageButtonResponse,
stimulus: "../media/blue.png",
choices: ["buttonChoice"],
button_html: '',
render_on_canvas: false,
},
]);
expect(getHTML()).toContain('');
});
test("display should clear after button click", async () => {
const { getHTML, expectFinished } = await startTimeline([
{
type: imageButtonResponse,
stimulus: "../media/blue.png",
choices: ["button-choice"],
render_on_canvas: false,
},
]);
expect(getHTML()).toContain(
'
{
const { getHTML } = await startTimeline([
{
type: imageButtonResponse,
stimulus: "../media/blue.png",
choices: ["button-choice"],
prompt: "
This is a prompt
", render_on_canvas: false, }, ]); expect(getHTML()).toContain( 'This is a prompt
' ); }); test("should hide stimulus if stimulus-duration is set", async () => { const { getHTML, displayElement } = await startTimeline([ { type: imageButtonResponse, stimulus: "../media/blue.png", choices: ["button-choice"], stimulus_duration: 500, render_on_canvas: false, }, ]); const stimulusElement = displayElement.querySelector
{
const { getHTML, expectFinished } = await startTimeline([
{
type: imageButtonResponse,
stimulus: "../media/blue.png",
choices: ["button-choice"],
response_ends_trial: true,
render_on_canvas: false,
},
]);
expect(getHTML()).toContain(
'
{
const spy = jest.spyOn(console, "warn").mockImplementation(() => {});
await startTimeline([
{
type: imageButtonResponse,
stimulus: "../media/blue.png",
choices: ["button-choice"],
response_ends_trial: false,
trial_duration: null,
render_on_canvas: false,
},
]);
expect(spy).toHaveBeenCalled();
spy.mockRestore();
});
});
describe("image-button-response simulation", () => {
test("data mode works", async () => {
const timeline = [
{
type: imageButtonResponse,
stimulus: "foo.png",
choices: ["a", "b", "c"],
render_on_canvas: false,
},
];
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: imageButtonResponse,
stimulus: "foo.png",
choices: ["a", "b", "c"],
render_on_canvas: false,
},
];
const { expectFinished, expectRunning, getHTML, getData } = await simulateTimeline(
timeline,
"visual"
);
await expectRunning();
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);
});
});