Git Product home page Git Product logo

testing-react-native's Introduction

Testing React Native

It's a list of snippets, that will help you test your component properly.

What is worth testing

  1. Proper values
  2. Passing undefined or null value
  3. In case you are calculating something:
  • proper numbers
  • zero values (helps to test dividing)
  • null/undefined (worth to check NaN or infinite values)

react-native-testing-library

Snapshot

import React from 'react';
import { render } from 'react-native-testing-library';

import { Button } from 'components';

describe('components/Button', () => {
  const defaultProps = {
    text: 'Hello world',
  };

  test('snapshot', () => {
    const tree = render(<Button {...defaultProps} />).toJSON();
    expect(tree).toMatchSnapshot();
  });
});

Button press

import React from 'react';
import { render, fireEvent } from 'react-native-testing-library';

 import { Button } from 'components';

 describe('components/Button', () => {
  const defaultProps = {
    text: 'Hello World'
    onPress: jest.fn(),
  };

  test('Should handle click', () => {
    const { getByTestId } = render(
      <ButtonCard testID="button" {...defaultProps} />,
    );
    fireEvent.press(getByTestId('button'));

    expect(defaultProps.onPress).toHaveBeenCalledTimes(1);
  });
});

Enzyme

Snapshot

Simple test to check changes in snapshots. Pretty useful, but not really testing logic, but just props and style. Easy to ignore.

It should be only your base. You will start here and add more tests to it.

describe('Component: EXAMPLECOMPONENT', () => {
  describe('with basic props', () => {
    const props = {
      ...defaultProps
    }

    const wrapper = shallow(
      <EXAMPLECOMPONENT {...props} />
    )

    it('Should render snapshot properly', () => {
      expect(wrapper).toMatchSnapshot()
    })
  })
})

Testing components

To make sure some components are on screen you can easily add test to find it.

Count number of elements

it('Should render properly', () => {
  expect(wrapper.find(View).length).toBe(1)
})

Find specific element

<View testID="COMPONENT_TO_TEST">
it('Should has component', () => {
  const component = wrapper.findWhere(node => node.prop('testID') === 'COMPONENT_TO_TEST')
  expect(component).toBeDefined()
})

Test styles

Sometimes we need to has some specific styles, e.g. mandatory style for button.

it('Should has mandatory style', () => {
  const component = wrapper.findWhere(node => node.prop('testID') === 'COMPONENT_TO_TEST')
  expect(component.props().style).toHaveProperty('color', 'red')
})
it('Should has mandatory style', () => {
  const expectedStyle = {
    color: 'red'
  }
  const component = wrapper.findWhere(node => node.prop('testID') === 'COMPONENT_TO_TEST')
  expect(component.props().style).toMatch(expectedStyle)
})

Test component logic

Sometimes we have some helpers functions inside our components, e.g. getIcon, getValue or anything.

import { icons } from '../assets'
it('Should return default icon', () => {
  const icon = wrapper.instance().getIcon()
  expect(icon).toEqual(icons.default)
})
it('Should return 42', () => {
  wrapper.instance().makeCalculation()
  wrapper.update()
  const value = wrapper.instance().getValue()
  expect(value).toEqual(42)
})

Test buttons

Sometimes we pass function through props like this:

  <MyComponent onPress={() => amazing()} />

and inside MyComponent we use it like this to call this amazing function

  <TouchableOpacity onPress={() => this.props.onPress()}>

So we can check does this prop was called and how many times

it('Should handle item click', () => {
  wrapper.find(TouchableOpacity).simulate('press')
  expect(props.onPress).toHaveBeenCalledTimes(1)
})

testing-react-native's People

Contributors

patys avatar

Stargazers

Marcin Kornek avatar Daniel Idaszak 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.