-
Notifications
You must be signed in to change notification settings - Fork 277
Description
Description
When testing component events, we often trigger an event on an inner sub-component to mimic user input, and observe a final event get emitted from the component under test, sometimes with a different / transformed value. However the current version of vue-test-utils-next makes this difficult by returning all emitted events with the same name, even if they were not emitted directly by the component under test.
This has become more of an issue with the recommendation not to use shallow mount, and the prevalence of update:modelValue events when using v-model heavily (ie in forms).
Expected behaviour
I would expect wrapper.emitted() to only include events emitted by the top-level component provided as the first argument to mount.
Observed behaviour
wrapper.emitted() includes events emitted from sub-components, adding to events observed under the same name.
Example
import { mount } from '@vue/test-utils';
const RawInput = {
template: '<input v-model="computedValue" />',
emits: ['update:modelValue'],
props: ['modelValue'],
computed: {
computedValue: {
get() { return this.modelValue; },
set(val) { this.$emit('update:modelValue', val); },
},
},
};
const CSVInput = {
template: '<div class="container"><RawInput v-model="computedValue" /></div>',
components: { RawInput },
emits: ['update:modelValue'],
props: ['modelValue'],
computed: {
computedValue: {
get() { return this.modelValue.join(' '); },
set(val) { this.$emit('update:modelValue', val.split(',')); },
},
},
};
test('emitted events', () => {
const wrapper = mount(CSVInput, { props: { modelValue: [] } });
// inner component emits 'foo,bar'
wrapper.get('input').setValue('foo,bar');
// we want to test outer component emits ['foo', 'bar']
expect(wrapper.emitted('update:modelValue')).toStrictEqual([[['foo', 'bar']]]);
// instead wrapper.emitted('update:modelValue') contains events for both
// inner and outer components
});Error: expect(received).toStrictEqual(expected) // deep equality
- Expected
+ Received
@@ -1,7 +1,10 @@
Array [
Array [
+ "foo,bar",
+ ],
+ Array [
Array [
"foo",
"bar",
],
],
Version
2.0.0-rc.0
This also appears to be a regression that was not present in earlier versions, as updating to 2.0.0-rc.0 broke about 12 tests in our test suite that were previously working with the expected behaviour.