The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
import {
ORDERS_COUNT_QUESTION_ID,
ORDERS_DASHBOARD_ENTITY_ID,
ORDERS_DASHBOARD_ID,
ORDERS_QUESTION_ENTITY_ID,
ORDERS_QUESTION_ID,
} from "e2e/support/cypress_sample_instance_data";
const { H } = cy;
const { ORDERS_ID: ORDERS_TABLE_ID, ORDERS } = SAMPLE_DATABASE;
describe("scenarios > embedding > modular embedding", () => {
beforeEach(() => {
H.prepareSdkIframeEmbedTest();
});
it("can find the embed.js file", { tags: ["@prerelease", "@EE"] }, () => {
cy.request("http://localhost:4000/app/embed.js").then((response) => {
expect(response.status).to.eq(200);
expect(response.body).to.be.a("string").and.not.be.empty;
});
});
it("table visualization should span the full width of the container (metabase#69831)", () => {
cy.signInAsAdmin();
H.createQuestion({
name: "Narrow table question",
query: {
"source-table": ORDERS_TABLE_ID,
fields: [
["field", ORDERS.ID, { "base-type": "type/BigInteger" }],
["field", ORDERS.QUANTITY, { "base-type": "type/Integer" }],
],
limit: 5,
},
}).then(({ body: question }) => {
cy.signOut();
const frame = H.loadSdkIframeEmbedTestPage({
elements: [
{
component: "metabase-question",
attributes: {
questionId: question.id,
},
},
],
});
cy.wait("@getCardQuery");
frame.within(() => {
// clientWidth excludes the scrollbar gutter, giving us the actual content area.
// Use .should() instead of .then() so Cypress retries until the
// async column-expansion cycle has finished painting.
H.tableInteractive()
.findByTestId("table-scroll-container")
.should(($scrollContainer) => {
const contentWidth = $scrollContainer[0].clientWidth;
expect(contentWidth).to.be.greaterThan(0);
const $headerCells = $scrollContainer.find(
'[data-testid="header-cell"]',
);
expect($headerCells.length).to.be.greaterThan(0);
let totalHeaderWidth = 0;
$headerCells.each((_, el) => {
totalHeaderWidth += el.getBoundingClientRect().width;
});
expect(Math.round(totalHeaderWidth)).to.be.at.least(
Math.round(contentWidth),
);
});
});
});
});
it("displays a dashboard using entity id", () => {
const frame = H.loadSdkIframeEmbedTestPage({
elements: [
{
component: "metabase-dashboard",
attributes: {
dashboardId: ORDERS_DASHBOARD_ENTITY_ID,
},
},
],
});
cy.wait("@getDashCardQuery");
frame.within(() => {
cy.findByText("Orders in a dashboard").should("be.visible");
cy.findByText("Orders").should("be.visible");
H.assertTableRowsCount(2000);
});
});
it(
"displays a question using entity id",
{ tags: ["@prerelease", "@EE"] },
() => {
const frame = H.loadSdkIframeEmbedTestPage({
elements: [
{
component: "metabase-question",
attributes: {
questionId: ORDERS_QUESTION_ENTITY_ID,
},
},
],
});
cy.wait("@getCardQuery");
frame.within(() => {
H.assertSdkInteractiveQuestionOrdersUsable();
});
},
);
it("displays the exploration template", () => {
const frame = H.loadSdkIframeEmbedTestPage({
elements: [
{
component: "metabase-question",
attributes: {
questionId: "new",
},
},
],
});
frame.within(() => {
H.assertSdkNotebookEditorUsable(frame);
// We hide the "Save" button for now. This will be customizable in the future.
cy.findByRole("button", { name: "Save" }).should("not.exist");
});
});
it("applies the provided locale", () => {
const frame = H.loadSdkIframeEmbedTestPage({
elements: [
{
component: "metabase-dashboard",
attributes: {
dashboardId: ORDERS_DASHBOARD_ID,
},
},
],
metabaseConfig: {
locale: "de",
},
});
frame.within(() => {
cy.findByText("Zeige die ersten 2,000 Zeilen").should("exist");
});
});
it("updates the question id with embed.setAttribute", () => {
const frame = H.loadSdkIframeEmbedTestPage({
elements: [
{
component: "metabase-question",
attributes: {
questionId: ORDERS_QUESTION_ID,
},
},
],
});
cy.wait("@getCardQuery");
frame.findByText("Orders, Count").should("not.exist");
cy.log("1. call embed.setAttribute to update the question id");
frame.window().then((win) => {
win.document
.querySelector("metabase-question")!
.setAttribute("question-id", ORDERS_COUNT_QUESTION_ID.toString());
});
cy.wait("@getCardQuery");
cy.get("iframe")
.should("be.visible")
.its("0.contentDocument")
.should("exist")
.within(() => {
cy.log("2. the question should be updated");
cy.findByText("Orders, Count").should("be.visible");
H.tableInteractive().within(() => {
cy.findByText("Count").should("be.visible");
cy.findByText("18,760").should("be.visible");
});
});
});
it("fires ready event after iframe is loaded", () => {
const frame = H.loadSdkIframeEmbedTestPage({
elements: [
{
component: "metabase-question",
attributes: {
questionId: ORDERS_QUESTION_ID,
},
},
],
onVisitPage: (win) => {
const element = win.document.querySelector("metabase-question")!;
element.addEventListener("ready", () => {
win.document.body.setAttribute(
"data-consumer-event-triggered",
"true",
);
});
// assert that the attribute is not set at the start
const attrValue = win.document.body.getAttribute(
"data-consumer-event-triggered",
);
expect(attrValue).to.not.equal("true");
},
});
cy.wait("@getCardQuery");
cy.log("ready event should be fired after the iframe is loaded");
cy.get("iframe").should("be.visible");
cy.get("body").should("have.attr", "data-consumer-event-triggered", "true");
cy.log("iframe content should now be loaded");
frame.within(() => {
H.assertSdkInteractiveQuestionOrdersUsable();
});
});
it("shows dashboard title when updateSettings({ withTitle: true }) is called", () => {
const frame = H.loadSdkIframeEmbedTestPage({
elements: [
{
component: "metabase-dashboard",
attributes: {
dashboardId: ORDERS_DASHBOARD_ID,
withTitle: false,
},
},
],
});
cy.wait("@getDashCardQuery");
cy.log("1. dashboard title should initially be hidden");
frame.within(() => {
cy.findByText("Orders in a dashboard").should("not.exist");
cy.findByText("Orders").should("be.visible");
});
cy.log("2. call setAttribute to show the title");
frame.window().then((win) => {
const element = win.document.querySelector("metabase-dashboard")!;
element.setAttribute("with-title", "true");
});
cy.log("3. dashboard title should now be visible");
getIframeWindow().findByText("Orders in a dashboard").should("be.visible");
});
describe("auto-refreshing dashboard", () => {
/**
* Unfortunately, cy.clock() doesn't seem to work with mocking the timing inside the iframe,
* so we have to use real timeouts here.
*/
it('does not automatically refresh the dashboard when "auto-refresh-interval" is not set', () => {
const frame = H.loadSdkIframeEmbedTestPage({
elements: [
{
component: "metabase-dashboard",
attributes: {
dashboardId: ORDERS_DASHBOARD_ID,
},
},
],
});
cy.wait("@getDashCardQuery").then(({ request }) => {
expect(request?.headers?.["x-metabase-client"]).to.equal(
"embedding-simple",
);
});
frame.within(() => {
cy.findByText("Orders in a dashboard").should("be.visible");
cy.findByText("Orders").should("be.visible");
H.assertTableRowsCount(2000);
});
cy.get("@getDashCardQuery.all").then((requests) => {
cy.wrap(requests.length).as("initialRequestCount");
});
cy.log("wait for the retrigger");
cy.wait(1000);
cy.get("@initialRequestCount").then((initialRequestCount) => {
cy.get("@getDashCardQuery.all").should(
"have.length",
initialRequestCount,
);
});
});
it('automatically refresh the dashboard when "auto-refresh-interval" is set', () => {
const frame = H.loadSdkIframeEmbedTestPage({
elements: [
{
component: "metabase-dashboard",
attributes: {
dashboardId: ORDERS_DASHBOARD_ID,
autoRefreshInterval: 1,
},
},
],
});
frame.within(() => {
cy.findByText("Orders in a dashboard").should("be.visible");
cy.findByText("Orders").should("be.visible");
H.assertTableRowsCount(2000);
});
cy.get("@getDashCardQuery.all").then((requests) => {
cy.wrap(requests.length).as("initialRequestCount");
});
cy.log("wait for the retrigger");
cy.get("@initialRequestCount").then((initialRequestCount) => {
cy.get("@getDashCardQuery.all").should(
"have.length.above",
initialRequestCount,
);
});
});
});
it("CSP nonces are set for custom expression styles (EMB-707)", () => {
const frame = H.loadSdkIframeEmbedTestPage({
elements: [
{
component: "metabase-question",
attributes: {
questionId: "new",
},
},
],
});
frame.within(() => {
cy.findByText("Orders").should("be.visible");
H.popover().within(() => {
cy.findByText("Orders").click();
});
cy.log("csp nonces should be set");
cy.get("style[nonce]")
.should("have.length.greaterThan", 0)
.first()
.should("have.attr", "nonce")
.and("have.length.greaterThan", 4);
cy.findByRole("button", { name: "Custom column" }).click();
cy.log("injected codemirror styles should be set");
cy.findByTestId("custom-expression-query-editor")
.should("be.visible")
.find(".cm-editor .cm-placeholder")
.and("have.css", "color", "rgb(136, 136, 136)");
cy.findByRole("button", { name: "Cancel" }).click();
});
});
describe("handleLink", () => {
beforeEach(() => {
// Create a question with a URL column for testing handleLink
cy.signInAsAdmin();
H.createDashboardWithQuestions({
dashboardName: "Dashboard with links",
questions: [
{
name: "Question with link column",
query: {
"source-table": ORDERS_TABLE_ID,
expressions: {
"link url": [
"concat",
"https://example.org/order/",
["field", ORDERS.ID, { "base-type": "type/BigInteger" }],
],
},
fields: [
["field", ORDERS.ID, { "base-type": "type/BigInteger" }],
["expression", "link url", { "base-type": "type/Text" }],
],
limit: 5,
},
visualization_settings: {
column_settings: {
'["name","ID"]': {
view_as: "link",
link_text: "Order {{ID}}",
link_url: "https://example.org/order/{{ID}}",
},
},
},
},
],
cards: [{ size_x: 24, size_y: 6, col: 0, row: 0 }],
}).then(({ dashboard }) => {
cy.wrap(dashboard.id).as("linkDashboardId");
});
cy.signOut();
});
it("calls handleLink when a link is clicked and prevents navigation when handled: true", function () {
const frame = H.loadSdkIframeEmbedTestPage({
elements: [
{
component: "metabase-dashboard",
attributes: {
dashboardId: this.linkDashboardId,
},
},
],
insertHtml: {
afterEmbed: `
<script>
window.handleLinkCalls = [];
window.metabaseConfig.pluginsConfig = {
handleLink: (url) => {
window.handleLinkCalls.push(url);
return { handled: true };
},
};
</script>
`,
},
});
cy.wait("@getDashCardQuery");
// Spy on the iframe's anchor clicks and window.open
// so we can check the default behavior is prevented
cy.get("iframe").then(($iframe) => {
const iframeWin: any = $iframe[0].contentWindow;
expect(iframeWin).to.not.be.null;
iframeWin.__blankLinkClicks = [];
iframeWin.__windowOpenCalls = [];
iframeWin.HTMLAnchorElement.prototype.click = function () {
iframeWin.__blankLinkClicks.push(this.href);
};
iframeWin.open = function (...args: unknown[]) {
iframeWin.__windowOpenCalls.push(args);
};
});
frame.within(() => {
cy.findByText("Order 1").click();
});
cy.log("Verify handleLink was called with the correct URL");
cy.window()
.its("handleLinkCalls")
.should("have.length", 1)
.its(0)
.should("include", "https://example.org/order/1");
cy.log("Verify that no default navigation happened");
cy.get("iframe")
.its("0.contentWindow.__blankLinkClicks")
.should("have.length", 0);
cy.get("iframe")
.its("0.contentWindow.__windowOpenCalls")
.should("have.length", 0);
});
});
describe("analytics", () => {
beforeEach(() => {
H.resetSnowplow();
H.prepareSdkIframeEmbedTest({
enabledAuthMethods: ["jwt"],
});
H.enableTracking();
});
it("should send an modular embedding usage event", () => {
let proxyCallCount = 0;
cy.intercept("POST", "/api/analytics-proxy", () => {
proxyCallCount++;
}).as("analyticsProxy");
cy.signOut();
cy.visit("http://localhost:4000");
const frame = H.loadSdkIframeEmbedTestPage({
origin: "http://different-than-metabase-instance.com",
elements: [
{
component: "metabase-dashboard",
attributes: {
dashboardId: ORDERS_DASHBOARD_ID,
"with-subscriptions": true,
},
},
{
component: "metabase-question",
attributes: {
questionId: ORDERS_QUESTION_ID,
},
},
{
component: "metabase-question",
attributes: {
questionId: "new",
},
},
{
component: "metabase-browser",
attributes: {},
},
],
selector: `[dashboard-id="${ORDERS_DASHBOARD_ID}"] > iframe`, // get only the first iframe
});
frame.within(() => {
cy.findByText("Orders in a dashboard").should("be.visible");
cy.findByText("Orders").should("be.visible");
H.assertTableRowsCount(2000);
});
H.expectUnstructuredSnowplowEvent({
event: "setup",
global: {
auth_method: "sso",
locale_used: false,
},
components: [
{
name: "dashboard",
properties: [
{
name: "drills",
values: [
{ group: "false", value: 0 },
{ group: "true", value: 1 },
],
},
{
name: "with_downloads",
values: [
{ group: "false", value: 1 },
{ group: "true", value: 0 },
],
},
{
name: "with_title",
values: [
{ group: "false", value: 0 },
{ group: "true", value: 1 },
],
},
{
name: "with_subscriptions",
values: [
{ group: "false", value: 0 },
{ group: "true", value: 1 },
],
},
{
name: "auto_refresh_interval",
values: [
{ group: "false", value: 1 },
{ group: "true", value: 0 },
],
},
{
name: "enable_entity_navigation",
values: [
{ group: "false", value: 1 },
{ group: "true", value: 0 },
],
},
],
},
{
name: "question",
properties: [
{
name: "drills",
values: [
{ group: "false", value: 0 },
{ group: "true", value: 1 },
],
},
{
name: "with_downloads",
values: [
{ group: "false", value: 1 },
{ group: "true", value: 0 },
],
},
{
name: "with_title",
values: [
{ group: "false", value: 0 },
{ group: "true", value: 1 },
],
},
{
name: "is_save_enabled",
values: [
{ group: "false", value: 1 },
{ group: "true", value: 0 },
],
},
{
name: "with_alerts",
values: [
{ group: "false", value: 1 },
{ group: "true", value: 0 },
],
},
],
},
{
name: "exploration",
properties: [
{
name: "is_save_enabled",
values: [
{ group: "false", value: 1 },
{ group: "true", value: 0 },
],
},
{
name: "id_new_native",
values: [
{ group: "false", value: 1 },
{ group: "true", value: 0 },
],
},
{
name: "id_new",
values: [
{ group: "false", value: 0 },
{ group: "true", value: 1 },
],
},
],
},
{
name: "browser",
properties: [
{
name: "read_only",
values: [
{ group: "false", value: 0 },
{ group: "true", value: 1 },
],
},
{
name: "enable_entity_navigation",
values: [
{ group: "false", value: 1 },
{ group: "true", value: 0 },
],
},
],
},
],
});
cy.wrap(null).then(() => {
expect(proxyCallCount).to.eq(0);
});
});
it("should send locale_used=true when locale is configured", () => {
cy.signOut();
cy.visit("http://localhost:4000");
const frame = H.loadSdkIframeEmbedTestPage({
origin: "http://different-than-metabase-instance.com",
elements: [
{
component: "metabase-dashboard",
attributes: {
dashboardId: ORDERS_DASHBOARD_ID,
},
},
],
metabaseConfig: {
locale: "de",
},
selector: `[dashboard-id="${ORDERS_DASHBOARD_ID}"] > iframe`,
});
frame.within(() => {
cy.findByText("Orders in a dashboard").should("be.visible");
});
H.expectUnstructuredSnowplowEvent({
event: "setup",
global: {
auth_method: "sso",
locale_used: true,
},
components: [
{
name: "dashboard",
properties: [
{
name: "drills",
values: [
{ group: "false", value: 0 },
{ group: "true", value: 1 },
],
},
{
name: "with_downloads",
values: [
{ group: "false", value: 1 },
{ group: "true", value: 0 },
],
},
{
name: "with_title",
values: [
{ group: "false", value: 0 },
{ group: "true", value: 1 },
],
},
{
name: "with_subscriptions",
values: [
{ group: "false", value: 1 },
{ group: "true", value: 0 },
],
},
{
name: "auto_refresh_interval",
values: [
{ group: "false", value: 1 },
{ group: "true", value: 0 },
],
},
{
name: "enable_entity_navigation",
values: [
{ group: "false", value: 1 },
{ group: "true", value: 0 },
],
},
],
},
],
});
});
it("should not send an modular embedding usage event in the preview", () => {
cy.visit(`/question/${ORDERS_QUESTION_ID}`);
H.openEmbedJsModal();
H.embedModalEnableEmbedding();
H.waitForSimpleEmbedIframesToLoad();
H.getSimpleEmbedIframeContent().within(() => {
cy.findByText("Orders").should("be.visible");
});
H.expectUnstructuredSnowplowEvent(
{
event: "setup",
global: {
auth_method: "session",
locale_used: false,
},
components: [],
},
// Expect that the usage event shouldn't be sent
0,
);
});
});
});
const getIframeWindow = () =>
cy
.get("iframe")
.should("be.visible")
.its("0.contentDocument")
.should("exist")
.its("body")
.should("not.be.empty");