-
Notifications
You must be signed in to change notification settings - Fork 668
[ShallowMount] Cannot click on Vuetify icon when stubbing VIcon #919
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
Comments
Same problem here. I guess this is because Vuetify components are now seen as child components and stubs it therefore. |
So the question is: "How could it be possible to trigger event on stubbed components?" |
A temporary workaround is to stub the component manually: const Stub = {
template: '<div />'
}
const wrapper = shallowMount(TestComponent, {
stubs: {
'VIcon': Stub
}
})
wrapper.find(Stub) |
I have same problem on |
@eddyerburgh is this what you were thinking for a workaround? https://codesandbox.io/s/42rxjjvz44 |
@eddyerburgh : to me, the issue is not fixed in beta 25. Are you sure, this issue is fixed? |
Confirming. v25 didn't fix stubs problem @trollepierre did you find some solution with shallowMount? |
No. I had to mount. :'( @eddyerburgh is it possible to reopen this issue? |
@eddyerburgh , thank you for your suggestion, but it does not help in the case, when there is a click handler passed as prop to parent component: <div>
I am parent
<v-btn @click="callbackFromProp"></v-btn>
</div> export default {
props: {
callbackFromProp: Function
}
} |
@p1pchenk0 In that case you need to supply a stub that calls the listener on the root element: const wrapper = shallowMount(TestComponent, {
stubs: {
'v-icon': {
template: '<div class="v-icon" @click="$listeners.click" />'
}
}
})
wrapper.find('.icon').trigger('click') I don't think we should add all listeners to the auto stubs. With normal components we use the |
I'm going to close this issue, since it's not a bug. Let me clear up some misconceptions.
The only guaranteed ways to select an auto stubbed component is to use a component selector: const wrapper = shallowMount(TestComponent)
wrapper.find(Icon) Or a ref selector: const wrapper = shallowMount(TestComponent)
wrapper.find({ ref: 'icon' }) If you don't have access to the component, you must use the const Stub = { template: '<div />' }
const wrapper = shallowMount(TestComponent, {
stubs: { icon: Stub }
})
wrapper.find(Stub) |
If anyone is still trying to figure this out and is confused by the conversation in this issue, I found the solution in the documentation. In short, instead of triggering through the wrapper API like this:
you should call the vue instance
Hope that helps. |
I've used this solution before, but it doesn't seem to work now. On further inspection I noted the following: // Default stub of a v-text-field (Vuetify) child component
const vTextFieldStub = wrapper.find('v-text-field-stub[name="password"]')
// Output: `Wrapper { isFunctionalComponent: true }`
console.log(vTextFieldStub)
// Output: `undefined`
console.log(vTextFieldStub.vm) I can't emit the click event because vm is undefined. I assume it is because it is a
// Output:
//VueWrapper {
// isFunctionalComponent: undefined,
// _emitted: [Object: null prototype] {},
// _emittedByOrder: []
//}
console.log(wrapper.find('v-container-stub')) I found this question, but I can't seem to get it to work properly. |
I think this problem is not solved. could you reopen ? |
in my case, I use |
I have a similar issue with Doing it this way makes my assertion pass as expected. |
I have the same issue as well (in beta.24) In my component, the child component I'm targetting is a functional component, and so it can't trigger its custom event by using const wrapper = shallowMount(MyComponent);
// ChildComp is a functional component
const childWrapper = wrapper.find({ name: 'ChildComp' });
// cannot trigger with this method, as `vm` is undefined
childWrapper.vm.$emit('customEvt'); // ERROR: cannot read property $emit of undefined |
I am running beta 34 and I can't reproduce any of the solutions. https://vue-test-utils.vuejs.org/guides/#emitting-event-from-child-component @lukaVarga My test: it('increments the internal counter when the + button is clicked', () => {
const wrap = mount(NumericSpinner)
wrap.find(SpinnerValueChangeButton).vm.$emit('click')
console.log(wrap.emitted())
}) The console.log() outputs |
@ccarstens beta 34? Do you mean beta 31 (which is the latest version atm), or beta 24? Regardless, for I'm currently running beta 31 and I can do |
Usually I have a lot of same child components, like buttons, so the implementation |
If anyone else stumbles on this thread looking for a way to use shallowMount yet still interact with simple ui-components like buttons, this worked for me:
|
Version
1.0.0-beta.24
Reproduction link
https://codesandbox.io/s/61jzxnovnk
Steps to reproduce
Trying to test click when using VIcon
I reproduce some scenarii :
it works when shallowMounting without using Vuetify, it works when mounting using Vuetify
I doesn't work when shallowMounting with Vuetify when selecting Component, by ref, by name
What is expected?
a way to trigger click on stub when shallowMount
What is actually happening?
it doesn't work
The text was updated successfully, but these errors were encountered: