Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

4.9 KB
Raw Blame
const { H } = cy;
import { SAMPLE_DB_ID } from "e2e/support/cypress_data";

describe("scenarios > question > custom column > expression shortcuts > combine", () => {
  beforeEach(() => {
    H.restore();
    cy.signInAsNormalUser();
  });

  it("should be possible to select a combine columns shortcut", () => {
    H.openOrdersTable({ mode: "notebook", limit: 5 });
    H.addCustomColumn();
    selectCombineColumns();

    selectColumn(0, "Total");

    H.expressionEditorWidget().findByText("Total").should("exist");

    selectColumn(1, "Product", "Rating");

    H.expressionEditorWidget().within(() => {
      cy.findByText("Product → Rating").should("exist");

      cy.findByTestId("combine-example").should(
        "have.text",
        "123.45678901234567 123.45678901234567",
      );

      cy.findByText(/Separated by/).click();
      cy.findByLabelText("Separator").type("__");

      cy.findByTestId("combine-example").should(
        "have.text",
        "123.45678901234567__123.45678901234567",
      );

      cy.button("Done").click();

      H.CustomExpressionEditor.value().should(
        "equal",
        'concat([Total], "__", [Product → Rating])',
      );
      cy.findByTestId("expression-name").should(
        "have.value",
        "Combined Total, Rating",
      );
    });
  });

  it("should be possible to cancel when using the combine column shortcut", () => {
    H.openOrdersTable({ mode: "notebook" });
    H.addCustomColumn();
    selectCombineColumns();

    selectColumn(0, "Total");
    selectColumn(1, "Product", "Rating");

    H.expressionEditorWidget().within(() => {
      // Click the back button, in the header
      cy.findByText("Select columns to combine").click();
    });

    H.CustomExpressionEditor.value().should("equal", "");
    cy.findByTestId("expression-name").should("have.value", "");
  });

  it("should be possible to add and remove more than one column", () => {
    H.openOrdersTable({ mode: "notebook" });
    H.addCustomColumn();
    selectCombineColumns();

    selectColumn(0, "Total");
    selectColumn(1, "Product", "Rating");
    addColumn();
    selectColumn(2, "User", "Email");

    cy.findByTestId("combine-example").should(
      "contain",
      "123.45678901234567 123.45678901234567 [email protected]",
    );

    // eslint-disable-next-line metabase/no-unsafe-element-filtering
    cy.findAllByLabelText("Remove column").last().click();

    cy.findByTestId("combine-example").should(
      "contain",
      "123.45678901234567 123.45678901234567",
    );
  });

  it("should be possible to edit a previous stages' columns when there is an aggregation (metabase#43226)", () => {
    H.openOrdersTable({ mode: "notebook" });

    cy.button("Summarize").click();

    H.popover().findByText("Count of rows").click();

    // add custom column
    cy.findAllByTestId("action-buttons").first().icon("add_data").click();
    selectCombineColumns();

    selectColumn(0, "User", "Email");

    H.expressionEditorWidget().within(() => {
      cy.findByText("Separated by (empty)").should("exist");
      cy.findByText(/Separated by/).click();

      cy.findByLabelText("Separator").should("have.value", "");
    });
  });
});

describe("scenarios > question > custom column > combine shortcuts", () => {
  beforeEach(() => {
    H.restore();
    H.resetSnowplow();
    cy.signInAsNormalUser();
  });

  afterEach(() => {
    H.expectNoBadSnowplowEvents();
  });

  it("should send an event for combine columns", () => {
    H.openOrdersTable({ mode: "notebook" });
    H.addCustomColumn();
    selectCombineColumns();

    selectColumn(0, "User", "Email");

    cy.log("a text column as the first column defaults to an empty separator");
    H.expressionEditorWidget().within(() => {
      cy.findByText("Separated by (empty)").should("be.visible");
      cy.findByText(/Separated by/).click();
      cy.findByLabelText("Separator").should("have.value", "");
    });

    selectColumn(1, "User", "Email");

    H.expressionEditorWidget().button("Done").click();

    H.expectUnstructuredSnowplowEvent({
      event: "column_combine_via_shortcut",
      custom_expressions_used: ["concat"],
      database_id: SAMPLE_DB_ID,
      question_id: 0,
    });
  });
});

function selectCombineColumns() {
  H.popover().findByText("Combine columns").click();
}

function selectColumn(index: number, table: string, name?: string) {
  H.expressionEditorWidget().within(() => {
    // eslint-disable-next-line metabase/no-unsafe-element-filtering
    cy.findAllByTestId("column-input").eq(index).click();
  });

  // eslint-disable-next-line metabase/no-unsafe-element-filtering
  H.popover()
    .last()
    .within(() => {
      if (name) {
        // both table and name were given
        cy.findByText(table).click();
        cy.findByText(name).click();
      } else {
        cy.findByText(table).click();
      }
    });
}

function addColumn() {
  H.expressionEditorWidget().within(() => {
    cy.findByText("Add column").click();
  });
}