Hone logo
Hone
Problems

Testing a Simple Vue Component with TypeScript

This challenge focuses on writing unit tests for a basic Vue component using Jest and Vue Test Utils. Testing components is crucial for ensuring code quality and preventing regressions as your application grows. You'll be verifying that the component renders correctly, emits events, and responds to user interactions.

Problem Description

You are given a simple Vue component named HelloWorld.vue that displays a message and emits an event when a button is clicked. Your task is to write a suite of unit tests using Jest and Vue Test Utils to verify the component's behavior. The tests should cover rendering the component, verifying the initial message, and confirming that the event is emitted correctly when the button is clicked. Consider edge cases like the message being empty.

HelloWorld.vue:

<template>
  <div>
    <p>{{ message }}</p>
    <button @click="emitClick">Click Me</button>
  </div>
</template>

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

export default defineComponent({
  name: 'HelloWorld',
  setup(props, { emit }) {
    const message = ref('Hello, Vue!');

    const emitClick = () => {
      emit('clicked');
    };

    return {
      message,
      emitClick,
    };
  },
});
</script>

What needs to be achieved:

  • Create a Jest test file (e.g., HelloWorld.spec.ts).
  • Import the HelloWorld component.
  • Write tests to:
    • Verify that the component renders the initial message "Hello, Vue!".
    • Verify that a button with the text "Click Me" is rendered.
    • Verify that when the button is clicked, the clicked event is emitted.

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.
  • The tests should be written in TypeScript.

Expected Behavior:

The tests should pass if the component renders correctly, the message is displayed as expected, and the clicked event is emitted when the button is clicked.

Edge Cases to Consider:

  • What happens if the message prop is initially empty? The component should still render without errors, displaying an empty paragraph.

Examples

Example 1:

Input: HelloWorld component with message "Hello, World!"
Output: Test passes, verifying the component renders "Hello, World!"
Explanation: The test mounts the component and asserts that the rendered text content matches "Hello, World!".

Example 2:

Input: HelloWorld component with an empty message.
Output: Test passes, verifying the component renders an empty paragraph.
Explanation: The test mounts the component and asserts that the rendered text content is an empty string.

Constraints

  • You must use Jest and Vue Test Utils.
  • The tests must be written in TypeScript.
  • The tests should focus solely on the HelloWorld component's functionality. No external dependencies or API calls are required.
  • The tests should be reasonably performant (avoid unnecessary complexity).

Notes

  • Make sure you have Jest, Vue Test Utils, and TypeScript set up in your project.
  • Consider using shallowMount or mount from Vue Test Utils depending on your testing strategy. shallowMount is generally faster and more focused.
  • Use find or get from Vue Test Utils to locate elements within the component.
  • Use trigger from Vue Test Utils to simulate user interactions like button clicks.
  • Think about how to properly mock or stub any external dependencies if the component had them (though this component doesn't).
  • Focus on writing tests that are easy to understand and maintain. Good test names are important!
Loading editor...
typescript