Skip to content

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

13.9 KB
Raw Blame
import {
  DOCUMENT_WITH_SUPPORTING_TEXT,
  DOCUMENT_WITH_THREE_CARDS_AND_COLUMNS,
  DOCUMENT_WITH_TWO_CARDS,
  DOCUMENT_WITH_TWO_LIGHTWEIGHT_CARDS,
} from "e2e/support/document-initial-data";

const { H } = cy;

describe("documents supporting text", () => {
  beforeEach(() => {
    H.restore();
    cy.signInAsAdmin();
  });

  it("should add supporting text to a standalone cardEmbed", () => {
    H.createDocument({
      name: "Supporting Text Test Document",
      document: DOCUMENT_WITH_TWO_LIGHTWEIGHT_CARDS,
      collection_id: null,
      idAlias: "documentId",
    });

    H.visitDocument("@documentId");

    const ORDERS_BY_YEAR_CARD_TITLE =
      "Orders, Count, Grouped by Created At (year)";

    // Wait for cards to load
    H.getDocumentCard(ORDERS_BY_YEAR_CARD_TITLE)
      .should("be.visible")
      .findByTestId("visualization-root")
      .should("exist");

    // Verify no supporting text or flexContainer exists initially
    H.documentContent()
      .findByTestId("document-card-supporting-text")
      .should("not.exist");
    H.documentContent().find('[data-type="flexContainer"]').should("not.exist");

    // Open the card menu and click "Add supporting text". The menu item is
    // rendered disabled while the node view can't resolve its position, and a
    // click on the label of a disabled button is dropped without an error, so
    // click the button itself and wait for it to be enabled.
    H.openDocumentCardMenu(ORDERS_BY_YEAR_CARD_TITLE);
    H.popover()
      .findByText("Add supporting text")
      .closest("button")
      .should("be.enabled")
      .click();

    // Verify a flexContainer was created
    H.documentContent().find('[data-type="flexContainer"]').should("exist");

    // Verify supporting text was added
    H.documentContent()
      .findByTestId("document-card-supporting-text")
      .should("exist");

    // Verify the supporting text has the placeholder text
    H.documentContent()
      .findByTestId("document-card-supporting-text")
      .should("contain.text", "Write whatever you'd like to");

    // Verify the flexContainer contains both supporting text and the card.
    // Kept as two re-queried chains: `.within()` freezes its subject, and the
    // card's node view is recreated when it moves into the new flexContainer.
    H.documentContent()
      .find('[data-type="flexContainer"]')
      .findByTestId("document-card-supporting-text")
      .should("exist");
    H.documentContent()
      .find('[data-type="flexContainer"]')
      .findByTestId("document-card-embed")
      .should("exist");

    // Verify the card is still there
    H.getDocumentCard(ORDERS_BY_YEAR_CARD_TITLE).should("exist");
  });

  it("should add, remove, edit and resize supporting text, disable adding more to its group, and persist width after save", () => {
    H.createDocument({
      name: "Resize Supporting Text Test Document",
      document: DOCUMENT_WITH_TWO_CARDS,
      collection_id: null,
      idAlias: "documentId",
    });

    H.visitDocument("@documentId");

    // Wait for cards to load
    H.getDocumentCard("Orders")
      .should("be.visible")
      .findByTestId("table-root")
      .should("exist");

    cy.log("add supporting text");
    H.openDocumentCardMenu("Orders");
    H.popover()
      .findByText("Add supporting text")
      .closest("button")
      .should("be.enabled")
      .click();

    // Verify supporting text was added
    H.documentContent()
      .findByTestId("document-card-supporting-text")
      .should("exist");

    cy.log(
      "'Add supporting text' is disabled when the group has supporting text",
    );
    H.openDocumentCardMenu("Orders");
    H.popover()
      .findByText("Add supporting text")
      .closest("button")
      .should("have.attr", "data-disabled");
    cy.realPress("Escape");
    cy.get(H.POPOVER_ELEMENT).should("not.exist");

    cy.log("empty supporting text is removed on Backspace");
    // Click into the supporting text
    H.documentContent()
      .findByTestId("document-card-supporting-text")
      .within(() => cy.get(".node-paragraph").click());

    // Press Backspace to delete the empty supporting text
    cy.realPress("Backspace");

    // Verify supporting text was removed
    H.documentContent()
      .findByTestId("document-card-supporting-text")
      .should("not.exist");

    // Verify the flexContainer was also removed (unwrapped)
    H.documentContent().find('[data-type="flexContainer"]').should("not.exist");

    // Verify the card is still there as a standalone card
    H.getDocumentCard("Orders").should("exist");

    cy.log("add supporting text again and edit it");
    H.openDocumentCardMenu("Orders");
    H.popover()
      .findByText("Add supporting text")
      .closest("button")
      .should("be.enabled")
      .click();

    // Verify flexContainer and supporting text were created
    H.documentContent().find('[data-type="flexContainer"]').should("exist");
    H.documentContent()
      .findByTestId("document-card-supporting-text")
      .should("exist");

    // Type some content
    cy.realType("# Hdg{enter}Lorem ipsum");

    // Verify the text was added
    H.documentContent()
      .findByTestId("document-card-supporting-text")
      .contains("h1", "Hdg")
      .should("exist");
    H.documentContent()
      .findByTestId("document-card-supporting-text")
      .contains("p", "Lorem ipsum")
      .should("exist");

    cy.log("resize supporting text and persist width after save");
    // Get initial widths of supporting text and card
    H.documentContent()
      .findByTestId("document-card-supporting-text")
      .then(($supportingText) => {
        // Unjustified type cast. FIXME
        const initialSupportingTextWidth = $supportingText.width() as number;
        cy.wrap(initialSupportingTextWidth).as("initialSupportingTextWidth");

        H.getDocumentCard("Orders").then(($card) => {
          // Unjustified type cast. FIXME
          const initialCardWidth = $card.width() as number;
          cy.wrap(initialCardWidth).as("initialCardWidth");
        });
      });

    // Get the flex container and resize handles
    const flexContainer = H.documentContent().find(
      '[data-type="flexContainer"]',
    );
    const handles = H.getResizeHandlesForFlexContianer(flexContainer);

    // Drag the handle to resize (increase supporting text width, decrease card width)
    H.documentDoDrag(handles.eq(0), { x: 150 });

    // Verify the widths changed
    H.documentContent()
      .findByTestId("document-card-supporting-text")
      .then(($supportingText) => {
        // Unjustified type cast. FIXME
        const newSupportingTextWidth = $supportingText.width() as number;
        cy.wrap(newSupportingTextWidth).as("newSupportingTextWidth");

        H.getDocumentCard("Orders").then(($card) => {
          // Unjustified type cast. FIXME
          const newCardWidth = $card.width() as number;
          cy.wrap(newCardWidth).as("newCardWidth");

          cy.get<number>("@initialSupportingTextWidth").then((initialWidth) => {
            // Supporting text should be wider
            expect(newSupportingTextWidth).to.be.greaterThan(initialWidth);
            // The change should be close to the drag distance
            expect(newSupportingTextWidth).to.be.closeTo(
              initialWidth + 150,
              10,
            );
          });

          cy.get<number>("@initialCardWidth").then((initialWidth) => {
            // Card should be narrower
            expect(newCardWidth).to.be.lessThan(initialWidth);
            // The change should be close to the drag distance
            expect(newCardWidth).to.be.closeTo(initialWidth - 150, 10);
          });
        });
      });

    // Click into the supporting text to edit it
    H.documentContent()
      .findByTestId("document-card-supporting-text")
      .within(() => cy.get(".node-paragraph").click());

    // Type some content
    const testText = "Supporting text for Orders chart";
    cy.realType(testText);
    cy.realPress("Tab");

    // Verify the text was added
    H.documentContent()
      .findByTestId("document-card-supporting-text")
      .should("contain.text", testText);

    // Save the document
    H.documentSaveButton().should("not.be.disabled").click();

    // Wait for save confirmation
    cy.findByTestId("toast-undo")
      .should("be.visible")
      .and("contain.text", "Document saved");

    // Reload the page to verify persistence
    cy.intercept({
      method: "GET",
      path: "/api/document/*",
    }).as("documentGet");
    cy.reload();
    H.closeNavigationSidebar();

    cy.wait("@documentGet");

    // Wait for the document to load
    H.getDocumentCard("Orders")
      .should("be.visible")
      .findByTestId("table-root")
      .should("exist");

    // Verify the widths are still the same after reload
    H.documentContent()
      .findByTestId("document-card-supporting-text")
      .then(($supportingText) => {
        // Unjustified type cast. FIXME
        const reloadedSupportingTextWidth = $supportingText.width() as number;

        H.getDocumentCard("Orders").then(($card) => {
          // Unjustified type cast. FIXME
          const reloadedCardWidth = $card.width() as number;

          cy.get<number>("@newSupportingTextWidth").then((savedWidth) => {
            // Supporting text width should be preserved
            expect(reloadedSupportingTextWidth).to.be.closeTo(savedWidth, 10);
          });

          cy.get<number>("@newCardWidth").then((savedWidth) => {
            // Card width should be preserved
            expect(reloadedCardWidth).to.be.closeTo(savedWidth, 10);
          });
        });
      });
  });

  it("should remove supporting text when the last chart in its group is removed", () => {
    H.createDocument({
      name: "Supporting Text auto-cleanup",
      document: DOCUMENT_WITH_THREE_CARDS_AND_COLUMNS,
      collection_id: null,
      idAlias: "documentId",
    });
    H.visitDocument("@documentId");

    H.openDocumentCardMenu("Orders");
    H.popover()
      .findByText("Add supporting text")
      .closest("button")
      .should("be.enabled")
      .click();
    cy.realType("Lorem ipsum");

    const targetCardTitle = "Orders, Count, Grouped by Created At (year)";

    cy.log("One card remaining in group, supportingText should exist");
    H.dragAndDropCardOnAnotherCard("Orders", targetCardTitle);
    H.documentContent().findByText("Lorem ipsum").should("exist");

    cy.log("No cards remaining in group, supportingText should not exist");
    H.dragAndDropCardOnAnotherCard("Orders, Count", targetCardTitle);
    H.documentContent().findByText("Lorem ipsum").should("not.exist");
  });

  describe("drag and drop", () => {
    beforeEach(() => {
      H.createDocument({
        name: "DnD Test Document",
        document: DOCUMENT_WITH_SUPPORTING_TEXT,
        collection_id: null,
        idAlias: "documentId",
      });
      H.visitDocument("@documentId");
    });

    const getSupportingText = (contents: string = "Lorem ipsum") => {
      const testId = "document-card-supporting-text";
      return cy
        .findAllByTestId(testId)
        .contains(contents)
        .closest(`[data-testid="${testId}"]`);
    };

    const assertHorizontalLayout = (
      c1: Cypress.Chainable<JQuery<HTMLElement>>,
      c2: Cypress.Chainable<JQuery<HTMLElement>>,
    ) => {
      c1.then(($c1) => {
        c2.then(($c2) => {
          const rect1 = $c1[0].getBoundingClientRect();
          const rect2 = $c2[0].getBoundingClientRect();
          expect(rect2.left).to.gte(rect1.right);
          expect(rect1.top).to.be.closeTo(rect2.top, 2);
        });
      });
    };

    const assertVerticalLayout = (
      c1: Cypress.Chainable<JQuery<HTMLElement>>,
      c2: Cypress.Chainable<JQuery<HTMLElement>>,
    ) => {
      c1.then(($c1) => {
        c2.then(($c2) => {
          const rect1 = $c1[0].getBoundingClientRect();
          const rect2 = $c2[0].getBoundingClientRect();
          expect(rect2.top).to.gte(rect1.bottom);
          expect(rect1.left).to.be.closeTo(rect2.left, 2);
        });
      });
    };

    it("should do nothing when dropping a supporting text block outside of a group, and reorder when dropping it onto a card", () => {
      H.getDocumentCard("Orders").should("exist");
      H.documentsDragAndDrop({
        getSource: () => getSupportingText().find("[data-drag-handle]"),
        getTarget: () => H.getDocumentCard("Orders, Count"),
      });

      assertHorizontalLayout(getSupportingText(), H.getDocumentCard("Orders"));
      assertVerticalLayout(
        getSupportingText(),
        H.getDocumentCard("Orders, Count"),
      );

      cy.log("drop supporting text onto a card");
      H.getDocumentCard("Orders").should("exist");
      H.documentsDragAndDrop({
        getSource: () => getSupportingText().find("[data-drag-handle]"),
        getTarget: () => H.getDocumentCard("Orders"),
        side: "right",
      });

      assertHorizontalLayout(H.getDocumentCard("Orders"), getSupportingText());
      assertVerticalLayout(
        H.getDocumentCard("Orders"),
        H.getDocumentCard("Orders, Count"),
      );
    });

    it("should reorder when dropping a card onto a supporting text block", () => {
      H.getDocumentCard("Orders").should("exist");
      H.documentsDragAndDrop({
        getSource: () => H.getDocumentCard("Orders"),
        getTarget: () => getSupportingText(),
        side: "left",
      });

      assertHorizontalLayout(H.getDocumentCard("Orders"), getSupportingText());
      assertVerticalLayout(
        H.getDocumentCard("Orders"),
        H.getDocumentCard("Orders, Count"),
      );
    });

    it("should insert a card when dropped onto a supporting text block", () => {
      H.getDocumentCard("Orders").should("exist");
      H.documentsDragAndDrop({
        getSource: () => H.getDocumentCard("Orders, Count"),
        getTarget: () => getSupportingText(),
        side: "left",
      });

      assertHorizontalLayout(
        H.getDocumentCard("Orders, Count"),
        getSupportingText(),
      );
      assertHorizontalLayout(getSupportingText(), H.getDocumentCard("Orders"));
    });
  });
});