React-router: Uniting testing the clicking on a link

Created on 23 Mar 2015  路  4Comments  路  Source: ReactTraining/react-router

I was trying to test that one page properly has a link to another page and my current helper method was not working. I ended up updating my helper method to this:

testPage: function(initialPath, steps) {
  if (!_.isArray(steps)) {
    steps = [steps];
  };

  var component;
  var routerMainComponent;
  var div = document.createElement('div');
  var routes = require('../web/app/components/core/routes.jsx');
  var location = new TestLocation([initialPath]);

  Router.run(routes, location, function (Handler) {
    var step = steps.shift();

    //this is the confusing part to me
    this.unmountComponent(routerMainComponent);

    routerMainComponent = React.render(React.createFactory(Handler)({}), div);
    step(routerMainComponent);
  }.bind(this));
}

Also some other relative helper methods are:

unmountComponent: function(component) {
  if(component && component.isMounted()) {
    //console.log('unmounted: ' + component.constructor.displayName);
    React.unmountComponentAtNode(component.getDOMNode().parentNode);
  }
},

simulateRouterLinkClick: function(linkComponent) {
  reactTestUtils.Simulate.click(linkComponent, {button: 0});
},

An example test using this code:

it('should have link to desktop page', function(done) {
  var steps = [];

  steps.push(function(mainElement) {
    var link = reactTestUtils.findRenderedDOMComponentWithClass(mainElement, 'header-desktop-link');

    testHelper.simulateRouterLinkClick(link);
  });

  steps.push(function(mainElement) {
    var page = reactTestUtils.scryRenderedDOMComponentsWithClass(mainElement, 'p-desktop');

    expect(page.length).to.equal(1);
    done();
  });

  testHelper.testPage('/prevent-double-click', steps);
});

The confusion I am having is that if I don't do this.unmountComponent(routerMainComponent); in the testPage()'s Router.run(), the test will fail because the routerMainComponent variable will not properly update to the new page that should be loaded from the testHelper.simulateRouterLinkClick(link); in the test.

Do I have to unmount the routerMainComponent each time a Router.Link is clicked in order to get the routerMainComponent to properly update? It just seems weird and I want to make sure that I am not just writing something will never fail (since there is a lack of unit test documentation for react router based components in general, not sure if I am doing this the correct way).

Most helpful comment

same as @gylaz I'm interested in the it('changes the location') test

All 4 comments

we already wrote that test, you don't need to :)

https://github.com/rackt/react-router/blob/master/modules/components/__tests__/Link-test.js#L185-L220

If you still feel like testing that our links work, hopefully that example helps. I'm not sure why you'd be unmounting the component in Router.run

The dead link in the message by @ryanflorence has become https://github.com/rackt/react-router/blob/master/packages/react-router-dom/modules/__tests__/Link-test.js (however, the line numbers have changed).

same as @gylaz I'm interested in the it('changes the location') test

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Waquo picture Waquo  路  3Comments

yormi picture yormi  路  3Comments

wzup picture wzup  路  3Comments

jzimmek picture jzimmek  路  3Comments

stnwk picture stnwk  路  3Comments