-
Notifications
You must be signed in to change notification settings - Fork 665
Can't trigger event of child component #1410
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
The reproduction sandbox isn't running for me - complaining about Jest and It been undefined. Can you try using it("demo without stub", async () => {
const cb = jest.fn();
const wrapper = mount(Demo, {
localVue,
stubs: {
Dummy: false
}
});
wrapper.setData({
cb
});
console.log(wrapper.html());
wrapper.find("div").trigger("input");
// or
wrapper.find("div").vm.$emit("input");
await wrapper.vm.$nextTick()
expect(cb).toBeCalled();
}); And see if that works |
Yes, codesandbox broken for @vue/vue-test-utils or jest. You can export it to local and test. I debugged it, found |
Try |
I wasn't thinking when I posted - it should it be I tried Export to ZIP but nothing happened. I can create a template this evening and try this out. |
I tried |
Weird, I'll give this a look this evening. |
Sorry, I forget about Is this a bug or my incorrect usage? I'm not sure. Maybe we should point it out in docs: |
You should not need to use ref - @dobromir-hristov seems to have some info around this. |
isnt it same as #1385 ? |
I think you could be right @aldarund. @CzBiX are you able to share your exact use case for this functionality with some more context? I'm starting to think instead of attempting to fix/cover every edge case for using |
I'm using Vuetify, which provide Add @aldarund In my use case child component is not the root component. |
I see. This is a pretty common use case (testing Vuetify). I use this approach in my Vuetify apps (and when people ask for help with testing Vuetify). Component: <template>
<div>
<div id="count">
Count: {{ count }}
</div>
<v-btn data-test-btn @click="handleInput">Ok</v-btn>
</div>
</template>
<script>
export default {
name: 'App',
methods: {
handleInput() {
this.count++
}
},
data: () => ({
count: 0
}),
};
</script> Test: import { mount, createLocalVue } from '@vue/test-utils'
import Vuetify from 'vuetify'
import App from '@/App.vue'
describe('', () => {
it('renders props.msg when passed', async () => {
const localVue = createLocalVue()
localVue.use(Vuetify)
const vuetify = new Vuetify()
const wrapper = mount(App, {
localVue,
vuetify
})
wrapper.find('[data-test-btn]').find('button').trigger('click')
await wrapper.vm.$nextTick()
expect(wrapper.find('#count').text()).toBe('Count: 1')
})
}) I added a I am not sure if this solution is ideal for every case, but I've found it pretty good. The reason I like it is because you do not need to use Anyway, we will still try out best to fix all the |
@CzBiX but in your provided CBS in Demo.vue and test you are testing the root component . How its not the root? |
* Update JS vue-base * yarn dev * yarn test -u * Fix vuejs/vue-test-utils#1410 * More async goodness * async gotta be async Co-authored-by: WhiteSource Renovate <[email protected]> Co-authored-by: Dario <[email protected]>
@aldarund This issue still exists when the child component is not root component. The CSB is just a simplified demo for reproduction/debug. |
You are correct. There are a few issues with this.
Is this confusing? Yes, definitely... Is there a fix for this atm? No... We will try to find a sensible solution to this in the future. For now, if you must trigger a custom Vue event, you need to find the DOM component that emits it, or use |
I think we need an alternative API to stubbing in VTU for Vue 3. The current API is confusing and requires the user to know the implementation details of their component, which is not how tests should work - coupling your tests to implementation details leads to brittle tests. This is not really the fault of the user, but of VTU, to provide cohesive API and guidance. Anyway, there is a work around and the reason is known, I think we should close this as a wontfix. The docs should definitely be updated, I'll think about how to explain it best and update them soon. At the moment their is a lot of bloat with guides on how to configure things, we need to reduce that kind of content and publish more guides on how to actually use VTU. |
@dobromir-hristov I must to point out, the |
correct. |
Version
1.0.0-beta.29
Reproduction link
https://codesandbox.io/s/ecstatic-moser-b402n
Steps to reproduce
Run test with
npx vue-cli-service test:unit
.What is expected?
Test passed.
What is actually happening?
Test failed.
The text was updated successfully, but these errors were encountered: