Files
api/web/test/mocks/utils
Michael Datelle 0e008aaf1e test: setup initial test, config and testing libraries (#1309)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Introduced comprehensive testing utilities for Vue components
utilizing the composition API.
- Enhanced testing coverage for the `DownloadApiLogs` and `KeyActions`
components, ensuring robust functionality and user interaction
validation.
- Added mock implementations for various libraries and components to
facilitate isolated unit testing.
- Improved flexibility in the `DummyServerSwitcher` component's input
handling.
- Added a new test setup file to configure the testing environment for
Vue applications.
- Added new test files for `AuthComponent` and `KeyActions` with
comprehensive test cases.
- Introduced a new mock implementation for UI components to streamline
testing.
- Added a new mock implementation for the `useRequest` composable to
prevent hanging issues during tests.
- Added a new mock implementation for the server store used by the Auth
component.

- **Bug Fixes**
- Improved sanitization process to block inline styles for a safer and
more consistent display.

- **Documentation**
- Added README documentation for Vue Component Testing Utilities,
detailing usage and examples.
  - Updated ESLint configuration to ignore coverage directory files.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: mdatelle <mike@datelle.net>
Co-authored-by: Eli Bosley <ekbosley@gmail.com>
2025-04-03 15:50:49 -04:00
..

Vue Component Testing Utilities

This directory contains utilities to help test Vue components, particularly those that use the composition API which can be challenging to test directly.

The Challenge

Vue components that use the composition API (setup(), computed(), ref(), etc.) can be difficult to test for several reasons:

  1. Composition API mocking issues - It's difficult to mock the composition API functions like computed() and ref() in a TypeScript-safe way.
  2. Store integration - Components that use Pinia stores are tricky to test because of how the stores are injected.
  3. TypeScript errors - Attempting to directly test components that use the composition API can lead to TypeScript errors like "computed is not defined".

The Solution: Component Mocking Pattern

Instead of directly testing the component with all its complexity, we create a simplified mock component that implements the same business logic and interface. This approach:

  1. Focuses on behavior - Tests what the component actually does, not how it's implemented.
  2. Avoids composition API issues - By using the Options API for the mock component.
  3. Maintains type safety - Keeps all TypeScript types intact.
  4. Simplifies test setup - Makes tests cleaner and more focused.

Available Utilities

createMockComponent

A generic utility for creating mock components:

function createMockComponent<Props, Data>(template: string, logicFn: (props: Props) => Data);

createListComponentMockFactory

A specialized utility for creating mock component factories for list components with filtering:

function createListComponentMockFactory<ItemType, FilterOptions>(
  templateFn: (options: {
    filteredItems: ItemType[] | undefined;
    maxWidth?: boolean;
    t: (key: string) => string;
  }) => string,
  getItemKey: (item: ItemType) => string
);

Usage Examples

See the examples directory for complete examples of how to use these utilities:

  • key-actions-mock.ts - Shows how to create a factory for the KeyActions component
  • KeyActions.test.example.ts - Shows how to use the factory in actual tests

When to Use This Approach

This approach is ideal for:

  1. Components with complex computed properties
  2. Components that integrate with Pinia stores
  3. Components with conditional rendering logic
  4. Components that need to be thoroughly tested with different prop combinations

Implementation Notes

The one tradeoff with this approach is that you need to keep the mock implementation in sync with the actual component's logic if the component changes. However, this is generally outweighed by the benefits of having reliable, maintainable tests.

Contributing

If you encounter a component that doesn't work well with this pattern, please consider extending these utilities or creating a new utility that addresses the specific challenge.