Skip to content

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

e2e2unit: Binning (#85407)

Nemanja Glumac516af13bc7e2
diff --git a/e2e/test/scenarios/binning/binning-options.cy.spec.js b/e2e/test/scenarios/binning/binning-options.cy.spec.js
deleted file mode 100644
index a98b30319fa..00000000000
--- a/e2e/test/scenarios/binning/binning-options.cy.spec.js
+++ /dev/null
@@ -1,321 +0,0 @@
-const { H } = cy;
-import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
-
-const { ORDERS_ID, PEOPLE_ID } = SAMPLE_DATABASE;
-
-const NUMBER_BUCKETS = [
- "Auto bin",
- "10 bins",
- "50 bins",
- "100 bins",
- "Don't bin",
-];
-
-const TIME_BUCKETS = [
- "Minute",
- "Hour",
- "Day",
- "Week",
- "Month",
- "Quarter",
- "Year",
- "Minute of hour",
- "Hour of day",
- "Day of week",
- "Day of month",
- "Day of year",
- "Week of year",
- "Month of year",
- "Quarter of year",
- "Don't bin",
-];
-
-const LONGITUDE_BUCKETS = [
- "Auto bin",
- "Bin every 0.1 degrees",
- "Bin every 1 degree",
- "Bin every 10 degrees",
- "Bin every 20 degrees",
- "Bin every 0.05 degrees",
- "Bin every 0.01 degrees",
- "Bin every 0.005 degrees",
- "Don't bin",
-];
-
-/**
- * Makes sure that all binning options (bucket sizes) are rendered correctly for the regular table.
- * 1. no option should be rendered multiple times
- * 2. the selected option should be highlighted when the popover with all options opens
- * 3. choosing another option re-runs the query with the new bucket size
- *
- * This spec covers the following issues:
- * - metabase#15574
- */
-
-describe("scenarios > binning > binning options", () => {
- beforeEach(() => {
- H.restore();
- cy.signInAsAdmin();
- });
-
- context("via simple question", () => {
- it("should render and apply number binning options", () => {
- chooseInitialBinningOption({ table: ORDERS_ID, column: "Total" });
- getTitle("Count by Total: Auto binned");
-
- openBinningListForDimension("Total", "Auto binned");
- getAllOptions({ options: NUMBER_BUCKETS, isSelected: "Auto bin" });
- cy.intercept("POST", "/api/dataset").as("rerun");
- selectOption("50 bins");
- cy.wait("@rerun");
-
- getTitle("Count by Total: 50 bins");
- H.chartPathWithFillColor("#509EE3");
- // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
- cy.findByText("70");
- });
-
- it("should render and apply time series binning options", () => {
- chooseInitialBinningOption({ table: ORDERS_ID, column: "Created At" });
- getTitle("Count by Created At: Month");
-
- openBinningListForDimension("Created At", "by month");
- getAllOptions({
- options: TIME_BUCKETS,
- isSelected: "Month",
- shouldExpandList: true,
- });
- cy.intercept("POST", "/api/dataset").as("rerun");
- selectOption("Quarter");
- cy.wait("@rerun");
-
- getTitle("Count by Created At: Quarter");
- H.cartesianChartCircle();
- // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
- cy.findByText("Q2 2026");
- });
-
- it("should render and apply longitude/latitude binning options", () => {
- chooseInitialBinningOption({ table: PEOPLE_ID, column: "Longitude" });
- getTitle("Count by Longitude: Auto binned");
-
- openBinningListForDimension("Longitude", "Auto binned");
- getAllOptions({
- options: LONGITUDE_BUCKETS,
- isSelected: "Auto bin",
- shouldExpandList: true,
- });
- cy.intercept("POST", "/api/dataset").as("rerun");
- selectOption("Bin every 20 degrees");
- cy.wait("@rerun");
-
- getTitle("Count by Longitude: 20°");
- H.chartPathWithFillColor("#509EE3");
- // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
- cy.findByText("180° W");
- });
-
- it("should apply a number bucket picked from an unselected column", () => {
- H.openTable({ table: ORDERS_ID });
- H.summarize();
- H.changeBinningForDimension({
- name: "Total",
- fromBinning: "Auto bin",
- toBinning: "50 bins",
- });
-
- getTitle("Count by Total: 50 bins");
- H.chartPathWithFillColor("#509EE3");
- // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
- cy.findByText("70");
- });
- });
-
- context("via custom question", () => {
- it("should render and apply number binning options", () => {
- chooseInitialBinningOption({
- table: ORDERS_ID,
- mode: "notebook",
- column: "Total",
- });
-
- getTitle("Count by Total: Auto binned");
-
- // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
- cy.findByText("Total: Auto binned").click();
- openBinningListForDimension("Total", "Auto binned");
-
- getAllOptions({ options: NUMBER_BUCKETS, isSelected: "Auto bin" });
- selectOption("50 bins");
-
- H.getNotebookStep("summarize").findByText("Total: 50 bins");
- H.visualize();
-
- getTitle("Count by Total: 50 bins");
- H.chartPathWithFillColor("#509EE3");
- // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
- cy.findByText("70");
- });
-
- it("should render and apply time series binning options", () => {
- chooseInitialBinningOption({
- table: ORDERS_ID,
- mode: "notebook",
- column: "Created At",
- });
-
- getTitle("Count by Created At: Month");
-
- // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
- cy.findByText("Created At: Month").click();
- openBinningListForDimension("Created At", "by month");
-
- getAllOptions({
- options: TIME_BUCKETS,
- isSelected: "Month",
- shouldExpandList: true,
- });
- selectOption("Quarter");
-
- H.getNotebookStep("summarize").findByText("Created At: Quarter");
- H.visualize();
-
- getTitle("Count by Created At: Quarter");
- H.cartesianChartCircle();
- // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
- cy.findByText("Q2 2026");
- });
-
- it("should render and apply longitude/latitude binning options", () => {
- chooseInitialBinningOption({
- table: PEOPLE_ID,
- mode: "notebook",
- column: "Longitude",
- });
-
- getTitle("Count by Longitude: Auto binned");
-
- // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
- cy.findByText("Longitude: Auto binned").click();
- openBinningListForDimension("Longitude", "Auto binned");
-
- getAllOptions({
- options: LONGITUDE_BUCKETS,
- isSelected: "Auto bin",
- shouldExpandList: true,
- });
- selectOption("Bin every 20 degrees");
-
- H.getNotebookStep("summarize").findByText("Longitude: 20°");
- H.visualize();
-
- getTitle("Count by Longitude: 20°");
- H.chartPathWithFillColor("#509EE3");
- // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
- cy.findByText("180° W");
- });
- });
-
- context("via column popover", () => {
- it("should work for number", () => {
- H.openTable({ table: ORDERS_ID });
- H.tableHeaderClick("Total");
- H.popover().findByText("Distribution").click();
-
- getTitle("Count by Total: Auto binned");
-
- H.chartPathWithFillColor("#509EE3");
- // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
- cy.findByText("60");
- });
-
- it("should work for time series", () => {
- H.openTable({ table: ORDERS_ID });
- H.tableHeaderClick("Created At");
- H.popover().findByText("Distribution").click();
-
- getTitle("Count by Created At: Month");
-
- H.cartesianChartCircle();
- // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
- cy.findByText("April 2026");
-
- cy.log(
- "time series footer highlights the current bucket (metabase#11183)",
- );
- cy.findByTestId("timeseries-bucket-button").click();
- H.popover()
- .findByText("Month")
- .parent()
- .should("have.attr", "aria-selected", "true");
- });
-
- it("should work for longitude/latitude", () => {
- H.openTable({ table: PEOPLE_ID });
- H.tableHeaderClick("Longitude");
- H.popover().findByText("Distribution").click();
-
- getTitle("Count by Longitude: Auto binned");
-
- H.chartPathWithFillColor("#509EE3");
- // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
- cy.findByText("170° W");
- });
- });
-});
-
-function chooseInitialBinningOption({ table, column, mode = null } = {}) {
- H.openTable({ table, mode });
- H.summarize({ mode });
-
- if (mode === "notebook") {
- cy.findByText("Count of rows").click();
- cy.findByText("Pick a column to group by").click();
- cy.findByText(column).click();
- } else {
- cy.findByTestId("sidebar-right").contains(column).first().click();
- }
-}
-
-function openBinningListForDimension(column, binning) {
- H.getBinningButtonForDimension({ name: column, isSelected: true })
- .should("contain", binning)
- .click();
-}
-
-function getTitle(title) {
- cy.findByText(title);
-}
-
-function getAllOptions({ options, isSelected, shouldExpandList } = {}) {
- const selectedOption = options.find((option) => option === isSelected);
- const regularOptions = options.filter((option) => option !== isSelected);
-
- // Custom question has two popovers open.
- // The binning options are in the latest (last) one.
- // Using `.last()` works even when only one popover is open so it covers both scenarios.
- // eslint-disable-next-line metabase/no-unsafe-element-filtering
- H.popover()
- .last()
- .within(() => {
- if (shouldExpandList) {
- cy.findByText("More…").click();
- }
-
- regularOptions.forEach((option) => {
- // Implicit assertion - will fail if string is rendered multiple times
- cy.findByText(option);
- });
-
- if (isSelected) {
- cy.findByText(selectedOption)
- .closest("li")
- .should("have.attr", "aria-selected", "true");
- }
- });
-}
-
-function selectOption(option) {
- // eslint-disable-next-line metabase/no-unsafe-element-filtering
- H.popover().last().findByText(option).click();
-}
diff --git a/e2e/test/scenarios/binning/binning-reproductions.cy.spec.js b/e2e/test/scenarios/binning/binning-reproductions.cy.spec.js
deleted file mode 100644
index 1003001678d..00000000000
--- a/e2e/test/scenarios/binning/binning-reproductions.cy.spec.js
+++ /dev/null
@@ -1,112 +0,0 @@
-const { H } = cy;
-import { SAMPLE_DB_ID } from "e2e/support/cypress_data";
-import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
-
-const { ORDERS, ORDERS_ID } = SAMPLE_DATABASE;
-
-const CREATED_AT_DAY_OF_WEEK_BREAKOUT = [
- "field",
- ORDERS.CREATED_AT,
- {
- "base-type": "type/DateTime",
- "temporal-unit": "day-of-week",
- },
-];
-
-describe("binning related reproductions", () => {
- beforeEach(() => {
- H.restore();
- cy.signInAsAdmin();
- });
-
- it("should run a query that breaks out by a temporal column of a joined native question (metabase#18646)", () => {
- cy.log("Reproduces metabase#18646");
- const joinAlias = "18646 - Product";
-
- H.createNativeQuestion(
- {
- name: "18646",
- native: { query: "select * from products" },
- },
- { loadMetadata: true, wrapId: true },
- );
-
- cy.get("@questionId").then((cardId) => {
- H.visitQuestionAdhoc({
- dataset_query: {
- database: SAMPLE_DB_ID,
- type: "query",
- query: {
- "source-table": ORDERS_ID,
- joins: [
- {
- alias: joinAlias,
- "source-table": `card__${cardId}`,
- fields: "all",
- condition: [
- "=",
- ["field", ORDERS.PRODUCT_ID, null],
- [
- "field",
- "ID",
- { "base-type": "type/BigInteger", "join-alias": joinAlias },
- ],
- ],
- },
- ],
- aggregation: [["count"]],
- breakout: [
- [
- "field",
- "CREATED_AT",
- {
- "base-type": "type/DateTimeWithLocalTZ",
- "join-alias": joinAlias,
- "temporal-unit": "month",
- },
- ],
- ],
- },
- },
- display: "line",
- });
- });
-
- H.cartesianChartCircle().should("have.length.greaterThan", 1);
- });
-
- it("can drill through question with temporal extraction breakout without semantic type defined for the column (metabase#23851)", () => {
- cy.log("Reproduces metabase#23851");
- cy.intercept("POST", "/api/dataset").as("dataset");
- cy.request("PUT", `/api/field/${ORDERS.CREATED_AT}`, {
- semantic_type: null,
- });
- H.createQuestion(
- {
- query: {
- "source-table": ORDERS_ID,
- aggregation: [["count"]],
- breakout: [CREATED_AT_DAY_OF_WEEK_BREAKOUT],
- },
- display: "bar",
- },
- { visitQuestion: true },
- );
-
- H.chartPathWithFillColor("#509EE3").should("have.length", 7);
- H.chartPathWithFillColor("#509EE3").eq(5).click();
- H.popover().findByText("See these Orders").click();
-
- cy.wait("@dataset");
-
- cy.findByTestId("filter-pill").should(
- "have.text",
- "Created At: Day of week is equal to 6",
- );
- cy.findAllByTestId("cell-data").should("contain", "37.65"); // might change based on a year
- H.openNotebook();
- H.getNotebookStep("filter")
- .findByText("Created At: Day of week is equal to 6")
- .should("exist");
- });
-});
diff --git a/e2e/test/scenarios/binning/binning.cy.spec.ts b/e2e/test/scenarios/binning/binning.cy.spec.ts
new file mode 100644
index 00000000000..d19e0fd1d57
--- /dev/null
+++ b/e2e/test/scenarios/binning/binning.cy.spec.ts
@@ -0,0 +1,181 @@
+const { H } = cy;
+import { SAMPLE_DB_ID } from "e2e/support/cypress_data";
+import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
+
+const { ORDERS, ORDERS_ID } = SAMPLE_DATABASE;
+
+const BAR_COLOR = "#509EE3";
+
+describe("scenarios > binning", () => {
+ beforeEach(() => {
+ H.restore();
+ cy.signInAsAdmin();
+ });
+
+ it("should bin a breakout, change its bucket and keep it after a save", () => {
+ H.openOrdersTable();
+ H.summarize();
+ cy.intercept("POST", "/api/dataset").as("breakoutQuery");
+
+ cy.log("temporal bucket");
+ H.getDimensionByName({ name: "Created At" }).click();
+ cy.wait("@breakoutQuery")
+ .its("request.body.stages.0.breakout.0.1.temporal-unit")
+ .should("eq", "month");
+ H.changeBinningForDimension({
+ name: "Created At",
+ fromBinning: "by month",
+ toBinning: "Year",
+ isSelected: true,
+ });
+ cy.wait("@breakoutQuery").then(({ request, response }) => {
+ expect(request.body.stages[0].breakout[0][1]["temporal-unit"]).to.eq(
+ "year",
+ );
+ const rows: [string, number][] = response?.body.data.rows ?? [];
+ expect(rows).to.have.length.greaterThan(1);
+ rows.forEach(([year]) => expect(year).to.match(/^\d{4}-01-01T/));
+ H.echartsContainer()
+ .findByText(rows[0][0].slice(0, 4))
+ .should("be.visible");
+ });
+
+ cy.log("default binning");
+ H.getDimensionByName({ name: "Total" }).click();
+ cy.wait("@breakoutQuery")
+ .its("request.body.stages.0.breakout.0.1.binning")
+ .should("include", { strategy: "default" });
+ H.queryBuilderHeader()
+ .findByText("Count by Total: Auto binned")
+ .should("be.visible");
+ H.chartPathWithFillColor(BAR_COLOR).should("have.length.greaterThan", 1);
+
+ cy.log("explicit binning strategy");
+ H.changeBinningForDimension({
+ name: "Total",
+ fromBinning: "Auto binned",
+ toBinning: "10 bins",
+ isSelected: true,
+ });
+ cy.wait("@breakoutQuery")
+ .its("request.body.stages.0.breakout.0.1.binning")
+ .should("include", { strategy: "num-bins", "num-bins": 10 });
+ H.queryBuilderHeader()
+ .findByText("Count by Total: 10 bins")
+ .should("be.visible");
+ H.chartPathWithFillColor(BAR_COLOR)
+ .should("have.length.greaterThan", 1)
+ .and("have.length.at.most", 10);
+ H.rightSidebar().button("Done").click();
+
+ cy.log("the binning survives a save and a reload");
+ H.saveQuestionToCollection("Binned totals");
+ cy.intercept("POST", "/api/card/*/query").as("cardQuery");
+ cy.reload();
+ cy.wait("@cardQuery")
+ .its("response.body.data.cols.0.display_name")
+ .should("eq", "Total: 10 bins");
+ H.queryBuilderHeader()
+ .findByDisplayValue("Binned totals")
+ .should("be.visible");
+ H.echartsContainer().findByText("Total: 10 bins").should("be.visible");
+ H.chartPathWithFillColor(BAR_COLOR)
+ .should("have.length.greaterThan", 1)
+ .and("have.length.at.most", 10);
+ });
+
+ it("should run a query that breaks out by a temporal column of a joined native question (metabase#18646)", () => {
+ const joinAlias = "18646 - Product";
+
+ H.createNativeQuestion(
+ {
+ name: "18646",
+ native: { query: "select * from products" },
+ },
+ { loadMetadata: true, wrapId: true },
+ );
+
+ cy.get<number>("@questionId").then((cardId) => {
+ H.visitQuestionAdhoc({
+ dataset_query: {
+ database: SAMPLE_DB_ID,
+ type: "query",
+ query: {
+ "source-table": ORDERS_ID,
+ joins: [
+ {
+ alias: joinAlias,
+ "source-table": `card__${cardId}`,
+ fields: "all",
+ condition: [
+ "=",
+ ["field", ORDERS.PRODUCT_ID, null],
+ [
+ "field",
+ "ID",
+ { "base-type": "type/BigInteger", "join-alias": joinAlias },
+ ],
+ ],
+ },
+ ],
+ aggregation: [["count"]],
+ breakout: [
+ [
+ "field",
+ "CREATED_AT",
+ {
+ "base-type": "type/DateTimeWithLocalTZ",
+ "join-alias": joinAlias,
+ "temporal-unit": "month",
+ },
+ ],
+ ],
+ },
+ },
+ display: "line",
+ });
+ });
+
+ H.cartesianChartCircle().should("have.length.greaterThan", 1);
+ });
+
+ it("can drill through question with temporal extraction breakout without semantic type defined for the column (metabase#23851)", () => {
+ cy.intercept("POST", "/api/dataset").as("dataset");
+ cy.request("PUT", `/api/field/${ORDERS.CREATED_AT}`, {
+ semantic_type: null,
+ });
+ H.createQuestion(
+ {
+ query: {
+ "source-table": ORDERS_ID,
+ aggregation: [["count"]],
+ breakout: [
+ [
+ "field",
+ ORDERS.CREATED_AT,
+ { "base-type": "type/DateTime", "temporal-unit": "day-of-week" },
+ ],
+ ],
+ },
+ display: "bar",
+ },
+ { visitQuestion: true },
+ );
+
+ H.chartPathWithFillColor(BAR_COLOR).should("have.length", 7);
+ H.chartPathWithFillColor(BAR_COLOR).eq(5).click();
+ H.popover().findByText("See these Orders").click();
+
+ cy.wait("@dataset");
+
+ cy.findByTestId("filter-pill").should(
+ "have.text",
+ "Created At: Day of week is equal to 6",
+ );
+ cy.findAllByTestId("cell-data").should("contain", "37.65");
+ H.openNotebook();
+ H.getNotebookStep("filter")
+ .findByText("Created At: Day of week is equal to 6")
+ .should("be.visible");
+ });
+});
diff --git a/e2e/test/scenarios/binning/correctness/longitude.cy.spec.js b/e2e/test/scenarios/binning/correctness/longitude.cy.spec.js
deleted file mode 100644
index 7e0629b0ffc..00000000000
--- a/e2e/test/scenarios/binning/correctness/longitude.cy.spec.js
+++ /dev/null
@@ -1,121 +0,0 @@
-import { openPopoverFromDefaultBucketSize } from "e2e/support/helpers";
-
-const { H } = cy;
-
-import { LONGITUDE_OPTIONS } from "./shared/constants";
-
-describe("scenarios > binning > correctness > longitude", () => {
- beforeEach(() => {
- H.restore();
- cy.signInAsAdmin();
- });
-
- it("should return correct values for every bucket size", () => {
- // Increase viewport to allow checking x-axis ticks values on dense data
- cy.viewport(1440, 800);
- H.openPeopleTable();
- H.summarize();
-
- cy.log("the first bucket is picked from the unselected column");
- openPopoverFromDefaultBucketSize("Longitude", "Auto bin");
-
- Object.entries(LONGITUDE_OPTIONS).forEach(
- (
- [bucketSize, { selected, representativeValues, isHiddenByDefault }],
- index,
- entries,
- ) => {
- cy.log(bucketSize);
-
- if (index > 0) {
- openSelectedBinningPopover();
- }
-
- // The popover opens expanded when the current bucket is a hidden one.
- const isExpanded =
- index > 0 && Boolean(entries[index - 1][1].isHiddenByDefault);
-
- pickBucket({
- bucketSize,
- shouldExpand: Boolean(isHiddenByDefault) && !isExpanded,
- alias: `dataset-${index}`,
- });
-
- cy.get("li[aria-selected='true']")
- .should("contain", "Longitude")
- .and("contain", selected);
-
- cy.findByText("Done").click();
-
- getTitle(`Count by Longitude: ${selected}`);
- H.chartPathWithFillColor("#509EE3");
-
- assertOnXYAxisLabels();
- assertOnXAxisTicks(representativeValues);
- },
- );
-
- cy.log("Don't bin");
- openSelectedBinningPopover();
- // The previous bucket is a hidden one, so the popover is already expanded.
- pickBucket({
- bucketSize: "Don't bin",
- shouldExpand: false,
- alias: "dataset-unbinned",
- });
-
- cy.get("li[aria-selected='true']")
- .should("contain", "Longitude")
- .and("contain", "Unbinned");
-
- // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
- cy.findByText("Done").click();
-
- getTitle("Count by Longitude");
- H.assertTableData({
- columns: ["Longitude", "Count"],
- firstRows: [
- ["166.54257260° W", "1"],
- ["166.09897770° W", "1"],
- ],
- });
- });
-});
-
-function openSelectedBinningPopover() {
- H.summarize();
- H.getBinningButtonForDimension({
- name: "Longitude",
- isSelected: true,
- }).click({ force: true });
-}
-
-function pickBucket({ bucketSize, shouldExpand, alias }) {
- cy.intercept("POST", "/api/dataset").as(alias);
- H.popover().within(() => {
- if (shouldExpand) {
- cy.findByText("More…").click();
- }
- cy.findByText(bucketSize).click();
- });
- cy.wait(`@${alias}`);
-}
-
-function getTitle(title) {
- cy.findByText(title);
-}
-
-function assertOnXYAxisLabels() {
- H.echartsContainer().find("text").should("contain", "Count");
- H.echartsContainer().find("text").should("contain", "Longitude");
-}
-
-function assertOnXAxisTicks(values) {
- if (values) {
- H.echartsContainer().within(() => {
- values.forEach((value) => {
- cy.findByText(value);
- });
- });
- }
-}
diff --git a/e2e/test/scenarios/binning/correctness/shared/constants.js b/e2e/test/scenarios/binning/correctness/shared/constants.js
deleted file mode 100644
index ad0a675ef13..00000000000
--- a/e2e/test/scenarios/binning/correctness/shared/constants.js
+++ /dev/null
@@ -1,172 +0,0 @@
-export const TIME_OPTIONS = {
- Minute: {
- selected: "by minute",
- firstRows: [
- ["April 30, 2025, 6:56 PM", "1"],
- ["May 4, 2025, 6:29 PM", "1"],
- ["May 6, 2025, 12:28 AM", "1"],
- ],
- },
- Hour: {
- selected: "by hour",
- firstRows: [
- ["April 30, 2025, 6:00 PM", "1"],
- ["May 4, 2025, 6:00 PM", "1"],
- ["May 6, 2025, 12:00 AM", "1"],
- ],
- },
- Day: {
- selected: "by day",
- firstRows: [
- ["April 30, 2025", "1"],
- ["May 4, 2025", "1"],
- ["May 6, 2025", "1"],
- ],
- },
- Week: {
- selected: "by week",
- firstRows: [
- ["April 27, 2025 – May 3, 2025", "1"],
- ["May 4, 2025 – May 10, 2025", "4"],
- ["May 11, 2025 – May 17, 2025", "3"],
- ],
- },
- Month: {
- selected: "by month",
- firstRows: [
- ["April 2025", "1"],
- ["May 2025", "19"],
- ["June 2025", "37"],
- ],
- },
- Quarter: {
- selected: "by quarter",
- firstRows: [
- ["Q2 2025", "57"],
- ["Q3 2025", "235"],
- ["Q4 2025", "452"],
- ],
- },
- Year: {
- selected: "by year",
- firstRows: [
- ["2025", "744"],
- ["2026", "3,610"],
- ["2027", "5,834"],
- ["2028", "6,578"],
- ["2029", "1,994"],
- ],
- },
- "Minute of hour": {
- selected: "by minute of hour",
- firstRows: [
- ["0", "297"],
- ["1", "313"],
- ["2", "295"],
- ],
- isHiddenByDefault: true,
- },
- "Hour of day": {
- selected: "by hour of day",
- firstRows: [
- ["12:00 AM", "766"],
- ["1:00 AM", "807"],
- ["2:00 AM", "741"],
- ],
- isHiddenByDefault: true,
- },
- "Day of week": {
- selected: "by day of week",
- firstRows: [
- ["Sunday", "2,678"],
- ["Monday", "2,719"],
- ["Tuesday", "2,590"],
- ],
- isHiddenByDefault: true,
- },
- "Day of month": {
- selected: "by day of month",
- firstRows: [
- ["1", "604"],
- ["2", "604"],
- ["3", "582"],
- ],
- isHiddenByDefault: true,
- },
- "Day of year": {
- selected: "by day of year",
- firstRows: [
- ["1", "45"],
- ["2", "56"],
- ["3", "58"],
- ],
- isHiddenByDefault: true,
- },
- "Week of year": {
- selected: "by week of year",
- firstRows: [
- ["1st", "402"],
- ["2nd", "405"],
- ["3rd", "424"],
- ],
- isHiddenByDefault: true,
- },
- "Month of year": {
- selected: "by month of year",
- firstRows: [
- ["January", "1,826"],
- ["February", "1,686"],
- ["March", "1,801"],
- ],
- isHiddenByDefault: true,
- },
- "Quarter of year": {
- selected: "by quarter of year",
- firstRows: [
- ["Q1", "5,313"],
- ["Q2", "4,203"],
- ["Q3", "4,402"],
- ["Q4", "4,842"],
- ],
- isHiddenByDefault: true,
- },
-};
-
-// "Bin every 20 degrees" comes first so it is picked from the unselected column.
-export const LONGITUDE_OPTIONS = {
- "Bin every 20 degrees": {
- selected: "20°",
- representativeValues: ["180° W", "160° W", "100° W", "80° W", "60° W"],
- },
- "Auto bin": {
- selected: "Auto binned",
- representativeValues: ["170° W", "100° W", "60° W"],
- },
- "Bin every 0.1 degrees": {
- selected: "0.1°",
- representativeValues: null,
- },
- "Bin every 1 degree": {
- selected: "1°",
- representativeValues: ["167° W", "159° W", "69° W"],
- },
- "Bin every 10 degrees": {
- selected: "10°",
- representativeValues: ["170° W", "100° W", "60° W"],
- },
- "Bin every 0.05 degrees": {
- selected: "0.05°",
- representativeValues: null,
- isHiddenByDefault: true,
- },
- "Bin every 0.01 degrees": {
- selected: "0.01°",
- representativeValues: null,
- isHiddenByDefault: true,
- },
- "Bin every 0.005 degrees": {
- selected: "0.005°",
- representativeValues: null,
- isHiddenByDefault: true,
- },
-};
diff --git a/e2e/test/scenarios/binning/correctness/time-series.cy.spec.js b/e2e/test/scenarios/binning/correctness/time-series.cy.spec.js
deleted file mode 100644
index b715effdd99..00000000000
--- a/e2e/test/scenarios/binning/correctness/time-series.cy.spec.js
+++ /dev/null
@@ -1,91 +0,0 @@
-const { H } = cy;
-import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
-
-import { TIME_OPTIONS } from "./shared/constants";
-
-const { ORDERS_ID } = SAMPLE_DATABASE;
-
-const questionDetails = {
- name: "Test Question",
- query: {
- "source-table": ORDERS_ID,
- limit: 50,
- },
-};
-
-describe("scenarios > binning > correctness > time series", () => {
- beforeEach(() => {
- H.restore();
- cy.signInAsAdmin();
- });
-
- it("should return correct values for every bucket size", () => {
- H.createQuestion(questionDetails, { visitQuestion: true });
- H.summarize();
-
- cy.log("the first bucket is picked from the unselected column");
- H.getBinningButtonForDimension({ name: "Created At" })
- .should("have.text", "by month")
- .click({ force: true });
-
- Object.entries(TIME_OPTIONS).forEach(
- (
- [bucketSize, { selected, isHiddenByDefault, firstRows }],
- index,
- entries,
- ) => {
- cy.log(bucketSize);
-
- if (index > 0) {
- H.summarize();
- H.getBinningButtonForDimension({
- name: "Created At",
- isSelected: true,
- }).click({ force: true });
- }
-
- // The popover opens expanded when the current bucket is a hidden one.
- const isExpanded =
- index > 0 && Boolean(entries[index - 1][1].isHiddenByDefault);
-
- cy.intercept("POST", "/api/dataset").as(`dataset-${index}`);
- H.popover().within(() => {
- if (isHiddenByDefault && !isExpanded) {
- cy.button("More…").click();
- }
- cy.findByText(bucketSize).click();
- });
- cy.wait(`@dataset-${index}`);
-
- H.getBinningButtonForDimension({
- name: "Created At",
- isSelected: true,
- }).should("have.text", selected);
-
- H.rightSidebar().button("Done").click();
-
- getTitle(`Count by Created At: ${bucketSize}`);
-
- H.assertTableData({
- columns: [`Created At: ${bucketSize}`, "Count"],
- firstRows,
- });
-
- assertOnTimeSeriesFooter(bucketSize);
- },
- );
- });
-});
-
-function getTitle(title) {
- cy.findByText(title);
-}
-
-function assertOnTimeSeriesFooter(str) {
- cy.findByTestId("timeseries-filter-button")
- .invoke("text")
- .should("eq", "All time");
- cy.findByTestId("timeseries-bucket-button")
- .invoke("text")
- .should("contain", str);
-}
diff --git a/frontend/src/metabase/querying/common/components/QueryColumnPicker/ColumnBucketPickerPopover/ColumnBucketPickerPopover.unit.spec.tsx b/frontend/src/metabase/querying/common/components/QueryColumnPicker/ColumnBucketPickerPopover/ColumnBucketPickerPopover.unit.spec.tsx
index 961fb4f8eca..3227636429d 100644
--- a/frontend/src/metabase/querying/common/components/QueryColumnPicker/ColumnBucketPickerPopover/ColumnBucketPickerPopover.unit.spec.tsx
+++ b/frontend/src/metabase/querying/common/components/QueryColumnPicker/ColumnBucketPickerPopover/ColumnBucketPickerPopover.unit.spec.tsx
@@ -1,6 +1,7 @@
import userEvent from "@testing-library/user-event";
import { render, screen, waitFor } from "__support__/ui";
+import { checkNotNull } from "metabase/utils/types";
import * as Lib from "metabase-lib";
import {
DEFAULT_TEST_QUERY,
@@ -16,8 +17,12 @@ const dateColumn = findColumn("ORDERS", "CREATED_AT");
const numericColumn = findColumn("ORDERS", "TOTAL");
const coordinateColumn = findColumn("PEOPLE", "LATITUDE");
-function setup({ column }: { column: Lib.ColumnMetadata }) {
- const onSelect = jest.fn();
+interface SetupOpts {
+ column: Lib.ColumnMetadata;
+}
+
+function setup({ column }: SetupOpts) {
+ const onSelect = jest.fn<void, [Lib.ColumnMetadata]>();
render(
<div data-testid="container">
<ColumnBucketPickerPopover
@@ -31,25 +36,136 @@ function setup({ column }: { column: Lib.ColumnMetadata }) {
/>
</div>,
);
+ return { onSelect };
}
-async function setupBinningPicker({ column }: { column: Lib.ColumnMetadata }) {
- setup({ column });
+async function setupBinningPicker({ column }: SetupOpts) {
+ const result = setup({ column });
await userEvent.click(screen.getByLabelText("Binning strategy"));
await screen.findByText("Auto bin");
+ return result;
}
-async function setupTemporalBucketPicker({
- column,
-}: {
- column: Lib.ColumnMetadata;
-}) {
- setup({ column });
+async function setupTemporalBucketPicker({ column }: SetupOpts) {
+ const result = setup({ column });
await userEvent.click(screen.getByLabelText("Temporal bucket"));
await screen.findByText("Year");
+ return result;
+}
+
+function getOptionNames() {
+ return screen
+ .getAllByRole("menuitem")
+ .map((option) => option.textContent ?? "");
}
+const NUMBER_BUCKETS = [
+ "Auto bin",
+ "10 bins",
+ "50 bins",
+ "100 bins",
+ "Don't bin",
+];
+
+const TIME_BUCKETS = [
+ "Minute",
+ "Hour",
+ "Day",
+ "Week",
+ "Month",
+ "Quarter",
+ "Year",
+ "Minute of hour",
+ "Hour of day",
+ "Day of week",
+ "Day of month",
+ "Day of year",
+ "Week of year",
+ "Month of year",
+ "Quarter of year",
+ "Don't bin",
+];
+
+const COORDINATE_BUCKETS = [
+ "Auto bin",
+ "Bin every 0.1 degrees",
+ "Bin every 1 degree",
+ "Bin every 10 degrees",
+ "Bin every 20 degrees",
+ "Bin every 0.05 degrees",
+ "Bin every 0.01 degrees",
+ "Bin every 0.005 degrees",
+ "Don't bin",
+];
+
describe("ColumnBucketPickerPopover", () => {
+ it("should list every binning option of a numeric column once", async () => {
+ await setupBinningPicker({ column: numericColumn });
+ expect(getOptionNames()).toEqual(NUMBER_BUCKETS);
+ });
+
+ it("should list every temporal bucket once when expanded", async () => {
+ await setupTemporalBucketPicker({ column: dateColumn });
+ await userEvent.click(screen.getByRole("button", { name: "More…" }));
+ expect(getOptionNames()).toEqual(TIME_BUCKETS);
+ });
+
+ it("should list every binning option of a coordinate column once when expanded", async () => {
+ await setupBinningPicker({ column: coordinateColumn });
+ await userEvent.click(screen.getByRole("button", { name: "More…" }));
+ expect(getOptionNames()).toEqual(COORDINATE_BUCKETS);
+ });
+
+ it("should highlight the selected binning option of a coordinate column", async () => {
+ const strategy = Lib.availableBinningStrategies(
+ query,
+ 0,
+ coordinateColumn,
+ ).find(
+ (strategy) =>
+ Lib.displayInfo(query, 0, strategy).displayName ===
+ "Bin every 20 degrees",
+ );
+ const column = Lib.withBinning(coordinateColumn, checkNotNull(strategy));
+ await setupBinningPicker({ column });
+
+ expect(
+ screen.getByRole("menuitem", { name: "Bin every 20 degrees" }),
+ ).toHaveAttribute("aria-selected", "true");
+ expect(screen.getByRole("menuitem", { name: "Auto bin" })).toHaveAttribute(
+ "aria-selected",
+ "false",
+ );
+ });
+
+ it("should apply the picked binning option of a coordinate column", async () => {
+ const { onSelect } = await setupBinningPicker({ column: coordinateColumn });
+
+ await userEvent.click(
+ screen.getByRole("menuitem", { name: "Bin every 20 degrees" }),
+ );
+
+ expect(onSelect).toHaveBeenCalledTimes(1);
+ const [nextColumn] = onSelect.mock.calls[0];
+ const binning = Lib.binning(nextColumn);
+ expect(binning && Lib.displayInfo(query, 0, binning).displayName).toBe(
+ "20°",
+ );
+ });
+
+ it("should unbin a binned coordinate column", async () => {
+ const column = Lib.withDefaultBinning(query, 0, coordinateColumn);
+ expect(Lib.binning(column)).not.toBeNull();
+ const { onSelect } = await setupBinningPicker({ column });
+
+ await userEvent.click(screen.getByRole("button", { name: "More…" }));
+ await userEvent.click(screen.getByRole("menuitem", { name: "Don't bin" }));
+
+ expect(onSelect).toHaveBeenCalledTimes(1);
+ const [nextColumn] = onSelect.mock.calls[0];
+ expect(Lib.binning(nextColumn)).toBeNull();
+ });
+
it("should collapse advanced temporal bucket options", async () => {
const buckets = Lib.availableTemporalBuckets(query, 0, dateColumn);
await setupTemporalBucketPicker({ column: dateColumn });
diff --git a/frontend/src/metabase/querying/filters/components/TimeseriesChrome/TimeseriesBucketPicker/TimeseriesBucketPicker.unit.spec.tsx b/frontend/src/metabase/querying/filters/components/TimeseriesChrome/TimeseriesBucketPicker/TimeseriesBucketPicker.unit.spec.tsx
index 4c39ff9fbcd..9352902d670 100644
--- a/frontend/src/metabase/querying/filters/components/TimeseriesChrome/TimeseriesBucketPicker/TimeseriesBucketPicker.unit.spec.tsx
+++ b/frontend/src/metabase/querying/filters/components/TimeseriesChrome/TimeseriesBucketPicker/TimeseriesBucketPicker.unit.spec.tsx
@@ -124,6 +124,23 @@ describe("TimeseriesBucketPicker", () => {
expect(getNextBucketName()).toBeNull();
});
+ it("should highlight the current temporal bucket", async () => {
+ const { query, breakout, column } = createQueryWithBreakout({
+ bucket: "quarter",
+ });
+ setup({ query, breakout, column });
+
+ await userEvent.click(screen.getByText("Quarter"));
+
+ expect(
+ await screen.findByRole("option", { name: "Quarter" }),
+ ).toHaveAttribute("aria-selected", "true");
+ expect(screen.getByRole("option", { name: "Month" })).toHaveAttribute(
+ "aria-selected",
+ "false",
+ );
+ });
+
it("should show all options when the current bucket is below the More button", async () => {
const { query, breakout, column } = createQueryWithBreakout({
column: { type: "column", name: "CREATED_AT" },
diff --git a/frontend/src/metabase/utils/time-dayjs.unit.spec.ts b/frontend/src/metabase/utils/time-dayjs.unit.spec.ts
index a695e236690..e8a426f74dd 100644
--- a/frontend/src/metabase/utils/time-dayjs.unit.spec.ts
+++ b/frontend/src/metabase/utils/time-dayjs.unit.spec.ts
@@ -96,6 +96,24 @@ describe("time-dayjs", () => {
expect(parseTimestamp(53, "week-of-year").isoWeek()).toBe(53);
});
});
+
+ it("should parse numeric temporal extraction units", () => {
+ expect(parseTimestamp(7, "minute-of-hour").minute()).toBe(7);
+ expect(parseTimestamp(5, "day-of-month").date()).toBe(5);
+ expect(parseTimestamp(1, "month-of-year").month()).toBe(0);
+ expect(parseTimestamp(12, "month-of-year").month()).toBe(11);
+ expect(parseTimestamp(1, "quarter-of-year").quarter()).toBe(1);
+ expect(parseTimestamp(4, "quarter-of-year").quarter()).toBe(4);
+ });
+
+ it("should parse day of week relative to the first day of the week", () => {
+ expect(parseTimestamp(1, "day-of-week").day()).toBe(0);
+ expect(parseTimestamp(7, "day-of-week").day()).toBe(6);
+
+ dayjs.updateLocale(dayjs.locale(), { weekStart: 1 });
+ expect(parseTimestamp(1, "day-of-week").day()).toBe(1);
+ expect(parseTimestamp(7, "day-of-week").day()).toBe(0);
+ });
});
describe("timezoneToUTCOffset", () => {
diff --git a/frontend/src/metabase/value-formatting/date.unit.spec.tsx b/frontend/src/metabase/value-formatting/date.unit.spec.tsx
index fd40183cb2c..42cb0197be9 100644
--- a/frontend/src/metabase/value-formatting/date.unit.spec.tsx
+++ b/frontend/src/metabase/value-formatting/date.unit.spec.tsx
@@ -279,6 +279,18 @@ describe("formatDateTimeForParameter", () => {
).toEqual("6 AM");
});
+ test.each([
+ ["minute-of-hour", 7, "7"],
+ ["day-of-month", 5, "5"],
+ ["day-of-week", 1, "Sunday"],
+ ["month-of-year", 1, "January"],
+ ["month-of-year", 12, "December"],
+ ["quarter-of-year", 1, "Q1"],
+ ["quarter-of-year", 4, "Q4"],
+ ] as const)("should format %s %s as %s", (unit, value, formatted) => {
+ expect(formatDateTimeWithUnit(value, unit)).toEqual(formatted);
+ });
+
test.each([
["minute", "Wed, April 27, 2022, 6:00 AM"],
["hour", "Wed, April 27, 2022, 6:00 AM"],
diff --git a/frontend/src/metabase/value-formatting/value.unit.spec.tsx b/frontend/src/metabase/value-formatting/value.unit.spec.tsx
index bc47fc0ec53..8f38b4a7130 100644
--- a/frontend/src/metabase/value-formatting/value.unit.spec.tsx
+++ b/frontend/src/metabase/value-formatting/value.unit.spec.tsx
@@ -146,6 +146,22 @@ describe("formatValue", () => {
).toEqual("122.41940000° W");
});
+ it("should format binned coordinates as a range with the precision of the bin width", () => {
+ const column = {
+ base_type: TYPE.Number,
+ semantic_type: TYPE.Longitude,
+ binning_info: { bin_width: 20 },
+ };
+ expect(formatValue(-180, { column })).toEqual("180° W – 160° W");
+ expect(formatValue(-180, { column, noRange: true })).toEqual("180° W");
+ expect(
+ formatValue(-122.4194, {
+ column: { ...column, binning_info: { bin_width: 0.05 } },
+ noRange: true,
+ }),
+ ).toEqual("122.42° W");
+ });
+
it("should not add mailto prefix if there's a different semantic type", () => {
expect(
formatValue("[email protected]", {