Open juusojpak opened 2 years ago
We actually do mount an extra item in horizontal mode to measure the list. Can you incorporate that in your test? We will update the documentation to include this point.
Great if you can add a mention about this to the documentation 👍
We worked around this by forcing mocked FlashLists to always have horizontal
set as false in tests:
// jestSetup.js
jest.mock('@shopify/flash-list', () => {
const React = require('react')
const ActualFlashList = jest.requireActual('@shopify/flash-list').FlashList
return {
...jest.requireActual('@shopify/flash-list'),
FlashList: (props) => (
<ActualFlashList {...props} estimatedListSize={{ height: 1000, width: 400 }} horizontal={false} />
),
}
})
Great if you can add a mention about this to the documentation 👍
We worked around this by forcing mocked FlashLists to always have
horizontal
set as false in tests:// jestSetup.js jest.mock('@shopify/flash-list', () => { const React = require('react') const ActualFlashList = jest.requireActual('@shopify/flash-list').FlashList return { ...jest.requireActual('@shopify/flash-list'), FlashList: (props) => ( <ActualFlashList {...props} estimatedListSize={{ height: 1000, width: 400 }} horizontal={false} /> ), } })
I having same issue, when i try your workaround I seem to get a error TypeError Property declarations[0] of VariableDeclaration expected node to be of a type ["VariableDeclarator"] but instead got undefined
Anyone got any ideas?
estimatedListSize={{ height: 1000, width: 400 }}
Found I had to do it like the following:
jest.mock('@shopify/flash-list', () => {
const React = require('react')
const ActualFlashList = jest.requireActual('@shopify/flash-list').FlashList
class MockFlashList extends React.Component {
componentDidMount() {
if (super.componentDidMount) {
super.componentDidMount()
}
this.rlvRef?._scrollComponent?._scrollViewRef?.props.onLayout({
nativeEvent: { layout: { height: 900, width: 400 } },
})
}
render() {
const { horizontal, ...restProps } = this.props
return <ActualFlashList {...restProps} estimatedListSize={{ height: 1000, width: 400 }} horizontal={false} />
}
}
return {
...jest.requireActual('@shopify/flash-list'),
FlashList: MockFlashList,
AnimatedFlashList: MockFlashList,
}
})
Current behavior
Using @testing-library/react-native, unit tests for a FlashList component with
horizontal
prop set as true, that expect specific amount of items to be found, are failing due to duplicate occurrence of the last list item in the rendered JSON:Expected behavior
Unit tests using
getByText
function from React Native Testing Library, as is shown in the testing example in the documentation, should pass for horizontal lists.To Reproduce
Component file:
Test file:
Test will fail to:
Platform:
Environment
1.2.1