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
const { H } = cy;
import {
MetabaseProvider,
StaticQuestion,
type StaticQuestionProps,
} from "@metabase/embedding-sdk-react";
import { SAMPLE_DB_ID } from "e2e/support/cypress_data";
import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
import {
createQuestion,
modal,
popover,
selectScheduleTime,
} from "e2e/support/helpers";
import { getSdkRoot } from "e2e/support/helpers/e2e-embedding-sdk-helpers";
import {
DEFAULT_SDK_AUTH_PROVIDER_CONFIG,
mountSdk,
mountSdkContent,
mountStaticQuestion,
} from "e2e/support/helpers/embedding-sdk-component-testing";
import { signInAsAdminAndEnableEmbeddingSdk } from "e2e/support/helpers/embedding-sdk-testing";
import { mockAuthProviderAndJwtSignIn } from "e2e/support/helpers/embedding-sdk-testing/embedding-sdk-helpers";
const { ORDERS, ORDERS_ID } = SAMPLE_DATABASE;
describe("scenarios > embedding-sdk > static-question", () => {
beforeEach(() => {
signInAsAdminAndEnableEmbeddingSdk();
createQuestion({
name: "47563",
query: {
"source-table": ORDERS_ID,
aggregation: [["max", ["field", ORDERS.QUANTITY, null]]],
breakout: [["field", ORDERS.PRODUCT_ID, null]],
limit: 2,
},
}).then(({ body: question }) => {
cy.wrap(question.id).as("questionId");
cy.wrap(question.entity_id).as("questionEntityId");
});
cy.signOut();
mockAuthProviderAndJwtSignIn();
});
it("should show question content", () => {
mountStaticQuestion();
getSdkRoot().within(() => {
cy.findByText("Product ID").should("be.visible");
cy.findByText("Max of Quantity").should("be.visible");
cy.log("should not show question title by default");
cy.findByText("47563").should("not.exist");
});
});
it("should not render the top bar when it has no visible children, and restore it when children appear", () => {
cy.intercept("GET", "/api/card/*").as("getCard");
cy.intercept("POST", "/api/card/*/query").as("cardQuery");
cy.intercept("GET", "/api/user/current").as("getUser");
cy.get<number>("@questionId").then((questionId) => {
const consoleErrorSpy = cy.spy(console, "error").as("consoleError");
const renderQuestion = (props: Partial<StaticQuestionProps>) => (
<MetabaseProvider authConfig={DEFAULT_SDK_AUTH_PROVIDER_CONFIG}>
<StaticQuestion questionId={questionId} {...props} />
</MetabaseProvider>
);
mountSdk(
renderQuestion({ title: false, withChartTypeSelector: false }),
).then(({ rerender }) => {
cy.wait("@getUser");
cy.wait("@getCard");
getSdkRoot().within(() => {
cy.findByText("Product ID").should("be.visible");
cy.findByTestId("static-question-top-bar").should("not.exist");
});
rerender(renderQuestion({ title: true, withChartTypeSelector: false }));
getSdkRoot().within(() => {
cy.findByTestId("static-question-top-bar")
.should("exist")
.and("be.visible");
});
rerender(
renderQuestion({ title: false, withChartTypeSelector: false }),
);
getSdkRoot().within(() => {
cy.findByTestId("static-question-top-bar").should("not.exist");
});
rerender(renderQuestion({ title: false, withDownloads: true }));
getSdkRoot().within(() => {
cy.findByTestId("static-question-top-bar")
.should("exist")
.and("be.visible");
});
rerender(renderQuestion({ title: false, withDownloads: false }));
getSdkRoot().within(() => {
cy.findByTestId("static-question-top-bar").should("not.exist");
});
cy.then(() => {
const refWarningCalls = consoleErrorSpy
.getCalls()
.filter((call: sinon.SinonSpyCall) =>
String(call.args[0]).includes("cannot be given refs"),
);
expect(refWarningCalls).to.have.length(0);
});
});
});
});
it("should show question title", () => {
mountStaticQuestion({ title: true });
getSdkRoot().within(() => {
cy.findByText("47563").should("be.visible");
});
});
it("should show custom question title", () => {
mountStaticQuestion({ title: "Acme Inc" });
getSdkRoot().within(() => {
cy.findByText("47563").should("not.exist");
cy.findByText("Acme Inc").should("be.visible");
});
});
describe("loading behavior for both entity IDs and number IDs (metabase#49581)", () => {
const successTestCases = [
{
name: "correct entity ID",
questionIdAlias: "@questionEntityId",
},
{
name: "correct number ID",
questionIdAlias: "@questionId",
},
];
const failureTestCases = [
{
name: "wrong entity ID",
questionId: "VFCGVYPVtLzCtt4teeoW4",
},
{
name: "one too many entity ID character",
questionId: "VFCGVYPVtLzCtt4teeoW49",
},
{
name: "wrong number ID",
questionId: 9999,
},
];
successTestCases.forEach(({ name, questionIdAlias }) => {
it(`should load question content for ${name}`, () => {
cy.get<number>(questionIdAlias).then((questionId) => {
mountStaticQuestion({ questionId });
});
getSdkRoot().within(() => {
cy.findByText("Product ID").should("be.visible");
cy.findByText("Max of Quantity").should("be.visible");
});
});
});
failureTestCases.forEach(({ name, questionId }) => {
it(`should show an error message for ${name}`, () => {
mountStaticQuestion({ questionId }, { shouldAssertCardQuery: false });
getSdkRoot().within(() => {
const expectedErrorMessage = `Question ${questionId} not found. Make sure you pass the correct ID.`;
cy.findByRole("alert").should("have.text", expectedErrorMessage);
cy.findByText("Product ID").should("not.exist");
cy.findByText("Max of Quantity").should("not.exist");
});
});
});
});
describe("alerts button", () => {
beforeEach(() => {
cy.signInAsAdmin();
H.setupSMTP();
cy.signOut();
});
it("should be able to create, edit, and delete alerts", () => {
// QuestionAlertListModal stays in null-render limbo until
// /api/notification?card_id=... resolves. On fetch (microtask
// resolution), the click can land before the recipients/channels
// queries have fired; wait for the GET both on first open and on
// re-open so the modal has its picked variant by the time we assert.
cy.intercept("GET", "/api/notification?card_id=*").as("listAlerts");
mountStaticQuestion({
withAlerts: true,
});
cy.log("alerts button is visible and clickable");
getSdkRoot().button("Alerts").should("be.visible").click();
cy.log("alerts modal is open");
cy.wait("@listAlerts");
selectScheduleTime();
modal().within(() => {
cy.findByRole("heading", { name: "New alert" }).should("be.visible");
cy.button("Done").click();
});
modal().should("not.exist");
cy.log("alerts list modal");
getSdkRoot().button("Alerts").should("be.visible").click();
cy.wait("@listAlerts");
modal().within(() => {
cy.findByRole("heading", { name: "Edit alerts" }).should("be.visible");
cy.findByText("Alert when this has results").should("be.visible");
cy.findByText("[email protected]").should("be.visible");
cy.findByText("Check daily at 8:00 AM").should("be.visible").click();
cy.findByRole("heading", { name: "Edit alert" }).should("be.visible");
// The second input is a hidden input, so we need to ignore it.
cy.findAllByDisplayValue("daily").first().click();
});
popover().findByRole("option", { name: "weekly" }).click();
selectScheduleTime();
modal().within(() => {
cy.button("Save changes").click();
cy.findByRole("heading", { name: "Edit alerts" }).should("be.visible");
cy.findByText("Check on Monday at 8:00 AM").should("be.visible");
});
cy.log("delete the alert");
modal().within(() => {
cy.findByText("Check on Monday at 8:00 AM").realHover();
cy.button("Delete this alert").click();
cy.findByRole("heading", { name: "Delete this alert?" }).should(
"be.visible",
);
cy.button("Delete it").click();
});
cy.log("the alert is deleted");
getSdkRoot().button("Alerts").should("be.visible").click();
modal().findByRole("heading", { name: "New alert" }).should("be.visible");
});
it("should hide the Alerts button when the question's container is narrow", () => {
cy.intercept("GET", "/api/card/*").as("getCard");
cy.get<number>("@questionId").then((questionId) => {
mountSdkContent(
<div style={{ width: 400 }}>
<StaticQuestion questionId={questionId} withAlerts />
</div>,
);
});
cy.wait("@getCard");
getSdkRoot().button("Alerts").should("not.exist");
});
it("should show the Alerts button when the question's container is wide", () => {
mountStaticQuestion({ withAlerts: true });
getSdkRoot().button("Alerts").should("be.visible");
});
});
it("should not request /api/card/undefined when clicking a data point on a query-only ad-hoc card", () => {
// A query-only ad-hoc card has no saved card id. A static question must not
// navigate on a chart click — previously it did, fetching
// `GET /api/card/undefined` (the new card's id was `undefined`).
cy.intercept("GET", "/api/card/undefined").as("getUndefinedCard");
mountSdkContent(
<StaticQuestion
card={{
query: {
database: SAMPLE_DB_ID,
type: "query",
query: {
"source-table": ORDERS_ID,
aggregation: [["count"]],
breakout: [
["field", ORDERS.CREATED_AT, { "temporal-unit": "month" }],
],
limit: 5,
},
},
}}
/>,
);
getSdkRoot().within(() => {
cy.log("the aggregated query renders a cartesian chart");
H.cartesianChartCircle().should("have.length.greaterThan", 0);
cy.log("clicking a data point must not navigate to a new card");
H.cartesianChartCircle().first().click();
// The chart stays put (retry gives any erroneous navigation time to fire).
H.cartesianChartCircle().should("have.length.greaterThan", 0);
});
cy.get("@getUndefinedCard.all").should("have.length", 0);
});
});