/// <reference types="cypress" />

import { skipTheWelcomeScreen, random } from '../support/utils';

before(() => {
  skipTheWelcomeScreen();
});

it('allows uploading data', () => {
  cy.get('[data-test-subj="uploadFile"]').click();
  cy.get('[type=file]').selectFile(
    'cypress/fixtures/test-data-to-import.ndjson',
    {
      force: true,
    }
  );
  cy.get('[data-test-subj="dataVisualizerFileOpenImportPageButton"]').click();
  cy.fixture('indexes').then((index) => {
    cy.get('[data-test-subj="dataVisualizerFileIndexNameInput"]').type(
      `${index.newIndex.name}.${random}`
    );
  });
  cy.get('[data-test-subj="dataVisualizerFileImportButton"]').click();
  cy.get('[data-test-subj="dataVisualizerFileImportSuccessCallout"]');
});

it('allows creating a dashboard and visualisation', () => {
  cy.visit('app/visualize/');
  cy.get('[data-test-subj="newItemButton"]').click();
  cy.get('[data-test-subj="visType-vega"]').forceClick();
  cy.get('[data-test-subj="visualizeSaveButton"]').forceClick();
  cy.fixture('visualizations').then((visualization) => {
    cy.get('[data-test-subj="savedObjectTitle"]').type(
      `${visualization.newVisualization.title}.${random}`
    );
    cy.get('[data-test-subj="viewDescription"]').type(
      `${visualization.newVisualization.description}.${random}`
    );
  });
  cy.get('[id="new-dashboard-option"]').forceClick();
  cy.get('[data-test-subj="confirmSaveSavedObjectButton"]').forceClick();
  cy.get('[data-test-subj="dashboardSaveMenuItem"]').click();
  cy.fixture('dashboards').then((dashboard) => {
    cy.get('[data-test-subj="savedObjectTitle"]').type(
      `${dashboard.newDashboard.title}.${random}`
    );
    cy.get('[data-test-subj="dashboardDescription"]').type(
      `${dashboard.newDashboard.description}.${random}`
    );
    cy.get('[data-test-subj="confirmSaveSavedObjectButton"]').forceClick();
    cy.visit('/app/dashboards/list');
    cy.contains(`${dashboard.newDashboard.title}.${random}`);
  });
});
