import { pressKey, startTimeline } from "jspsych/tests/utils";
import htmlKeyboardResponse from ".";
jest.useFakeTimers();
describe("html-keyboard-response", () => {
test("displays html stimulus", async () => {
const { getHTML, expectFinished } = await startTimeline([
{
type: htmlKeyboardResponse,
stimulus: "this is html",
},
]);
expect(getHTML()).toBe('
this is html
');
pressKey("a");
await expectFinished();
});
test("display clears after key press", async () => {
const { getHTML, expectFinished } = await startTimeline([
{
type: htmlKeyboardResponse,
stimulus: "this is html",
choices: ["f", "j"],
},
]);
expect(getHTML()).toContain(
'this is html
'
);
pressKey("f");
await expectFinished();
});
test("prompt should append html below stimulus", async () => {
const { getHTML, expectFinished } = await startTimeline([
{
type: htmlKeyboardResponse,
stimulus: "this is html",
choices: ["f", "j"],
prompt: 'this is a prompt
',
},
]);
expect(getHTML()).toContain(
'this is html
this is a prompt
'
);
pressKey("f");
await expectFinished();
});
test("should hide stimulus if stimulus-duration is set", async () => {
const { displayElement, expectFinished } = await startTimeline([
{
type: htmlKeyboardResponse,
stimulus: "this is html",
choices: ["f", "j"],
stimulus_duration: 500,
},
]);
expect(
displayElement.querySelector("#jspsych-html-keyboard-response-stimulus").style
.visibility
).toMatch("");
jest.advanceTimersByTime(500);
expect(
displayElement.querySelector("#jspsych-html-keyboard-response-stimulus").style
.visibility
).toBe("hidden");
pressKey("f");
await expectFinished();
});
test("should end trial when trial duration is reached", async () => {
const { getHTML, expectFinished } = await startTimeline([
{
type: htmlKeyboardResponse,
stimulus: "this is html",
choices: ["f", "j"],
trial_duration: 500,
},
]);
expect(getHTML()).toBe('this is html
');
jest.advanceTimersByTime(500);
await expectFinished();
});
test("should end trial when key press", async () => {
const { getHTML, expectFinished } = await startTimeline([
{
type: htmlKeyboardResponse,
stimulus: "this is html",
choices: ["f", "j"],
response_ends_trial: true,
},
]);
expect(getHTML()).toContain(
'this is html
'
);
pressKey("f");
await expectFinished();
});
test("class should say responded when key is pressed", async () => {
const { getHTML, expectRunning } = await startTimeline([
{
type: htmlKeyboardResponse,
stimulus: "this is html",
choices: ["f", "j"],
response_ends_trial: false,
},
]);
expect(getHTML()).toContain(
'this is html
'
);
pressKey("f");
expect(document.querySelector("#jspsych-html-keyboard-response-stimulus").className).toBe(
" responded"
);
await expectRunning();
});
});