Skip to content

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

13.6 KB
Raw Blame
const { H } = cy;

function createThemeViaApi(name = "Test theme") {
  return cy
    .request("POST", "/api/embed-theme", {
      name,
      settings: {
        colors: {
          brand: "#509EE3",
          background: "#ffffff",
          "text-primary": "#2E353B",
          "text-secondary": "#697D8C",
          "text-tertiary": "#949AAB",
          border: "#EEECEC",
          "background-secondary": "#F9FBFC",
          filter: "#7172AD",
          summarize: "#88BF4D",
          positive: "#84BB4C",
          negative: "#ED6E6E",
          shadow: "#000000",
          charts: [
            "#509EE3",
            "#88BF4D",
            "#A989C5",
            "#EF8C8C",
            "#F9D45C",
            "#F2A86F",
            "#98D9D9",
            "#7172AD",
          ],
        },
        fontFamily: "",
        fontSize: "",
      },
    })
    .then((response) => response.body);
}

function visitThemeEditor(themeId: number) {
  cy.intercept("GET", `/api/embed-theme/${themeId}`).as("getTheme");
  cy.visit(`/embedding/appearance/theme/${themeId}`);
  cy.wait("@getTheme");
}

function changeColor(label: string, value: string) {
  H.main().within(() => {
    cy.findByText(label).parent().parent().click();
  });

  H.popover().should("be.visible").find("input").first().clear().type(value);

  cy.realPress("Escape");
}

describe(
  "scenarios > embedding > themes > theme editor",
  { tags: "@EE" },
  () => {
    beforeEach(() => {
      H.restore();
      cy.signInAsAdmin();
      H.activateToken("pro-self-hosted");
    });

    it("loads the theme editor page and shows the theme name", () => {
      H.updateSetting("enable-embedding-modular", false);
      H.updateSetting("show-modular-embed-terms", false);

      createThemeViaApi("My custom theme").then((theme) => {
        visitThemeEditor(theme.id);
      });

      cy.log("editor panel should show the theme name");
      cy.findByLabelText("Theme name").should("have.value", "My custom theme");

      cy.log("theme editor should render full width");
      cy.findByTestId("embedding-hub-content-cap").should("not.exist");

      cy.log("save button should be disabled when no changes");
      cy.findByRole("button", { name: /Save theme/ }).should("be.disabled");

      cy.log("should show prompt to enable embedding");
      H.main().findByText("Theme preview").should("be.visible");
      H.main()
        .findByText(
          "Enable modular embedding to see a live preview of your theme.",
        )
        .should("be.visible");
      H.main()
        .findByRole("button", { name: /Enable modular embedding/ })
        .should("be.visible");

      cy.log("cancel should navigate back to the themes listing");
      cy.findByRole("button", { name: /Cancel/ }).click();
      cy.url().should("match", /\/embedding\/appearance$/);
      H.main().findByText("My custom theme").should("be.visible");
    });

    it("can delete a theme that has unsaved changes without getting stuck on a 404", () => {
      cy.log("an unknown theme id shows the not found page");
      cy.visit("/embedding/appearance/theme/99999");
      H.main().findByText("We're a little lost...").should("be.visible");

      // Repro of a bug where deleting a dirty theme would trigger the
      // unsaved-changes guard. The redirect to the theme list got blocked,
      // leaving the user on the now-deleted theme's URL — which 404s once
      // the GET cache is invalidated.
      createThemeViaApi("Dirty delete").then((theme) => {
        visitThemeEditor(theme.id);
      });

      cy.log("dirty the editor by renaming the theme");
      cy.findByLabelText("Theme name").clear().type("Renamed but unsaved");

      cy.findByRole("button", { name: /Save theme/ }).should("be.enabled");

      cy.log("open the delete confirmation modal");
      cy.findByRole("button", { name: /Delete theme/ })
        .scrollIntoView()
        .should("be.visible")
        .click();

      cy.findByRole("dialog").within(() => {
        cy.findByText("Delete theme").should("be.visible");
        cy.findByText(
          "Are you sure you want to delete this theme? This action cannot be undone.",
        ).should("be.visible");

        cy.log("cancel the deletion");
        cy.findByRole("button", { name: /Cancel/ }).click();
      });

      cy.log("should remain on the editor with the unsaved changes");
      cy.findByRole("dialog").should("not.exist");
      cy.findByLabelText("Theme name").should(
        "have.value",
        "Renamed but unsaved",
      );

      cy.log("delete the theme");
      cy.findByRole("button", { name: /Delete theme/ }).click();
      cy.findByRole("dialog").within(() => {
        cy.findByRole("button", { name: /Delete/ }).click();
      });

      H.undoToast().findByText("Theme deleted successfully").should("exist");

      cy.log("should land on the themes listing — not 404 or leave-prompt");
      cy.url().should("match", /\/embedding\/appearance$/);
      H.main()
        .findByRole("button", { name: /New theme/ })
        .should("be.visible");
      H.main().findByText("Dirty delete").should("not.exist");
      H.main().findByText("We're a little lost...").should("not.exist");
    });

    describe("font settings", () => {
      it("can edit font settings and save them", () => {
        cy.intercept("PUT", "/api/embed-theme/*").as("updateTheme");

        createThemeViaApi("Font test").then((theme) => {
          visitThemeEditor(theme.id);
        });

        H.main().within(() => {
          cy.log("font fields should be visible");
          cy.findByLabelText("Font").should("be.visible");
          cy.findByLabelText("Base font size").should("be.visible");
        });

        cy.log("change the theme name");
        cy.findByLabelText("Theme name").clear().type("Updated name");

        cy.log("save button should be enabled");
        cy.findByRole("button", { name: /Save theme/ }).should("be.enabled");

        cy.log("select a font family");
        H.main().findByLabelText("Font").click();
        cy.findByRole("option", { name: "Lato" }).click();

        H.main().within(() => {
          cy.log("set base font size");
          cy.findByLabelText("Base font size").type("16");

          cy.log("save the theme");
          cy.findByRole("button", { name: /Save theme/ }).click();
        });

        cy.wait("@updateTheme").then((interception) => {
          const { name, settings } = interception.request.body;
          expect(name).to.eq("Updated name");
          expect(settings.fontFamily).to.eq("Lato");
          expect(settings.fontSize).to.eq("16px");
        });

        // An earlier undo toast (e.g. "reset to defaults") may still be on
        // screen, so use the toast list (plural) and filter by text — using
        // `undoToast()` (singular) yields undefined when multiple toasts match.
        H.undoToastList().contains("Theme saved").should("be.visible");

        cy.log("saving navigates back to the listing with the new name");
        cy.url().should("match", /\/embedding\/appearance$/);
        H.main().findByText("Updated name").should("be.visible");
      });
    });

    describe("main colors", () => {
      it("shows, edits, saves, and reverts main colors", () => {
        cy.intercept("PUT", "/api/embed-theme/*").as("updateTheme");

        createThemeViaApi("Main colors").then((theme) => {
          visitThemeEditor(theme.id);
        });

        cy.log("main color swatches should be visible");
        H.main().within(() => {
          cy.findByText("Main colors").should("be.visible");
          cy.findByText("Brand").should("be.visible");
          cy.findByText("Background").should("be.visible");
          cy.findByText("Primary text").should("be.visible");
        });

        cy.log("change the brand color");
        changeColor("Brand", "FF0000");

        cy.log("revert button should appear after changing a main color");
        H.main()
          .findByLabelText("Reset main colors to defaults")
          .should("be.visible");

        cy.log("click revert to reset main colors back to defaults");
        H.main().findByLabelText("Reset main colors to defaults").click();

        cy.log("revert button should disappear after resetting");
        H.main()
          .findByLabelText("Reset main colors to defaults")
          .should("not.exist");

        cy.log("change the brand color again");
        changeColor("Brand", "FF0000");

        cy.log("save the theme with the changed brand color");
        cy.findByRole("button", { name: /Save theme/ }).click();

        cy.wait("@updateTheme").then((interception) => {
          const { settings } = interception.request.body;
          expect(settings.colors?.brand).to.eq("#ff0000ff");
        });

        // An earlier undo toast (e.g. "reset to defaults") may still be on
        // screen, so use the toast list (plural) and filter by text — using
        // `undoToast()` (singular) yields undefined when multiple toasts match.
        H.undoToastList().contains("Theme saved").should("be.visible");
      });
    });

    describe("additional colors", () => {
      it("shows, reverts, edits, and saves additional colors", () => {
        cy.intercept("PUT", "/api/embed-theme/*").as("updateTheme");

        createThemeViaApi("Colors test").then((theme) => {
          visitThemeEditor(theme.id);
        });

        H.main().within(() => {
          cy.log("additional colors should be hidden by default");
          cy.findByText("Secondary text").should("not.be.visible");

          cy.log("expand additional colors");
          cy.findByText("Show more colors").click();

          cy.log("additional color rows should be visible");
          cy.findByText("Secondary text").should("be.visible");
          cy.findByText("Border").should("be.visible");
          cy.findByText("Filter").should("be.visible");
          cy.findByText("Chart colors").should("exist");

          cy.log("collapse additional colors");
          cy.findByText("Show fewer colors").click();
          cy.findByText("Secondary text").should("not.be.visible");

          cy.findByText("Show more colors").click();

          cy.log(
            "revert button should be visible since the API theme has non-default additional colors",
          );
          cy.findByLabelText("Regenerate from brand color").should(
            "be.visible",
          );

          cy.log("click revert to reset additional colors to defaults");
          cy.findByLabelText("Regenerate from brand color").click();

          cy.log("revert button should disappear after resetting");
          cy.findByLabelText("Regenerate from brand color").should("not.exist");
        });

        cy.log("edit the border color via its inline input");
        changeColor("Border", "FF5733");

        cy.log("edit the filter color via its inline input");
        changeColor("Filter", "2D2D30");

        cy.log("save the theme");
        cy.findByRole("button", { name: /Save theme/ }).click();

        cy.wait("@updateTheme").then((interception) => {
          const { settings } = interception.request.body;
          expect(settings.colors?.border).to.eq("#ff5733ff");
          expect(settings.colors?.filter).to.eq("#2d2d30ff");
        });

        // An earlier undo toast (e.g. "reset to defaults") may still be on
        // screen, so use the toast list (plural) and filter by text — using
        // `undoToast()` (singular) yields undefined when multiple toasts match.
        H.undoToastList().contains("Theme saved").should("be.visible");
      });
    });

    it("asks to accept the usage conditions before enabling embedding for the first time", () => {
      H.updateSetting("enable-embedding-modular", false);
      H.updateSetting("show-modular-embed-terms", true);

      createThemeViaApi("Preview test").then((theme) => {
        visitThemeEditor(theme.id);
      });

      H.main()
        .findByText(/enable modular embedding and agree to the/)
        .should("be.visible");

      H.main()
        .findByRole("button", { name: /Enable modular embedding/ })
        .click();

      H.modal()
        .findByText("Each end user needs their own Metabase account")
        .should("be.visible");
      cy.findByRole("button", { name: "Agree" }).click();

      H.main().findByText("Theme preview").should("be.visible");
    });

    describe("preview picker", () => {
      beforeEach(() => {
        H.updateSetting("enable-embedding-modular", true);
        H.updateSetting("show-modular-embed-terms", false);
      });

      it("defaults to a dashboard and can switch to a question", () => {
        createThemeViaApi("Picker test").then((theme) => {
          visitThemeEditor(theme.id);
        });

        cy.log("picker button shows the default dashboard name");
        H.main()
          .findByLabelText("Change preview resource")
          .should("be.visible")
          .and("contain", "Orders in a dashboard");

        cy.log("preview renders the dashboard web component");
        H.main().find("metabase-dashboard").should("exist");

        cy.log("opens the entity picker modal");
        H.main().findByLabelText("Change preview resource").click();

        H.entityPickerModal().within(() => {
          cy.findByText("Select data to preview").should("be.visible");
          cy.findByText("Our analytics").click();
          cy.findByText("Orders, Count").click();
        });

        cy.log("picker button updates to the selected question");
        H.main()
          .findByLabelText("Change preview resource")
          .should("contain", "Orders, Count");

        cy.log("preview switches to the question web component");
        H.main().find("metabase-question").should("exist");
        H.main().find("metabase-dashboard").should("not.exist");
      });
    });
  },
);