-
-
Notifications
You must be signed in to change notification settings - Fork 44
Got unexpected null
when using FlatList's ref #108
Description
react-native
orexpo
: react-nativenative-testing-library
version: 5.0.3jest-preset
: @testing-library/react-nativereact-native
version: 0.61.5node
version: 13.8.0
Relevant code or config:
import React, {useRef} from 'react';
import {FlatList, TextInput, Button, SafeAreaView} from 'react-native';
const App = ({onPress}) => {
const flatList = useRef(null);
const textInput = useRef(null);
const handlePress = () => {
onPress();
textInput.current && textInput.current.focus();
flatList.current && flatList.current.scrollToOffset({offset: 0});
};
return (
<SafeAreaView style={{flex: 1}}>
<FlatList ref={flatList} />
<TextInput ref={textInput} />
<Button
accessibilityLabel="Press Me"
title="Press Me"
onPress={handlePress}
/>
</SafeAreaView>
);
};
export default App;
import {fireEvent, render} from '@testing-library/react-native';
import React from 'react';
import App from '../App';
test('it handles onPress', () => {
const onPress = jest.fn();
const {getByLabelText} = render(<App onPress={onPress} />);
const button = getByLabelText('Press Me');
fireEvent.press(button);
expect(onPress).toHaveBeenCalled();
});
What you did:
My use case was testing onPress
prop passed to a component, and this prop was called in a local handlePress
function. This function also calling FlatList's scrollToOffset
using ref. (I have send button in a chat view, where send button will send a message and scroll messages to this latest one)
What happened:
Got unexpected null
9 | onPress();
10 | textInput.current && textInput.current.focus();
> 11 | flatList.current && flatList.current.scrollToOffset({offset: 0});
| ^
12 | };
13 |
14 | return (
Note that other refs are working correctly (I tried simple View and TextInput).
Reproduction:
I have added a drop in replacement for the App.js
and __tests__/App-test.js
files in a new React Native project above. I also did the same in this fork https://github.com/demchenkoalex/ntl-sample but please note I didn't update React Native in a fork because migration to 0.61 is required. In my project I use latest version as specified in this issue.
Problem description:
Tests will pass if I will remove the line which scrolls FlatList, but this is a requirement, can't do that.
Suggested solution:
Tried to modify my code to add null checks, making another function which will call scrollToOffset
and other probably stupid stuff, I don't think I can suggest anything :/
Can you help us fix this issue by submitting a pull request?
N/A