import { CommandbarPage } from '../../support/commandbar.po';

describe('General functionality in the commandbar', {retries: {openMode: 2, runMode: 2}}, () => {
  let page: CommandbarPage;

  before(() => {
    page = new CommandbarPage();
    page.navigateTo();
  });

  beforeEach(() => {
    cy.viewport(1920, 1200);
  });

  describe('The table', () => {
    it('should have 11 entries', () => {
      page.getAllTableRows().should('have.length', 11);
      page.getItemCount().contains(/^11 items$/);
    });
    it('should show only one matching item after entering search term', () => {
      page.getSearchInput().type('ora');
      page.getAllTableRows().should('have.length', 1);
    });

    it('should show item count label with singular form', () => {
      page.getItemCount().contains(/^1 item$/);
    });

    it('should show no items and plural form when no item matches search', () => {
      page.getSearchInput().clear().type('not-mathcing');
      page.getAllTableRows().should('have.length', 0);
      page.getItemCount().contains(/^0 items$/);
    });

    it('should show all items again if search input is cleared.', () => {
      page.getSearchInput().clear();
      page.getAllTableRows().should('have.length', 11);
      page.getItemCount().contains(/^11 items$/);
    });
  });

  describe('The add button without overridden properties', () => {
    it('should add 1 new entry in the table', () => {
      page.getStandardAddButton().click();
      page.getAllTableRows().should('have.length', 12);
      page.getAllTableRows().last().get('mat-cell.mat-column-name').last().should('contain.text', 'User 12');
    });
  });

  describe('The add button with overridden properties', () => {
    it('should open a list tooltip with predefined items', () => {
      page.getOverriddenAddButton().click();
    });

    it('should add a predefined item in the table when clicking on it in the list', () => {
      page.getAddMenuFirstItem().click();
      page.getAllTableRows().should('have.length', 13);
      page.getAllTableRows().last().get('mat-cell.mat-column-name').last().should('contain.text', 'User Test A');
    });
  });

  describe('Aria-label for CommandBar Button', () => {
    it('should have aria-label attribute for that button', () => {
      page.getCommandBarButtonWithAriaLabel().should('have.attr','aria-label','do some magic');
    });
  });

  describe('check if CommandBar Button with aria-label exists', () => {
    it('should have button visible', () => {
      page.getCommandBarButtonWithAriaLabel().should('exist');
    });
  });

  describe('check if CommandBar Button with disabled add button exists', () => {
    it('should have button with disabled attribute', () => {
      page.getDisabledAddButton().should('have.attr','disabled');
    });
  });

});
