Mounting only the slots of stubbed child components
See original GitHub issueOriginal: https://github.com/vuejs/vue-test-utils/issues/1216
What problem does this feature solve?
We currently have a helper file in our codebase we pull in to testing files, it contains this beauty:
/**
* Create a component stub that will render all included slots from the
* parent component. This lets you test the slots you've included in child component
* without having to fully mount the child component.
*
* * Notes on implementation *
* There is no one place this is clearly laid out. This thread gave the starting point:
*
* https://github.com/vuejs/vue-test-utils/issues/85
*
* but modifying the function requires understanding Vue's functional components
* and the `render` function:
*
* https://vuejs.org/v2/guide/render-function.html
*
* especially the arguments that `createElement` can take:
*
* https://vuejs.org/v2/guide/render-function.html#createElement-Arguments
*
* In short, this produces a <div> with only the child components slots, but none of
* its other functionality.
*
* @return {object} The functional component definition
*/
componentStubWithSlots: function () {
return {
render: function (createElement) {
return createElement('div', [Object.values(this.$slots)]);
}
};
}
It would be nice if there was a more elegant way of handling this, or at least better documented (not requiring links to 3 different places).
What does the proposed API look like?
That helper function gets used like so:
test('Show only mine checkbox adds showOnlyMine to the query params', async () => {
const wrapper = shallow(reservationsList, {
store,
localVue,
stubs: {
RouterLink: RouterLinkStub,
'base-table': helpers.componentStubWithSlots()
},
mocks: { $ga }
});
let checkbox = wrapper.find('[data-test="showOnlyMineCheckbox"]');
checkbox.trigger('click');
expect(wrapper.vm.paramsForGettingListItems)
.toEqual({ showOnlyMine: true });
});
I think it would be nicer if there was something like that built in, so we could just do this:
test('Show only mine checkbox adds showOnlyMine to the query params', async () => {
const wrapper = shallow(reservationsList, {
store,
localVue,
stubs: {
RouterLink: RouterLinkStub
},
stubComponentSlots: [
'base-table'
],
mocks: { $ga }
});
let checkbox = wrapper.find('[data-test="showOnlyMineCheckbox"]');
checkbox.trigger('click');
expect(wrapper.vm.paramsForGettingListItems)
.toEqual({ showOnlyMine: true });
});
We use this helper a lot, it seems like the kind of common “utility” that would be in Vue-Test-Utils.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Mounting only the slots of stubbed child components #69
Create a component stub that will render all included slots from the * parent component. This lets you test the slots you've included...
Read more >Stubs and Shallow Mount | Vue Test Utils
VTU has a shallow mounting option that will automatically stub out all the child components: test('shallow stubs out all child components', ...
Read more >Vue how to test component with slot and slot-props
A toBe matcher would check that all the text in a component strictly equals the expected value.
Read more >Migration to Vue 3 - GitLab Docs
To fix rendering slots in shallowMount , we need to stub a child component with slots explicitly. <!-- MyAwesomeComponent.vue --> <script> import ...
Read more >[vue-test-utils]: operations on destroyed component are ...
A stubbed child component is a replacement for a child component rendered by the component under test. Imagine you have a ParentComponent component...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Another use case for this to shallow mount a view/component that contains a 3rd-party component. Where you don’t want to load the complexity of that 3rd party component, but you do want to test your code being passed into the slot. It can make snapshots more useful and less cluttered.
@dobromir-hristov is looking into this. The idea is looking to be to provide this as a config users can enable, so you can have the behavior you like. eg
config.shallowMount = { renderSlots: true }
or something, since the default is stubbing them.