import { jest } from "@jest/globals";
import jsPsych from "jspsych";
import { clickTarget } from "jspsych/tests/utils";

import surveyMultiChoice from ".";

jest.useFakeTimers();

const getInputElement = (choiceId: number, value: string) =>
  document.querySelector(
    `#jspsych-survey-multi-choice-${choiceId} input[value="${value}"]`
  ) as HTMLInputElement;

describe("survey-multi-choice plugin", function () {
  test("data are logged with the right question when randomize order is true", function () {
    var scale = ["a", "b", "c", "d", "e"];
    var t = {
      type: surveyMultiChoice,
      questions: [
        { prompt: "Q0", options: scale },
        { prompt: "Q1", options: scale },
        { prompt: "Q2", options: scale },
        { prompt: "Q3", options: scale },
        { prompt: "Q4", options: scale },
      ],
      randomize_question_order: true,
    };
    jsPsych.init({ timeline: [t] });

    getInputElement(0, "a").checked = true;
    getInputElement(1, "b").checked = true;
    getInputElement(2, "c").checked = true;
    getInputElement(3, "d").checked = true;
    getInputElement(4, "e").checked = true;

    clickTarget(document.querySelector("#jspsych-survey-multi-choice-next"));

    var survey_data = jsPsych.data.get().values()[0].response;
    expect(survey_data.Q0).toBe("a");
    expect(survey_data.Q1).toBe("b");
    expect(survey_data.Q2).toBe("c");
    expect(survey_data.Q3).toBe("d");
    expect(survey_data.Q4).toBe("e");
  });
});
