Skip to content

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

8.1 KB
Raw Blame
import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";

import {
  codeBlock,
  getEmbedSidebar,
  navigateToEmbedOptionsStep,
} from "./helpers";

const { ORDERS, ORDERS_ID } = SAMPLE_DATABASE;

const { H } = cy;

const suiteTitle =
  "scenarios > embedding > sdk iframe embed setup > embed parameters";

describe(suiteTitle, () => {
  beforeEach(() => {
    H.restore();
    H.resetSnowplow();
    cy.signInAsAdmin();
    H.activateToken("pro-self-hosted");
    H.enableTracking();
    // Accept both sets of embedding terms up front so the wizard never shows
    // the Agree CTA. Every test here preselects SSO, but the wizard opens in
    // guest mode while SSO is unconfigured, so an unaccepted guest CTA would
    // otherwise have to be clicked through first. That flow is covered by
    // embed-flow-enable-embed-js.cy.spec.ts.
    H.updateSetting("enable-embedding-modular", true);
    H.updateSetting("show-modular-embed-terms", false);
    H.updateSetting("show-static-embed-terms", false);
    H.mockEmbedJsToDevServer();
  });

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

  describe("dashboards with parameters", () => {
    beforeEach(() => {
      H.createQuestionAndDashboard({
        questionDetails: {
          name: "Orders table",
          query: { "source-table": ORDERS_ID },
        },
        dashboardDetails: {
          name: "Dashboard with Parameters",
          parameters: DASHBOARD_PARAMETERS,
        },
      }).then(({ body: card }) => {
        H.editDashboardCard(card, {
          parameter_mappings: DASHBOARD_PARAMETERS.map((parameter) => ({
            card_id: card.card_id,
            parameter_id: parameter.id,
            target: ["dimension", ["field", ORDERS.ID, null]],
          })),
        });
      });
    });

    it("can set default parameter values", () => {
      navigateToEmbedOptionsStep({
        experience: "dashboard",
        resourceName: "Dashboard with Parameters",
        preselectSso: true,
      });

      cy.log("set default value for id");
      getEmbedSidebar().findByLabelText("ID").type("123");
      H.popover().findByText("Add filter").click();

      H.getSimpleEmbedIframeContent()
        .findByTestId("dashboard-parameters-widget-container")
        .findByLabelText("ID")
        .should("contain", "123");

      cy.log("set default value for product id");
      getEmbedSidebar().findByLabelText("Product ID").type("456");
      H.popover().findByText("Add filter").click();

      H.getSimpleEmbedIframeContent()
        .findByTestId("dashboard-parameters-widget-container")
        .findByLabelText("Product ID")
        .should("contain", "456");

      cy.log("both default values should be in the code snippet");
      getEmbedSidebar().within(() => {
        cy.findByText("Get code").click();
        codeBlock().should("contain", "initial-parameters=");
        codeBlock().should("contain", '"id":[123]');
        codeBlock().should("contain", '"product_id":[456]');
      });

      cy.get("metabase-dashboard")
        .invoke("attr", "initial-parameters")
        .then((attr) => {
          expect(JSON.parse(attr!)).to.deep.equal({
            id: [123],
            product_id: [456],
          });
        });
    });

    it("loads parameters into parameter settings and can hide them", () => {
      navigateToEmbedOptionsStep({
        experience: "dashboard",
        resourceName: "Dashboard with Parameters",
        preselectSso: true,
      });

      getEmbedSidebar().within(() => {
        cy.findByText("Parameters").should("be.visible");

        cy.log("parameter inputs should be visible");
        cy.findByLabelText("ID").should("be.visible");
        cy.findByLabelText("Product ID").should("be.visible");

        cy.log("parameters should be visible by default");

        parameterVisibilityToggle("id").should(
          "have.attr",
          "data-hidden",
          "false",
        );

        parameterVisibilityToggle("product_id").should(
          "have.attr",
          "data-hidden",
          "false",
        );
      });

      H.getSimpleEmbedIframeContent()
        .findByTestId("dashboard-parameters-widget-container")
        .should("exist");

      cy.log("hide both parameters");
      getEmbedSidebar().within(() => {
        parameterVisibilityToggle("id").click();
        parameterVisibilityToggle("product_id").click();

        parameterVisibilityToggle("id").should(
          "have.attr",
          "data-hidden",
          "true",
        );
        parameterVisibilityToggle("product_id").should(
          "have.attr",
          "data-hidden",
          "true",
        );
      });

      cy.log("parameter widget container should not exist");
      H.getSimpleEmbedIframeContent()
        .findByText("Dashboard with Parameters")
        .should("be.visible");
      H.getSimpleEmbedIframeContent()
        .findByTestId("dashboard-parameters-widget-container")
        .should("not.exist");

      cy.log("code snippet should contain the hidden parameters");
      getEmbedSidebar().within(() => {
        cy.findByText("Get code").click();
        codeBlock().should("contain", "hidden-parameters=");
        codeBlock().should("contain", '"id"');
        codeBlock().should("contain", '"product_id"');
      });

      cy.get("metabase-dashboard")
        .invoke("attr", "hidden-parameters")
        .then((attr) => {
          expect(JSON.parse(attr!)).to.have.members(["id", "product_id"]);
        });
    });
  });

  describe("questions with parameters", () => {
    beforeEach(() => {
      H.createNativeQuestion({
        name: "Question with Parameters",
        native: {
          query: "SELECT * FROM orders WHERE id = {{id}}",
          "template-tags": {
            id: {
              id: "11111111",
              name: "id",
              "display-name": "ID",
              type: "number",
              default: null,
            },
          },
        },
      });
    });

    it("can set default parameters for SQL questions", () => {
      cy.intercept("POST", "/api/card/*/query").as("cardQuery");

      navigateToEmbedOptionsStep({
        experience: "chart",
        resourceName: "Question with Parameters",
        preselectSso: true,
      });

      getEmbedSidebar().within(() => {
        cy.findByText("Parameters").should("be.visible");
        cy.findByLabelText("ID").should("be.visible");
      });

      cy.log("the preview runs {{id}} unset, so the backend rejects the query");
      cy.wait("@cardQuery");

      H.getSimpleEmbedIframeContent()
        .findByText(/missing required parameters/)
        .should("be.visible");

      getEmbedSidebar().within(() => {
        cy.findByLabelText("ID").type("123");
        cy.press("Tab"); //.blur() doesn't easily work here
      });

      cy.log("the new value re-runs the question in the preview iframe");
      cy.wait("@cardQuery");

      H.getSimpleEmbedIframeContent().within(() => {
        // Anchor on the row first: a mid-rerender iframe body is empty, which
        // satisfies the "not.exist" check below on its own (EMB-2338).
        cy.findByText("123").should("be.visible");

        // value in a subtotal field
        cy.findAllByText("75.41").first().should("be.visible");

        cy.findByText(/missing required parameters/).should("not.exist");
      });

      getEmbedSidebar().within(() => {
        cy.findByText("Get code").click();
        codeBlock().should("contain", "initial-sql-parameters=");
        codeBlock().should("not.contain", "hidden-parameters="); // not supported for questions yet
        codeBlock().should("contain", '"id":"123"');
      });

      cy.get("metabase-question")
        .invoke("attr", "initial-sql-parameters")
        .then((attr) => {
          expect(JSON.parse(attr!)).to.deep.equal({ id: "123" });
        });
    });
  });
});

const parameterVisibilityToggle = (slug: string) =>
  cy
    .findAllByTestId("parameter-visibility-toggle")
    .get(`[data-parameter-slug="${slug}"]`);

const DASHBOARD_PARAMETERS = [
  {
    name: "ID",
    slug: "id",
    id: "11111111",
    type: "id",
  },
  {
    name: "Product ID",
    slug: "product_id",
    id: "22222222",
    type: "id",
  },
];