import {
  frameworks,
  url,
  setOptions,
  initSurvey,
  getSurveyResult
} from "../settings";
import { Selector, ClientFunction } from "testcafe";
const assert = require("assert");
const title = `matrix`;

const json = {
  questions: [
    {
      type: "matrix",
      name: "Quality",
      title:
        "Please indicate if you agree or disagree with the following statements",
      columns: [
        { value: 1, text: "Strongly Disagree" },
        { value: 2, text: "Disagree" },
        { value: 3, text: "Neutral" },
        { value: 4, text: "Agree" },
        { value: 5, text: "Strongly Agree" }
      ],
      rows: [
        { value: "affordable", text: "Product is affordable" },
        { value: "does what it claims", text: "Product does what it claims" },
        {
          value: "better then others",
          text: "Product is better than other products on the market"
        },
        { value: "easy to use", text: "Product is easy to use" }
      ]
    }
  ]
};

frameworks.forEach(framework => {
  fixture`${framework} ${title}`.page`${url}${framework}`.beforeEach(
    async t => {
      await initSurvey(framework, json);
    }
  );

  test(`choose value`, async t => {
    let surveyResult;

    await t
      .click(`input[name="Quality_easy to use"][value="5"]`)
      .click(`input[value=Complete]`);

    surveyResult = await getSurveyResult();
    assert.equal(surveyResult.Quality[`easy to use`], `5`);
  });

  test(`choose several values`, async t => {
    let surveyResult;

    await t
      .click(`input[name="Quality_does what it claims"][value="4"]`)
      .click(`input[name="Quality_easy to use"][value="5"]`)
      .click(`input[value=Complete]`);

    surveyResult = await getSurveyResult();
    assert.deepEqual(surveyResult.Quality, {
      "does what it claims": "4",
      "easy to use": "5"
    });
  });

  test(`require answer for all rows`, async t => {
    let surveyResult;
    const getPosition = ClientFunction(() =>
      document.documentElement.innerHTML.indexOf(
        "Please answer questions in all rows"
      )
    );
    let position;

    await setOptions("Quality", { isAllRowRequired: true });
    await t.click(`input[value=Complete]`);

    position = await getPosition();
    assert.notEqual(position, -1);

    surveyResult = await getSurveyResult();
    assert.equal(typeof surveyResult, `undefined`);

    await t
      .click(`input[name="Quality_affordable"][value="3"]`)
      .click(`input[name="Quality_does what it claims"][value="4"]`)
      .click(`input[name="Quality_better then others"][value="2"]`)
      .click(`input[name="Quality_easy to use"][value="5"]`)
      .click(`input[value=Complete]`);

    surveyResult = await getSurveyResult();
    assert.deepEqual(surveyResult.Quality, {
      affordable: "3",
      "does what it claims": "4",
      "better then others": "2",
      "easy to use": "5"
    });
  });
});

@@ -2909,13 +2909,772 @@
 );%0A  %7D);
+%0A%0A  test(%60checked class%60, async t =%3E %7B%0A    const isCheckedClassExistsByIndex = ClientFunction(index =%3E%0A      document%0A        .querySelector(%60fieldset tbody tr td:nth-child($%7Bindex + 1%7D) label%60)%0A        .classList.contains(%22checked%22)%0A    );%0A%0A    assert.equal(await isCheckedClassExistsByIndex(2), false);%0A    assert.equal(await isCheckedClassExistsByIndex(3), false);%0A%0A    await t.click(%60input%5Bname=%22Quality_affordable%22%5D%5Bvalue=%222%22%5D%60);%0A%0A    assert.equal(await isCheckedClassExistsByIndex(2), true);%0A    assert.equal(await isCheckedClassExistsByIndex(3), false);%0A%0A    await t.click(%60input%5Bname=%22Quality_affordable%22%5D%5Bvalue=%223%22%5D%60);%0A%0A    assert.equal(await isCheckedClassExistsByIndex(2), false);%0A    assert.equal(await isCheckedClassExistsByIndex(3), true);%0A  %7D);
 %0A%7D);%0A

add "check other" test for matrix https://github.com/surveyjs/surveyjs/issues/804