Vue Component Slot Testing with Vitest
This challenge focuses on unit testing Vue components that utilize slots. Understanding how to test components that accept and render dynamic content via slots is crucial for building robust and maintainable Vue applications. You will be writing tests to ensure your component correctly renders content provided to its default and named slots.
Problem Description
Your task is to implement unit tests for a Vue 3 component using TypeScript and Vitest. This component, MyWrapperComponent, is designed to accept content through its default slot and a named slot called footer. You need to verify that the content passed to these slots is rendered correctly within the MyWrapperComponent.
Key Requirements:
- Test Default Slot: Write a test that mounts
MyWrapperComponentand provides content to its default slot. Assert that the provided content is rendered within the component. - Test Named Slot: Write a test that mounts
MyWrapperComponentand provides content to itsfooternamed slot. Assert that the provided content is rendered within the designated area for thefooterslot. - Test Multiple Slots: Write a test that mounts
MyWrapperComponentand provides content to both the default and thefooternamed slot simultaneously. Assert that both sets of content are rendered correctly in their respective places. - Component Structure: The
MyWrapperComponentwill have a basic structure that clearly delineates where the default slot and thefooterslot content should appear.
Expected Behavior:
- When content is provided to the default slot, it should be rendered in the default slot's location.
- When content is provided to the
footerslot, it should be rendered in thefooterslot's location. - When content is provided to both, they should render independently and correctly.
Edge Cases to Consider:
- What happens if no content is provided to a slot? (While not explicitly required to test for an empty slot, be aware of how your assertions might behave).
- Consider testing with different types of content (plain text, HTML, or even other Vue components if you were to extend this). For this challenge, plain text or simple HTML elements will suffice.
Examples
For the purpose of this challenge, assume MyWrapperComponent looks like this:
<template>
<div class="wrapper">
<h1>My Wrapper Component</h1>
<div class="default-content">
<slot></slot>
</div>
<div class="footer-content">
<h2>Footer Section</h2>
<slot name="footer"></slot>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
name: 'MyWrapperComponent',
});
</script>
Example 1: Testing the Default Slot
Input:
- Mount `MyWrapperComponent`.
- Pass the string "Hello from default slot!" to the default slot.
Output:
- The rendered output should contain:
- `<h1>My Wrapper Component</h1>`
- `<div class="default-content">Hello from default slot!</div>`
- `<div class="footer-content"><h2>Footer Section</h2></div>` (footer slot content will be absent)
Explanation: This test verifies that plain text provided to the default slot is correctly rendered.
Example 2: Testing the Named Slot
Input:
- Mount `MyWrapperComponent`.
- Pass the string "Special footer message." to the `footer` named slot.
Output:
- The rendered output should contain:
- `<h1>My Wrapper Component</h1>`
- `<div class="default-content"></div>` (default slot content will be absent)
- `<div class="footer-content"><h2>Footer Section</h2>Footer message.</div>`
Explanation: This test confirms that content assigned to a named slot (`footer`) appears in its designated area.
Example 3: Testing Both Slots Simultaneously
Input:
- Mount `MyWrapperComponent`.
- Pass "Main content here." to the default slot.
- Pass "Copyright 2023." to the `footer` named slot.
Output:
- The rendered output should contain:
- `<h1>My Wrapper Component</h1>`
- `<div class="default-content">Main content here.</div>`
- `<div class="footer-content"><h2>Footer Section</h2>Copyright 2023.</div>`
Explanation: This test ensures that the component can handle and render content from multiple slots concurrently.
Constraints
- You must use TypeScript for your test files.
- You must use Vitest as your testing framework.
- You should use
@vue/test-utilsfor mounting and interacting with Vue components. - The tests should be designed for Vue 3.
- The solution should be clear, concise, and well-organized.
- Focus on rendering and content assertion; no complex event handling or prop testing is required for this challenge.
Notes
- Recall how to pass content to default and named slots when mounting components with
@vue/test-utils. - Consider using
wrapper.html()to inspect the rendered HTML output for your assertions. - Pay attention to how you structure your test suites and individual test cases for readability.
- Think about how you would import and use
MyWrapperComponentwithin your test file.