-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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 handlers #437
Comments
Made a comment on the gist. It's missing the merge function so I copy pasta'd one. Seems to be working, but looks as though its stripping all methods from the component I'm testing. I assume its something to do with either the So If I have a
Now I'm trying to play around with the right way to render this with the existing Class' methods and stuff, but any guidance would be incredibly helpful. |
I'm also having issues getting Jest to deal with the react-router components. I tried the
And getting the following error:
|
Yeah, fwiw, I spent quite a few hours on this with 0 luck. It kinda sucks because Ive been unable to test any of the code we're working on. :\ |
We're going for a release this week with the new API that makes testing We've got several apps now using the router and we don't commit code On Tuesday, November 11, 2014, Oscar Godson notifications@github.com
|
Thanks you so much. My team is really looking forward to this. We look forward to the announcement / release! |
thanks @rpflorence! Since this ticket is closed, is there anyway you could just comment on this ticket when its released so I can get notified? Id like to implement the update once its done :) |
Yep :) On Wednesday, November 12, 2014, Oscar Godson notifications@github.com
|
Any update on this? |
I am still getting the same error as #453 (I assume 0.11.x was suppose to fix this). |
Well I did finally figure out how to get tests working without huge hacks. This test file : https://github.com/rackt/react-router/blob/master/modules/components/__tests__/Link-test.js : gave me the best insight on how to structure my tests. |
@ryanzec could you post the setup code and an example unit test? |
@OscarGodson well my setup is using Mocha to run the tests. I have a react seed project that has the test setup: Test Helper: https://github.com/ryanzec/react-seed/blob/master/test/test-helper.js#L31-L47 I think that helper method should handle a lot of use cases and most of the time I expect to be test 1 component with 1 route in my unit tests but I am sure as I build out my application, i'll be making tweaks to the helper method or even added more. |
@ryanzec you rock, thank you! |
@rpflorence Did this ever happen? |
@dmwyatt Yes, it did. See the part about testing in https://github.com/rackt/react-router/wiki/Announcements |
Ahh thanks. That page doesn't come up in googlin for stuff about On Fri Dec 19 2014 at 12:36:32 PM Michael Jackson notifications@github.com
|
There are also quite a few good examples in our own tests. Just look for Michael Jackson On Fri, Dec 19, 2014 at 10:41 AM, Dustin Wyatt notifications@github.com
|
Hi the above gist, is 404ing, was hoping it would have some answers to my problem if it's still relevant. I'm having trouble understanding how one can unit test routeHandlers. In my case I have setup an app which is quite similar to the async data example in this repo, my handlers are very basic injecting async data into a another react component. |
@azaharakis Here is a basic snippet of a TestContext provider that is based off the above gist. Hopefully this helps: https://gist.github.com/wayoutmind/76e17f07409be07ffdcb |
@wayoutmind, that's something I was after however, the problem still remains if I need to stub out a subcomponent. I've modeled what you have supplied to something I'm trying to do: |
Actually, the above method seems to work... Nothing to see here... Thanks for your help! I'll leave it up incase it helps anyone else. |
For people that are still having issues with testing React-Router based components in their own project. I've forked https://github.com/danvk/mocha-react and utilized the solution by @wayoutmind to provide test context to complicated components. This suite uses Mocha instead of Jest and allows you to have a lot more flexibility with stubs, mocks, spies. https://github.com/adjavaherian/mocha-react |
Getting weird errors with the TestLocation recommended way to use the router to test. TestLocation.history = ['/congratulations/id-1']
Router.run routes,TestLocation.history, (Handler) ->
TestUtils.renderIntoDocument(<Handler />) |
@gregbenner did you ever get this worked out? i'm just starting out on getting unit tests working again with the new recommended way |
is memory history now the preferred way? |
@kellyrmilligan @gregbenner You'll need to construct the TestLocation in newer versions of react-router instead of setting history: new TestLocation(['/test']) |
I've updated my gist to show this: https://gist.github.com/wayoutmind/76e17f07409be07ffdcb |
I'd also recommend not using Jest, it's really terrible. It's super slow.. depends on old versions of Node. This article has some tips for setting up testing with React |
agreed. i'm using tape with karma and just tape straight up with node. trying out wayoutminds gist |
i'm still hitting a bit of a brick wall here. does test location match the default route? here's my config:
I included my routes definition into the getcontext method, so it's as follows:
in the console, i'm getting: i can't get it to work with a route that has a param either. still no match. I feel like I need to include my routes, since in my handlers i'm using named routes in link components everywhere, so if I don't then I get an invariant violation. any ideas? |
https://gist.github.com/rpflorence/1f72da0cd9e507ebec29
Until the new API shows up, this works out pretty great.
The text was updated successfully, but these errors were encountered: