react-stub-context
v0.8.1
Published
Stub context for a component, to be used for testing purposes.
Downloads
5,884
Readme
NOTE: You might not need this. I recommend looking at other alternatives before stubbing context.
react-stub-context
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');
});
});