When using shallowMount
/mount
, the second parameter represents the mounting options that can be utilized to replace the component's data props during mounting. This allows for the implementation of a setter to simulate changes to the isLoading
data prop, enabling verification of property modifications within the method being tested:
it('sets isLoading', () => {
const isLoadingSetter = jest.fn()
const wrapper = shallowMount(MyComponent, {
data() {
return {
// This setter is triggered when `this.isLoading = value` is called in the component.
set isLoading(value) {
isLoadingSetter(value)
}
}
}
})
//...
})
Subsequently, you can employ toHaveBeenCalledTimes()
in conjunction with isLoadingSetter.mock.calls[]
to inspect the arguments passed to the mocked setter in each invocation. To evaluate the consequences of an async
function, await the method execution before asserting any outcomes:
it('sets isLoading', async () => {
//...
await wrapper.vm.method()
expect(isLoadingSetter).toHaveBeenCalledTimes(2)
expect(isLoadingSetter.mock.calls.[0][0]).toBe(true)
expect(isLoadingSetter.mock.calls.[1][0]).toBe(false)
})
If validating the invocation of GET_OFFERS()
is also required, utilize jest.spyOn()
on the component's method prior to mounting:
it('gets offers', async () => {
const getOfferSpy = jest.spyOn(MyComponent.methods, 'GET_OFFERS')
const wrapper = shallowMount(MyComponent, /*...*/)
await wrapper.vm.method()
expect(getOfferSpy).toHaveBeenCalledTimes(1)
})