Files
api/web/test/mocks/utils/component-mock.ts
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

100 lines
2.8 KiB
TypeScript

/**
* Component Mock Utilities
*
* This file provides utilities for testing Vue components that use the composition API
* without having to deal with the complexity of mocking computed properties, refs, etc.
*
* The approach creates simplified Vue option API components that mimic the behavior
* of the original components, making them easier to test.
*/
import { mount } from '@vue/test-utils';
/**
* Creates a mock component that simulates the behavior of a component with composition API
*
* @param template The Vue template string for the mock component
* @param logicFn Function that transforms the input options into component data
*/
export function createMockComponent<
Props extends Record<string, unknown>,
Data extends Record<string, unknown>,
>(template: string, logicFn: (props: Props) => Data) {
return (props: Props) => {
const componentData = logicFn(props);
const mockComponent = {
template,
data() {
return componentData;
},
};
return mount(mockComponent);
};
}
/**
* Creates a mock component factory specialized for list-rendering components with filtering
*
* @param itemType Type of items being rendered in the list
* @param templateFn Function that generates the template based on the passed options
* @returns A factory function that creates test components
*/
export function createListComponentMockFactory<
ItemType,
FilterOptions extends {
items?: ItemType[];
storeItems?: ItemType[];
filterBy?: string[];
filterOut?: string[];
},
>(
templateFn: (options: {
filteredItems: ItemType[] | undefined;
maxWidth?: boolean;
t: (key: string) => string;
}) => string,
getItemKey: (item: ItemType) => string
) {
return (options: FilterOptions & { maxWidth?: boolean; t?: (key: string) => string }) => {
const { items, storeItems, filterBy, filterOut, maxWidth } = options;
// Default translator function
const t = options.t || ((key: string) => `translated_${key}`);
// Function that emulates the component's filtering logic
function getFilteredItems() {
// Emulate computedActions logic
const allItems = items || storeItems;
if (!allItems || (!filterBy && !filterOut)) {
return allItems;
}
// Emulate filtering logic
return allItems.filter((item) => {
const key = getItemKey(item);
return filterOut ? !filterOut.includes(key) : filterBy?.includes(key);
});
}
const mockComponent = {
template: templateFn({
filteredItems: getFilteredItems(),
maxWidth,
t,
}),
data() {
return {
filteredItems: getFilteredItems(),
maxWidth: maxWidth || false,
t,
};
},
};
return mount(mockComponent);
};
}