Luc*_*lte 4 reactjs jestjs react-virtualized react-testing-library
我有一个使用 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>
Run Code Online (Sandbox Code Playgroud)
它工作得很好,但它不适用于测试。我看不到列表中的任何内容,并且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"
/>
Run Code Online (Sandbox Code Playgroud)
List 组件永远不会呈现。有任何想法吗?
Luc*_*lte 10
感谢 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
})
Run Code Online (Sandbox Code Playgroud)
List 组件现在正在渲染!
| 归档时间: |
|
| 查看次数: |
2116 次 |
| 最近记录: |