@aortman/react-stub-context

0.7.1 • Public • Published

react-stub-context Build Status

Stub context for a component, to be used for testing purposes.

Installation

npm install react-stub-context

Usage

var React = require('react');
var createReactClass = require('create-react-class');
var Router = require('react-router');
var Link = Router.Link;

var TestHandler = createReactClass({
  getInitialState: function() {
    return {
      text: 'foo'
    }
  },

  changeText: function() {
    this.setState({ text: 'bar' });
  },

  render: function() {
    return React.createElement(Link, {
      to: 'home',
      onClick: this.changeText
    }, this.state.text);
  }
});

module.exports = TestHandler
jest.dontMock('../');

function noop() {}

describe('React Router Context Test', function() {
  var React, Router, TestUtils, stubContext, TestHandler;

  beforeEach(function() {
    React = require.requireActual('react');
    TestUtils = require.requireActual('react-dom/test-utils');
    stubContext = require.requireActual('react-stub-context');
    TestHandler = require('..');
    Router = function() {}
  });

  it('has context correctly', function() {
    Router.makeHref = noop;
    Router.isActive = noop;

    TestHandler = stubContext(TestHandler, { router: Router });

    var render = TestUtils.renderIntoDocument(React.createElement(TestHandler, {}));
    var link = TestUtils.findRenderedDOMComponentWithTag(render, 'a');

    expect(link).toBeDefined();
    expect(link.getDOMNode().innerHTML).toEqual('foo');

    TestUtils.Simulate.click(link);

    expect(link.getDOMNode().innerHTML).toEqual('bar');
  });
});

Dependents (0)

Package Sidebar

Install

npm i @aortman/react-stub-context

Weekly Downloads

1

Version

0.7.1

License

ISC

Last publish

Collaborators

  • aortman