Copilot commented on code in PR #41351: URL: https://github.com/apache/superset/pull/41351#discussion_r3476401787
########## superset-frontend/packages/superset-ui-core/src/components/DropdownContainer/DropdownContainer.overflow.test.tsx: ########## @@ -0,0 +1,325 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * Overflow-engine regression tests for DropdownContainer. + * + * jsdom has no real layout, so these tests drive the component's real overflow + * recalculation by mocking the two measurement sources it reads: + * 1. `useResizeDetector` — supplies the container width. + * 2. `getBoundingClientRect` — supplies per-element geometry. The inner + * `data-test="container"` spans [0, containerRight]; every child is + * ITEM_W wide and laid out left-to-right by its DOM index, so children + * whose right edge exceeds `containerRight` overflow. + * + * This exercises the production code path in DropdownContainer.tsx + * (useLayoutEffect → overflowingIndex → notOverflowedItems/overflowedItems → + * showDropdownButton) rather than mocking the result. + */ +import { screen, render, waitFor, act } from '@superset-ui/core/spec'; +import * as resizeDetector from 'react-resize-detector'; +import { DropdownContainer } from '..'; + +const ITEM_W = 100; +// 350px container ⇒ at most 3 items (rights 100/200/300) fit before overflow. +const BAR_WIDTH = 350; + +// Mutable so a test can simulate the transient layout window where a freshly +// enlarged item set is momentarily measured as fitting before reflow settles. +let containerRight = BAR_WIDTH; +// Mutable width fed to the component through the mocked resize detector. +let mockWidth = 0; +// Stable ref object React attaches the outer node to (mirrors useResizeDetector). +const fakeRef: { current: HTMLDivElement | null } = { current: null }; + +const buildRect = (left: number, right: number): DOMRect => + ({ + left, + right, + width: right - left, + top: 0, + bottom: 0, + height: 0, + x: left, + y: 0, + toJSON: () => ({}), + }) as DOMRect; + +const installLayoutMock = () => { + HTMLElement.prototype.getBoundingClientRect = function mockRect( + this: HTMLElement, + ) { + const dataTest = this.getAttribute?.('data-test'); + if (dataTest === 'container') { + return buildRect(0, containerRight); + } + const parent = this.parentElement; + if (parent?.getAttribute?.('data-test') === 'container') { + const index = Array.prototype.indexOf.call(parent.children, this); + return buildRect(index * ITEM_W, index * ITEM_W + ITEM_W); + } + // Outer wrapper div (its first child is the inner container). + if ( + (this.children[0] as HTMLElement | undefined)?.getAttribute?.( + 'data-test', + ) === 'container' + ) { + return buildRect(0, containerRight); + } + return buildRect(0, 0); + }; +}; Review Comment: This test overwrites `HTMLElement.prototype.getBoundingClientRect` but never restores it, which can leak into other test files and create hard-to-diagnose failures. Capture the original implementation at module scope so it can be restored in `afterEach`/`afterAll`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
