Documentation for testing a component without resolving sub-components
See original GitHub issueWhat problem does this feature solve?
Intro
Assume that there are two components, <x-icon>
and <x-checkbox>
.
The <x-icon>
component is for showing a font icon given by face="..."
attributes like
<x-icon face="error"/> <!-- It shows an error icon -->
<x-icon face="checkbox-empty"/> <!-- It shows a checkbox empty icon -->
<x-icon face="checkbox-checked"/> <!-- It shows a checkbox checked icon -->
And <x-checkbox>
uses <x-icon>
with face
attribute like
<div class="checkbox">
<x-icon :face="face"/> <!-- 'face' attributes are internal and linked to 'checked' attribute -->
<input v-model="checked" type="checkbox">
</div>
So the final HTML will be one of the followings (without resolving <x-icon>
)
<div class="checkbox">
<x-icon face="checkbox-empty"/>
<input type="checkbox">
</div>
<!-- or -->
<div class="checkbox">
<x-icon face="checkbox-checked"/>
<input type="checkbox" checked>
</div>
What I would like to do
I would like to test <x-checkbox>
component without understanding an implementation of the <x-icon>
.
For example, I would like to check
- Is initial HTML shows
<x-icon face="checkbox-empty">
- Is HTML shows
<x-icon face="checkbox-checked">
after<x-checkbox>
has clicked.
For this purpose, I cannot use stubs: { 'x-icon': true }
while
x-icon
is defined incomponents
attributes ofx-checkbox
component and in this case,stubs
seems ignored.stubs
creates an empty comment so I cannot check if the component isx-icon
or notstubs
removes attributes even I usesstubs: { 'x-icon': SomeThing }
so I cannot check if the component attributes ofx-icon
has correctly changed
What does the proposed API look like?
I’m not really proposing but currently, I use a wrapper function to
- Overwrite
localVue.config.isUnknownElement
function to allow elements incomponents
- Remove
components
to prevent element resolving
like
import { mount as mountBase } from '@vue/test-utils';
const original = Vue.config.isUnknownElement;
export function mount(component: any, options: any= {}) {
const localVue = options.localVue || createLocalVue();
if (!!component.options && !!component.options.components) {
const ignoredElements = Object.keys(component.options.components);
Vue.config.isUnknownElement = (tag: string): boolean => {
if (ignoredElements.includes(tag)) {
return false;
}
return original(tag);
}
component.options.components = {};
}
return mountBase(component, {
...options,
localVue,
});
}
Is this a correct way?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Component Testing Tutorial: A Comprehensive Guide With ...
Component testing is a subset of software testing that involves validating every component of software applications independently without merging them.
Read more >Component testing scenarios - Angular
The purpose of the spec is to test the component, not the service, and real services can be trouble.
Read more >Testing parent and child components separately #167 - GitHub
I can re-factor my component without my tests failing and requiring re-writes; If the API of my child component changes the tests will...
Read more >How YOU can use React Testing Library to test component ...
Asynchronous tests and working with input ... We have so far shown you how to render a component, find the resulting elements, and...
Read more >angular2 test, how do I mock sub component - Stack Overflow
First we'll define a mock directive for our sub component (same selector): ... .io/docs/ts/latest/api/testing/TestComponentBuilder-class.html.
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 Free
Top 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
I’m closing this issue as it’s not clear to me what the intention is.
The original issue is a feature request. We have recently changed stubs to render the component name, which I believe satisfied your use case. If you would like a different feature, please create a new issue with a proposal.
If you would like to report a bug, like you mentioned in your other components, please create an issue with a reproduction.
@ilyaztsv I’ve tried that one several days ago and it did not work in my environment so I ended up. Today I fixed that function (commented on #410) and that works perfectly. Thanks for pointing me a correct direction 👍