/// <reference types="Cypress" />
/* globals cy, expect */

context('Create Project Browser Testing', () => {
  before(() => {
    cy.clearLocalStorageSnapshot();
    cy.clearLocalStorage({ domain: null });
    cy.clearCookies({ domain: null });
  });
  beforeEach(() => cy.restoreLocalStorage());
  afterEach(() => cy.saveLocalStorage());
  it('should visit user login screen', () => cy.userLogin());
  it('should login', () => cy.login());
  describe('Project Creation process #1', () => {
    let projectName = 'Test Project ',
        customerName = 'Test Customer ',
        description = 'Test Description ';
    it('should visit the project creation screen', () => {
      cy.visit('/admin-projects-create');
      cy.location().should((loc) => {
        expect(loc.pathname).to.eq('/admin-projects-create');
      });
    });
    it('should enter correct project data', () => {
      cy.randomNum().then(num => {
        projectName += num;
        customerName += num;
        description += num;
        cy.get('#admin-projects-create input[name="customer.name"]').clear().type(customerName);
        cy.get('#admin-projects-create input[name="customer.name"]').should('have.value', customerName);
        cy.get('#admin-projects-create input[name="name"]').clear().type(projectName);
        cy.get('#admin-projects-create input[name="name"]').should('have.value', projectName);
        cy.get('#admin-projects-create [name="description"] [contenteditable="true"]').clear().type(description);
        cy.get('#admin-projects-create [name="description"] [contenteditable="true"]').should('have.value', description);
      });
    });
    it('should click on create project button', () => {
      cy.get('#admin-projects-create input[type="submit"]').click();
    });
    it('should land on projects list screen', () => {
      cy.location().should((loc) => {
        expect(loc.pathname).to.eq('/admin-projects');
      });
    });
    it('should land newly created project on projects list screen', () => {
      cy.contains('solid-display-value[name="project.name"]', projectName).should("exist");
      cy.fixture('admin.json').then(admin => {
        cy.contains('solid-display-value[name="username"]', admin.username).should("exist");
      });
    });
  });
  describe('Project Creation process #2', () => {
    let projectName = 'Test Project ',
        customerName = 'Test Customer ',
        description = 'Test Description ';
    it('should visit the project creation screen', () => {
      cy.visit('/admin-projects-create');
      cy.location().should((loc) => {
        expect(loc.pathname).to.eq('/admin-projects-create');
      });
    });
    it('should enter correct project data', () => {
      cy.randomNum().then(num => {
        projectName += num;
        customerName += num;
        description += num;
        cy.get('#admin-projects-create input[name="customer.name"]').clear().type(customerName);
        cy.get('#admin-projects-create input[name="customer.name"]').should('have.value', customerName);
        cy.get('#admin-projects-create input[name="name"]').clear().type(projectName);
        cy.get('#admin-projects-create input[name="name"]').should('have.value', projectName);
        cy.get('#admin-projects-create [name="description"] [contenteditable="true"]').clear().type(description);
        cy.get('#admin-projects-create [name="description"] [contenteditable="true"]').should('have.value', description);
      });
    });
    it('should click on create project button', () => {
      cy.get('#admin-projects-create input[type="submit"]').click();
    });
    it('should land on projects list screen', () => {
      cy.location().should((loc) => {
        expect(loc.pathname).to.eq('/admin-projects');
      });
    });
    it('should land newly created project on projects list screen', () => {
      cy.contains('solid-display-value[name="project.name"]', projectName).should("exist");
      cy.fixture('admin.json').then(admin => {
        cy.contains('solid-display-value[name="username"]', admin.username).should("exist");
      });
    });
  });
});