gpt4 book ai didi

reactjs - 使用 React-Virtualized AutoSizer 时,子项未在测试中呈现

转载 作者:行者123 更新时间:2023-12-03 19:42:13 33 4
gpt4 key购买 nike

我有一个使用 react-virtualized AutoSizer 的组件,在这个组件里面有一个 react-virtualized List。它的呈现方式是:

<AutoSizer>
{({width, height}) => (
<List ref={(ref) => (this.refVirtualizedList = ref)}
width={width}
height={height}
playlistLoading={playlistLoading}
playlistPlayingTrackId={playlistPlayingTrackId}
rowCount={rowCount}
deferredMeasurementCache={this.cellMeasurerCache}
overscanRowCount={12}
scrollToIndex={trackGroupToGo - 1}
scrollToAlignment="center"
rowHeight={this.cellMeasurerCache.rowHeight}
updateTrackListFlag={updateTrackListFlag}
noRowsRenderer={this.renderNoRowsResult}
rowRenderer={this.renderTrackGroupRow}
onRowsRendered={this.handleOnRowsRendered} />
)}
</AutoSizer>

它工作得很好,但它不适用于测试。我看不到列表中的任何内容,以及函数 rowRenderer永远不会被调用。我正在使用 Jest 和 React 测试库。使用 logDOM 检查组件内部的内容时方法,我所看到的是:

<div
aria-label="grid"
aria-readonly="true"
class="ReactVirtualized__Grid ReactVirtualized__List"
role="grid"
style="box-sizing: border-box; direction: ltr; height: 0px; position: relative; width: 0px; will-change: transform; overflow-x: hidden; overflow-y: auto;"
tabindex="0"
/>

List 组件永远不会呈现。有任何想法吗?

最佳答案

感谢 GitHub 上的这个问题:https://github.com/bvaughn/react-virtualized/issues/493

我需要做的就是设置测试以“模拟” AutoSizer 的某些行为:

复制您可以在问题中找到的相同解决方案:

describe("My Test", () => {
const originalOffsetHeight = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'offsetHeight');
const originalOffsetWidth = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'offsetWidth');

beforeAll(() => {
Object.defineProperty(HTMLElement.prototype, 'offsetHeight', { configurable: true, value: 50 });
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', { configurable: true, value: 50 });
});

afterAll(() => {
Object.defineProperty(HTMLElement.prototype, 'offsetHeight', originalOffsetHeight);
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', originalOffsetWidth);
});
// Your tests
})

List 组件现在正在渲染!

关于reactjs - 使用 React-Virtualized AutoSizer 时,子项未在测试中呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62214833/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com