Giter Site home page Giter Site logo

mzdunek93 / react-native-testing-library Goto Github PK

View Code? Open in Web Editor NEW

This project forked from callstack/react-native-testing-library

0.0 2.0 0.0 1.59 MB

Lightweight React Native testing utilities helping you write better tests with less effort

Home Page: https://callstack.github.io/react-native-testing-library/

License: MIT License

JavaScript 90.12% TypeScript 9.20% Dockerfile 0.21% CSS 0.46%

react-native-testing-library's Introduction

owl

React Native Testing Library

Lightweight React Native testing utilities helping you write better tests with less effort.

Version PRs Welcome Chat Greenkeeper badge Sponsored by Callstack

Appreciation notice: This project is heavily inspired by react-testing-library. Go check it out and use it to test your web React apps.

The problem

You want to write maintainable tests for your React Native components without testing implementation details, but then you're told to use Enzyme, which you learn has no React Native adapter, meaning only shallow rendering is supported. And you want to render deep! But deep rendering may otherwise require jsdom (React Native isn't the web!), while doing deep rendering with react-test-renderer is so painful.

You would also like to use the newest React features, but you need to wait for your testing library's abstractions to catch up and it takes a while.

You finally want to approach testing using only best practices, while Enzyme may encourage assertions on implementation details.

This solution

The react-native-testing-library is a lightweight solution for testing your React Native components. It provides light utility functions on top of react-test-renderer letting you always be up to date with latest React features and write any component tests you like. But really not any, it prevents you from testing implementation details because we stand this is a very bad practice.

This library is a replacement for Enzyme. It is tested to work with Jest, but it should work with other test runners as well.

Example

import { render, fireEvent } from 'react-native-testing-library';
import { QuestionsBoard } from '../QuestionsBoard';
import { Question } from '../Question';

function setAnswer(question, answer) {
  fireEvent.changeText(question, answer);
}

test('should verify two questions', () => {
  const { getAllByType, getByText } = render(<QuestionsBoard {...props} />);
  const allQuestions = getAllByType(Question);

  setAnswer(allQuestions[0], 'a1');
  setAnswer(allQuestions[1], 'a2');

  fireEvent.press(getByText('submit'));

  expect(props.verifyQuestions).toBeCalledWith({
    '1': { q: 'q1', a: 'a1' },
    '2': { q: 'q2', a: 'a2' },
  });
});

Installation

Open a Terminal in your project's folder and run:

yarn add --dev react-native-testing-library

This library has a peerDependencies listing for react-test-renderer and, of course, react. Make sure to install them too!

Note for Flow users – you'll also need to install typings for react-test-rednerer:

flow-typed install react-test-renderer

As you may have noticed, it's not tied to React Native at all – you can safely use it in your React components if you feel like not interacting directly with DOM.

API / Usage

The public API of react-native-testing-library is focused around these essential methods:

  • render – deeply renders given React element and returns helpers to query the output components.
  • fireEvent - invokes named event handler on the element.
  • waitForElement - waits for non-deterministic periods of time until your element appears or times out.
  • flushMicrotasksQueue - waits for microtasks queue to flush.

Note to users who are more familiar with react-testing-library: This API does not expose cleanup because it doesn't interact with the DOM. There's nothing to clean up.

Made with ❤️ at Callstack

React Native Testing Library is an open source project and will always remain free to use. If you think it's cool, please star it 🌟. Callstack is a group of React and React Native geeks, contact us at [email protected] if you need any help with these or just want to say hi!


Supported and used by Rally Health.

react-native-testing-library's People

Contributors

thymikee avatar greenkeeper[bot] avatar esemesek avatar ahmetcetin avatar danahartweg avatar dcalhoun avatar mzdunek93 avatar alinamihai avatar asday avatar benadamstyles avatar brmenchl avatar brooksbecton avatar trancever avatar eliperkins avatar enieber avatar ferrannp avatar janhesters avatar jmporchet avatar jukkish avatar kdbcinco avatar ozzieorca avatar shawnaxsom avatar simenb avatar usufruct99 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.