Skip to content

Clarify docs on how to mock $root #1437

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

Closed
aantipov opened this issue Feb 13, 2020 · 3 comments · Fixed by #1438
Closed

Clarify docs on how to mock $root #1437

aantipov opened this issue Feb 13, 2020 · 3 comments · Fixed by #1438

Comments

@aantipov
Copy link
Contributor

What problem does this feature solve?

The docs say that I can use mocks mounting option to mock "global injections" like $route.

I considered $root as a "global injection" and that I can use mocks to mock it. Unfortunately it didn't work for me, I spent lots of time trying to make it working, without any success. In the end I came up with a solution to use parentComponent option to mock $root.

As far as I understood it's not possible to mock $root using mocks and it's not considered as a bug.

If I'm right, then the docs for mocks need clarification stating that clearly and providing a way to mock $root

What does the proposed API look like?

The docs should say smth like this:

"One can't use mocks to mock $root though. If you need to mock $root, use "parentComponent" option instead."

And provide "parentComponent" section with an example how to mock $root

@lmiller1990
Copy link
Member

lmiller1990 commented Feb 13, 2020

I don't think $root is really a global injection in the same way Vuex and VueRouter are; global injection here really should be written as "properties injected by third party plugins, assigned to Vue.prototype".

I think a PR adding a note about $root would be a great idea.. I don't think we need a whole section dedicated to mocking $root; this isn't a very common use case, afaik, and the docs are already pretty bloated with a lot of information.

@aantipov
Copy link
Contributor Author

@lmiller1990
We, developers, all come with a different background and see at things from a different perspective.
So your point may be true for some and not for others.

Irrespective of that, I see the "Mounting Options" documentation covering all possible cases when you need to provide/mock some of the component data (arguments, data, methods, slots, components, etc.).
The only missing part is $root, and from the documentation it's not clear why can't one use mocks and should use parentComponent instead, it's not trivial at all.

I might agree that the need in mocking $root might not come often, but still it happens and it would be good for the docs to cover that use case.

I try prepare the PR, if I have time

@lmiller1990
Copy link
Member

lmiller1990 commented Feb 14, 2020

@aantipov I agree, everyone has different backgrounds - I can see why you might expect mocks to work with $root. Happy for a PR adding a note about this (I think under the mocks section in the API docs would be best). You could include a link to the issue with the work around.

Will mark this as "good first issue" so someone else can pick it up if you don't have time. Thanks!

aantipov added a commit to aantipov/vue-test-utils that referenced this issue Feb 14, 2020
lmiller1990 pushed a commit that referenced this issue Feb 14, 2020
* docs(mounting options): add a tip how to mock $root

Clarify how to mock $root

fix #1437

* docs(mounting-options): remove blank line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants