• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

testing-library/native-testing-library:

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

testing-library/native-testing-library

开源软件地址(OpenSource Url):

https://github.com/testing-library/native-testing-library

开源编程语言(OpenSource Language):

JavaScript 100.0%

开源软件介绍(OpenSource Introduction):

⚠️ Deprecation notice ⚠️

This repository has been deprecated in favor of https://github.com/callstack/react-native-testing-library and the @testing-library/react-native npm package will from now on (since v7.0) will be sourced from there. Please consult the migration guide.

Native Testing Library

whale

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

Read The Docs | Edit the docs


Build Status Code Coverage version downloads MIT License

All Contributors PRs Welcome Code of Conduct Discord

Watch on GitHub Star on GitHub

Table of Contents

The problem

You want to write maintainable tests for your React Native application. You love Kent Dodds' testing library, and you want to be able to write maintainable tests for your React Native application. You don't want to use a library that renders components to a fake DOM, and you've had a hard time finding what you need to write tests using that philosophy in React Native.

This solution

native-testing-library is an implementation of the well-known testing-library API that works for React Native. The primary goal is to mimic the testing library API as closely as possible while still accounting for the differences in the platforms.

Example

import React from 'react';
import { Button, Text, TextInput, View } from 'react-native';
import { fireEvent, render, wait } from '@testing-library/react-native';

function Example() {
  const [name, setUser] = React.useState('');
  const [show, setShow] = React.useState(false);

  return (
    <View>
      <TextInput value={name} onChangeText={setUser} testID="input" />
      <Button
        title="Print Username"
        onPress={() => {
          // let's pretend this is making a server request, so it's async
          // (you'd want to mock this imaginary request in your unit tests)...
          setTimeout(() => {
            setShow(!show);
          }, Math.floor(Math.random() * 200));
        }}
      />
      {show && <Text testID="printed-username">{name}</Text>}
    </View>
  );
}

test('examples of some things', async () => {
  const { getByTestId, getByText, queryByTestId, baseElement } = render(<Example />);
  const famousWomanInHistory = 'Ada Lovelace';

  const input = getByTestId('input');
  fireEvent.changeText(input, famousWomanInHistory);

  const button = getByText('Print Username');
  fireEvent.press(button);

  await wait(() => expect(queryByTestId('printed-username')).toBeTruthy());

  expect(getByTestId('printed-username').props.children).toBe(famousWomanInHistory);
  expect(baseElement).toMatchSnapshot();
});

Installation

This module should be installed in your project's devDependencies:

npm install --save-dev @testing-library/react-native

You will need react and react-native installed as dependencies in order to run this project.

Hooks

If you are interested in testing a custom hook, check out react-hooks-testing-library.

Other Solutions

Guiding principles

The more your tests resemble the way your software is used, the more confidence they can give you.

We try to only expose methods and utilities that encourage you to write tests that closely resemble how your apps are used.

Utilities are included in this project based on the following guiding principles:

  1. If it relates to rendering components, it deals with native views rather than component instances, nor should it encourage dealing with component instances.
  2. It should be generally useful for testing the application components in the way the user would use it. We are making some trade-offs here because we're using a computer and often a simulated environment, but in general, utilities should encourage tests that use the components the way they're intended to be used.
  3. Utility implementations and APIs should be simple and flexible.

In summary, we believe in the principles of testing-library, and adhere to them as closely as possible. At the end of the day, what we want is for this library to be pretty light-weight, simple, and understandable.

Inspiration

Huge thanks to Kent C. Dodds for evangelizing this approach to testing. We could have never come up with this library without him


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap