Coder Social home page Coder Social logo

ionic-react-test-utils's Introduction

Ionic React Test Utils

This is a set of helper methods to make testing easier in Ionic React with React Testing Library and Jest.

Installation

yarn add -D @ionic/react-test-utils

or

npm install --dev @ionic/react-test-utils

Custom ionFireEvent

ionFireEvent extends Testing Library's fireEvent by adding the custom ion* events. This can be used as a drop in replacement for fireEvent or used in conjunction with.

import { ionFireEvent as fireEvent } from '@ionic/react-test-utils';

...


fireEvent.ionChange(element, 'my text');

mockIonicReact

This method mocks out certain Ionic components that have issues rendering in JSDOM. To use it, open up setupTests.ts and add this to the file:

import { mockIonicReact } from '@ionic/react-test-utils';
mockIonicReact();

If you are using Ionic v6, you will also need to call setupIonicReact:

import { mockIonicReact } from '@ionic/react-test-utils';
import { setupIonicReact } from '@ionic/react';

setupIonicReact();
mockIonicReact();

waitForIonicReact

This function waits for Ionic React to be fully initialized. Use this in any test that renders Ionic components, to ensure the rendered markup has all classes etc. that Ionic adds at runtime.

import { render } from '@testing-library/react';
import { waitForIonicReact } from '@ionic/react-test-utils';
import MyComponent from './MyComponent';

describe('<MyComponent />', () => {
  it('renders consistently', async () => {
    const { baseElement } = render(<MyComponent/>);
    await waitForIonicReact();
    expect(baseElement).toMatchSnapshot();
  });
});

waitForIonicReact waits for the DOM to be stable, meaning no markup has changed for a certain period of time. This period is determined by the first parameter, timeout, which defaults to 750 milliseconds. There is also a global timeout determined by the second parameter, maxWaitTime. This defaults to 5000 milliseconds (five seconds) and, if hit, will cause the function to fail early. If you find your tests are running too slow or not detecting all Ionic behaviors, try tweaking these values.

ionic-react-test-utils's People

Contributors

amandaejohnston avatar coreyjv avatar elylucas avatar eric-horodyski avatar javiermartinz avatar liamdebeasi avatar menachem-stratfs avatar nicolasmaurotorres avatar sean-perkins avatar tarjei avatar veiko 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.