Skip to content

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

5.6 KB
Raw Blame
const { H } = cy;
import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";

const { ORDERS_ID, ORDERS, PRODUCTS_ID, PRODUCTS } = SAMPLE_DATABASE;

describe("scenarios > question > trendline", () => {
  function setup(questionDetails) {
    H.restore();
    cy.signInAsNormalUser();
    H.createQuestion(questionDetails, { visitQuestion: true });
  }

  it("should handle per-series trend line settings (metabase#12781)", () => {
    setup({
      name: "Per-series trend line settings",
      query: {
        "source-table": ORDERS_ID,
        aggregation: [
          ["avg", ["field", ORDERS.SUBTOTAL, null]],
          ["sum", ["field", ORDERS.TOTAL, null]],
        ],
        breakout: [["field", ORDERS.CREATED_AT, { "temporal-unit": "year" }]],
      },
      display: "line",
    });
    H.openVizSettingsSidebar();
    H.leftSidebar().within(() => {
      cy.findByText("Display").click();
      cy.findByText("Trend line").click();
    });

    cy.log("global customization is hidden when there are multiple series");
    H.leftSidebar().within(() => {
      cy.findByText("Trend line color").should("not.exist");
      cy.findByText("Trend line style").should("not.exist");
    });
    H.trendLine().should("have.length", 2);

    cy.log("trend lines are customized in the series settings popover");
    H.leftSidebar().within(() => {
      cy.findByText("Data").click();
      cy.findByTestId("settings-avg").click();
    });
    H.popover().within(() => {
      cy.findByText("Trend line color").should("be.visible");
      cy.findByText("Trend line style").should("be.visible");
      cy.findByTestId("chart-settings-widget-trendline.style")
        .icon("line_style_dashed")
        .click();
    });
    H.trendLine().filter("[stroke-dasharray]").should("have.length", 1);

    cy.log("pick a custom color for this series trend line");
    H.leftSidebar().findByTestId("settings-avg").click();
    // #8A5EB0 is the default trend line color, the shade of the avg series
    H.popover().findByLabelText("#8A5EB0").click();
    cy.findByTestId("color-selector-popover")
      .findByLabelText("#509EE3")
      .click();
    H.trendLine()
      .filter("[stroke='#509EE3']")
      .should("have.length", 1)
      .and("have.attr", "stroke-dasharray");

    cy.log("hiding the series trend line hides its customization settings");
    H.leftSidebar().findByTestId("settings-avg").click();
    H.popover().findByText("Show trend line for this series").click();
    H.trendLine().should("have.length", 1);
    H.leftSidebar().findByTestId("settings-avg").click();
    H.popover().within(() => {
      cy.findByText("Show trend line for this series").should("be.visible");
      cy.findByText("Trend line color").should("not.exist");
      cy.findByText("Trend line style").should("not.exist");
      cy.findByText("Show trend line for this series").click();
    });
    H.trendLine().should("have.length", 2);
    // The popover closes on Escape only when the key event starts inside it
    H.popover()
      .findByRole("switch", { name: "Show trend line for this series" })
      .focus();
    cy.realPress("Escape");
    cy.get("[data-element-id=mantine-popover]")
      .filter(":visible")
      .should("not.exist");

    H.leftSidebar().within(() => {
      cy.findByTestId("remove-sum").click();
      cy.findByText("Done").click();
    });
    H.trendLine().should("have.length", 1);
    H.cartesianChartCircle().should("be.visible");
  });

  it("should allow customizing the trend line color and style", () => {
    setup({
      name: "Trend line customization",
      query: {
        "source-table": ORDERS_ID,
        aggregation: [["count"]],
        breakout: [["field", ORDERS.CREATED_AT, { "temporal-unit": "year" }]],
      },
      display: "line",
    });

    cy.log("color and style settings appear only when the trend line is on");
    H.openVizSettingsSidebar();
    H.leftSidebar().within(() => {
      cy.findByText("Display").click();
      cy.findByText("Trend line color").should("not.exist");
      cy.findByText("Trend line style").should("not.exist");
      cy.findByText("Trend line").click();
      cy.findByText("Trend line color").should("be.visible");
      cy.findByText("Trend line style").should("be.visible");
    });

    cy.log("by default the trend line is solid and uses the series shade");
    H.trendLine()
      .should("be.visible")
      .and("have.attr", "stroke", "#227FD2")
      .and("not.have.attr", "stroke-dasharray");

    cy.log("changing the style renders a dashed trend line");
    H.leftSidebar().within(() => {
      cy.icon("line_style_dashed").click();
    });
    H.trendLine().should("be.visible").and("have.attr", "stroke-dasharray");

    cy.log("changing the color applies it to the trend line");
    H.leftSidebar().findByLabelText("#227FD2").click();
    H.popover().findByLabelText("#88BF4D").click();
    H.trendLine().should("be.visible").and("have.attr", "stroke", "#88BF4D");
  });

  it("should display trend line for stack-100% chart (metabase#25614)", () => {
    setup({
      name: "25614",
      query: {
        "source-table": PRODUCTS_ID,
        aggregation: [["count"], ["avg", ["field", PRODUCTS.PRICE, null]]],
        breakout: [["field", PRODUCTS.CREATED_AT, { "temporal-unit": "year" }]],
      },
      display: "bar",
    });
    H.openVizSettingsSidebar();
    // stack 100%, then enable trend line
    H.leftSidebar().within(() => {
      cy.findByText("Display").click();
      cy.findByText("Stack - 100%").click();
      cy.findByText("Trend line").click();
    });
    // ensure that two trend lines are present
    H.trendLine().should("have.length", 2);
  });
});