title: How to test events on the root element of component in vue 2?
body: <p>I'm writing unit tests for the following component:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;template&gt;
    &lt;sub-component
       @foo="bar"
    /&gt;
  &lt;/template&gt;
  &lt;script&gt;
  import SubComponent from './SubComponent';
  
  export default {
    name: 'MyComponent',
    components: { SubComponent },
    methods: {
      bar(payload) {
         this.$emit('baz', ...payload);
      }
    }
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>And the test would be: </p>
  
  <pre class="lang-js prettyprint-override"><code>import { shallowMount } from '@vue/test-utils';
  import _ from 'lodash';
  import MyComponent from '../../components/MyComponent';
  
  describe('MyComponent.vue', () =&gt; {
    let wrapper;
  
    beforeEach(() =&gt; {
      wrapper = shallowMount(MyComponent);
    });
  
    it('should emit baz on subcomponent foo', () =&gt; {
      const subComp = wrapper.find('sub-component-stub');
      expect(subComp.exists()).toBe(true);          // passes
  
      subComp.vm.$emit('foo');
  
      return wrapper.vm.$nextTick().then(() =&gt; {
        expect(wrapper.emitted().baz).toBeTruthy(); // does not pass;
  
        // upon logging: 
        console.log(_.isEqual(wrapper, subComp));   // =&gt; true 
      })
    })
  })
  </code></pre>
  
  <p>The example is oversimplified, but the principle here is I want a reusable <code>&lt;sub-component&gt;</code> (a modal) and various functional wrappers around it (related to one particular task the modal type performs) which map additional functionality. I don't want the functionality in the parent components, as it would violate DRY - i'd have to place it in each component containing a particular type of modal.</p>
  
  <p>This would work fine if <code>&lt;sub-component&gt;</code> was not the direct child of <code>&lt;template&gt;</code>. Somehow, it appears <code>wrapper</code> and <code>subComp</code> are hosted on the same element. </p>
  
  <p>How should this be tested properly?</p>