Skip to content

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

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

const { ORDERS_ID, ORDERS } = SAMPLE_DATABASE;

const ORDERS_SCALAR_METRIC = {
  name: "Count of orders",
  type: "metric",
  query: {
    "source-table": ORDERS_ID,
    aggregation: [["count"]],
  },
  display: "scalar",
  collection_position: 1,
};

const ORDERS_TIMESERIES_METRIC = {
  name: "Count of orders over time",
  type: "metric",
  query: {
    "source-table": ORDERS_ID,
    aggregation: [["count"]],
    breakout: [
      [
        "field",
        ORDERS.CREATED_AT,
        { "base-type": "type/DateTime", "temporal-unit": "month" },
      ],
    ],
  },
  display: "line",
  collection_position: 1,
};

describe("scenarios > metrics > collection", () => {
  beforeEach(() => {
    H.resetSnowplow();
    H.restore();
    cy.signInAsAdmin();
    H.enableTracking();
    cy.signInAsNormalUser();
  });

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

  it("should show pinned metrics in a collection, and unpin and pin them", () => {
    H.createQuestion(ORDERS_SCALAR_METRIC);
    H.createQuestion(ORDERS_TIMESERIES_METRIC);
    cy.visit("/collection/root");
    H.getPinnedSection().within(() => {
      cy.findByText(ORDERS_SCALAR_METRIC.name).should("be.visible");
      cy.findByText(ORDERS_TIMESERIES_METRIC.name).should("be.visible");
      cy.findAllByText("A metric").should("have.length", 2).and("be.visible");
    });
    // Pinned items also stay in the contents list below the pinned section
    H.getUnpinnedSection()
      .findByText(ORDERS_SCALAR_METRIC.name)
      .should("be.visible");
    H.openPinnedItemMenu(ORDERS_SCALAR_METRIC.name);
    H.popover().findByText("Unpin").click();
    H.getPinnedSection().within(() => {
      cy.findByText(ORDERS_TIMESERIES_METRIC.name).should("be.visible");
      cy.findByText(ORDERS_SCALAR_METRIC.name).should("not.exist");
    });
    H.getUnpinnedSection()
      .findByText(ORDERS_SCALAR_METRIC.name)
      .should("be.visible");

    cy.log("unpinning the last pinned metric removes the pinned section");
    H.openPinnedItemMenu(ORDERS_TIMESERIES_METRIC.name);
    H.popover().findByText("Unpin").click();
    H.getPinnedSection().should("not.exist");
    H.getUnpinnedSection()
      .findByText(ORDERS_TIMESERIES_METRIC.name)
      .should("be.visible");

    H.openUnpinnedItemMenu(ORDERS_SCALAR_METRIC.name);
    H.popover().findByText("Pin this").click();
    H.getPinnedSection()
      .findByText(ORDERS_SCALAR_METRIC.name)
      .should("be.visible");
    H.getUnpinnedSection()
      .findByText(ORDERS_SCALAR_METRIC.name)
      .should("be.visible");
  });

  it("should be possible to bookmark, trash, restore, and permanently delete a metric", () => {
    H.createQuestion(ORDERS_SCALAR_METRIC);
    H.createQuestion({
      ...ORDERS_TIMESERIES_METRIC,
      collection_position: null,
    });

    cy.log("add and remove bookmarks");
    cy.visit("/collection/root");

    H.getPinnedSection().should("contain", ORDERS_SCALAR_METRIC.name);
    H.openPinnedItemMenu(ORDERS_SCALAR_METRIC.name);

    H.popover().findByText("Bookmark").click();
    H.navigationSidebar()
      .findByText(ORDERS_SCALAR_METRIC.name)
      .should("be.visible");
    H.expectUnstructuredSnowplowEvent({
      event: "bookmark_added",
      event_detail: "metric",
      triggered_from: "collection_list",
    });

    H.openPinnedItemMenu(ORDERS_SCALAR_METRIC.name);
    H.popover().findByText("Remove from bookmarks").click();
    H.navigationSidebar()
      .findByText(ORDERS_SCALAR_METRIC.name)
      .should("not.exist");

    H.openUnpinnedItemMenu(ORDERS_TIMESERIES_METRIC.name);
    H.popover().findByText("Bookmark").click();
    H.navigationSidebar()
      .findByText(ORDERS_TIMESERIES_METRIC.name)
      .should("be.visible");
    H.expectUnstructuredSnowplowEvent(
      {
        event: "bookmark_added",
        event_detail: "metric",
        triggered_from: "collection_list",
      },
      2,
    );
    H.openUnpinnedItemMenu(ORDERS_TIMESERIES_METRIC.name);
    H.popover().findByText("Remove from bookmarks").click();
    H.navigationSidebar()
      .findByText(ORDERS_TIMESERIES_METRIC.name)
      .should("not.exist");

    cy.log("trash, restore, and permanently delete");
    H.openPinnedItemMenu(ORDERS_SCALAR_METRIC.name);
    H.popover().findByText("Move to trash").click();
    H.getPinnedSection().should("not.exist");
    H.undoToast().findByText("Trashed metric").should("be.visible");
    H.undo();
    H.getPinnedSection()
      .findByText(ORDERS_SCALAR_METRIC.name)
      .should("be.visible");

    H.openUnpinnedItemMenu(ORDERS_TIMESERIES_METRIC.name);
    H.popover().findByText("Move to trash").click();
    H.getUnpinnedSection()
      .findByText(ORDERS_TIMESERIES_METRIC.name)
      .should("not.exist");
    // eslint-disable-next-line metabase/no-unsafe-element-filtering
    H.undoToastList().last().findByText("Trashed metric").should("be.visible");

    openArchive();
    H.openUnpinnedItemMenu(ORDERS_TIMESERIES_METRIC.name);
    H.popover().findByText("Restore").click();
    H.getUnpinnedSection()
      .findByText(ORDERS_TIMESERIES_METRIC.name)
      .should("not.exist");
    // eslint-disable-next-line metabase/no-unsafe-element-filtering
    H.undoToastList()
      .last()
      .findByText(`${ORDERS_TIMESERIES_METRIC.name} has been restored.`)
      .should("be.visible");

    H.navigationSidebar().findByText("Our analytics").click();
    H.openUnpinnedItemMenu(ORDERS_TIMESERIES_METRIC.name);
    H.popover().findByText("Move to trash").click();
    openArchive();
    H.openUnpinnedItemMenu(ORDERS_TIMESERIES_METRIC.name);
    H.popover().findByText("Delete permanently").click();
    H.modal().button("Delete permanently").click();
    H.getUnpinnedSection().should("not.exist");
    // eslint-disable-next-line metabase/no-unsafe-element-filtering
    H.undoToastList()
      .last()
      .findByText("This item has been permanently deleted.")
      .should("be.visible");
  });
});

function openArchive() {
  H.navigationSidebar().findByText("Trash").click();
}