jestjs - Jest testing of react-router -



jestjs - Jest testing of react-router -

i trying write simple jest test react-router route module.

the component has button , when clicking on it, there programatic navigation route using 'transitionto' method.

i maintain getting next error, after adding stubroutercontext utils (as explained here), , wrapping userdetails component in stubroutercontext:

typeerror: property 'transitionto' of object #<object> not function

i using react 12.2, react-router 12.4, , jest 2.2

my dummy component:

var navigation, react, router; react = require('react/addons'); router = require('react-router'); navigation = require('react-router').navigation; module.exports = react.createclass({ mixins: [navigation], onbuttonclick: function() { this.transitionto('next-page'); }, render: function() { homecoming (<button onclick={@onbuttonclick}>go next page</button>) } });

my test file:

jest.dontmock('./../utils/stub-router-context') .dontmock('../dummy-component'); describe('dummycomponent', function() { it('let navigate next page', function() { var react = require('react/addons'); var testutils = react.addons.testutils; var stubroutercontext = require('./../utils/stub-router-context'); var dummycomponent = require('../dummy-component'); var subject = stubroutercontext(dummycomponent); dummycomponent = testutils.renderintodocument(<subject/>); button = testutils.findrendereddomcomponentwithtag(dummycomponent, 'button'); react.addons.testutils.simulate.click(button); }); });

my stub-router-context.cjsx file:

var react = require('react/addons'); var func = react.proptypes.func; var _ = require('lodash'); module.exports = function(component, props, stubs) { homecoming react.createclass({ childcontexttypes: { makepath: func, makehref: func, transitionto: func, replacewith: func, goback: func, getcurrentpath: func, getcurrentroutes: func, getcurrentpathname: func, getcurrentparams: func, getcurrentquery: func, isactive: func }, getchildcontext: function() { homecoming _.merge({}, { makepath: function() {}, makehref: function() {}, transitionto: function() {}, replacewith: function() {}, goback: function() {}, getcurrentpath: function() {}, getcurrentroutes: function() {}, getcurrentpathname: function() {}, getcurrentparams: function() {}, getcurrentquery: function() {}, isactive: function() {} }, stubs); }, render: function() { homecoming react.createelement(component, props); } }); };

i whipped , wrote prepare based on code maintainers wrote. helps!

https://labs.chie.do/jest-testing-with-react-router/

react-router jestjs

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -