Skip to content

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

4.2 KB
Raw Blame
import {
  DEFAULT_EMBEDDED_COMPONENT_THEME,
  getEmbeddingComponentOverrides,
} from "metabase/embedding-sdk/theme";
import { getDarkTheme, getLightTheme } from "metabase/ui/colors";

import { getEmbeddingThemeOverride } from "./get-embedding-theme";

describe("Transform Embedding Theme Override", () => {
  it("should transform MetabaseTheme to EmbeddingThemeOverride", () => {
    const theme = getEmbeddingThemeOverride(
      {
        lineHeight: 1.5,
        fontSize: "2rem",
        fontFamily: "Roboto",
        colors: {
          brand: "hotpink",
          "text-primary": "yellow",
          "text-tertiary": "green",
          "background-disabled": "pink",
          background: "orange",
          "background-secondary": "brown",
          "text-white": "white",
          focus: "blue",
        },

        // we should strip any explicit "undefined" values and apply default component values
        components: {
          popover: {
            zIndex: undefined,
          },
        },
      },
      "Roboto",
    );

    expect(theme).toEqual({
      lineHeight: 1.5,
      fontFamily: "Roboto",
      colors: {
        "chart-axis": expect.arrayContaining([
          getLightTheme().colors["chart-axis"],
        ]),
        "core-brand": expect.arrayContaining(["hotpink"]),
        "text-primary": expect.arrayContaining(["yellow"]),
        "button_label-default-neutral-default": expect.arrayContaining([
          "yellow",
        ]),
        "button_label-light-neutral-default": expect.arrayContaining([
          "yellow",
        ]),
        "button_label-subtle-neutral-default": expect.arrayContaining([
          "yellow",
        ]),
        "icon-primary": expect.arrayContaining(["yellow"]),
        "text-disabled": expect.arrayContaining(["green"]),
        "background_page-primary": expect.arrayContaining(["orange"]),
        "background_surface-primary": expect.arrayContaining(["orange"]),
        "button-default-neutral-default": expect.arrayContaining(["orange"]),
        "background_page-secondary": expect.arrayContaining(["brown"]),
        "background_page-tertiary": expect.arrayContaining(["brown"]),
        "background_surface-disabled": expect.arrayContaining(["pink"]),
        "text-primary-inverse": expect.arrayContaining(["white"]),
        "core-white": expect.arrayContaining(["white"]),
        "input-focus": expect.arrayContaining(["blue"]),
      },
      other: {
        fontSize: "2rem",
        hasCustomChartFontSize: true,
        ...DEFAULT_EMBEDDED_COMPONENT_THEME,
      },
      components: getEmbeddingComponentOverrides(),
    });
  });

  it("sets background-secondary to the value of background when it is unset", () => {
    const theme = getEmbeddingThemeOverride(
      { colors: { background: "green" } },
      "Roboto",
    );

    expect(theme).toEqual({
      fontFamily: "Roboto",
      colors: {
        "chart-axis": expect.arrayContaining([
          getDarkTheme().colors["chart-axis"],
        ]),
        "background_page-primary": expect.arrayContaining(["green"]),
        "background_surface-primary": expect.arrayContaining(["green"]),
        "button-default-neutral-default": expect.arrayContaining(["green"]),
        "background_page-secondary": expect.arrayContaining(["green"]),
        "background_page-tertiary": expect.arrayContaining(["green"]),
      },
      other: {
        fontSize: "14px",
        hasCustomChartFontSize: false,
        ...DEFAULT_EMBEDDED_COMPONENT_THEME,
      },
      components: getEmbeddingComponentOverrides(),
    });
  });

  it.each(["13px", DEFAULT_EMBEDDED_COMPONENT_THEME.cartesian.label.fontSize])(
    "preserves an explicit chart label font size of %s",
    (fontSize) => {
      const theme = getEmbeddingThemeOverride(
        { components: { cartesian: { label: { fontSize } } } },
        undefined,
      );

      expect(theme.other).toMatchObject({
        hasCustomChartFontSize: true,
        cartesian: { label: { fontSize } },
      });
    },
  );

  it("preserves an explicit base font size matching the default", () => {
    const theme = getEmbeddingThemeOverride({ fontSize: "14px" }, undefined);

    expect(theme.other).toMatchObject({
      fontSize: "14px",
      hasCustomChartFontSize: true,
    });
  });
});