I'm currently trying to get a single file component (ComponentB) unit tested using Jest and vue-test-utils. ComponentB extends ComponentA, which has a method update(product) defined in it.
/* -------- Component B -------- */
<script>
import ComponentA from './ComponentA'
export default {
extends: ComponentA,
props: [...],
data: () => {
productData: {foo: 'bar', baz: 'shiz'}
},
methods: {
updateProduct() {
this.update(this.productData)
}
}
}
</script>
/* -------- Component A -------- */
<script>
export default {
props: [...],
data: () => {...},
methods: {
update(productData) {
...
}
}
}
</script>
I then attempt a unit test in which I shallowMount() my ComponentB and try to jest.spyOn that update(productData) method that is defined in ComponentA. The test looks like this:
it('calls update with data when input is added to the field', () => {
const spy = jest.spyOn(ComponentA, 'update);
const wrapper = shallowMount(ComponentB, { propsData: { ... } });
const contractIdInput = wrapper.find('#contract-id-input > b-form-input');
contractIdInput.element.value = 'foobar';
contractIdInput.trigger('input')
expect(spy).toHaveBeenCalledWith(...someDataHere...)
});
When I run this test, I get a Cannot spy the update property because it is not a function; undefined given instead.
I'm not entirely sure why this isn't working, though I do have some ideas.
First, because I am shallowMount()ing my ComponentB, it isn't going to know anything about its parent component, and thus not have access to the update(productData) method that is defined on ComponentA.
Second, perhaps I'm not calling jest.spyOn() at the right time, and should instead call it after I create the wrapper object of ComponentB. However, I tried changing this around and didn't have any success or different behavior.
So my question is, how do I spy on a method that is provided by an extended component when I am shallow mounting the component under test?