import {login} from "../helpers";

describe("selfEvaluation.cy.js", () => {
  beforeEach(() => {
    cy.manageCommand("cypress_reset");

    login("test-student1@example.com", "test");

    // test-lehrgang-lp-circle-reisen-lu-reisen ist eine Selbstevaluation
    // mit mehreren Schritten
    cy.visit("/course/test-lehrgang/learn/reisen");
  });

  it("self evaluation should be neutral", () => {
    cy.get('[data-cy="test-lehrgang-lp-circle-reisen-lu-reisen"]')
      .find('[data-cy="unknown"]')
      .should("exist");
  });

  it("self evaluation from KompetenzNavi", () => {
    cy.get('[data-cy="test-lehrgang-lp-circle-reisen-lu-reisen"]')
      .find('[data-cy="unknown"]')
      .should("exist");

    // data in KompetenzNavi/Übersicht is correct
    cy.visit("/course/test-lehrgang/competence");
    cy.get('[data-cy="self-evaluation-fail"]').should("have.text", "0");
    cy.get('[data-cy="self-evaluation-success"]').should("have.text", "0");
    cy.get('[data-cy="self-evaluation-unknown"]').should("have.text", "4");


    // learning unit id = 687 also known as:
    // Bedarfsanalyse, Ist- und Soll-Situation <<Reisen>>
    const identifier = "self-eval-687"

    // data in KompetenzNavi/Selbsteinschätzungen is correct
    cy.visit("/course/test-lehrgang/competence/self-evaluation-and-feedback");
    cy.get(`[data-cy="${identifier}-fail"]`).should("not.exist");
    cy.get(`[data-cy="${identifier}-pass"]`).should("not.exist");
    cy.get(`[data-cy="${identifier}-unknown"]`).should("have.text", "2");

    // it can open self evaluation from within KompetenzNavi
    cy.get(`[data-cy="${identifier}-detail-url"]`).click();

    // starting the self evaluation will return to KompetenzNavi
    cy.makeSelfEvaluation([true, false]);
    cy.url().should("include", "/course/test-lehrgang/competence/self-evaluation-and-feedback");

    // check data again on KompetenzNavi
    cy.get(`[data-cy="${identifier}-fail"]`).should("have.text", "1");
    cy.get(`[data-cy="${identifier}-pass"]`).should("have.text", "1");
    cy.get(`[data-cy="${identifier}-unknown"]`).should("not.exist");

    // data in KompetenzNavi/Übersicht is correct
    cy.visit("/course/test-lehrgang/competence");
    cy.get('[data-cy="self-evaluation-fail"]').should("have.text", "1");
    cy.get('[data-cy="self-evaluation-success"]').should("have.text", "1");
    cy.get('[data-cy="self-evaluation-unknown"]').should("have.text", "2");
  });

  it("should be able to make a happy self evaluation", () => {
    cy.get('[data-cy="test-lehrgang-lp-circle-reisen-lu-reisen"]').click();
    cy.makeSelfEvaluation([true, true]);
    cy.get('[data-cy="test-lehrgang-lp-circle-reisen-lu-reisen"]')
      .find('[data-cy="success"]')
      .should("exist");

    // starting the self evaluation from circle should return to circle
    cy.url().should("include", "/course/test-lehrgang/learn/reisen");
  });

  it("should be able to make a fail self evaluation", () => {
    cy.get('[data-cy="test-lehrgang-lp-circle-reisen-lu-reisen"]').click();
    cy.makeSelfEvaluation([false, false]);
    cy.get('[data-cy="test-lehrgang-lp-circle-reisen-lu-reisen"]')
      .find('[data-cy="fail"]')
      .should("exist");
  });

  it("should be able to make a mixed self evaluation", () => {
    cy.get('[data-cy="test-lehrgang-lp-circle-reisen-lu-reisen"]').click();
    cy.makeSelfEvaluation([false, true]);
    cy.get('[data-cy="test-lehrgang-lp-circle-reisen-lu-reisen"]')
      .find('[data-cy="fail"]')
      .should("exist");
  });
});
