Skip to content

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

e2e2unit: Time series chrome (#85726)

Nemanja Glumaceeeb14f8cf04
diff --git a/e2e/test/scenarios/filters/time-series-chrome.cy.spec.ts b/e2e/test/scenarios/filters/time-series-chrome.cy.spec.ts
deleted file mode 100644
index 1290ca1b190..00000000000
--- a/e2e/test/scenarios/filters/time-series-chrome.cy.spec.ts
+++ /dev/null
@@ -1,229 +0,0 @@
-const { H } = cy;
-import { SAMPLE_DB_ID } from "e2e/support/cypress_data";
-import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
-
-const { PRODUCTS, PRODUCTS_ID } = SAMPLE_DATABASE;
-
-describe("time-series chrome filter widget", () => {
- beforeEach(() => {
- H.restore();
- cy.signInAsNormalUser();
- });
-
- describe("smoke tests", () => {
- beforeEach(() => {
- H.visitQuestionAdhoc({
- dataset_query: {
- database: SAMPLE_DB_ID,
- query: {
- "source-table": PRODUCTS_ID,
- aggregation: [["count"]],
- breakout: [
- ["field", PRODUCTS.CREATED_AT, { "temporal-unit": "month" }],
- ],
- },
- type: "query",
- },
- });
- });
-
- it("should display all operators and stay in sync with the relative date filter", () => {
- const operators = [
- "Previous",
- "Next",
- "Current",
- "Before",
- "After",
- "On",
- "Between",
- ];
-
- cy.log(
- "should display 'All time' as the initialy selected operator (metabase#22247)",
- );
- cy.findByTestId("timeseries-filter-button")
- .should("have.text", "All time")
- .click();
-
- cy.findByTestId("date-filter-picker")
- .findByDisplayValue("All time")
- .click();
-
- cy.findByRole("listbox").within(() => {
- cy.findByRole("option", { name: "All time" }).should(
- "have.attr",
- "aria-selected",
- "true",
- );
-
- cy.log("Make sure we display all the operators");
- operators.forEach((operator) => {
- cy.findByRole("option", { name: operator }).should("be.visible");
- });
- });
-
- cy.findByTestId("date-filter-picker").within(() => {
- cy.log("Include 'current' interval switch should not be displayed");
- cy.findByLabelText(/^Include/).should("not.exist");
- cy.button("Apply").should("not.be.disabled");
- });
-
- cy.findByRole("listbox")
- .findByRole("option", { name: "Previous" })
- .click();
-
- cy.log("Check the state of the time-series chrome");
- cy.findByTestId("date-filter-picker").within(() => {
- // Top row
- cy.findByDisplayValue("Previous").should("be.visible");
- cy.findByDisplayValue("30").should("be.visible");
- cy.findByDisplayValue("days").should("be.visible");
-
- cy.log("Toggle should be always off initially");
- // This is targeting the input checkbox that is hidden
- cy.findByLabelText("Include today").should("not.be.checked");
-
- // This is clicking on an actual label in the UI
- cy.findByLabelText("Include today").click();
-
- cy.findByLabelText("Include today").should("be.checked");
- cy.button("Apply").click();
- cy.wait("@dataset");
- });
-
- cy.findByTestId("filter-pill")
- .should("have.text", "Created At is in the previous 30 days or today")
- .click();
-
- cy.log(
- "Make sure the relative date picker reflects the state of the time-series chrome",
- );
- cy.findByTestId("date-filter-picker").within(() => {
- cy.findByRole("tab", { name: "Previous" }).should(
- "have.attr",
- "aria-selected",
- "true",
- );
-
- cy.findByLabelText("Include today").should("be.checked");
-
- cy.log(
- "Switch should preserve its state after we change the direction",
- );
- cy.findByRole("tab", { name: "Next" }).click();
- cy.findByLabelText("Include today").should("be.checked");
- });
- });
- });
-
- describe("'Include this' switch", () => {
- beforeEach(() => {
- H.visitQuestionAdhoc({
- dataset_query: {
- database: SAMPLE_DB_ID,
- query: {
- "source-table": PRODUCTS_ID,
- aggregation: [["count"]],
- breakout: [
- ["field", PRODUCTS.CREATED_AT, { "temporal-unit": "month" }],
- ],
- filter: [
- "time-interval",
- [
- "field",
- PRODUCTS.CREATED_AT,
- {
- "base-type": "type/DateTime",
- },
- ],
- 30,
- "year",
- {
- "include-current": true,
- },
- ],
- },
- type: "query",
- },
- });
-
- cy.findByTestId("filter-pill").should(
- "have.text",
- "Created At is in the next 30 years or this year",
- );
- cy.findByTestId("timeseries-filter-button")
- .should("have.text", "Next 30 years or this year")
- .click();
- });
-
- it("should preserve the state of 'Include current' switch when changing direction or the interval", () => {
- cy.findByTestId("date-filter-picker").within(() => {
- cy.findByLabelText("Include this year").should(
- "have.attr",
- "aria-checked",
- "true",
- );
- });
-
- cy.log("Change the interval");
- cy.findByTestId("date-filter-picker").findByDisplayValue("years").click();
- cy.findByRole("listbox")
- .findByRole("option", { name: "quarters" })
- .click();
-
- cy.findByTestId("date-filter-picker").within(() => {
- cy.findByDisplayValue("quarters").should("be.visible");
- cy.findByLabelText("Include this quarter").should(
- "have.attr",
- "aria-checked",
- "true",
- );
-
- // Toggle off
- cy.findByLabelText("Include this quarter").click();
- });
-
- cy.log("Change the direction");
- updateOperator("Next", "Previous");
- cy.findByTestId("date-filter-picker").within(() => {
- cy.findByDisplayValue("Previous").should("be.visible");
- cy.findByLabelText("Include this quarter").should(
- "have.attr",
- "aria-checked",
- "false",
- );
- });
- });
-
- it("should reset the 'Include current' switch state when navigating away from the relative interval date filter", () => {
- cy.findByTestId("date-filter-picker").within(() => {
- cy.findByLabelText("Include this year").should(
- "have.attr",
- "aria-checked",
- "true",
- );
- });
-
- updateOperator("Next", "Current");
- cy.findByTestId("date-filter-picker").within(() => {
- cy.findByDisplayValue("Current").should("be.visible");
- cy.findByLabelText("Include this year").should("not.exist");
- });
-
- updateOperator("Current", "Previous");
- cy.findByTestId("date-filter-picker").within(() => {
- cy.findByDisplayValue("Previous").should("be.visible");
- cy.findByLabelText("Include this year").should(
- "have.attr",
- "aria-checked",
- "false",
- );
- });
- });
- });
-});
-
-function updateOperator(from: string, to: string) {
- cy.findByTestId("date-filter-picker").findByDisplayValue(from).click();
- cy.findByRole("listbox").findByRole("option", { name: to }).click();
-}
diff --git a/frontend/src/metabase/querying/common/components/DatePicker/RelativeDatePicker/RelativeDatePicker.unit.spec.tsx b/frontend/src/metabase/querying/common/components/DatePicker/RelativeDatePicker/RelativeDatePicker.unit.spec.tsx
index fc06e05d104..ed43e6f6bc7 100644
--- a/frontend/src/metabase/querying/common/components/DatePicker/RelativeDatePicker/RelativeDatePicker.unit.spec.tsx
+++ b/frontend/src/metabase/querying/common/components/DatePicker/RelativeDatePicker/RelativeDatePicker.unit.spec.tsx
@@ -104,6 +104,26 @@ describe("RelativeDatePicker", () => {
});
});
+ it("should not lose the 'Include current' switch state when navigating from Past to Next tab", async () => {
+ setup({
+ value: {
+ type: "relative",
+ value: -30,
+ unit: "day",
+ options: { includeCurrent: true },
+ },
+ });
+
+ expect(screen.getByRole("tab", { name: "Previous" })).toHaveAttribute(
+ "aria-selected",
+ "true",
+ );
+ expect(screen.getByLabelText("Include today")).toBeChecked();
+
+ await userEvent.click(screen.getByRole("tab", { name: "Next" }));
+ expect(screen.getByLabelText("Include today")).toBeChecked();
+ });
+
it("should not lose values when navigating from Past to Next tab", async () => {
setup();
diff --git a/frontend/src/metabase/querying/common/components/DatePicker/SimpleDatePicker/SimpleDatePicker.unit.spec.tsx b/frontend/src/metabase/querying/common/components/DatePicker/SimpleDatePicker/SimpleDatePicker.unit.spec.tsx
index c0c213a3015..3e77ac4eae5 100644
--- a/frontend/src/metabase/querying/common/components/DatePicker/SimpleDatePicker/SimpleDatePicker.unit.spec.tsx
+++ b/frontend/src/metabase/querying/common/components/DatePicker/SimpleDatePicker/SimpleDatePicker.unit.spec.tsx
@@ -38,7 +38,107 @@ function setup({
return { onChange };
}
+async function selectOperator(from: string, to: string) {
+ await userEvent.click(screen.getByDisplayValue(from));
+ await userEvent.click(screen.getByRole("option", { name: to }));
+}
+
describe("SimpleDatePicker", () => {
+ it("should list all operators with 'All time' selected by default", async () => {
+ setup();
+
+ await userEvent.click(screen.getByDisplayValue("All time"));
+
+ const options = screen.getAllByRole("option");
+ expect(options.map((option) => option.textContent)).toEqual([
+ "All time",
+ "Previous",
+ "Next",
+ "Current",
+ "Before",
+ "After",
+ "On",
+ "Between",
+ ]);
+ expect(screen.getByRole("option", { name: "All time" })).toHaveAttribute(
+ "aria-selected",
+ "true",
+ );
+ expect(screen.getByRole("button", { name: "Apply" })).toBeEnabled();
+ });
+
+ it("should submit a past value that includes the current day", async () => {
+ const { onChange } = setup();
+
+ await selectOperator("All time", "Previous");
+ expect(screen.getByLabelText("Interval")).toHaveValue("30");
+ expect(screen.getByRole("textbox", { name: "Unit" })).toHaveValue("days");
+
+ const includeSwitch = screen.getByLabelText("Include today");
+ expect(includeSwitch).not.toBeChecked();
+ await userEvent.click(includeSwitch);
+ expect(includeSwitch).toBeChecked();
+ await userEvent.click(screen.getByRole("button", { name: "Apply" }));
+
+ expect(onChange).toHaveBeenCalledTimes(1);
+ expect(onChange).toHaveBeenCalledWith({
+ type: "relative",
+ value: -30,
+ unit: "day",
+ options: { includeCurrent: true },
+ });
+ });
+
+ it("should preserve the 'Include current' switch across unit and direction changes and submit its last state", async () => {
+ const { onChange } = setup({
+ value: {
+ type: "relative",
+ value: 30,
+ unit: "year",
+ options: { includeCurrent: true },
+ },
+ });
+
+ expect(screen.getByLabelText("Include this year")).toBeChecked();
+
+ await userEvent.click(screen.getByRole("textbox", { name: "Unit" }));
+ await userEvent.click(screen.getByRole("option", { name: "quarters" }));
+ expect(screen.getByLabelText("Include this quarter")).toBeChecked();
+
+ await selectOperator("Next", "Previous");
+ expect(screen.getByLabelText("Include this quarter")).toBeChecked();
+
+ await userEvent.click(screen.getByLabelText("Include this quarter"));
+ await selectOperator("Previous", "Next");
+ expect(screen.getByLabelText("Include this quarter")).not.toBeChecked();
+ await userEvent.click(screen.getByRole("button", { name: "Apply" }));
+
+ expect(onChange).toHaveBeenCalledTimes(1);
+ expect(onChange).toHaveBeenCalledWith({
+ type: "relative",
+ value: 30,
+ unit: "quarter",
+ options: { includeCurrent: false },
+ });
+ });
+
+ it("should reset the 'Include current' switch after switching to the `Current` relative date", async () => {
+ setup({
+ value: {
+ type: "relative",
+ value: 30,
+ unit: "year",
+ options: { includeCurrent: true },
+ },
+ });
+
+ await selectOperator("Next", "Current");
+ expect(screen.queryByLabelText(/^Include/)).not.toBeInTheDocument();
+
+ await selectOperator("Current", "Previous");
+ expect(screen.getByLabelText("Include this year")).not.toBeChecked();
+ });
+
it("should be able change and submit the value", async () => {
const { onChange } = setup();
diff --git a/frontend/src/metabase/querying/filters/components/FilterPicker/DateFilterPicker/DateFilterPicker.unit.spec.tsx b/frontend/src/metabase/querying/filters/components/FilterPicker/DateFilterPicker/DateFilterPicker.unit.spec.tsx
index f8b45a28332..012cbcc92ba 100644
--- a/frontend/src/metabase/querying/filters/components/FilterPicker/DateFilterPicker/DateFilterPicker.unit.spec.tsx
+++ b/frontend/src/metabase/querying/filters/components/FilterPicker/DateFilterPicker/DateFilterPicker.unit.spec.tsx
@@ -205,6 +205,25 @@ describe("DateFilterPicker", () => {
});
});
+ it("should keep the 'Include current' option when updating a relative date filter", async () => {
+ const { query, filter } = createQueryWithRelativeDateFilter({
+ query: initialQuery,
+ value: -30,
+ unit: "day",
+ options: { includeCurrent: true },
+ });
+ const { getNextRelativeFilterParts } = setup({ query, column, filter });
+
+ expect(screen.getByLabelText("Include today")).toBeChecked();
+ await userEvent.click(screen.getByText("Update filter"));
+
+ expect(getNextRelativeFilterParts()).toMatchObject({
+ value: -30,
+ unit: "day",
+ options: { includeCurrent: true },
+ });
+ });
+
it("should add an exclude date filter", async () => {
const { getNextFilterColumnName, getNextExcludeFilterParts } = setup({
query: initialQuery,
diff --git a/frontend/src/metabase/querying/filters/components/TimeseriesChrome/TimeseriesFilterPicker/TimeseriesFilterPicker.unit.spec.tsx b/frontend/src/metabase/querying/filters/components/TimeseriesChrome/TimeseriesFilterPicker/TimeseriesFilterPicker.unit.spec.tsx
index 1c56b5c6f2f..6d1b32bfb79 100644
--- a/frontend/src/metabase/querying/filters/components/TimeseriesChrome/TimeseriesFilterPicker/TimeseriesFilterPicker.unit.spec.tsx
+++ b/frontend/src/metabase/querying/filters/components/TimeseriesChrome/TimeseriesFilterPicker/TimeseriesFilterPicker.unit.spec.tsx
@@ -109,6 +109,31 @@ describe("TimeseriesFilterPicker", () => {
});
});
+ it("should show an existing relative filter that includes the current unit", async () => {
+ const initialQuery = Lib.createTestQuery(
+ SAMPLE_PROVIDER,
+ DEFAULT_TEST_QUERY,
+ );
+ const { query, column, filter } = createQueryWithFilter(
+ initialQuery,
+ Lib.relativeDateFilterClause({
+ column: findDateColumn(initialQuery),
+ value: 30,
+ unit: "year",
+ offsetValue: null,
+ offsetUnit: null,
+ options: { includeCurrent: true },
+ }),
+ );
+ setup({ query, column, filter });
+
+ await userEvent.click(screen.getByText("Next 30 years or this year"));
+ await waitForFloatingPosition();
+
+ expect(await screen.findByDisplayValue("Next")).toBeInTheDocument();
+ expect(screen.getByLabelText("Include this year")).toBeChecked();
+ });
+
it("should allow to remove a filter", async () => {
const { query, column, filter } = createQueryWithFilter();
const { getNextFilterParts } = setup({ query, column, filter });
diff --git a/frontend/src/metabase/querying/filters/utils/dates.unit.spec.ts b/frontend/src/metabase/querying/filters/utils/dates.unit.spec.ts
index 496e60f581c..6b5f2c38c73 100644
--- a/frontend/src/metabase/querying/filters/utils/dates.unit.spec.ts
+++ b/frontend/src/metabase/querying/filters/utils/dates.unit.spec.ts
@@ -111,6 +111,24 @@ describe("getDateFilterClause", () => {
value: { type: "relative", value: -2, unit: "year" },
displayName: "Created At is in the previous 2 years",
},
+ {
+ value: {
+ type: "relative",
+ value: -30,
+ unit: "day",
+ options: { includeCurrent: true },
+ },
+ displayName: "Created At is in the previous 30 days or today",
+ },
+ {
+ value: {
+ type: "relative",
+ value: 30,
+ unit: "year",
+ options: { includeCurrent: true },
+ },
+ displayName: "Created At is in the next 30 years or this year",
+ },
{
value: {
type: "relative",
@@ -339,6 +357,15 @@ describe("getDateFilterDisplayName", () => {
value: { type: "relative", value: 2, unit: "month" },
displayName: "Next 2 months",
},
+ {
+ value: {
+ type: "relative",
+ value: 30,
+ unit: "year",
+ options: { includeCurrent: true },
+ },
+ displayName: "Next 30 years or this year",
+ },
{
value: {
type: "relative",