test(wait-for-element-to-be-removed): use fireEvent.press instead of rerender for more realistic user interaction

This commit is contained in:
Maciej Jastrzębski
2026-01-08 23:59:26 +01:00
parent fb0d2171f2
commit e38cc3257e
@@ -1,7 +1,7 @@
import * as React from 'react';
import { Text, View } from 'react-native';
import { Pressable, Text, View } from 'react-native';
import { render, screen, waitForElementToBeRemoved } from '..';
import { fireEvent, render, screen, waitForElementToBeRemoved } from '..';
it('throws error when element is already removed at the time of calling', async () => {
await expect(waitForElementToBeRemoved(() => null)).rejects.toThrow(
@@ -10,16 +10,25 @@ it('throws error when element is already removed at the time of calling', async
});
it('resolves when query throws error after element is removed', async () => {
const Component = ({ showText }: { showText: boolean }) => (
<View>{showText && <Text>Hello</Text>}</View>
);
const Component = () => {
const [showText, setShowText] = React.useState(true);
const { rerender } = await render(<Component showText={true} />);
return (
<View>
{showText && <Text>Hello</Text>}
<Pressable onPress={() => setShowText(false)} testID="remove-button">
<Text>Remove</Text>
</Pressable>
</View>
);
};
await render(<Component />);
expect(screen.getByText('Hello')).toBeOnTheScreen();
setTimeout(async () => {
await rerender(<Component showText={false} />);
void setTimeout(() => {
void fireEvent.press(screen.getByTestId('remove-button'));
}, 50);
await waitForElementToBeRemoved(() => screen.getByText('Hello'));