Description
What problem does this feature solve?
In my workflow it is a pretty common use case to have a unit test that checks if some nested component is being rendered conditionally.
For example i have a component with a template like this (I use pug for templating, just so there won't be any confusion):
.container
MyCustomComponent(v-if="condition" :title="cog.title")
And I want to have a unit test that makes sure that MyCustomComponent
is only rendered when that condition is satisfied.
I'm coming from React background, where enzyme
's shallow render passes component's display name by default. So if I would shallow render the setup above - the wrapper.html()
would look something like this (if the v-if
condition was met):
<div class="container">
<MyCustomComponent title="something from cog.title" />
</div>
But in vue-test-utils
the result would actually look like this:
<div class="container">
<!---->
</div>
There is currently an option to supply a stubs object to shallow
render method, but that requires you to manually write a list of components and their respective stubs, which is not time-efficient.
Currently I wrote a simple stub-generator, which acts somewhat like identity-object-proxy, and basically looks at the list of components
in options of the component we want to render, and generates an object of stubs.
That allows me to achieve a result like this:
<div class="container">
<mycustomcomponent title="something from cog.title" />
</div>
Which is basically what I need.
I can see more people that might want this kind of behaviour to speed up the unit testing, as this is a pretty common approach in other systems.
My stubs generator code for context:
// Using custom proxy to automatically stub with proper component names
const idObj = new Proxy({}, {
get: function getter(target, key) {
return `<${key} />`;
},
});
// Generate the object of stubs with proxy
const genStubs = component => Object.keys(component.options.components).reduce((acc, comp) => {
acc[comp] = idObj[comp];
return acc;
}, {});
Pretty basic, but it allows me to simply use wrapper.find('<component name>');
to conditionally check if something was rendered, without manually supplying the stubs object every time, since I have a custom wrapper around shallow
that passes my generator to the stubs
option.
What does the proposed API look like?
I'm not sure if this should be the default behaviour, since people might rely on current way of stubbing in the existing code, but I can see something like a flag "useNamedStubs" for shallow
options, or something similar.