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

context('List Job Offers Browser Testing', () => {
  let listQuery = 'solid-display.job-board__list',
      filtersQuery = [
        listQuery,
        'solid-form',
        'input'
      ],
      listingCountQuery = [
        listQuery,
        'solid-display'
      ],
      firstListingQuery = [
        listQuery,
        'solid-display:first-child',
        'solid-display-value'
      ];
  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('Job Offer Listing process', () => {
    let listingTitle,
        listingDesccription,
        listingCount;
    it('should visit the job offers listing screen', () => {
      cy.visit('/job-offers');
      cy.location().should((loc) => {
        expect(loc.pathname).to.eq('/job-offers');
      });
    });
    it('should get the listing count', () => {
      cy.get(listingCountQuery.join(' ')).its('length').then(length => listingCount = length);
    });
    it('should get the first job offer listing data', () => {
      cy.get(firstListingQuery.join(' ') + '[name="title"]').invoke('text').then(text => listingTitle = text);
      cy.get(firstListingQuery.join(' ') + '[name="description"]').invoke('text').then(text => listingDesccription = text);
    });
    // TO-DO: Search by title and description
    /*it('should filter the listing by first name', () => {
      cy.get(filtersQuery.join(' ') + '[name="title"]').clear().type(listingTitle);
      cy.get(filtersQuery.join(' ') + '[name="title"]').should('have.value', listingTitle);
    });
    it('should get filtered listing count', () => {
      cy.get(listingCountQuery.join(' ')).its('length').then(length => {
        expect(length).to.eq(1);
      });
    });
    it('should clear the first name filter', () => {
      cy.get(filtersQuery.join(' ') + '[name="title"]').clear();
      cy.get(filtersQuery.join(' ') + '[name="title"]').should('have.value', '');
    });
    it('should get previous listing count', () => {
      cy.get(listingCountQuery.join(' ')).its('length').then(length => {
        expect(length).to.eq(listingCount);
      });
    });
    it('should filter the listing by first name', () => {
      cy.get(filtersQuery.join(' ') + '[name="description"]').clear().type(listingDesccription);
      cy.get(filtersQuery.join(' ') + '[name="description"]').should('have.value', listingDesccription);
    });
    it('should get filtered listing count', () => {
      cy.get(listingCountQuery.join(' ')).its('length').then(length => {
        expect(length).to.eq(1);
      });
    });
    it('should clear the first name filter', () => {
      cy.get(filtersQuery.join(' ') + '[name="description"]').clear();
      cy.get(filtersQuery.join(' ') + '[name="description"]').should('have.value', '');
    });
    it('should get previous listing count', () => {
      cy.get(listingCountQuery.join(' ')).its('length').then(length => {
        expect(length).to.eq(listingCount);
      });
    });*/
  });
});