Skip to content
This repository was archived by the owner on Jul 30, 2020. It is now read-only.
This repository was archived by the owner on Jul 30, 2020. It is now read-only.

fireEvent.layout doesn't trigger onLayout for TouchableOpacity #142

Closed
@colinfwren

Description

@colinfwren
  • react-native or expo: Expo
  • native-testing-library version: 5.0.3
  • jest-preset: @testing-library/react-native
  • react-native version: Expo 38 / RN 0.62
  • node version: 14

Relevant code or config:

    import { act, render, fireEvent, wait } from '@testing-library/react-native';

    it('Passes with View', async () => {
        const mockOnLayout = jest.fn();
        function MyComponent() {
            return (
                <View onLayout={(e) => mockOnLayout(e)} testID='my-view'>
                    <View style={{ height: 200, width: 200}} />
                </View>
            )
        }
        const { getByTestId } = render(<MyComponent />);
        const view = getByTestId('my-view');
        act(() => {
            fireEvent.layout(view, {
                nativeEvent: {
                    layout: {
                        width: 300,
                    },
                },
            });
        });
        await wait(() => expect(mockOnLayout).toHaveBeenCalled());
    });
    it('Fails with TouchableOpacity', async () => {
        const mockOnLayout = jest.fn();
        function MyComponent() {
            return (
                <TouchableOpacity onLayout={(e) => mockOnLayout(e)} testID='my-view'>
                    <View style={{ height: 200, width: 200}} />
                </TouchableOpacity>
            )
        }
        const { getByTestId } = render(<MyComponent />);
        const view = getByTestId('my-view');
        act(() => {
            fireEvent.layout(view, {
                nativeEvent: {
                    layout: {
                        width: 300,
                    },
                },
            });
        });
        await wait(() => expect(mockOnLayout).toHaveBeenCalled());
    });

What you did:

Tried to test the onLayout functionality of a component that uses TouchableOpacity.

What happened:

Mocked onLayout handler was never called.

I had followed https://stackoverflow.com/questions/57961440/testing-onlayout-in-react-native/61774123#61774123 which suggested that it should work and on testing a View based component like in the example it worked, however on changing the View to TouchableOpacity it no longer worked.

Reproduction:

See attached test cases.

Problem description:

onLayout is a prop supported by TouchableOpacity so it'd be good to be able to verify that my component called it's onLayout handler that I pass to it when a layout event is triggered.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions