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

context('Edit User Browser Testing', () => {
  let userFirstName = 'Edited User First Name ',
      userLastName = 'Edited User Last Name ',
      jobDescription = 'Edited Job Description ',
      city = 'Edited City ',
      phone = '+1234',
      website = 'https://test.site/';
  before(() => {
    cy.randomNum().then(num => {
      userFirstName += num;
      userLastName += num;
      jobDescription += num;
      city += num;
      phone += num;
      website += num;
    });
    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('User Edition process', () => {
    it('should visit the user edit screen', () => {
      cy.visit('/members/members-edit-profile');
      cy.location().should((loc) => {
        expect(loc.pathname).to.eq('/members/members-edit-profile');
      });
    });
    /// Workaround - Routing bug - user won't land on edit profile screen
    it('should navigate to user edit screen', () => {
      cy.get('#members-my-profile solid-link[next="members-edit-profile"]').click();
    });
    /// End workaround
    it('should enter new user data', () => {
      cy.get('#members-edit-profile input[name="first_name"]').clear().type(userFirstName);
      cy.get('#members-edit-profile input[name="first_name"]').should('have.value', userFirstName);
      cy.get('#members-edit-profile input[name="last_name"]').clear().type(userLastName);
      cy.get('#members-edit-profile input[name="last_name"]').should('have.value', userLastName);
      cy.get('#members-edit-profile textarea[name="profile.job"]').clear().type(jobDescription);
      cy.get('#members-edit-profile textarea[name="profile.job"]').should('have.value', jobDescription);
      cy.get('#members-edit-profile input[name="profile.city"]').clear().type(city);
      cy.get('#members-edit-profile input[name="profile.city"]').should('have.value', city);
      cy.get('#members-edit-profile input[name="profile.phone"]').clear().type(phone);
      cy.get('#members-edit-profile input[name="profile.phone"]').should('have.value', phone);
      cy.get('#members-edit-profile input[name="profile.website"]').clear().type(website);
      cy.get('#members-edit-profile input[name="profile.website"]').should('have.value', website);
    });
    it('should click button to save the user', () => {
      cy.get('#members-edit-profile input[value="ENREGISTRER"]').click();
    });
    it('should land on user information screen', () => {
      cy.location().should(location => {
        /// Workaround - Routing bug - route pathname won't be /profile as it should
        expect(location.pathname).to.eq('/');
        // expect(location.pathname).to.eq('/members');
        /// End workaround
      });
    });
    it('should show edited user data on user information screen', () => {
      cy.contains('solid-display-value[name="name"]', userFirstName + ' ' + userLastName).should("exist");
      cy.contains('solid-display-value[name="profile.job"]', jobDescription).should("exist");
      cy.contains('solid-display-value[name="profile.city"]', city).should("exist");
      cy.contains('directory-link-tel a', phone).should("exist");
      cy.contains('directory-website a', website).should("exist");
    });
  });
});