20

I'm new to unit testing and I'm trying to render a component to learn more about the library.

I'm trying to follow this guide.

Component

<TouchableOpacity
    style={style}
    onPress={onPress}
    accessibilityRole="button"
>
    <AppText style={textStyle}>{title.toUpperCase()}</AppText>
</TouchableOpacity> 

Test

it("Has the correct title in the button", () => {
    const { getByText } = render(<AppButton title="Hello" />);
  
    expect(getByText("HELLO")).toBeInTheDocument();
});

I'm simply trying to see that the component renders correctly but I get the error

received value must be an HTMLElement or an SVGElement.
    Received has type:  object
    Received has value: {"_fiber": {"_debugHookTypes": null, "_debugID": 40, "_debugIsCurrentlyTiming": false, "_debugNeedsRemount": false, "_debugOwner": [FiberNode], "_debugSource": null, "actualDuration": 0, "actualStartTime": -1, "alternate": null, "child": [FiberNode], "childExpirationTime": 0, "dependencies": null, "effectTag": 1, "elementType": [Function Component], "expirationTime": 0, "firstEffect": null, "index": 0, "key": null, "lastEffect": null, "memoizedProps": [Object], "memoizedState": null, "mode": 0, "nextEffect": null, "pendingProps": [Object], "ref": null, "return": [FiberNode], "selfBaseDuration": 0, "sibling": null, "stateNode": [Component], "tag": 1, "treeBaseDuration": 0, "type": [Function Component], "updateQueue": [Object]}}

Any advice on what I'm doing wrong?

Liam
  • 27,717
  • 28
  • 128
  • 190
theabrar
  • 360
  • 1
  • 6
  • 15
  • 1
    Hey, I'm using react-native-testing-library :) – theabrar Mar 07 '21 at 18:54
  • I used testID and it's working as expected :) – theabrar Mar 08 '21 at 09:00
  • Here is the working solution, it works if I access by testID. it("Has the correct title in the button", () => { const { getByTestId } = render(); const eins = getByTestId("text"); expect(eins.children[0]).toEqual("HELLO"); }); Although it'd have been good to understand why I couldn't get the value with `getByText`, at least it's working. :) – theabrar Aug 18 '22 at 09:39

2 Answers2

23

You can use waitFor

Sample:

import { waitFor } from "@testing-library/react-native";


waitFor(() => expect(getByText("Your-text")).toBeInTheDocument());

// or

waitFor(() => expect(getByTestId("Your-Test-Id")).toBeInTheDocument());
sia
  • 513
  • 4
  • 14
15

The issue is that you're trying to use the toBeInTheDocument helper function from @testing-library/jest-dom with React Native code. @testing-library/jest-dom expects DOM elements to be passed to its functions, and is meant to be used with @testing-library/react instead - it won't work with the native elements from React Native.

When using @testing-library/react-native you can assert the presence of an element like so.

it("has the correct title in the button", () => {
    const { getByText } = render(<AppButton title="Hello" />);
    expect(getByText("HELLO")).toBeTruthy();
});
juliomalves
  • 42,130
  • 20
  • 150
  • 146