{"size": 4168, "ext": "js", "max_stars_repo_path": "frontend/cypress/integration/search.e2e.js", "max_stars_repo_name": "amosproj/amos2021ss02-project2-context-map", "max_stars_repo_stars_event_min_datetime": "2021-05-12T21:19:51.000Z", "max_stars_repo_stars_event_max_datetime": "2021-05-12T21:19:51.000Z", "max_issues_repo_path": "frontend/cypress/integration/search.e2e.js", "max_issues_repo_name": "amosproj/amos-ss2021-project2-context-map", "max_issues_repo_issues_event_min_datetime": "2021-04-14T11:20:11.000Z", "max_issues_repo_issues_event_max_datetime": "2021-07-13T14:30:46.000Z", "max_forks_repo_path": "frontend/cypress/integration/search.e2e.js", "max_forks_repo_name": "amosproj/amos2021ss02-project2-context-map", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 288, "max_forks_count": null, "avg_line_length": 27.0649350649, "max_line_length": 95, "alphanum_fraction": 0.5873320537}
import {
  customSearch,
  emptySearch,
  longSearch,
} from '../fixtures/search/search';

// TODO: Remove interception when real e2e tests are done
context('Searchbar', () => {
  // Global setup
  const apiBaseUrl = 'http://localhost:8080/api';
  beforeEach(() => {
    cy.visit('http://localhost:3000');
  });

  it('Shows results', () => {
    // Act
    cy.get('.SearchBar').type('keanu');
    // Assert
    cy.contains('Nodes');
    cy.contains('Keanu Reeves');
  });

  it('Does not search on every keydown', () => {
    // Arrange
    /* eslint-disable no-unused-expressions -- expect(..).to.Be.Called return can be ignored */
    const spySearch = cy.spy((req) => req.continue());

    cy.intercept(`${apiBaseUrl}/search/all*`, spySearch).as('searchQuery');

    // Act 1
    cy.get('.SearchBar').type('keanu');

    // Assert 1
    cy.wait(['@searchQuery']).then(() => {
      expect(spySearch).to.be.calledOnce;
    });

    // Act 2
    cy.get('.SearchBar').type(' reeves');
    // Assert 2
    cy.wait(['@searchQuery']).then(() => {
      expect(spySearch).to.be.calledTwice;
    });
    /* eslint-enable */
  });

  it('Shows Nodes, NodeTypes, Edges, EdgeTypes', () => {
    // Arrange
    cy.intercept(`${apiBaseUrl}/search/all*`, customSearch.search);

    // Act
    cy.get('.SearchBar').type('Hello');

    // Assert
    cy.contains('Nodes');
    cy.contains('Edges');
    cy.contains('Node Types');
    cy.contains('Edge Types');
  });

  it('Shows and hides Loading Icon', () => {
    // Arrange
    /** Finishes, when the loading icon is found */
    const loadingIconFound = Cypress.Promise.defer();

    cy.intercept(`${apiBaseUrl}/search/all*`, (req) =>
      loadingIconFound.promise.then(() => req.reply(emptySearch.search))
    ).as('searchQuery');

    // Act
    cy.get('.SearchBar').type('keanu');
    // Assert
    cy.get('.LoadingIcon')
      /// Waits on the loading icon
      .should('be.visible')
      /// Then finishes the API call
      .then(() => loadingIconFound.resolve());
    cy.get('.LoadingIcon').should('not.exist');
  });

  it('Cancels queries if next query comes in without error', () => {
    // Arrange
    let numQuery = 0;

    cy.intercept(`${apiBaseUrl}/search/all*`, (req) => {
      if (numQuery === 0) {
        numQuery += 1;
        // Will never return.
        // eslint-disable-next-line @typescript-eslint/no-empty-function
        return new Promise(() => {});
      }
      return req.reply(emptySearch.search);
    }).as('searchQuery');

    // Act
    cy.get('.SearchBar').type('keanu');

    // It fires a query after 300ms without typing
    // eslint-disable-next-line cypress/no-unnecessary-waiting
    cy.wait(325);

    cy.get('.SearchBar').type(' reeves');
  });

  it('Can handle error', () => {
    // Arrange
    cy.intercept(`${apiBaseUrl}/**`, (req) => req.destroy());

    // Act
    cy.get('.SearchBar').type('keanu');

    // Assert
    cy.contains('error occurred');
  });

  it('Shows expandable list if result list is too long', () => {
    // Arrange
    cy.intercept(`${apiBaseUrl}/search/all*`, longSearch.search);

    // Act
    cy.get('.SearchBar').type('Hello');

    // Assert
    cy.get('.SubList .MuiListItem-root').should('have.length', 6);
    cy.contains(/show more/i).click();
    cy.get('.SubList .MuiListItem-root').should('have.length', 8);
    cy.contains(/show more/i).should('not.exist');
  });

  it('Redirects to graph page', () => {
    // Act
    cy.get('.SearchBar').type('keanu');
    cy.contains('Keanu Reeves').click();

    // Assert
    cy.url().should('eq', `http://localhost:3000/visualization/graph`);
  });

  // was created since #393 happened
  it('Redirects to graph page (multiple times)', () => {
    cy.get('.SearchBar').type('keanu');
    cy.contains('Keanu Reeves').click();
    cy.url().should('eq', `http://localhost:3000/visualization/graph`);

    cy.contains('Visualization').click();
    cy.url().should('eq', `http://localhost:3000/visualization`);

    // second time
    cy.get('.SearchBar').type('keanu');
    cy.contains('Keanu Reeves').click();
    cy.url().should('eq', `http://localhost:3000/visualization/graph`);
  });
});
