Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
How to test an object property of a child functional component (inside a non-functional component)? #1507
Comments
How does your functional child use the prop? Presumably to render in the DOM (that's all functional components do). You can test it by using |
I tried to do that, but then I need to check the "href" attribute of the result; which means the route need to be resolved and I also need to put the app routes on it. I tried to go that way as well, but I can't get it to work either; one of the nested components (BLink from vue-bootstrap specifically) uses this.$router , which is undefined when installing VueRouter on the localVue. It works if I'm installing VueRouter on the global Vue, but this mess up mocking $route for other tests (as you probably know). Code on using local vue that ends up with an undefined this.$router:
As said, only when the 2 commented out lines are "commented-in" the result .html() will contain a valid href (and not just "#") |
I managed to create a small sandbox code that shows the problem with the global vue (using mount like you suggested @lmiller1990 ): Currently if you run the test it will fail. If you go to |
This should work. I will debug this now. |
I looked into this. I tried I verified this here: https://gist.github.com/lmiller1990/f6b9f6c5fa28e4e6cb4089b5765cb4c4 If you run that in a browser you will see the normal Original example from here: https://router.vuejs.org/guide/essentials/named-routes.html You may need to investigate BootstrapVue and how it works with the router. On first look, their link component does not seem to check if the |
@lmiller1990 I'm confused - you do realize the codesandbox link I gave does work if you use VueRouter globally, even with VTU? That is, it does NOT give "#". I haven't actually tried your gist, but I'm pretty sure it's not working due to case sensitive. That is, the route is 'home' (lowercase 'h')
while the nav item name is Home (uppercase 'H')
Unlike the router links which all uses lowercase, matching the routes. Regarding vue bootstrap link: the important part (regarding my issue) is actually However, when installing VueRouter locally (unlike globally), the $router is undefined and hence the p.s. |
I think I missed that you had the global usage commented out. I see what you are explaining now with the global router. One you you forgot was I'll investigate some more. Do you have a real repo by any chance? It is very difficult to debug on code sandbox - constantly I get random errors like "record is undefined" for no reason. It not, no problem, I can copy paste from the sandbox. |
This is so weird. Any non |
I dug deeper. This is incorrectly returning when using localVue. For some reason BootstrapVue is not finding the locally installed router? Is it using the global vue instance instead maybe? There is a function in BoostrapVue that does I think this particular bug is unique to BoostrapVue. As you demonstrated, regular functional components receive their props (as object Object). BootstrapVue seems to do a bunch of stuff to integrate with VueRouter, something is going on there. This will require some more digging into BootstrapVue, I think. Do you know their codebase well? Maybe we can work together on this. |
@lmiller1990 First let me say thanks for looking into it! And I have a code base but it's only local on my computer and it's not something I can share. from my debugging, the code you referred to in The problem is with I can debug a bit more to see why the $router is null, but I'm not sure when it's suppose to be "filled". One thing that could be is that when installing Vue-Router locally, |
Sorry, by code base I meant the BootstrapVue code base. Something weird is definitely happening where BootstrapVue doesn't know about the mock router. I don't know if I can look too much more into it, but if anything comes to mind I will post it here The fact it works w/ the global router is super weird to me. My guess is BootstrapVue does |
@lmiller1990 The BootstrapVue code base is open source on github: https://github.com/bootstrap-vue/bootstrap-vue Also, the code that calls computeTag() is simply passing "this", where |
@lmiller1990 Got a simpler code for you to check: https://codesandbox.io/s/vue-template-bj9he I pretty much copied the 'BLink' and 'BNavItem' code in there, but reduced a lot of code that's unrelated. So now a router-link will always be created, but without the global router the path will either be "#" (incorrect) or "/hello" (correct). |
^ Yep thanks, I ended up making my own case just like this. I think we need to do a deep dive in the bootstrap-vue codebase and explore how it is using vue router. |
@lmiller1990 What? are you sure you looked at the code I provided? To be clear, the code I provided doesn't use Bootstrap-vue at all. There are leftovers in there, but it's not part of the test. Here, I even removed the import lines and the package.json usage, so now there's no mention to bootstrap-vue other than comments: |
I'm silly, I assumed something incorrectly without properly looking at the code - I was looking at some left overs from previously. This is very good minimal repro. I'll make a repo locally with this and debug a bit - sandbox does not appear to expose node_modules :( I guess we are doing something incorrectly with functional components? |
@lmiller1990 all good :) I'm not too familiar with functional components to be honest. I suspected as well, which is why I've tried to make MyNav non functional, but since I'm not familiar with it enough it threw some errors I didn't know how to handle. I might give it another try tomorrow |
I don't see anywhere in our codebase where we do anything special with functional components except for the shallowMount hacks. This reproduction is very good though, let's dig a bit deeper, probably with a log console.logs and debug statements. Thanks @matanshukry! |
@lmiller1990 Made it even smaller now: https://codesandbox.io/s/vue-template-v2urw Changes:
So it's either something with functional or something with the short code in MyNav.js. Hopefully it helps more! |
Version
1.0.0-beta.33
Reproduction link
https://codesandbox.io/s/vue-template-yy6q9
Steps to reproduce
What is expected?
To have a way to test the value passed as an object property to a child functional component
What is actually happening?
There is no way to test the value passed as an object property to a child functional component