count state starting at 0count when clickedcount state starting at 0count when clickedCounterButton using <script setup>. Inside, create a ref called count and set it to 0. Add a <template> with a <button> that displays the text Clicked {{ count }} times.Use ref(0) to create a reactive count variable. Display it inside the button text.
@click event handler to the <button> that increases count.value by 1 when clicked.Use @click="count.value++" on the button to increase count on each click.
mount from @vue/test-utils and imports the CounterButton component. Use mount(CounterButton) to create a wrapper for testing.Use mount to create a test wrapper for the component.
wrapper.find('button').trigger('click') to simulate a click on the button. Then check that wrapper.text() contains the text 'Clicked 1 times' to confirm the counter incremented.Use trigger('click') on the button and check the text updates to 'Clicked 1 times'.
trigger in Vue Test Utils when testing user interactions?triggerThe trigger method simulates user events such as clicks or keyboard input on a mounted Vue component.
Mounting is done by mount, checking data is done by assertions, and importing components is unrelated to trigger.
First, find the button element using find('button'), then call trigger('click') on it.
wrapper.find('button').click() uses click() which is not a Vue Test Utils method. wrapper.trigger('click') calls trigger on wrapper, not on the button. wrapper.click('button') uses a non-existent click method.
const wrapper = mount({
template: `Clicked {{ count }} times`,
data() { return { count: 0 } }
})
await wrapper.find('button').trigger('click')
await wrapper.find('button').trigger('click')
console.log(wrapper.text())The button shows the count and increments it by 1 on each click.
Two clicks are triggered, so count increases from 0 to 2.
const wrapper = mount({
template: ``,
data() { return { value: '' } },
methods: {
updateValue(event) { this.value = event.target.value }
}
})
await wrapper.find('input').trigger('input', { target: { value: 'hello' } })
console.log(wrapper.vm.value)The trigger method emits the event using the second argument as properties of the synthetic event object, so event.target.value is set to 'hello'.
value is updatedThe updateValue method reads event.target.value ('hello') and sets this.value = 'hello'.
template: `{{ message }}
`, data() { return { text: '', message: '' } }, watch: { text(newVal) { this.message = `Typed: ${newVal}` } }
The setValue method sets the input value and triggers the input event properly, updating v-model and watchers.
A uses setValue correctly. B: await wrapper.find('input').trigger('input')
expect(wrapper.text()).toContain('Typed: Vue') triggers input without event data so v-model unchanged. C manually sets value but triggers 'change' not updating v-model. D uses invalid setAttribute.
Pass the test and this pattern moves unknown