Giter VIP home page Giter VIP logo

react-mock-router's Introduction

react-mock-router

React Mock Router is a simple-to-use tool for testing React components that use React Router v4. It can be used in tests as a replacement for React Router's MemoryRouter and StaticRouter.

Installation

npm install react-mock-router --save-dev

API

The MockRouter component can receive any of the following arguments as props.

  • location
  • params
  • path
  • createHref
  • push
  • replace

Use

React Mock Router takes mocked router props as arguments, passing them to children components.

import MockRouter from 'react-mock-router';

it('has a button that links to the todo edit page', () => {

  const testProps = {
    todo: {
      id: 1,
      title: "Title",
      body: "Body"
    }
  };
  
  const push = jest.fn();

  const todoItemWrapper = mount(
    <MockRouter push={push}>
      <Route render={(props) => (
        <TodoItem {...props} {...testProps}/>
      )}/>
    </MockRouter>
  ).find('TodoItem');

  const editButton = todoItemWrapper.find('button').filterWhere(button =>
    /edit/i.test(button.props().children)
  );

  editButton.simulate('click', { preventDefault() { } });
  expect(push).toBeCalledWith(`/todo/${todo.id}/edit`);

});

React Mock Router also provides the correct context for components being tested, so you won't recieve errors if those components use React Router components like Route or Link. If you're testing a component that isn't considered a route component (i.e. not rendered as part of a Route) but need to wrap it in a router, you can do the following.

import MockRouter from 'react-mock-router';

it('correctly defines the action when given a formType', () => {
  
  const todoFormWrapper = mount(
    <MockRouter>
        <TodoFormContainer formType="new" />
    </MockRouter>
  ).find('TodoForm');

  expect(todoFormWrapper.props().action).toBeDefined();
});

License

This project is released under the MIT license.

react-mock-router's People

Contributors

rkoeze avatar noriste avatar

Watchers

James Cloos 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.