Hone logo
Hone
Problems

Testing a Simple Vue Counter Component

This challenge focuses on writing component tests for a basic Vue counter component using Jest and Vue Test Utils. Component testing is crucial for ensuring the reliability and correctness of your Vue applications, allowing you to isolate and verify individual components before integrating them into larger systems. You'll be writing tests to verify the component renders correctly, responds to user interactions (button clicks), and updates its displayed count accordingly.

Problem Description

You are given a simple Vue component called Counter.vue that displays a counter value and provides two buttons: "Increment" and "Decrement." Your task is to write a suite of unit tests for this component using Jest and Vue Test Utils. The tests should cover the following aspects:

  • Initial Render: Verify that the component initially renders with the correct counter value (0).
  • Increment Button Click: Verify that clicking the "Increment" button increases the counter value by 1.
  • Decrement Button Click: Verify that clicking the "Decrement" button decreases the counter value by 1.
  • Boundary Conditions: Verify that the counter value does not go below 0 when decrementing.

Key Requirements:

  • Use Jest as the testing framework.
  • Use Vue Test Utils for mounting and interacting with the component.
  • Write clear and concise test assertions.
  • Ensure the tests are isolated and do not rely on external dependencies.

Expected Behavior:

The tests should pass if the component behaves as described above. Each test should clearly assert a specific aspect of the component's functionality.

Edge Cases to Consider:

  • What happens when the counter reaches 0 and the "Decrement" button is clicked? The counter should remain at 0.
  • Ensure the component renders correctly with the initial value of 0.

Examples

Example 1:

Input: Counter component mounted with initial value 0.
Output: The component renders with text "Count: 0".
Explanation: This verifies the initial render state of the component.

Example 2:

Input: Counter component mounted, "Increment" button clicked.
Output: The component renders with text "Count: 1".
Explanation: This verifies the increment functionality.

Example 3:

Input: Counter component mounted, "Decrement" button clicked when count is 0.
Output: The component renders with text "Count: 0".
Explanation: This verifies the boundary condition where the counter should not go below 0.

Constraints

  • You must use TypeScript for the tests.
  • The tests should be written in a single test file (e.g., Counter.spec.ts).
  • The component Counter.vue is provided below. Do not modify the component itself.
  • Assume you have Jest, Vue Test Utils, and TypeScript set up in your project.

Notes

  • Consider using shallowMount or mount from Vue Test Utils based on your preference. shallowMount is generally faster for unit testing as it doesn't render child components.
  • Use find or get from Vue Test Utils to locate elements within the component.
  • Pay attention to the order of your assertions to ensure they accurately reflect the expected behavior.
  • Think about how to simulate user interactions (button clicks) within your tests. trigger method is useful for this.
  • The component code is provided below for reference.
<template>
  <div>
    <p>Count: {{ count }}</p>
    <button @click="increment">Increment</button>
    <button @click="decrement">Decrement</button>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue';

export default defineComponent({
  name: 'Counter',
  data() {
    return {
      count: 0,
    };
  },
  methods: {
    increment() {
      this.count++;
    },
    decrement() {
      if (this.count > 0) {
        this.count--;
      }
    },
  },
});
</script>
Loading editor...
typescript