The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Add ExplicitSize test for measuring a child that renders content after mount (#76559)
Nemanja Glumacf5015de183a0
diff --git a/e2e/test/scenarios/visualizations-charts/visualizations-charts-reproductions.cy.spec.ts b/e2e/test/scenarios/visualizations-charts/visualizations-charts-reproductions.cy.spec.tsindex 895f3090fcc..84969b0f6d0 100644--- a/e2e/test/scenarios/visualizations-charts/visualizations-charts-reproductions.cy.spec.ts+++ b/e2e/test/scenarios/visualizations-charts/visualizations-charts-reproductions.cy.spec.ts@@ -229,37 +229,6 @@ describe("issue 47847", () => {});});-describe("issue 51926", () => {- beforeEach(() => {- H.restore();- cy.signInAsNormalUser();- });-- it("should render pivot table when selecting it from another viz type", () => {- H.visitQuestionAdhoc({- dataset_query: {- type: "query",- query: {- "source-table": ORDERS_ID,- aggregation: [["count"]],- breakout: [["field", ORDERS.CREATED_AT, { "temporal-unit": "week" }]],- },- database: SAMPLE_DB_ID,- },- display: "pivot",- });-- H.openVizTypeSidebar();- H.leftSidebar().within(() => {- cy.findByTestId("Table-button").click();- cy.wait(300); // wait for rerender- cy.findByTestId("Pivot Table-button").click();- });-- cy.findAllByTestId("pivot-table-cell").should("contain", "April 27, 2025"); // expect this to break when we shift years in the Sample Database- });-});-describe("issue 51952", () => {beforeEach(() => {H.restore();diff --git a/frontend/src/metabase/common/components/ExplicitSize/ExplicitSize.unit.spec.tsx b/frontend/src/metabase/common/components/ExplicitSize/ExplicitSize.unit.spec.tsxindex b4b0e237a37..3fc7dd15bc2 100644--- a/frontend/src/metabase/common/components/ExplicitSize/ExplicitSize.unit.spec.tsx+++ b/frontend/src/metabase/common/components/ExplicitSize/ExplicitSize.unit.spec.tsx@@ -67,6 +67,29 @@ const SizedComponent = ExplicitSize<{height: number | null;}>()(Base);+// A child that renders nothing until it has content, mirroring PivotTable,+// which returns null on its first render (before its data is pivoted) and only+// later renders a measurable element. See metabase#51926.+const ConditionalBase = forwardRef<+ HTMLDivElement,+ { width: number | null; height: number | null; hasContent: boolean }+>(function ConditionalBase({ width, height, hasContent }, ref) {+ if (!hasContent) {+ return null;+ }+ return (+ <div ref={ref} data-testid="sized">+ {String(width)}x{String(height)}+ </div>+ );+});++const ConditionalSizedComponent = ExplicitSize<{+ width: number | null;+ height: number | null;+ hasContent: boolean;+}>()(ConditionalBase);+const createEntry = (target: Element,inlineSize: number,@@ -131,4 +154,38 @@ describe("ExplicitSize", () => {expect(screen.getByTestId("sized")).toHaveTextContent("400x868");});++ it("should measure a child that renders content only after mount and pass it non-null dimensions (metabase#51926)", () => {+ // Regression test for the pivot table rendering blank until resized.+ // PivotTable returns null on its first render, so there is no element for+ // ExplicitSize to measure at mount. When it later renders content, the size+ // must be recalculated even though no resize event ever fires — otherwise+ // the child stays stuck with null dimensions and never lays out.+ const { rerender } = render(+ <ConditionalSizedComponent hasContent={false} />,+ );++ // Mount: the child renders nothing, so there is nothing to measure.+ act(() => {+ jest.advanceTimersByTime(500);+ });+ expect(screen.queryByTestId("sized")).not.toBeInTheDocument();++ // The child now renders a measurable element. jsdom reports 0x0 by default,+ // so stub a real size — there is no resize event to supply one.+ rerender(<ConditionalSizedComponent hasContent={true} />);+ const element = screen.getByTestId("sized");+ jest.spyOn(element, "getBoundingClientRect").mockReturnValue({+ width: 400,+ height: 300,+ } as DOMRect);+ expect(element).toHaveTextContent("nullxnull");++ // ExplicitSize re-runs measurement on the next tick once content appears.+ act(() => {+ jest.advanceTimersByTime(500);+ });++ expect(screen.getByTestId("sized")).toHaveTextContent("400x300");+ });});