Skip to content

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

e2e2unit: Filter bulk (#85745)

Nemanja Glumac2c962cce8258
diff --git a/e2e/support/helpers/e2e-boolean-helpers.js b/e2e/support/helpers/e2e-boolean-helpers.js
deleted file mode 100644
index d1d758b6a74..00000000000
--- a/e2e/support/helpers/e2e-boolean-helpers.js
+++ /dev/null
@@ -1,27 +0,0 @@
-import { createNativeQuestion } from "./api";
-
-// until we have a test dataset that includes boolean data, we can use this questions to test booleans
-export function setupBooleanQuery(questionName = "Boolean Query") {
- cy.intercept("POST", "/api/dataset").as("dataset");
-
- createNativeQuestion(
- {
- name: questionName,
- native: {
- query: BOOLEAN_QUERY,
- "template-tags": {},
- },
- visualization_settings: {
- "table.pivot_column": "boolean",
- "table.cell_column": "integer",
- },
- },
- { visitQuestion: true },
- );
-
- cy.findByText("Explore results").click();
- cy.wait("@dataset");
-}
-
-const BOOLEAN_QUERY =
- 'select 0::integer as "integer", true::boolean AS "boolean" union all \nselect 1::integer as "integer", false::boolean AS "boolean" union all \nselect null as "integer", true::boolean AS "boolean" union all \nselect -1::integer as "integer", null AS "boolean"';
diff --git a/e2e/support/helpers/index.ts b/e2e/support/helpers/index.ts
index 87b2cb3d5d4..319bfa6711b 100644
--- a/e2e/support/helpers/index.ts
+++ b/e2e/support/helpers/index.ts
@@ -3,7 +3,6 @@ export * from "./e2e-action-helpers";
export * from "./e2e-ad-hoc-question-helpers";
export * from "./e2e-api-key-helpers";
export * from "./e2e-bi-basics-helpers";
-export * from "./e2e-boolean-helpers";
export * from "./e2e-browser-helpers";
export * from "./e2e-caching-helpers";
export * from "./e2e-cloud-helpers";
diff --git a/e2e/test/scenarios/filters/filter-bulk.cy.spec.js b/e2e/test/scenarios/filters/filter-bulk.cy.spec.js
deleted file mode 100644
index fabb2455765..00000000000
--- a/e2e/test/scenarios/filters/filter-bulk.cy.spec.js
+++ /dev/null
@@ -1,607 +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_ID, ORDERS, PEOPLE_ID, PRODUCTS_ID, PRODUCTS } = SAMPLE_DATABASE;
-
-const rawQuestionDetails = {
- dataset_query: {
- database: SAMPLE_DB_ID,
- type: "query",
- query: {
- "source-table": ORDERS_ID,
- },
- },
-};
-
-const peopleQuestion = {
- dataset_query: {
- database: SAMPLE_DB_ID,
- type: "query",
- query: {
- "source-table": PEOPLE_ID,
- },
- },
-};
-
-const productsQuestion = {
- dataset_query: {
- database: SAMPLE_DB_ID,
- type: "query",
- query: {
- "source-table": PRODUCTS_ID,
- },
- },
-};
-
-const filteredQuestionDetails = {
- dataset_query: {
- database: SAMPLE_DB_ID,
- type: "query",
- query: {
- "source-table": ORDERS_ID,
- filter: [
- "and",
- [">", ["field", ORDERS.QUANTITY, null], 20],
- ["<", ["field", ORDERS.QUANTITY, null], 30],
- ],
- },
- },
-};
-
-const aggregatedQuestionDetails = {
- dataset_query: {
- database: SAMPLE_DB_ID,
- type: "query",
- query: {
- "source-table": ORDERS_ID,
- breakout: [["field", ORDERS.CREATED_AT, { "temporal-unit": "month" }]],
- aggregation: [["count"]],
- },
- },
-};
-
-const multiStageQuestionDetails = {
- name: "Test question",
- dataset_query: {
- database: SAMPLE_DB_ID,
- type: "query",
- query: {
- "source-query": {
- "source-query": {
- "source-table": PRODUCTS_ID,
- aggregation: [["count"]],
- breakout: [["field", PRODUCTS.CATEGORY, null]],
- },
- aggregation: [["count"]],
- breakout: [["field", PRODUCTS.CATEGORY, null]],
- },
- aggregation: [["count"]],
- breakout: [["field", PRODUCTS.CATEGORY, null]],
- },
- },
-};
-
-describe("scenarios > filters > bulk filtering", () => {
- beforeEach(() => {
- H.restore();
- cy.signInAsAdmin();
- });
-
- it("should add a filter for a raw query", () => {
- H.visitQuestionAdhoc(rawQuestionDetails);
- H.filter();
- H.popover().within(() => {
- cy.findByText("Quantity").click();
- cy.findByText("20").click();
- cy.button("Apply filter").click();
- });
- cy.wait("@dataset");
-
- H.queryBuilderFiltersPanel()
- .findByText("Quantity is equal to 20")
- .should("be.visible");
- H.assertQueryBuilderRowCount(4);
- });
-
- it("should have an info icon on the filter picker filters", () => {
- H.visitQuestionAdhoc(rawQuestionDetails);
- H.filter();
- H.popover().within(() => {
- cy.findByLabelText("Created At").findByLabelText("More info").realHover();
- });
-
- H.hovercard().within(() => {
- cy.contains("The date and time an order was submitted");
- cy.contains("Creation timestamp");
- });
- });
-
- it("should add a filter for an aggregated query", () => {
- H.visitQuestionAdhoc(aggregatedQuestionDetails);
- H.filter();
- H.popover().within(() => {
- cy.findByText("Summaries").click();
- cy.findByText("Count").click();
- cy.findByPlaceholderText("Min").type("500");
- cy.button("Apply filter").click();
- });
- cy.wait("@dataset");
- H.queryBuilderFiltersPanel()
- .findByText("Count is greater than or equal to 500")
- .should("be.visible");
- H.assertQueryBuilderRowCount(21);
- });
-
- it("should add a filter for linked tables", () => {
- H.visitQuestionAdhoc(rawQuestionDetails);
- H.filter();
- H.popover().within(() => {
- cy.findByText("Product").click();
- cy.findByText("Category").click();
- cy.findByText("Gadget").click();
- cy.button("Apply filter").click();
- });
- cy.wait("@dataset");
- H.queryBuilderFiltersPanel()
- .findByText("Product → Category is Gadget")
- .should("be.visible");
-
- H.summarize();
- H.rightSidebar().button("Done").click();
- cy.wait("@dataset");
- H.queryBuilderMain()
- .findByTestId("scalar-value")
- .should("have.text", "4,939");
- });
-
- it("should update an existing filter", () => {
- H.visitQuestionAdhoc(filteredQuestionDetails);
- H.queryBuilderFiltersPanel().findByText("Quantity is less than 30").click();
- H.popover().within(() => {
- cy.findByLabelText("Filter value").type("{backspace}{backspace}25");
- cy.button("Update filter").click();
- });
- cy.wait("@dataset");
-
- H.queryBuilderFiltersPanel().within(() => {
- cy.findByText("Quantity is greater than 20").should("be.visible");
- cy.findByText("Quantity is less than 25").should("be.visible");
- });
- H.assertQueryBuilderRowCount(17);
- });
-
- it("should remove an existing filter", () => {
- H.visitQuestionAdhoc(filteredQuestionDetails);
- H.queryBuilderFiltersPanel()
- .findByText("Quantity is less than 30")
- .icon("close")
- .click();
- cy.wait("@dataset");
- H.queryBuilderFiltersPanel().within(() => {
- cy.findByText("Quantity is greater than 20").should("be.visible");
- cy.findByText("Quantity is less than 30").should("not.exist");
- });
- H.assertQueryBuilderRowCount(138);
- });
-
- it("should be able to add and remove filters for all query stages", () => {
- H.visitQuestionAdhoc(multiStageQuestionDetails);
- cy.get("@dataset.all").should("have.length", 1);
-
- cy.log("add filters for all stages in the filter modal");
- cy.log("stage 0");
- H.filter();
- H.popover().within(() => {
- cy.findByText("Category").click();
- cy.findByText("Gadget").click();
- cy.button("Add another filter").click();
- });
- cy.get("@dataset.all").should("have.length", 1);
-
- cy.log("stage 1");
- H.popover().within(() => {
- cy.findByText("Summaries").click();
- cy.findByText("Category").click();
- cy.findByText("Widget").click();
- cy.button("Add another filter").click();
- });
- cy.get("@dataset.all").should("have.length", 1);
-
- cy.log("stage 2");
- H.popover().within(() => {
- cy.findByText("Summaries (2)").click();
- cy.findByText("Category").click();
- cy.findByText("Gizmo").click();
- cy.button("Add another filter").click();
- });
- cy.get("@dataset.all").should("have.length", 1);
-
- cy.log("stage 3");
- H.popover().within(() => {
- cy.findByText("Summaries (3)").click();
- cy.findByText("Category").click();
- cy.findByText("Doohickey").click();
- cy.button("Apply filter").click();
- });
- cy.wait("@dataset");
- cy.get("@dataset.all").should("have.length", 2);
-
- cy.log("check filters from all stages to be present in the filter panel");
- H.queryBuilderFiltersPanel().within(() => {
- cy.findByText("Category is Gadget").should("be.visible");
- cy.findByText("Category is Widget").should("be.visible");
- cy.findByText("Category is Gizmo").should("be.visible");
- cy.findByText("Category is Doohickey").should("be.visible");
- });
-
- cy.log("clear all filters");
- H.queryBuilderFiltersPanel().within(() => {
- cy.findByText("Category is Gadget").icon("close").click();
- cy.wait("@dataset");
- cy.findByText("Category is Widget").icon("close").click();
- cy.wait("@dataset");
- cy.findByText("Category is Gizmo").icon("close").click();
- cy.wait("@dataset");
- cy.findByText("Category is Doohickey").icon("close").click();
- cy.wait("@dataset");
- });
- cy.findByTestId("qb-filters-panel").should("not.exist");
- });
-
- describe("segment filters", () => {
- const SEGMENT_1_NAME = "Orders < 100";
- const SEGMENT_2_NAME = "Discounted Orders";
-
- beforeEach(() => {
- H.createSegment({
- name: SEGMENT_1_NAME,
- description: "All orders with a total under $100.",
- definition: {
- database: SAMPLE_DB_ID,
- type: "query",
- query: {
- "source-table": ORDERS_ID,
- filter: ["<", ["field", ORDERS.TOTAL, null], 100],
- },
- },
- });
-
- H.createSegment({
- name: SEGMENT_2_NAME,
- description: "All orders with a discount",
- definition: {
- database: SAMPLE_DB_ID,
- type: "query",
- query: {
- "source-table": ORDERS_ID,
- filter: [">", ["field", ORDERS.DISCOUNT, null], 0],
- },
- },
- });
- });
-
- it("should apply and remove segment filter (metabase#50734)", () => {
- H.visitQuestionAdhoc(rawQuestionDetails);
- H.filter();
-
- // Only the H.modal().within(() => { ... }) block is the repro. The rest is a regular test.
- cy.log(
- "segment filter icon should be aligned with other filter icons (metabase#50734)",
- );
- H.popover().within(() => {
- cy.findByLabelText(SEGMENT_1_NAME)
- .findByRole("img")
- .should("be.visible")
- .then(([$segmentsIcon]) => {
- const segmentsIconRect = $segmentsIcon.getBoundingClientRect();
-
- cy.findByLabelText("Discount")
- .findByRole("img")
- .should(([$discountIcon]) => {
- const discountIconRect = $discountIcon.getBoundingClientRect();
- expect(segmentsIconRect.left).to.eq(discountIconRect.left);
- expect(segmentsIconRect.right).to.eq(discountIconRect.right);
- });
- });
- });
-
- H.popover().findByText(SEGMENT_2_NAME).click();
- cy.wait("@dataset");
- H.queryBuilderFiltersPanel()
- .findByText(SEGMENT_2_NAME)
- .should("be.visible");
- H.assertQueryBuilderRowCount(1915);
-
- H.queryBuilderFiltersPanel()
- .findByText(SEGMENT_2_NAME)
- .icon("close")
- .click();
- cy.wait("@dataset");
- H.queryBuilderFiltersPanel().should("not.exist");
- H.queryBuilderFooter().findByText("Showing first 2,000 rows");
- });
-
- it("should load already applied segments", () => {
- const segmentFilterQuestion = {
- dataset_query: {
- database: SAMPLE_DB_ID,
- type: "query",
- query: {
- "source-table": ORDERS_ID,
- filter: ["segment", 1],
- },
- },
- };
-
- H.visitQuestionAdhoc(segmentFilterQuestion);
- H.queryBuilderFiltersPanel().within(() => {
- cy.findByText(SEGMENT_2_NAME).should("not.exist");
- cy.findByText(SEGMENT_1_NAME).click();
- });
- H.popover()
- .findByLabelText(SEGMENT_1_NAME)
- .should("have.attr", "aria-selected", "true");
- });
- });
-
- describe("boolean filters", () => {
- beforeEach(() => {
- H.setupBooleanQuery();
- H.filter();
- });
-
- it("should change a boolean filter", () => {
- H.popover().within(() => {
- cy.findByText("boolean").click();
- cy.findByText("True").click();
- cy.button("Apply filter").click();
- });
- cy.wait("@dataset");
- H.assertQueryBuilderRowCount(2);
-
- H.queryBuilderFiltersPanel().findByText("boolean is true").click();
- H.popover().within(() => {
- cy.findByText("False").click();
- cy.button("Update filter").click();
- });
- cy.wait("@dataset");
- H.assertQueryBuilderRowCount(1);
- });
-
- it("should remove a boolean filter", () => {
- H.popover().within(() => {
- cy.findByText("boolean").click();
- cy.findByText("True").click();
- cy.button("Apply filter").click();
- });
- cy.wait("@dataset");
- H.assertQueryBuilderRowCount(2);
-
- H.queryBuilderFiltersPanel()
- .findByText("boolean is true")
- .icon("close")
- .click();
- cy.wait("@dataset");
- H.assertQueryBuilderRowCount(4);
- });
- });
-
- describe("date filters", () => {
- beforeEach(() => {
- H.visitQuestionAdhoc(rawQuestionDetails);
- H.filter();
- });
-
- it("can add a date shortcut filter", () => {
- H.popover().within(() => {
- cy.findByText("Created At").click();
- cy.findByText("Today").click();
- });
- cy.wait("@dataset");
-
- H.queryBuilderFiltersPanel()
- .findByText("Created At is today")
- .should("be.visible");
- });
-
- it("can add a date shortcut filter from the popover", () => {
- H.popover().within(() => {
- cy.findByText("Created At").click();
- cy.findByText("Previous 3 months").click();
- });
- cy.wait("@dataset");
- H.queryBuilderFiltersPanel()
- .findByText("Created At is in the previous 3 months")
- .should("be.visible");
- });
- });
-
- describe("category filters", () => {
- beforeEach(() => {
- H.visitQuestionAdhoc(peopleQuestion);
- H.filter();
- });
-
- it("should show inline category picker for referral source", () => {
- H.popover().within(() => {
- cy.findByText("Source").click();
- cy.findByText("Affiliate").click();
- cy.button("Apply filter").click();
- });
- cy.wait("@dataset");
- H.queryBuilderFiltersPanel()
- .findByText("Source is Affiliate")
- .should("be.visible");
- H.assertQueryBuilderRowCount(506);
- });
-
- it("should show value picker for state", () => {
- H.popover().within(() => {
- cy.findByText("State").click();
- cy.findByText("AZ").click();
- cy.button("Apply filter").click();
- });
- cy.wait("@dataset");
- H.queryBuilderFiltersPanel()
- .findByText("State is AZ")
- .should("be.visible");
- H.assertQueryBuilderRowCount(20);
- });
- });
-
- describe("key filters", () => {
- beforeEach(() => {
- H.visitQuestionAdhoc(rawQuestionDetails);
- H.filter();
- });
-
- it("filters by primary keys", () => {
- H.popover().within(() => {
- cy.findByText("ID").click();
- cy.findByLabelText("Filter value").type("17,18");
- cy.button("Apply filter").click();
- });
- cy.wait("@dataset");
- H.assertQueryBuilderRowCount(2);
- H.tableInteractive().within(() => {
- cy.findByText("131.68").should("be.visible"); // total for order id 17
- cy.findByText("123.99").should("be.visible"); // total for order id 18
- });
- });
-
- it("filters by a foreign key", () => {
- H.popover().within(() => {
- cy.findByText("Product ID").click();
- cy.findByLabelText("Filter value").type("65");
- cy.button("Apply filter").click();
- });
- cy.wait("@dataset");
- H.assertQueryBuilderRowCount(107);
- });
- });
-
- describe("text filters", () => {
- beforeEach(() => {
- H.visitQuestionAdhoc(peopleQuestion);
- H.filter();
- });
-
- it("adds a contains text filter", () => {
- H.popover().findByText("City").click();
- H.selectFilterOperator("Contains");
- H.popover().within(() => {
- cy.findByLabelText("Filter value").type("Indian");
- cy.button("Apply filter").click();
- });
- cy.wait("@dataset");
- H.assertQueryBuilderRowCount(5);
- });
-
- it("adds an ends with text filter", () => {
- H.popover().findByText("City").click();
- H.selectFilterOperator("Ends with");
- H.popover().within(() => {
- cy.findByLabelText("Filter value").type("Valley");
- cy.button("Apply filter").click();
- });
- cy.wait("@dataset");
- H.assertQueryBuilderRowCount(8);
- });
-
- it("adds multiple is text filters", () => {
- H.popover().within(() => {
- cy.findByText("City").click();
- cy.findByLabelText("Filter value")
- .type("Indiantown,Indian Valley")
- .blur();
- cy.button("Apply filter").click();
- });
- cy.wait("@dataset");
- H.queryBuilderFiltersPanel()
- .findByText("City is 2 selections")
- .should("be.visible");
- H.assertQueryBuilderRowCount(3);
- });
- });
-
- describe("number filters", () => {
- beforeEach(() => {
- H.visitQuestionAdhoc(productsQuestion);
- H.filter();
- });
-
- it("applies a between filter", () => {
- H.popover().within(() => {
- cy.findByText("Price").click();
- cy.findByPlaceholderText("Min").type("50");
- cy.findByPlaceholderText("Max").type("80");
- cy.button("Apply filter").click();
- });
- cy.wait("@dataset");
- H.queryBuilderFiltersPanel()
- .findByText("Price is between 50 and 80")
- .should("be.visible");
- H.assertQueryBuilderRowCount(72);
- });
-
- it("applies a greater than filter", () => {
- H.popover().findByText("Price").click();
- H.selectFilterOperator("Greater than");
- H.popover().within(() => {
- cy.findByLabelText("Filter value").type("50");
- cy.button("Apply filter").click();
- });
- cy.wait("@dataset");
- H.queryBuilderFiltersPanel()
- .findByText("Price is greater than 50")
- .should("be.visible");
- H.assertQueryBuilderRowCount(106);
- });
-
- it("infers a <= filter from an invalid between filter", () => {
- H.popover().within(() => {
- cy.findByText("Price").click();
- cy.findByPlaceholderText("Max").type("50");
- cy.button("Apply filter").click();
- });
- cy.wait("@dataset");
- H.queryBuilderFiltersPanel()
- .findByText("Price is less than or equal to 50")
- .should("be.visible");
- H.assertQueryBuilderRowCount(94);
- });
- });
-
- describe("column search", () => {
- beforeEach(() => {
- H.visitQuestionAdhoc(productsQuestion);
- H.filter();
- });
-
- it("can search for a column and apply a filter from it", () => {
- H.popover().within(() => {
- cy.findByText("Category").should("be.visible");
- cy.findByText("Vendor").should("be.visible");
-
- cy.findByPlaceholderText("Find...").type("vend");
- cy.findByText("Category").should("not.exist");
- cy.findByText("Vendor").should("be.visible");
-
- cy.findByPlaceholderText("Find...").clear().type("price");
- cy.findByText("Category").should("not.exist");
- cy.findByText("Price").click();
- });
- H.selectFilterOperator("Greater than");
- H.popover().within(() => {
- cy.findByLabelText("Filter value").type("90");
- cy.button("Apply filter").click();
- });
- cy.wait("@dataset");
- H.queryBuilderFiltersPanel()
- .findByText("Price is greater than 90")
- .should("be.visible");
- H.assertQueryBuilderRowCount(10);
- });
- });
-});
diff --git a/frontend/src/metabase/query_builder/components/view/ViewHeader/components/QuestionFiltersHeader/QuestionFiltersHeader.unit.spec.tsx b/frontend/src/metabase/query_builder/components/view/ViewHeader/components/QuestionFiltersHeader/QuestionFiltersHeader.unit.spec.tsx
index 7d6c9e8ffc2..3829ea1da37 100644
--- a/frontend/src/metabase/query_builder/components/view/ViewHeader/components/QuestionFiltersHeader/QuestionFiltersHeader.unit.spec.tsx
+++ b/frontend/src/metabase/query_builder/components/view/ViewHeader/components/QuestionFiltersHeader/QuestionFiltersHeader.unit.spec.tsx
@@ -186,6 +186,29 @@ describe("QuestionFiltersHeader", () => {
expect(Lib.filters(query, 1)).toHaveLength(1);
});
+ it("should hide the filter panel after removing all filters", async () => {
+ const { getNextQuery } = setup();
+ const removeFilter = (filterName: string) =>
+ userEvent.click(
+ within(screen.getByText(filterName)).getByLabelText("Remove"),
+ );
+
+ await removeFilter("Quantity is greater than 4");
+ expect(screen.getByTestId("qb-filters-panel")).toBeInTheDocument();
+
+ await removeFilter("Count is greater than 5");
+ expect(screen.getByTestId("qb-filters-panel")).toBeInTheDocument();
+
+ await removeFilter("User → Source is Organic");
+ expect(screen.queryByTestId("qb-filters-panel")).not.toBeInTheDocument();
+ const query = getNextQuery();
+ expect(
+ Lib.stageIndexes(query).flatMap((stageIndex) =>
+ Lib.filters(query, stageIndex),
+ ),
+ ).toHaveLength(0);
+ });
+
it("should remove a filter from the previous stage", async () => {
const { getNextQuery } = setup();
diff --git a/frontend/src/metabase/querying/filters/components/FilterPicker/FilterColumnPicker/FilterColumnPicker.unit.spec.tsx b/frontend/src/metabase/querying/filters/components/FilterPicker/FilterColumnPicker/FilterColumnPicker.unit.spec.tsx
index 8eab70728d8..c6c4e22c2b6 100644
--- a/frontend/src/metabase/querying/filters/components/FilterPicker/FilterColumnPicker/FilterColumnPicker.unit.spec.tsx
+++ b/frontend/src/metabase/querying/filters/components/FilterPicker/FilterColumnPicker/FilterColumnPicker.unit.spec.tsx
@@ -2,6 +2,7 @@ import userEvent from "@testing-library/user-event";
import { setupFieldsValuesEndpoints } from "__support__/server-mocks";
import { renderWithProviders, screen, within } from "__support__/ui";
+import { checkNotNull } from "metabase/utils/types";
import * as Lib from "metabase-lib";
import { DEFAULT_TEST_QUERY, SAMPLE_PROVIDER } from "metabase-lib/test-helpers";
import {
@@ -45,6 +46,54 @@ describe("FilterColumnPicker", () => {
);
});
+ it("should show the column description and semantic type in the info hovercard", async () => {
+ const query = Lib.createTestQuery(SAMPLE_PROVIDER, DEFAULT_TEST_QUERY);
+ setup({ query, stageIndexes: [0] });
+
+ await userEvent.hover(
+ within(screen.getByLabelText("Created At")).getByLabelText("More info"),
+ );
+
+ expect(
+ await screen.findByText("The timestamp the order was created"),
+ ).toBeInTheDocument();
+ expect(screen.getByText("Creation timestamp")).toBeInTheDocument();
+ });
+
+ it("should filter columns by the search text", async () => {
+ const query = Lib.createTestQuery(SAMPLE_PROVIDER, DEFAULT_TEST_QUERY);
+ const { onColumnSelect } = setup({ query, stageIndexes: [0] });
+
+ await userEvent.click(screen.getByText("Product"));
+ expect(
+ screen.getByRole("option", { name: "Category" }),
+ ).toBeInTheDocument();
+ expect(screen.getByRole("option", { name: "Vendor" })).toBeInTheDocument();
+
+ const searchInput = screen.getByTestId("list-search-field");
+ await userEvent.type(searchInput, "vend");
+ expect(
+ await screen.findByRole("option", { name: "Vendor" }),
+ ).toBeInTheDocument();
+ expect(
+ screen.queryByRole("option", { name: "Category" }),
+ ).not.toBeInTheDocument();
+
+ await userEvent.clear(searchInput);
+ await userEvent.type(searchInput, "price");
+ const priceOption = await screen.findByRole("option", { name: "Price" });
+ expect(
+ screen.queryByRole("option", { name: "Category" }),
+ ).not.toBeInTheDocument();
+
+ await userEvent.click(within(priceOption).getByText("Price"));
+ expect(onColumnSelect).toHaveBeenCalledTimes(1);
+ const [{ column }] = checkNotNull(onColumnSelect.mock.lastCall);
+ expect(Lib.displayInfo(query, 0, column).longDisplayName).toBe(
+ "Product → Price",
+ );
+ });
+
it("Searching by displayName should work (#39622)", async () => {
const query = Lib.createTestQuery(SAMPLE_PROVIDER, DEFAULT_TEST_QUERY);
const stageIndexes = [0];
diff --git a/frontend/src/metabase/querying/filters/components/FilterPicker/MultiStageFilterPicker/MultiStageFilterPicker.unit.spec.tsx b/frontend/src/metabase/querying/filters/components/FilterPicker/MultiStageFilterPicker/MultiStageFilterPicker.unit.spec.tsx
index 89cde42d38f..9137d1a9d87 100644
--- a/frontend/src/metabase/querying/filters/components/FilterPicker/MultiStageFilterPicker/MultiStageFilterPicker.unit.spec.tsx
+++ b/frontend/src/metabase/querying/filters/components/FilterPicker/MultiStageFilterPicker/MultiStageFilterPicker.unit.spec.tsx
@@ -1,9 +1,22 @@
import userEvent from "@testing-library/user-event";
+import { useState } from "react";
+import {
+ setupDatabaseEndpoints,
+ setupFieldsValuesEndpoints,
+} from "__support__/server-mocks";
import { renderWithProviders, screen } from "__support__/ui";
+import { checkNotNull } from "metabase/utils/types";
import * as Lib from "metabase-lib";
import { SAMPLE_PROVIDER } from "metabase-lib/test-helpers";
-import { ORDERS, ORDERS_ID } from "metabase-types/api/mocks/presets";
+import {
+ ORDERS,
+ ORDERS_ID,
+ PRODUCT_CATEGORY_VALUES,
+ createSampleDatabase,
+} from "metabase-types/api/mocks/presets";
+
+import type { FilterChangeOpts } from "../types";
import { MultiStageFilterPicker } from "./MultiStageFilterPicker";
@@ -13,25 +26,61 @@ type SetupOpts = {
};
function setup({ query, canAppendStage = true }: SetupOpts) {
- const onChange = jest.fn();
+ const onChange = jest.fn<void, [Lib.Query, FilterChangeOpts]>();
const onClose = jest.fn();
- renderWithProviders(
- <MultiStageFilterPicker
- query={query}
- canAppendStage={canAppendStage}
- onChange={onChange}
- onClose={onClose}
- />,
- );
+ function Wrapper() {
+ const [currentQuery, setCurrentQuery] = useState(query);
+
+ const handleChange = (newQuery: Lib.Query, opts: FilterChangeOpts) => {
+ setCurrentQuery(newQuery);
+ onChange(newQuery, opts);
+ };
+
+ return (
+ <MultiStageFilterPicker
+ query={currentQuery}
+ canAppendStage={canAppendStage}
+ onChange={handleChange}
+ onClose={onClose}
+ />
+ );
+ }
+
+ renderWithProviders(<Wrapper />);
const getNewQuery = (): Lib.Query => {
- return onChange.mock.lastCall[0];
+ const [newQuery] = checkNotNull(onChange.mock.lastCall);
+ return newQuery;
};
return { getNewQuery, onChange, onClose };
}
+function createSummarizedQuery() {
+ const summaryStage = {
+ aggregations: [{ type: "operator" as const, operator: "count" as const }],
+ breakouts: [{ type: "column" as const, name: "CATEGORY" }],
+ };
+ return Lib.createTestQuery(SAMPLE_PROVIDER, {
+ stages: [
+ {
+ source: { type: "table", id: ORDERS_ID },
+ aggregations: [{ type: "operator", operator: "count" }],
+ breakouts: [
+ {
+ type: "column",
+ name: "CATEGORY",
+ sourceFieldId: ORDERS.PRODUCT_ID,
+ },
+ ],
+ },
+ summaryStage,
+ summaryStage,
+ ],
+ });
+}
+
describe("MultiStageFilterPicker", () => {
it("should drop empty stages if there is no filter in a post-aggregation stage (metabase#57573)", async () => {
const { getNewQuery } = setup({
@@ -62,6 +111,67 @@ describe("MultiStageFilterPicker", () => {
expect(Lib.filters(newQuery, 0)).toHaveLength(1);
});
+ it("should add filters to several summary stages in one session", async () => {
+ setupDatabaseEndpoints(createSampleDatabase());
+ setupFieldsValuesEndpoints([PRODUCT_CATEGORY_VALUES]);
+ const { getNewQuery, onChange, onClose } = setup({
+ query: createSummarizedQuery(),
+ });
+ expect(screen.getByText("Summaries")).toBeInTheDocument();
+ expect(screen.getByText("Summaries (2)")).toBeInTheDocument();
+
+ await userEvent.click(screen.getByText("Summaries (3)"));
+ await userEvent.click(screen.getByText("Product → Category"));
+ await userEvent.click(await screen.findByLabelText("Gizmo"));
+ await userEvent.click(
+ screen.getByRole("button", { name: "Add another filter" }),
+ );
+ expect(
+ screen.queryByRole("button", { name: "Add another filter" }),
+ ).not.toBeInTheDocument();
+ expect(screen.getByText("Summaries (3)")).toBeInTheDocument();
+
+ await userEvent.click(screen.getByText("Summaries"));
+ await userEvent.click(screen.getByText("Product → Category"));
+ await userEvent.click(await screen.findByLabelText("Gadget"));
+ await userEvent.click(
+ screen.getByRole("button", { name: "Add another filter" }),
+ );
+
+ expect(onChange).toHaveBeenCalledTimes(2);
+ expect(onClose).not.toHaveBeenCalled();
+ const newQuery = getNewQuery();
+ expect(Lib.stageCount(newQuery)).toBe(4);
+ expect(Lib.filters(newQuery, 1)).toHaveLength(1);
+ expect(Lib.filters(newQuery, 3)).toHaveLength(1);
+ });
+
+ it.each([
+ { label: "Apply filter", run: true },
+ { label: "Add another filter", run: false },
+ ])(
+ "should add a filter to a later summary stage with '$label'",
+ async ({ label, run }) => {
+ setupDatabaseEndpoints(createSampleDatabase());
+ setupFieldsValuesEndpoints([PRODUCT_CATEGORY_VALUES]);
+ const { getNewQuery, onChange, onClose } = setup({
+ query: createSummarizedQuery(),
+ });
+
+ await userEvent.click(screen.getByText("Summaries (3)"));
+ await userEvent.click(screen.getByText("Product → Category"));
+ await userEvent.click(await screen.findByLabelText("Gizmo"));
+ await userEvent.click(screen.getByRole("button", { name: label }));
+
+ expect(onChange).toHaveBeenCalledTimes(1);
+ expect(onChange).toHaveBeenLastCalledWith(expect.anything(), { run });
+ expect(onClose).toHaveBeenCalledTimes(run ? 1 : 0);
+ const newQuery = getNewQuery();
+ expect(Lib.stageCount(newQuery)).toBe(4);
+ expect(Lib.filters(newQuery, 3)).toHaveLength(1);
+ },
+ );
+
it("should not drop the post-aggregation stage if there is a new filter", async () => {
const { getNewQuery } = setup({
query: Lib.createTestQuery(SAMPLE_PROVIDER, {
diff --git a/frontend/src/metabase/querying/filters/components/FilterPicker/NumberFilterPicker/NumberFilterPicker.unit.spec.tsx b/frontend/src/metabase/querying/filters/components/FilterPicker/NumberFilterPicker/NumberFilterPicker.unit.spec.tsx
index 1d229a1cf15..8ba6f518a26 100644
--- a/frontend/src/metabase/querying/filters/components/FilterPicker/NumberFilterPicker/NumberFilterPicker.unit.spec.tsx
+++ b/frontend/src/metabase/querying/filters/components/FilterPicker/NumberFilterPicker/NumberFilterPicker.unit.spec.tsx
@@ -320,6 +320,25 @@ describe("NumberFilterPicker", () => {
expect(getNextFilterColumnName()).toBe("Total");
});
+ it.each([
+ { placeholder: "Min", operator: ">=" },
+ { placeholder: "Max", operator: "<=" },
+ ])(
+ "should add a $operator filter when only $placeholder is set",
+ async ({ placeholder, operator }) => {
+ const { getNextFilterParts } = setup();
+
+ await userEvent.type(screen.getByPlaceholderText(placeholder), "50");
+ await userEvent.click(screen.getByText("Add filter"));
+
+ expect(getNextFilterParts()).toMatchObject({
+ operator,
+ column: expect.anything(),
+ values: [50],
+ });
+ },
+ );
+
it("should add a filter via keyboard", async () => {
const { onChange, getNextFilterParts, getNextFilterColumnName } =
setup();
@@ -360,6 +379,26 @@ describe("NumberFilterPicker", () => {
});
expect(getNextFilterColumnName()).toBe("Total");
});
+
+ it("should add a primary key filter with comma-separated values", async () => {
+ const query = createQuery();
+ const columns = Lib.filterableColumns(query, 0);
+ const column = columnFinder(query, columns)("ORDERS", "ID");
+ const { getNextFilterParts, getNextFilterColumnName } = setup({
+ query,
+ column,
+ });
+
+ await userEvent.type(screen.getByLabelText("Filter value"), "17,18");
+ await userEvent.click(screen.getByText("Add filter"));
+
+ expect(getNextFilterParts()).toMatchObject({
+ operator: "=",
+ column: expect.anything(),
+ values: [17, 18],
+ });
+ expect(getNextFilterColumnName()).toBe("ID");
+ });
});
describe("with no values", () => {