Skip to content

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

35.3 KB
Raw Blame
import {
  SAMPLE_DB_ID,
  SAMPLE_DB_SCHEMA_ID,
  WRITABLE_DB_ID,
} from "e2e/support/cypress_data";
import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";

const { H } = cy;
const { TablePicker, TableSection, FieldSection, PreviewSection, Shared } =
  cy.H.DataModel;

const {
  verifyTablePreview,
  verifyObjectDetailPreview,
  visitArea,
  getTriggeredFromArea,
} = Shared;

const { ORDERS_ID, ORDERS, FEEDBACK_ID, FEEDBACK, PRODUCTS, PRODUCTS_ID } =
  SAMPLE_DATABASE;

const MYSQL_DB_ID = SAMPLE_DB_ID + 1;
const MYSQL_DB_SCHEMA_ID = `${MYSQL_DB_ID}:`;

const areas: ("admin" | "data studio")[] = ["admin", "data studio"];

areas.forEach((area) => {
  describe(`data model > ${area}`, () => {
    const visit = visitArea(area);
    const getTriggeredFrom = getTriggeredFromArea(area);

    beforeEach(() => {
      H.restore();
      H.resetSnowplow();
      cy.signInAsAdmin();
      H.activateToken("pro-self-hosted");

      cy.intercept("GET", "/api/database/*/schemas?*").as("schemas");
      cy.intercept("GET", "/api/table/*/query_metadata*").as("metadata");
      cy.intercept("GET", "/api/database/*/schema/*").as("schema");
      cy.intercept("POST", "/api/dataset*").as("dataset");
      cy.intercept("GET", "/api/field/*/values").as("fieldValues");
      cy.intercept("PUT", "/api/field/*", cy.spy().as("updateFieldSpy")).as(
        "updateField",
      );
      cy.intercept("PUT", "/api/table/*/fields/order").as("updateFieldOrder");
      cy.intercept("POST", "/api/field/*/values").as("updateFieldValues");
      cy.intercept("POST", "/api/field/*/dimension").as("updateFieldDimension");
      cy.intercept("PUT", "/api/table").as("updateTables");
      cy.intercept("PUT", "/api/table/*").as("updateTable");

      cy.intercept({ method: "GET", pathname: "/api/database" }).as(
        "databases",
      );

      if (area === "admin") {
        cy.intercept("GET", "/api/field/*/values").as("fieldValues");
        cy.intercept("PUT", "/api/table/*").as("updateTable");
      }
    });

    describe("Field section", () => {
      beforeEach(() => {
        H.resetSnowplow();
        H.enableTracking();
      });

      afterEach(() => {
        H.expectNoBadSnowplowEvents();
      });

      describe("Name and description", () => {
        it("should allow changing the field name", () => {
          visit({
            databaseId: SAMPLE_DB_ID,
            schemaId: SAMPLE_DB_SCHEMA_ID,
            tableId: ORDERS_ID,
            fieldId: ORDERS.TAX,
          });

          FieldSection.getNameInput().clear().type("New tax").blur();
          cy.wait("@updateField");
          verifyAndCloseToast("Name of Tax updated");
          TableSection.getFieldNameInput("New tax").should("exist");

          cy.log("verify preview");
          TableSection.clickField("New tax");
          FieldSection.getPreviewButton().click();
          verifyTablePreview({
            column: "New tax",
            values: ["2.07", "6.1", "2.9", "6.01", "7.03"],
          });
          verifyObjectDetailPreview({ rowNumber: 4, row: ["New tax", "2.07"] });

          cy.log("verify viz");
          H.openOrdersTable();
          H.tableHeaderColumn("New tax").should("be.visible");
          H.tableHeaderColumn("Tax", { scrollIntoView: false }).should(
            "not.exist",
          );
        });

        it("should allow changing the field description", () => {
          visit({
            databaseId: SAMPLE_DB_ID,
            schemaId: SAMPLE_DB_SCHEMA_ID,
            tableId: ORDERS_ID,
            fieldId: ORDERS.TOTAL,
          });

          FieldSection.getDescriptionInput()
            .clear()
            .type("New description")
            .blur();
          cy.wait("@updateField");
          verifyAndCloseToast("Description of Total updated");
          TableSection.getFieldDescriptionInput("Total").should(
            "have.value",
            "New description",
          );

          cy.log("verify preview");
          TableSection.clickField("Total");
          FieldSection.getPreviewButton().click();
          verifyTablePreview({
            column: "Total",
            description: "New description",
            values: ["39.72", "117.03", "49.21", "115.23", "134.91"],
          });

          cy.visit(
            `/reference/databases/${SAMPLE_DB_ID}/tables/${ORDERS_ID}/fields/${ORDERS.TOTAL}`,
          );
          // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
          cy.findByText("Total").should("be.visible");
          // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
          cy.findByText("New description").should("be.visible");
        });

        it("should remap FK display value from field section", () => {
          visit({
            databaseId: SAMPLE_DB_ID,
            schemaId: SAMPLE_DB_SCHEMA_ID,
            tableId: ORDERS_ID,
            fieldId: ORDERS.PRODUCT_ID,
          });

          FieldSection.getNameInput()
            .clear()
            .type("Remapped Product ID")
            .realPress("Tab");
          cy.wait("@updateField");
          verifyAndCloseToast("Name of Product ID updated");

          cy.log("verify preview");
          FieldSection.getPreviewButton().click();
          verifyTablePreview({
            column: "Remapped Product ID",
            values: ["14", "123", "105", "94", "132"],
          });
          verifyObjectDetailPreview({
            rowNumber: 2,
            row: ["Remapped Product ID", "14"],
          });

          cy.log("verify viz");
          H.openOrdersTable({ limit: 5 });
          H.tableHeaderColumn("Remapped Product ID").should("be.visible");
        });
      });

      describe("Field values", () => {
        it("should allow to sync table schema, re-scan table, and discard cached field values", () => {
          visit({
            databaseId: SAMPLE_DB_ID,
            schemaId: SAMPLE_DB_SCHEMA_ID,
            tableId: PRODUCTS_ID,
            fieldId: PRODUCTS.CATEGORY,
          });
          FieldSection.getFieldValuesButton().click();

          cy.log("re-scan field");
          H.modal().within(() => {
            cy.button("Re-scan field").click();
            cy.button("Re-scan field").should("not.exist");
            cy.button("Scan triggered!").should("be.visible");
            cy.button("Scan triggered!").should("not.exist");
            cy.button("Re-scan field").should("be.visible");
          });

          cy.log("discard cached field values");
          H.modal().within(() => {
            cy.button("Discard cached field values").click();
            cy.button("Discard cached field values").should("not.exist");
            cy.button("Discard triggered!").should("be.visible");
            cy.button("Discard triggered!").should("not.exist");
            cy.button("Discard cached field values").should("be.visible");
          });

          // synthetic escape: realPress depends on real browser focus, which
          // the app may not hold in CI, so the keystroke can miss the modal
          cy.get("body").type("{esc}");
          H.modal().should("not.exist");
        });

        it("should not automatically re-fetch field values when they are discarded unless 'Custom mapping' is used (metabase#62626)", () => {
          visit({
            databaseId: SAMPLE_DB_ID,
            schemaId: SAMPLE_DB_SCHEMA_ID,
            tableId: PRODUCTS_ID,
            fieldId: PRODUCTS.CATEGORY,
          });

          FieldSection.getFieldValuesButton().click();
          H.modal().within(() => {
            cy.button("Discard cached field values").click();
            cy.button("Discard triggered!").should("be.visible");
            cy.button("Discard triggered!").should("not.exist");
          });

          cy.get("@fieldValues.all").should("have.length", 0);
        });
      });

      describe("Data", () => {
        describe("Coercion strategy", () => {
          it("should allow you to cast a field to a data type", () => {
            visit({
              databaseId: SAMPLE_DB_ID,
              schemaId: SAMPLE_DB_SCHEMA_ID,
              tableId: FEEDBACK_ID,
              fieldId: FEEDBACK.RATING,
            });

            cy.log(
              "Ensure that Coercion strategy has been humanized (metabase#44723)",
            );
            FieldSection.getCoercionToggle().parent().scrollIntoView().click();
            H.popover().should("not.contain.text", "Coercion");
            H.popover().findByText("UNIX seconds → Datetime").click();
            cy.wait("@updateField");
            H.expectUnstructuredSnowplowEvent({
              event: "metadata_edited",
              event_detail: "type_casting",
              triggered_from: getTriggeredFrom(),
            });
            verifyAndCloseToast("Casting enabled for Rating");

            cy.log("verify preview");
            FieldSection.getPreviewButton().click();
            verifyTablePreview({
              column: "Rating",
              values: [
                "December 31, 1969, 4:00 PM",
                "December 31, 1969, 4:00 PM",
                "December 31, 1969, 4:00 PM",
                "December 31, 1969, 4:00 PM",
                "December 31, 1969, 4:00 PM",
              ],
            });
            verifyObjectDetailPreview({
              rowNumber: 4,
              row: ["Rating", "December 31, 1969, 4:00 PM"],
            });

            cy.log("verify viz");
            H.openTable({ database: SAMPLE_DB_ID, table: FEEDBACK_ID });
            cy.findAllByTestId("cell-data")
              .contains("December 31, 1969, 4:00 PM")
              .should("have.length.greaterThan", 0);
          });

          it("should allow to enable, change, and disable coercion strategy", () => {
            visit({
              databaseId: SAMPLE_DB_ID,
              schemaId: SAMPLE_DB_SCHEMA_ID,
              tableId: FEEDBACK_ID,
              fieldId: FEEDBACK.RATING,
            });

            cy.log("show error when strategy not chosen after toggling");
            FieldSection.getCoercionToggle()
              .parent()
              .click({ scrollBehavior: "center" });
            clickAway();
            FieldSection.get()
              .findByText("To enable casting, please select a data type")
              .should("be.visible");

            cy.log("enable casting");
            FieldSection.getCoercionInput().click({ scrollBehavior: "center" });
            H.popover().findByText("UNIX nanoseconds → Datetime").click();
            cy.wait("@updateField");
            verifyAndCloseToast("Casting enabled for Rating");

            cy.log("verify preview");
            FieldSection.getPreviewButton().click();
            // ideally we should change the formatting to show smaller values and assert those
            // but we can't set formatting on a coerced field (metabase#60483)
            verifyTablePreview({
              column: "Rating",
              values: [
                "December 31, 1969, 4:00 PM",
                "December 31, 1969, 4:00 PM",
                "December 31, 1969, 4:00 PM",
                "December 31, 1969, 4:00 PM",
                "December 31, 1969, 4:00 PM",
              ],
            });
            verifyObjectDetailPreview({
              rowNumber: 4,
              row: ["Rating", "December 31, 1969, 4:00 PM"],
            });

            cy.log("change casting");
            FieldSection.getCoercionInput().click({ scrollBehavior: "center" });
            H.popover().findByText("UNIX seconds → Datetime").click();
            cy.wait("@updateField");
            verifyAndCloseToast("Casting updated for Rating");

            cy.log("disable casting");
            FieldSection.getCoercionToggle()
              .parent()
              .click({ scrollBehavior: "center" });
            cy.wait("@updateField");
            verifyAndCloseToast("Casting disabled for Rating");

            cy.log("enable casting");
            FieldSection.getCoercionToggle()
              .parent()
              .click({ scrollBehavior: "center" });
            H.popover().findByText("UNIX seconds → Datetime").click();
            cy.wait("@updateField");
            verifyAndCloseToast("Casting enabled for Rating");

            H.openTable({ database: SAMPLE_DB_ID, table: FEEDBACK_ID });
            cy.findAllByTestId("cell-data")
              .contains("December 31, 1969, 4:00 PM")
              .should("have.length.greaterThan", 0);
          });
        });
      });

      describe("Metadata", () => {
        describe("Semantic type", () => {
          it("should allow to change the type to 'No semantic type' (metabase#59052)", () => {
            visit({
              databaseId: SAMPLE_DB_ID,
              schemaId: SAMPLE_DB_SCHEMA_ID,
              tableId: ORDERS_ID,
              fieldId: ORDERS.PRODUCT_ID,
            });
            cy.wait(["@metadata", "@metadata"]);

            FieldSection.getSemanticTypeInput()
              .should("have.value", "Foreign Key")
              // it should allow to just type to search (metabase#59052)
              .type("no sema{downarrow}{enter}");
            cy.wait("@updateField");
            H.expectUnstructuredSnowplowEvent({
              event: "metadata_edited",
              event_detail: "semantic_type_change",
              triggered_from: getTriggeredFrom(),
            });
            H.undoToast().should(
              "contain.text",
              "Semantic type of Product ID updated",
            );

            cy.log("verify preview");
            FieldSection.getPreviewButton().click();
            cy.wait("@dataset");
            PreviewSection.get()
              .findAllByTestId("cell-data")
              .should("have.length", 6)
              .eq(1)
              // FKs get blueish background
              .should("have.css", "background-color", "rgba(0, 0, 0, 0)");

            cy.reload();
            cy.wait("@metadata");

            FieldSection.getSemanticTypeInput().should(
              "have.value",
              "No semantic type",
            );
          });

          it("should allow to change the type to 'Foreign Key' and choose the target field (metabase#59052)", () => {
            visit({
              databaseId: SAMPLE_DB_ID,
              schemaId: SAMPLE_DB_SCHEMA_ID,
              tableId: ORDERS_ID,
              fieldId: ORDERS.QUANTITY,
            });

            FieldSection.getSemanticTypeInput()
              .should("have.value", "Quantity")
              .click();
            H.popover().findByText("Foreign Key").click();
            cy.wait("@updateField");
            verifyAndCloseToast("Semantic type of Quantity updated");

            cy.log("verify preview");
            FieldSection.getPreviewButton().click();
            cy.wait("@dataset");
            PreviewSection.get()
              .findAllByTestId("cell-data")
              .should("have.length", 6)
              .eq(1)
              // FKs get blueish background
              .should("not.have.css", "background-color", "rgba(0, 0, 0, 0)");

            FieldSection.getSemanticTypeFkTarget()
              .should("have.value", "")
              // it should allow to just type to search (metabase#59052)
              .type("products{downarrow}{enter}");
            cy.wait("@updateField");
            H.undoToast().should(
              "contain.text",
              "Semantic type of Quantity updated",
            );

            cy.log("verify preview");
            cy.wait("@dataset");
            PreviewSection.get()
              .findAllByTestId("cell-data")
              .should("have.length", 6)
              .eq(1)
              // FKs get blueish background
              .should("not.have.css", "background-color", "rgba(0, 0, 0, 0)");

            cy.reload();
            cy.wait(["@metadata", "@metadata"]);

            FieldSection.getSemanticTypeFkTarget()
              .scrollIntoView() //This should not be necessary, but CI consistently fails to scroll into view on mount
              .should("be.visible")
              .and("have.value", "Products → ID");
          });

          it("should allow to change the foreign key target", () => {
            visit({
              databaseId: SAMPLE_DB_ID,
              schemaId: SAMPLE_DB_SCHEMA_ID,
              tableId: ORDERS_ID,
              fieldId: ORDERS.USER_ID,
            });

            FieldSection.getSemanticTypeFkTarget()
              .should("have.value", "People → ID")
              .click();
            H.popover().within(() => {
              cy.findByText("Reviews → ID").should("be.visible");
              cy.findByText("Products → ID").click();
            });
            cy.wait("@updateField");
            H.undoToast().should(
              "contain.text",
              "Semantic type of User ID updated",
            );
            FieldSection.getSemanticTypeFkTarget().should(
              "have.value",
              "Products → ID",
            );

            H.openTable({
              database: SAMPLE_DB_ID,
              table: ORDERS_ID,
              mode: "notebook",
            });
            cy.icon("join_left_outer").click();
            H.miniPicker().within(() => {
              cy.findByText("Sample Database").click();
              cy.findByText("Products").click();
            });
            cy.findByLabelText("Left column").should("contain.text", "User ID");
          });

          it("should allow to change the type to 'Currency' and choose the currency (metabase#59052)", () => {
            visit({
              databaseId: SAMPLE_DB_ID,
              schemaId: SAMPLE_DB_SCHEMA_ID,
              tableId: ORDERS_ID,
              fieldId: ORDERS.TAX,
            });

            FieldSection.getSemanticTypeInput()
              .should("have.value", "No semantic type")
              .click();
            H.popover().findByText("Currency").click();
            cy.wait("@updateField");
            verifyAndCloseToast("Semantic type of Tax updated");

            cy.log("verify preview");
            TableSection.clickField("Tax");
            FieldSection.getPreviewButton().click();
            verifyTablePreview({
              column: "Tax ($)",
              values: ["2.07", "6.10", "2.90", "6.01", "7.03"],
            });
            verifyObjectDetailPreview({
              rowNumber: 4,
              row: ["Tax ($)", "2.07"],
            });

            cy.log("change currency");
            FieldSection.getSemanticTypeCurrencyInput()
              .scrollIntoView()
              .should("be.visible")
              .and("have.value", "US Dollar")
              // it should allow to just type to search (metabase#59052)
              .type("canadian{downarrow}{enter}");
            cy.wait("@updateField");
            verifyAndCloseToast("Semantic type of Tax updated");

            cy.log("verify preview");
            verifyTablePreview({
              column: "Tax (CA$)",
              values: ["2.07", "6.10", "2.90", "6.01", "7.03"],
            });
            verifyObjectDetailPreview({
              rowNumber: 4,
              row: ["Tax (CA$)", "2.07"],
            });

            cy.log("verify viz");
            H.openOrdersTable();
            // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
            cy.findByText("Tax (CA$)").should("be.visible");
          });

          it("should correctly filter out options in Foreign Key picker (metabase#56839)", () => {
            visit({
              databaseId: SAMPLE_DB_ID,
              schemaId: SAMPLE_DB_SCHEMA_ID,
              tableId: ORDERS_ID,
              fieldId: ORDERS.PRODUCT_ID,
            });
            cy.wait(["@metadata", "@metadata"]);

            FieldSection.getSemanticTypeFkTarget().focus().clear();
            H.popover()
              .should("contain.text", "Orders → ID")
              .and("contain.text", "People → ID")
              .and("contain.text", "Products → ID")
              .and("contain.text", "Reviews → ID");

            cy.log("should case-insensitive match field display name");
            FieldSection.getSemanticTypeFkTarget().focus().type("id");
            H.popover()
              .should("contain.text", "Orders → ID")
              .and("contain.text", "People → ID")
              .and("contain.text", "Products → ID")
              .and("contain.text", "Reviews → ID");

            cy.log("should case-insensitive match field description");
            FieldSection.getSemanticTypeFkTarget().focus().clear().type("EXT");
            H.popover()
              .should("not.contain.text", "Orders → ID")
              .and("not.contain.text", "People → ID")
              .and("contain.text", "Products → ID")
              .and("contain.text", "Reviews → ID");
          });

          it("should not let you change the type to 'Number' (metabase#16781)", () => {
            visit({
              databaseId: SAMPLE_DB_ID,
              schemaId: SAMPLE_DB_SCHEMA_ID,
              tableId: ORDERS_ID,
              fieldId: ORDERS.PRODUCT_ID,
            });
            cy.wait(["@metadata", "@metadata"]);

            FieldSection.getSemanticTypeInput().click();
            H.popover()
              .should("contain.text", "Foreign Key")
              .and("not.contain.text", "Number");
          });

          it("should not overflow the screen on smaller viewports (metabase#56442)", () => {
            const viewportHeight = 400;

            cy.viewport(1280, viewportHeight);
            visit({ databaseId: SAMPLE_DB_ID });
            TablePicker.getTable("Reviews").scrollIntoView().click();
            if (area === "data studio") {
              TableSection.clickFieldsTab();
            }
            TableSection.clickField("ID");
            FieldSection.getSemanticTypeInput().click();

            H.popover().scrollTo("top");
            H.popover()
              .findByText("Entity Key")
              .should(($element) => {
                const rect = $element[0].getBoundingClientRect();
                expect(rect.top).greaterThan(0);
              });

            H.popover().scrollTo("bottom");
            H.popover()
              .findByText("No semantic type")
              .should(($element) => {
                const rect = $element[0].getBoundingClientRect();
                expect(rect.bottom).lessThan(viewportHeight);
              });
          });

          it(
            "should show an error with links to other fields with 'Entity name' semantic type",
            { tags: "@external" },
            () => {
              H.restore("postgres-writable");
              H.resetTestTable({ type: "postgres", table: "many_data_types" });
              cy.signInAsAdmin();
              H.resyncDatabase({
                dbId: WRITABLE_DB_ID,
                tableName: "many_data_types",
              });

              visit({ databaseId: WRITABLE_DB_ID });
              TablePicker.getTable("Many Data Types").click();
              if (area === "data studio") {
                TableSection.clickFieldsTab();
              }
              TableSection.clickField("Json → D");
              FieldSection.getSemanticTypeInput().click();
              H.popover().findByText("Entity Name").click();

              TableSection.clickField("Text");
              FieldSection.getSemanticTypeInput().click();
              H.popover().findByText("Entity Name").click();

              FieldSection.get().should(
                "contain.text",
                "There are other fields with this semantic type: Json: Json → D",
              );
              FieldSection.get()
                .findByRole("link", { name: "Json: Json → D" })
                .should("be.visible")
                .click();

              FieldSection.getNameInput().should("have.value", "Json → D");

              FieldSection.get().should(
                "contain.text",
                "There are other fields with this semantic type: Text",
              );
              FieldSection.get()
                .findByRole("link", { name: "Text" })
                .should("be.visible")
                .click();

              FieldSection.getNameInput().should("have.value", "Text");
            },
          );
        });
      });

      describe("Behavior", () => {
        describe("Visibility", () => {
          it("should let you change field visibility to 'Everywhere'", () => {
            cy.request("PUT", `/api/field/${ORDERS.TAX}`, {
              visibility_type: "sensitive",
            });
            visit({
              databaseId: SAMPLE_DB_ID,
              schemaId: SAMPLE_DB_SCHEMA_ID,
              tableId: ORDERS_ID,
              fieldId: ORDERS.TAX,
            });

            FieldSection.getVisibilityInput()
              .should("have.value", "Do not include")
              .click();
            H.popover().findByText("Everywhere").click();
            cy.wait("@updateField");
            H.expectUnstructuredSnowplowEvent({
              event: "metadata_edited",
              event_detail: "visibility_change",
              triggered_from: getTriggeredFrom(),
            });
            verifyAndCloseToast("Visibility of Tax updated");
            FieldSection.getVisibilityInput().should(
              "have.value",
              "Everywhere",
            );

            cy.log("verify preview");
            TableSection.clickField("Tax");
            FieldSection.getPreviewButton().click();
            verifyTablePreview({
              column: "Tax",
              values: ["2.07", "6.1", "2.9", "6.01", "7.03"],
            });
            verifyObjectDetailPreview({
              rowNumber: 4,
              row: ["Tax", "2.07"],
            });

            cy.log("table viz");
            H.openOrdersTable();
            H.tableHeaderColumn("Total").should("be.visible");
            H.tableHeaderColumn("Tax").should("be.visible");

            cy.log("object detail viz");
            cy.findByTestId("table-body")
              .findAllByTestId("cell-data")
              .eq(0)
              .click();
            H.modal().findByText("Tax").should("be.visible");
            H.modal().findByText("2.07").should("be.visible");
          });

          it("should let you change field visibility to 'Do not include'", () => {
            visit({
              databaseId: SAMPLE_DB_ID,
              schemaId: SAMPLE_DB_SCHEMA_ID,
              tableId: ORDERS_ID,
              fieldId: ORDERS.TAX,
            });

            FieldSection.getVisibilityInput()
              .should("have.value", "Everywhere")
              .click();
            H.popover().findByText("Do not include").click();
            cy.wait("@updateField");
            verifyAndCloseToast("Visibility of Tax updated");
            FieldSection.getVisibilityInput().should(
              "have.value",
              "Do not include",
            );

            cy.log("verify preview");
            TableSection.clickField("Tax");
            FieldSection.getPreviewButton().click();
            PreviewSection.get()
              .findByText("This field is hidden")
              .should("exist");
            cy.get("@dataset.all").should("have.length", 0);
            PreviewSection.getPreviewTypeInput().findByText("Detail").click();
            cy.wait("@dataset");
            PreviewSection.get().findByText("Tax").should("not.exist");

            cy.log("table viz");
            H.openOrdersTable();
            H.tableHeaderColumn("Total").should("be.visible");
            H.tableHeaderColumn("Tax", { scrollIntoView: false }).should(
              "not.exist",
            );

            cy.log("object detail viz");
            cy.findByTestId("table-body")
              .findAllByTestId("cell-data")
              .eq(0)
              .click();
            H.modal().findByText("Tax").should("not.exist");
            H.modal().findByText("2.07").should("not.exist");
          });

          it("should let you change field visibility to 'Do not include' even if Preview is opened (metabase#61806)", () => {
            visit({
              databaseId: SAMPLE_DB_ID,
              schemaId: SAMPLE_DB_SCHEMA_ID,
              tableId: ORDERS_ID,
              fieldId: ORDERS.TAX,
            });

            TableSection.clickField("Tax");
            FieldSection.getPreviewButton().click();
            PreviewSection.get().within(() => {
              cy.findByText("Filtering").click();

              cy.findByTestId("number-filter-picker").should("be.visible");
            });

            FieldSection.getVisibilityInput()
              .should("have.value", "Everywhere")
              .click();
            H.popover().findByText("Do not include").click();
            cy.wait("@updateField");

            PreviewSection.get()
              .findByText("This field is hidden")
              .should("exist");
          });

          it("should let you change field visibility to 'Only in detail views'", () => {
            visit({
              databaseId: SAMPLE_DB_ID,
              schemaId: SAMPLE_DB_SCHEMA_ID,
              tableId: ORDERS_ID,
              fieldId: ORDERS.TAX,
            });

            FieldSection.getVisibilityInput()
              .should("have.value", "Everywhere")
              .click();
            H.popover().findByText("Only in detail views").click();
            cy.wait("@updateField");
            verifyAndCloseToast("Visibility of Tax updated");
            FieldSection.getVisibilityInput().should(
              "have.value",
              "Only in detail views",
            );

            cy.log("verify preview");
            TableSection.clickField("Tax");
            FieldSection.getPreviewButton().click();
            PreviewSection.get()
              .findByText("This field is hidden")
              .should("exist");
            cy.get("@dataset.all").should("have.length", 0);
            verifyObjectDetailPreview({
              rowNumber: 4,
              row: ["Tax", "2.07"],
            });

            cy.log("table viz");
            H.openOrdersTable();
            H.tableHeaderColumn("Total").should("be.visible");
            H.tableHeaderColumn("Tax", { scrollIntoView: false }).should(
              "not.exist",
            );

            cy.log("object detail viz");
            cy.findByTestId("table-body")
              .findAllByTestId("cell-data")
              .eq(0)
              .click();
            H.modal().findByText("Tax").should("be.visible");
            H.modal().findByText("2.07").should("be.visible");
          });

          it(
            "should be able to select and update a field in a database without schemas",
            { tags: ["@external"] },
            () => {
              H.restore("mysql-8");
              visit({
                databaseId: MYSQL_DB_ID,
                schemaId: MYSQL_DB_SCHEMA_ID,
                tableId: ORDERS_ID,
              });

              if (area === "data studio") {
                TableSection.clickFieldsTab();
              }
              TableSection.clickField("Tax");
              FieldSection.getVisibilityInput().click();
              H.popover().findByText("Do not include").click();
              cy.wait("@updateField");
              verifyAndCloseToast("Visibility of Tax updated");
              FieldSection.getVisibilityInput().should(
                "have.value",
                "Do not include",
              );
            },
          );
        });

        describe("Filtering", () => {
          it("should let you change filtering to 'Search box'", () => {
            visit({
              databaseId: SAMPLE_DB_ID,
              schemaId: SAMPLE_DB_SCHEMA_ID,
              tableId: ORDERS_ID,
              fieldId: ORDERS.QUANTITY,
            });

            FieldSection.getFilteringInput()
              .should("have.value", "A list of all values")
              .click();
            H.popover().findByText("Search box").click();
            cy.wait("@updateField");
            H.expectUnstructuredSnowplowEvent({
              event: "metadata_edited",
              event_detail: "filtering_change",
              triggered_from: getTriggeredFrom(),
            });
            verifyAndCloseToast("Filtering of Quantity updated");

            cy.log("verify preview");
            TableSection.clickField("Quantity");
            FieldSection.getPreviewButton().click();
            PreviewSection.getPreviewTypeInput()
              .findByText("Filtering")
              .click();
            PreviewSection.get().within(() => {
              cy.findByPlaceholderText("Enter a number").should("be.visible");
              cy.button(/Add filter/).should("not.exist");
            });

            cy.reload();
            FieldSection.getFilteringInput()
              .scrollIntoView()
              .should("be.visible")
              .and("have.value", "Search box");
          });

          it("should let you change filtering to 'Plain input box'", () => {
            visit({
              databaseId: SAMPLE_DB_ID,
              schemaId: SAMPLE_DB_SCHEMA_ID,
              tableId: ORDERS_ID,
              fieldId: ORDERS.QUANTITY,
            });

            FieldSection.getFilteringInput()
              .should("have.value", "A list of all values")
              .click();
            H.popover().findByText("Plain input box").click();
            cy.wait("@updateField");
            verifyAndCloseToast("Filtering of Quantity updated");

            cy.log("verify preview");
            TableSection.clickField("Quantity");
            FieldSection.getPreviewButton().click();
            PreviewSection.getPreviewTypeInput()
              .findByText("Filtering")
              .click();
            PreviewSection.get().within(() => {
              cy.findByPlaceholderText("Min").should("be.visible");
              cy.findByPlaceholderText("Max").should("be.visible");
              cy.button(/Add filter/).should("not.exist");
            });

            cy.reload();
            FieldSection.getFilteringInput()
              .scrollIntoView()
              .should("be.visible")
              .and("have.value", "Plain input box");
          });

          it("should let you change filtering to 'A list of all values'", () => {
            cy.request("PUT", `/api/field/${ORDERS.QUANTITY}`, {
              has_field_values: "none",
            });
            visit({
              databaseId: SAMPLE_DB_ID,
              schemaId: SAMPLE_DB_SCHEMA_ID,
              tableId: ORDERS_ID,
              fieldId: ORDERS.QUANTITY,
            });

            FieldSection.getFilteringInput()
              .should("have.value", "Plain input box")
              .click();
            H.popover().findByText("A list of all values").click();
            cy.wait("@updateField");
            verifyAndCloseToast("Filtering of Quantity updated");

            cy.log("verify preview");
            TableSection.clickField("Quantity");
            FieldSection.getPreviewButton().click();
            PreviewSection.getPreviewTypeInput()
              .findByText("Filtering")
              .click();
            PreviewSection.get().within(() => {
              cy.findByPlaceholderText("Search the list").should("be.visible");
              cy.button(/Add filter/).should("not.exist");
            });

            cy.reload();
            FieldSection.getFilteringInput()
              .scrollIntoView()
              .should("be.visible")
              .and("have.value", "A list of all values");
          });
        });
      });
    });
  });
});

function verifyAndCloseToast(message: string) {
  H.undoToast().should("contain.text", message);
  H.undoToast().icon("close").click({ force: true });
}

function clickAway() {
  cy.get("body").click(0, 0);
}