Bird
Raised Fist0
Vueframework~10 mins

Testing props and events in Vue - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Testing props and events
Render Component with Props
↓
Component Receives Props
↓
User Interaction or Internal Logic
↓
Component Emits Event
↓
Test Listens for Event
↓
Assert Props and Event Data
The test renders the component with props, triggers events, listens for emitted events, and checks values.
Execution Sample
Vue
import { mount } from '@vue/test-utils'
import MyButton from './MyButton.vue'

const wrapper = mount(MyButton, { props: { label: 'Click me' } })
await wrapper.trigger('click')
expect(wrapper.emitted()).toHaveProperty('clicked')
This code mounts a button component with a label prop, triggers a click, and checks if 'clicked' event was emitted.
Execution Table
StepActionProps StateEvent EmittedTest Assertion
1Mount component with label='Click me'{ label: 'Click me' }NoneProps set correctly
2Trigger click event on component{ label: 'Click me' }clickedEvent 'clicked' emitted
3Check emitted events{ label: 'Click me' }clickedTest passes: 'clicked' event found
4Test ends{ label: 'Click me' }clickedNo more events, test complete
💡 Test ends after confirming 'clicked' event emitted and props are correct
Variable Tracker
VariableStartAfter Step 1After Step 2Final
props.labelundefined'Click me''Click me''Click me'
emittedEvents{}{}{ clicked: [[]] }{ clicked: [[]] }
Key Moments - 2 Insights
Why do we check emitted events after triggering the click?
Because the component emits events only after user actions or internal logic, checking after click confirms the event was triggered (see execution_table step 2 and 3).
How do we know the prop was passed correctly to the component?
The prop value is set during mounting (step 1) and remains unchanged, so the test confirms the component received the correct prop (execution_table step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of props.label after step 2?
Aundefined
B'Click me'
Cnull
D'' (empty string)
💡 Hint
Check variable_tracker row for props.label after Step 2
At which step does the component emit the 'clicked' event?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
See execution_table column 'Event Emitted' for each step
If the click event was not triggered, what would the emittedEvents variable look like after step 2?
A{ clicked: [ [] ] }
B{ click: [ [] ] }
C{}
Dnull
💡 Hint
Check variable_tracker for emittedEvents before and after triggering click
Concept Snapshot
Testing props and events in Vue:
- Mount component with props using mount()
- Trigger user events with wrapper.trigger()
- Listen for emitted events with wrapper.emitted()
- Assert props and events to verify behavior
- Helps ensure component reacts correctly to inputs and user actions
Full Transcript
This visual execution shows how to test Vue components by passing props and checking emitted events. First, the component is mounted with a prop 'label' set to 'Click me'. Then, a click event is triggered on the component. The test listens for the 'clicked' event emitted by the component after the click. The variable tracker shows the prop remains set and the emitted events object updates after the click. Key moments clarify why we check emitted events after triggering and how props are confirmed. The quiz questions reinforce understanding of prop values and event emission timing.

Practice

(1/5)
1. What is the main purpose of testing props in a Vue component?
easy
A. To check if the component receives the correct data from its parent
B. To verify the component's internal state changes
C. To ensure the component renders without errors
D. To test the component's CSS styles

Solution

  1. Step 1: Understand what props are in Vue

    Props are how a parent component passes data to a child component.
  2. Step 2: Identify the goal of testing props

    Testing props ensures the child component receives the right data as expected.
  3. Final Answer:

    To check if the component receives the correct data from its parent -> Option A
  4. Quick Check:

    Props test data passing = C [OK]
Hint: Props test data input from parent component [OK]
Common Mistakes:
  • Confusing props testing with state testing
  • Thinking props test styles or rendering only
  • Assuming props test event emissions
2. Which Vue Test Utils method is used to check if an event was emitted by a component?
easy
A. trigger()
B. props()
C. emitted()
D. setData()

Solution

  1. Step 1: Recall Vue Test Utils methods for events

    The method emitted() returns an object of events emitted by the component.
  2. Step 2: Differentiate from other methods

    trigger() simulates DOM events, props() reads props, and setData() changes component data.
  3. Final Answer:

    emitted() -> Option C
  4. Quick Check:

    Check emitted events = emitted() = B [OK]
Hint: Use emitted() to see events fired by component [OK]
Common Mistakes:
  • Using trigger() to check emitted events
  • Confusing props() with emitted()
  • Trying to use setData() for event checking
3. Given this test code:
const wrapper = mount(MyButton, { props: { label: 'Click' } })
await wrapper.find('button').trigger('click')
const events = wrapper.emitted()

What will events contain if MyButton emits a 'click' event on button click?
medium
A. {}
B. { click: [ 'Click' ] }
C. { click: [ [ 'Click' ] ] }
D. { click: [ [ ] ] }

Solution

  1. Step 1: Understand emitted() output format

    emitted() returns an object where keys are event names and values are arrays of argument arrays for each emit.
  2. Step 2: Analyze the event emission

    If the component emits 'click' without arguments, emitted().click is an array with empty arrays for each emit.
  3. Final Answer:

    { click: [ [ ] ] } -> Option D
  4. Quick Check:

    Event emitted without args = empty array inside emitted() = D [OK]
Hint: emitted() returns arrays of argument arrays per event [OK]
Common Mistakes:
  • Assuming emitted event has string argument by default
  • Confusing emitted() output structure
  • Expecting emitted() to return flat arrays
4. What is wrong with this test code snippet?
const wrapper = mount(MyInput, { props: { value: 'test' } })
wrapper.find('input').trigger('input', { target: { value: 'new' } })
expect(wrapper.emitted('input')[0]).toEqual(['new'])
medium
A. trigger() should be awaited because it returns a promise
B. The emitted event name should be 'update:modelValue' instead of 'input'
C. The props should be passed as 'modelValue' not 'value'
D. The event payload should be an event object, not a string

Solution

  1. Step 1: Check if trigger() needs awaiting

    In Vue Test Utils, trigger() returns a promise and should be awaited to ensure DOM updates.
  2. Step 2: Verify test correctness

    Without await, the emitted event may not be registered before the expect runs, causing test failure.
  3. Final Answer:

    trigger() should be awaited because it returns a promise -> Option A
  4. Quick Check:

    Always await trigger() for event emission tests [OK]
Hint: Always await trigger() to catch emitted events [OK]
Common Mistakes:
  • Not awaiting trigger() causing false negatives
  • Mixing up prop names for v-model
  • Expecting event payload as object instead of emitted args
5. You want to test a Vue component that emits a 'submit' event only if a prop 'isValid' is true. Which test approach correctly verifies this behavior?
hard
A. Mount component with isValid=true, trigger submit, expect no emitted event; then mount with isValid=false, trigger submit, expect emitted event
B. Mount component with isValid=false, trigger submit, expect no emitted event; then mount with isValid=true, trigger submit, expect emitted event
C. Mount component without props, trigger submit, expect emitted event regardless of isValid
D. Mount component with isValid=true, do not trigger submit, expect emitted event

Solution

  1. Step 1: Understand conditional event emission

    The component emits 'submit' only if prop 'isValid' is true, so test must check both true and false cases.
  2. Step 2: Design test cases accordingly

    Mount with isValid false and trigger submit to confirm no event; then mount with true and trigger submit to confirm event emitted.
  3. Final Answer:

    Mount component with isValid=false, trigger submit, expect no emitted event; then mount with isValid=true, trigger submit, expect emitted event -> Option B
  4. Quick Check:

    Test both prop states for conditional event emission [OK]
Hint: Test both true and false prop values for event emission [OK]
Common Mistakes:
  • Testing only one prop state
  • Expecting event without triggering action
  • Ignoring prop effect on event emission
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