I have a vue.js SearchTest component with a v-autocomplete
. It has watchers for select
and search
:
// SearchTest.vue
<template>
<v-autocomplete
v-model="select"
:items="items"
:loading="isLoading"
:search-input.sync="search"
hide-no-data
hide-selected
placeholder="Type search query"
return-object
></v-autocomplete>
</template>
<script>
export default {
data: () => ({
select: null,
isLoading: false,
search: null,
items: [],
}),
watch: {
select(val) {
console.log('select: ', val);
}
search(val) {
console.log('search: ', val);
},
},
};
</script>
<style></style>
I'm testing it as following. I expect both watchers should fire. In fact, only the select
watcher fired. The search
watcher did not.
// SearchTest.spec.js
import { createLocalVue, mount } from '@vue/test-utils'
import SearchTest from '@/components/SearchTest.vue'
import Vue from 'vue'
import Vuetify from 'vuetify'
Vue.use(Vuetify)
describe('SearchTest.vue', () => {
const localVue = createLocalVue();
const vuetify = new Vuetify({});
let spy;
beforeAll(() => {
spy = jest.spyOn(console, 'log');
})
afterEach(() => {
spy.mockClear();
})
it('Select', () => {
const wrapper = mount(SearchTest, { localVue, vuetify });
// Triggers select.
wrapper.vm.select = 'select';
wrapper.vm.$nextTick(() => {
expect(spy).toBeCalled(); // OK
});
})
it('Search', () => {
const wrapper = mount(SearchTest, { localVue, vuetify });
// Should trigger search, but fails.
wrapper.vm.search = 'search';
wrapper.vm.$nextTick(() => {
expect(spy).toBeCalled(); // Fail
});
})
})
Any idea for passing the Search
test?