Bird
Raised Fist0
Vueframework~20 mins

Testing user interactions in Vue - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Vue Interaction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when the button is clicked?

Consider this Vue 3 component using the Composition API. What will be the displayed count after clicking the button twice?

Vue
<template>
  <button @click="increment">Clicked {{ count }} times</button>
</template>

<script setup>
import { ref } from 'vue'
const count = ref(0)
function increment() {
  count.value++
}
</script>
AClicked 0 times
BClicked 1 times
CClicked 2 times
DClicked NaN times
Attempts:
2 left
💡 Hint

Remember how ref works and how the increment function changes the value.

📝 Syntax
intermediate
2:00remaining
Which option correctly binds a click event in Vue 3?

Identify the correct syntax to bind a click event to a method named handleClick in a Vue 3 template.

Vue
<template>
  <!-- Choose the correct button syntax -->
</template>
A<button @click="handleClick">Click me</button>
B<button @click="handleClick()">Click me</button>
C<button v-on:click="handleClick">Click me</button>
D<button v-click="handleClick">Click me</button>
Attempts:
2 left
💡 Hint

Vue 3 supports shorthand for event binding. Consider if parentheses are needed.

🔧 Debug
advanced
2:00remaining
Why does this Vue 3 component not update the displayed message on button click?

Examine the code below. The message does not update when the button is clicked. What is the cause?

Vue
<template>
  <div>
    <p>{{ message }}</p>
    <button @click="updateMessage">Update</button>
  </div>
</template>

<script setup>
import { reactive } from 'vue'
const state = reactive({ message: 'Hello' })
function updateMessage() {
  state.message = 'Updated!'
}
</script>
AThe template uses {{ message }} but the reactive object is named state, so message is undefined.
BReactive objects cannot be updated directly; use ref instead.
CThe updateMessage function is not called because the button has no click event.
DThe reactive object must be wrapped in ref to update properly.
Attempts:
2 left
💡 Hint

Check the variable names used in the template and script.

❓ state_output
advanced
2:00remaining
What is the final value of count after these updates?

Given this Vue 3 component, what is the value of count after the updateCount function runs?

Vue
<script setup>
import { ref } from 'vue'
const count = ref(0)
function updateCount() {
  count.value += 1
  count.value += 2
  count.value = count.value * 2
}
updateCount()
</script>
A3
B6
C4
D5
Attempts:
2 left
💡 Hint

Calculate step-by-step how count.value changes.

🧠 Conceptual
expert
2:00remaining
Which statement about Vue 3's reactive and ref is true?

Choose the correct statement about the difference between reactive and ref in Vue 3.

A<code>reactive</code> returns a ref object, while <code>ref</code> returns a reactive proxy.
B<code>reactive</code> can only be used with primitive values, <code>ref</code> only with objects.
C<code>ref</code> and <code>reactive</code> are interchangeable and have no differences.
D<code>ref</code> creates a reactive object for primitive values, while <code>reactive</code> creates a reactive proxy for objects and arrays.
Attempts:
2 left
💡 Hint

Think about how Vue tracks changes for simple values vs complex objects.

Practice

(1/5)
1. What is the main purpose of using trigger in Vue Test Utils when testing user interactions?
easy
A. To simulate user events like clicks or typing on components
B. To mount the component before testing
C. To check the component's initial data state
D. To import Vue components into the test file

Solution

  1. Step 1: Understand the role of trigger

    The trigger method simulates user events such as clicks or keyboard input on a mounted Vue component.

  2. Step 2: Differentiate from other methods

    Mounting is done by mount, checking data is done by assertions, and importing components is unrelated to trigger.

  3. Final Answer:

    To simulate user events like clicks or typing on components -> Option A
  4. Quick Check:

    trigger simulates events = B [OK]
Hint: Remember: trigger = simulate user event [OK]
Common Mistakes:
  • Confusing trigger with mount
  • Thinking trigger checks data directly
  • Assuming trigger imports components
2. Which of the following is the correct syntax to simulate a click event on a button with Vue Test Utils?
easy
A. wrapper.find('button').click()
B. wrapper.find('button').trigger('click')
C. wrapper.click('button')
D. wrapper.trigger('click')

Solution

  1. Step 1: Identify the correct method to find and trigger

    First, find the button element using find('button'), then call trigger('click') on it.

  2. Step 2: Check each option

    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.

  3. Final Answer:

    wrapper.find('button').trigger('click') -> Option B
  4. Quick Check:

    Find element then trigger event = D [OK]
Hint: Find element first, then trigger event [OK]
Common Mistakes:
  • Calling trigger on wrapper instead of element
  • Using .click() instead of .trigger('click')
  • Wrong method order or syntax
3. Given this Vue component test snippet:
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())

What will be logged to the console?
medium
A. Clicked 0 times
B. Clicked 1 times
C. Clicked NaN times
D. Clicked 2 times

Solution

  1. Step 1: Understand the component's data and event

    The button shows the count and increments it by 1 on each click.

  2. Step 2: Count the number of clicks triggered

    Two clicks are triggered, so count increases from 0 to 2.

  3. Final Answer:

    Clicked 2 times -> Option D
  4. Quick Check:

    2 clicks increment count to 2 = C [OK]
Hint: Each trigger('click') adds 1 to count [OK]
Common Mistakes:
  • Assuming count does not update
  • Confusing async behavior
  • Ignoring the number of triggers
4. In this test code:
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)

What is the output and why?
medium
A. hello
B. empty string
C. undefined
D. Error: event object missing

Solution

  1. Step 1: Understand how trigger passes event data

    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'.

  2. Step 2: Check why value is updated

    The updateValue method reads event.target.value ('hello') and sets this.value = 'hello'.

  3. Final Answer:

    hello -> Option A
  4. Quick Check:

    trigger('input', {target:{value:'hello'}}) updates handler = B [OK]
Hint: trigger('input', {target:{value:'x'}}) feeds to @input handler [OK]
Common Mistakes:
  • Triggering 'input' without payload
  • Confusing with v-model inputs (use setValue)
  • Confusing input event with change event
5. You want to test a Vue component with a text input that updates a message on typing. Which approach correctly simulates typing 'Vue' and verifies the message updates?
template: `

{{ message }}

`, data() { return { text: '', message: '' } }, watch: { text(newVal) { this.message = `Typed: ${newVal}` } }

Which test code is correct?
hard
A. wrapper.find('input').element.value = 'Vue' await wrapper.find('input').trigger('change') expect(wrapper.text()).toContain('Typed: Vue')
B. await wrapper.find('input').trigger('input') expect(wrapper.text()).toContain('Typed: Vue')
C. await wrapper.find('input').setValue('Vue') expect(wrapper.text()).toContain('Typed: Vue')
D. wrapper.find('input').setAttribute('value', 'Vue') await wrapper.find('input').trigger('input') expect(wrapper.text()).toContain('Typed: Vue')

Solution

  1. Step 1: Understand how to simulate typing in Vue Test Utils

    The setValue method sets the input value and triggers the input event properly, updating v-model and watchers.

  2. Step 2: Evaluate each option's correctness

    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.

  3. Final Answer:

    await wrapper.find('input').setValue('Vue') expect(wrapper.text()).toContain('Typed: Vue') -> Option C
  4. Quick Check:

    Use setValue to simulate typing = A [OK]
Hint: Use setValue() to simulate typing and update v-model [OK]
Common Mistakes:
  • Using trigger('input') without setValue()
  • Manually changing element.value without events
  • Using non-existent methods like setAttribute
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard