Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to unit test components with Link #39

Closed
ryanflorence opened this issue Jun 20, 2014 · 7 comments
Closed

How to unit test components with Link #39

ryanflorence opened this issue Jun 20, 2014 · 7 comments

Comments

@ryanflorence
Copy link
Member

Links throw errors if they can't find a route. When you're unit testing you probably don't have your routes configured.

Workaround (Solution?)

Have a file named routes.js that exports a router, but doesn't render:

// app/router.js
var Router = require('react-nested-router').Router;
module.exports = Router( /* ... */ );

Then a main file that renders it

// app/main.js
var AppRouter = require('./router');
AppRouter.renderComponent(document.body);

And then in your unit test, bring in the router, but don't render it. This should be enough to register all the routes so the links don't freak out.

// test/some-component.js
var AppRouter = require('../app/router');
var Component = require('../app/components/some-component');

// test as usual

Options for this lib

  • Don't throw an error, just warn, then who cares?
  • Make it easy to stub
  • Have some testing flag to ignore links to missing routes
  • Do nothing, having a routes.js file that doesn't render seems like the right thing to do, especially for integration tests, gives you control to render and teardown before each test.
@jaketrent
Copy link
Contributor

workaround worked for me.

@ryanflorence ryanflorence changed the title Unit testing components with Link How to unit testing components with Link Jun 20, 2014
@ryanflorence ryanflorence changed the title How to unit testing components with Link How to unit test components with Link Jun 20, 2014
@ryanflorence
Copy link
Member Author

Side note, the error is:

Error: Invariant Violation: Unable to find a route named "object".
Make sure you have a <Route name="object"> somewhere in the Route

Seems like maybe "object" is the wrong thing here?

@jaketrent
Copy link
Contributor

"object" was the actual name of my route:

<Route name="object" ... />

On Fri, Jun 20, 2014 at 1:43 PM, Ryan Florence notifications@github.com
wrote:

Side note, the error is:

Error: Invariant Violation: Unable to find a route named "object". Make sure you have a somewhere in the Route

Seems like maybe "object" is the wrong thing here?


Reply to this email directly or view it on GitHub
#39 (comment)
.

@ryanflorence
Copy link
Member Author

Oh, well then, looks like we're doing it right 👯

@mjackson
Copy link
Member

This is actually what I've been doing in practice. Funny, I'm even using the filename main.js. :D

@tarjei
Copy link

tarjei commented Jul 25, 2014

Hi, isn't this aproach a bit heavy handed for unit-tests?

Would it be possible to provide an API for registering dummy-routes?

@ryanflorence
Copy link
Member Author

yes #70

particularly #70 (comment)

@lock lock bot locked as resolved and limited conversation to collaborators Jan 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants