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 set initial router route? #15

Closed
amcsi opened this issue Feb 26, 2019 · 7 comments
Closed

How to set initial router route? #15

amcsi opened this issue Feb 26, 2019 · 7 comments

Comments

@amcsi
Copy link

amcsi commented Feb 26, 2019

I want to be able to change the current vue-router route by pushing a new path, but I don't seem to get access to the router with the render() function.

How am I supposed to change routes with this library?

EDIT: changed title from "How to change router route?"

@dfcook
Copy link
Collaborator

dfcook commented Mar 6, 2019

Hi, if you look at the examples around routing the idea is that you find the link on the page and fire the click event on it.

Would this suit your use case?

@amcsi
Copy link
Author

amcsi commented Mar 8, 2019

@dfcook Oh, I do understand that's what x-testing-library is meant to do... Actually, let me change the question:

How can I set the initial router route?

Although according to this library I should be changing routes by clicking on the appropriate buttons, but I think I should be able to at least set the initial route, because it's a valid thing for a user to "land" on a certain page.

@amcsi amcsi changed the title How to change router route? How to set initial router route? Mar 8, 2019
@dfcook
Copy link
Collaborator

dfcook commented Mar 23, 2019

I was thinking there must be some way of configuring the routes but it appears there isn't, a limitation of vue-router, see this issue:

vuejs/vue-router#866

The obvious way to get around this is to expose the created router in the object returned from render but I'm not sure if that's a bit leaky given the aims of the dom-testing-library.

Do you have any thoughts yourself on how you'd like it to work? I mean we could accept a default router property on the routes parameter and do a push immediately after rendering?

@amcsi
Copy link
Author

amcsi commented Mar 23, 2019

@dfcook the way I imagined it is that in the configurationCb, a function would be exposed that would allow me to change the route only 1 time, and that too only for the duration of configurationCb (it would stop working afterward). That would ensure that I could only change the route initially, but not later.
...or maybe you could have the same thing, but as a "route" string needed to be passed to a property of RenderOptions. I can see that react-testing-library uses a route property: https://github.com/kentcdodds/react-testing-library/blob/master/examples/__tests__/react-router.js#L38

@dfcook
Copy link
Collaborator

dfcook commented Mar 23, 2019

I've pushed 0.16.0 which adds the constructed store and router to the configuration callback, this allows a router to be pushed before the component is rendered.

https://github.com/testing-library/vue-testing-library/blob/master/src/__tests__/vue-router.js

@dfcook dfcook closed this as completed Mar 23, 2019
@amcsi
Copy link
Author

amcsi commented Mar 25, 2019

@dfcook thanks, that's great! But are you not worried that now one can have access to the router at any time by saving it to an outer variable from the init callback? I said the examples I said, because in those ways you would not have to expose the router.

@dfcook
Copy link
Collaborator

dfcook commented Mar 25, 2019

I suppose that's possible but I went for a middle path between dogmatism and pragmatism. This lets people control the initial setup then test against the DOM. If they want to start accessing the router directly then they're free to miss the point of such testing 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants