import { pressKey, simulateTimeline, startTimeline } from "@jspsych/test-utils";
import iatImage from ".";
jest.useFakeTimers();
describe("iat-image plugin", () => {
test("displays image by default", async () => {
const { getHTML, expectFinished } = await startTimeline([
{
type: iatImage,
stimulus: "../media/blue.png",
response_ends_trial: true,
display_feedback: false,
left_category_key: "f",
right_category_key: "j",
left_category_label: ["FRIENDLY"],
right_category_label: ["UNFRIENDLY"],
stim_key_association: "left",
trial_duration: 500,
},
]);
expect(getHTML()).toContain("blue.png");
pressKey("f");
await expectFinished();
});
test("display should only clear when left key is pressed", async () => {
const { getHTML, expectFinished } = await startTimeline([
{
type: iatImage,
stimulus: "../media/blue.png",
left_category_key: "f",
left_category_label: ["FRIENDLY"],
stim_key_association: "left",
key_to_move_forward: ["f"],
},
]);
pressKey("j");
expect(getHTML()).toContain('');
pressKey("f");
await expectFinished();
});
test("display should only clear when right key is pressed", async () => {
const { getHTML, expectFinished } = await startTimeline([
{
type: iatImage,
stimulus: "../media/blue.png",
right_category_key: "j",
right_category_label: ["UNFRIENDLY"],
stim_key_association: "right",
key_to_move_forward: ["j"],
},
]);
pressKey("f");
expect(getHTML()).toContain('
');
pressKey("j");
await expectFinished();
});
test("display should clear when any key is pressed", async () => {
const { getHTML, expectFinished } = await startTimeline([
{
type: iatImage,
stimulus: "../media/blue.png",
left_category_key: "f",
right_category_key: "j",
left_category_label: ["FRIENDLY"],
right_category_label: ["UNFRIENDLY"],
stim_key_association: "right",
key_to_move_forward: "ALL_KEYS",
display_feedback: true,
},
]);
pressKey("f");
expect(getHTML()).toContain(
'
'
);
pressKey("a");
await expectFinished();
});
test("display should clear only when key_to_move_forward is pressed", async () => {
const { getHTML, expectFinished } = await startTimeline([
{
type: iatImage,
stimulus: "../media/blue.png",
left_category_key: "f",
right_category_key: "j",
left_category_label: ["FRIENDLY"],
right_category_label: ["UNFRIENDLY"],
stim_key_association: "left",
display_feedback: true,
key_to_move_forward: ["x"],
},
]);
pressKey("j");
expect(getHTML()).toContain(
'
'
);
pressKey("x");
await expectFinished();
});
test("labels should be with assigned key characters", async () => {
const { getHTML, expectFinished } = await startTimeline([
{
type: iatImage,
stimulus: "../media/blue.png",
left_category_key: "f",
right_category_key: "j",
left_category_label: ["FRIENDLY"],
right_category_label: ["UNFRIENDLY"],
stim_key_association: "left",
},
]);
expect(getHTML()).toContain("
Press j for:
UNFRIENDLY");
expect(getHTML()).toContain("
Press f for:
FRIENDLY");
pressKey("f");
await expectFinished();
});
test("should display wrong image when wrong key is pressed", async () => {
const { getHTML, expectFinished, displayElement } = await startTimeline([
{
type: iatImage,
stimulus: "../media/blue.png",
html_when_wrong: 'X',
display_feedback: true,
left_category_key: "f",
right_category_key: "j",
left_category_label: ["FRIENDLY"],
right_category_label: ["UNFRIENDLY"],
stim_key_association: "left",
key_to_move_forward: "ALL_KEYS",
response_ends_trial: true,
},
]);
const wrongImageContainer = displayElement.querySelector');
jest.advanceTimersByTime(500);
await expectFinished();
});
test("trial should not end when response_ends_trial is false and stimulus should get responded class", async () => {
const { getHTML, expectRunning } = await startTimeline([
{
type: iatImage,
stimulus: "../media/blue.png",
response_ends_trial: false,
display_feedback: false,
left_category_key: "f",
right_category_key: "j",
left_category_label: ["FRIENDLY"],
right_category_label: ["UNFRIENDLY"],
stim_key_association: "left",
trial_duration: 500,
},
]);
pressKey("f");
expect(getHTML()).toContain(
'
'
);
await expectRunning();
});
test("should accept functions as parameters(trial_duration in use, response ends trial false)", async () => {
const { getHTML, expectFinished, displayElement } = await startTimeline([
{
type: iatImage,
stimulus: function () {
return "../media/blue.png";
},
display_feedback: function () {
return true;
},
html_when_wrong: function () {
return 'X';
},
left_category_key: function () {
return "e";
},
right_category_key: function () {
return "i";
},
left_category_label: function () {
return ["FRIENDLY"];
},
right_category_label: function () {
return ["UNFRIENDLY"];
},
stim_key_association: function () {
return "left";
},
trial_duration: function () {
return 1000;
},
response_ends_trial: function () {
return false;
},
},
]);
expect(getHTML()).toContain('
');
jest.advanceTimersByTime(500);
pressKey("i");
expect(displayElement.querySelector
');
pressKey("i");
expect(getHTML()).toContain(
'
'
);
jest.advanceTimersByTime(1000);
expect(getHTML()).toContain(
'
'
);
jest.advanceTimersByTime(500);
pressKey("a");
await expectFinished();
});
test("response not required after wrong answer (tests #1898)", async () => {
const { getHTML, expectFinished } = await startTimeline([
{
type: iatImage,
stimulus: "../media/blue.png",
response_ends_trial: true,
display_feedback: false,
left_category_key: "f",
right_category_key: "j",
left_category_label: ["FRIENDLY"],
right_category_label: ["UNFRIENDLY"],
stim_key_association: "left",
force_correct_key_press: false,
trial_duration: 3000,
},
]);
expect(getHTML()).toContain("blue.png");
pressKey("j");
await expectFinished();
});
});
describe("iat-image plugin simulation", () => {
test("data-only mode works", async () => {
const { getData, expectFinished } = await simulateTimeline([
{
type: iatImage,
stimulus: "dog.png",
response_ends_trial: true,
display_feedback: false,
left_category_key: "f",
right_category_key: "j",
left_category_label: ["FRIENDLY"],
right_category_label: ["UNFRIENDLY"],
stim_key_association: "left",
},
]);
await expectFinished();
expect(getData().values()[0].rt).toBeGreaterThan(0);
});
test("visual mode works", async () => {
const { getData, expectFinished, expectRunning } = await simulateTimeline(
[
{
type: iatImage,
stimulus: "dog.png",
response_ends_trial: true,
display_feedback: false,
left_category_key: "f",
right_category_key: "j",
left_category_label: ["FRIENDLY"],
right_category_label: ["UNFRIENDLY"],
stim_key_association: "left",
//trial_duration: 500,
},
],
"visual"
);
await expectRunning();
jest.runAllTimers();
await expectFinished();
expect(getData().values()[0].rt).toBeGreaterThan(0);
});
});