I am writing tests with Vitest and Vue Test Utils, but I am having a problem with one of the tests where I want to test the invocation of my reflectInput function when text is typed into the input. However, the test is failing.
View:
<script setup lang="ts">
import { ref, computed } from 'vue';
const inputText = ref('');
const reflectedText = ref('');
const outputText = computed(() => reflect(inputText.value));
const reflectInput = () => {
reflectedText.value = reflect(inputText.value);
};
function reflect(str: string) {
return str;
}
</script>
<template>
<div>
<div class="bg-gray-900 rounded-lg overflow-hidden mx-10">
<div class="p-6">
<div class="flex mb-6">
<textarea
id="textInput"
class="w-full bg-gray-800 text-font-color placeholder-font-color h-48 resize-none border border-black rounded-lg px-4 py-2"
v-model="inputText"
@input="reflectInput"
></textarea>
<textarea
id="textOutput"
class="w-full bg-gray-800 text-font-color placeholder-font-color h-48 resize-none border border-black rounded-lg px-4 py-2 ml-4"
:value="outputText"
readonly
></textarea>
</div>
</div>
</div>
</div>
</template>
my test:
it('should call reflectInput when @input event is triggered on textInput', async () => {
const wrapper = shallowMount(Home);
const reflectInputSpy = vi.spyOn(wrapper.vm, 'reflectInput');
const textInput = wrapper.find('#textInput');
await textInput.setValue('Hello');
await textInput.trigger('input');
expect(reflectInputSpy).toHaveBeenCalled();
});
I searched the Vitest documentation to see if there could be something wrong with spyOn, but I couldn't find the issue.