From f14edd8a0c38c59cc10fb218ba1e9875b9644996 Mon Sep 17 00:00:00 2001 From: Luis Flores Date: Mon, 10 Dec 2018 17:08:39 -0800 Subject: [PATCH] Fix ListViewMock unique key error (#14894) Summary: Add key prop to renderHeader and renderFooter in ListViewMock. Fix unique key error when using jest snapshots. It closes #12762 Pull Request resolved: https://github.com/facebook/react-native/pull/14894 Reviewed By: TheSavior Differential Revision: D13396721 Pulled By: cpojer fbshipit-source-id: 5bbcb8157e3cd98fe07f2a037e1dbc06ab599c87 --- .../Lists/ListView/__mocks__/ListViewMock.js | 51 ++++++++++++------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/Libraries/Lists/ListView/__mocks__/ListViewMock.js b/Libraries/Lists/ListView/__mocks__/ListViewMock.js index 7dd906dd4a..4813e7a428 100644 --- a/Libraries/Lists/ListView/__mocks__/ListViewMock.js +++ b/Libraries/Lists/ListView/__mocks__/ListViewMock.js @@ -24,34 +24,51 @@ class ListViewMock extends React.Component<$FlowFixMeProps> { * Flow. */ renderScrollComponent: props => , }; + componentDidMount() { ListViewMock.latestRef = this; } + render() { const {dataSource, renderFooter, renderHeader} = this.props; - const rows = [renderHeader && renderHeader()]; - const allRowIDs = dataSource.rowIdentities; - for (let sectionIdx = 0; sectionIdx < allRowIDs.length; sectionIdx++) { - const sectionID = dataSource.sectionIdentities[sectionIdx]; - const rowIDs = allRowIDs[sectionIdx]; - for (let rowIdx = 0; rowIdx < rowIDs.length; rowIdx++) { - const rowID = rowIDs[rowIdx]; - // Row IDs are only unique in a section - rows.push( + let rows = [ + renderHeader && ( + + ), + ]; + + const dataSourceRows = dataSource.rowIdentities.map( + (rowIdentity, rowIdentityIndex) => { + const sectionID = dataSource.sectionIdentities[rowIdentityIndex]; + return rowIdentity.map((row, rowIndex) => ( , - ); - } - } - renderFooter && rows.push(renderFooter()); + /> + )); + }, + ); + + rows = [...rows, ...dataSourceRows]; + renderFooter && + rows.push( + , + ); + return this.props.renderScrollComponent({...this.props, children: rows}); } static DataSource = ListViewDataSource;