Socket
Socket
Sign inDemoInstall

@testing-library/react-native

Package Overview
Dependencies
Maintainers
15
Versions
81
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@testing-library/react-native

Simple and complete React Native testing utilities that encourage good testing practices.


Version published
Maintainers
15
Created

What is @testing-library/react-native?

@testing-library/react-native is a testing library for React Native applications. It provides utilities to test React Native components in a way that resembles how users interact with your app. The library encourages writing tests that are more maintainable and less coupled to the implementation details of the components.

What are @testing-library/react-native's main functionalities?

Rendering Components

This feature allows you to render React Native components and query them for assertions. The example demonstrates rendering a simple Text component and verifying its content.

const { render } = require('@testing-library/react-native');
const { Text } = require('react-native');

const { getByText } = render(<Text>Hello, World!</Text>);
expect(getByText('Hello, World!')).toBeTruthy();

Fire Events

This feature allows you to simulate user interactions such as pressing a button. The example shows how to render a Button component and simulate a press event.

const { render, fireEvent } = require('@testing-library/react-native');
const { Button } = require('react-native');

const handlePress = jest.fn();
const { getByText } = render(<Button title="Press me" onPress={handlePress} />);
fireEvent.press(getByText('Press me'));
expect(handlePress).toHaveBeenCalled();

Async Utilities

This feature provides utilities to handle asynchronous operations in your components. The example demonstrates how to wait for a state change in a component that updates after a timeout.

const { render, waitFor } = require('@testing-library/react-native');
const { useEffect, useState } = require('react');
const { Text } = require('react-native');

const AsyncComponent = () => {
  const [data, setData] = useState(null);
  useEffect(() => {
    setTimeout(() => {
      setData('Loaded');
    }, 1000);
  }, []);
  return <Text>{data}</Text>;
};

const { getByText } = render(<AsyncComponent />);
await waitFor(() => expect(getByText('Loaded')).toBeTruthy());

Other packages similar to @testing-library/react-native

Keywords

FAQs

Package last updated on 28 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc