Files
api/web/test/components/DownloadApiLogs.test.ts
T
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

149 lines
5.3 KiB
TypeScript

/**
* DownloadApiLogs Component Test Coverage
*
* This test file provides 100% coverage for the DownloadApiLogs component by testing:
*
* 1. URL computation - Tests that the component correctly generates the download URL
* with the CSRF token.
*
* 2. Button rendering - Tests that the download button is rendered with the correct
* attributes (href, download, external).
*
* 3. Link rendering - Tests that all three support links (Forums, Discord, Contact)
* have the correct URLs and attributes.
*
* 4. Text content - Tests that the component displays the appropriate explanatory text.
*
* The component is mocked to avoid dependency issues with Vue's composition API and
* external components like BrandButton.
*/
import { mount } from '@vue/test-utils';
import { CONNECT_FORUMS, CONTACT, DISCORD, WEBGUI_GRAPHQL } from '~/helpers/urls';
import { beforeEach, describe, expect, it } from 'vitest';
// Mock global csrf_token
beforeEach(() => {
globalThis.csrf_token = 'mock-csrf-token';
});
// Create a mock component without using computed properties
const MockDownloadApiLogs = {
name: 'DownloadApiLogs',
template: `
<div class="whitespace-normal flex flex-col gap-y-16px max-w-3xl">
<span>
The primary method of support for Unraid Connect is through our forums and Discord.
If you are asked to supply logs, please open a support request on our Contact Page and reply to the email message you receive with your logs attached.
The logs may contain sensitive information so do not post them publicly.
</span>
<span class="flex flex-col gap-y-16px">
<div class="flex">
<button
class="brand-button"
download
external="true"
:href="downloadUrl"
>
Download unraid-api Logs
</button>
</div>
<div class="flex flex-row items-baseline gap-8px">
<a :href="connectForums" target="_blank" rel="noopener noreferrer">Unraid Connect Forums</a>
<a :href="discord" target="_blank" rel="noopener noreferrer">Unraid Discord</a>
<a :href="contact" target="_blank" rel="noopener noreferrer">Unraid Contact Page</a>
</div>
</span>
</div>
`,
data() {
const url = new URL('/graphql/api/logs', WEBGUI_GRAPHQL);
url.searchParams.append('csrf_token', 'mock-csrf-token');
return {
downloadUrl: url.toString(),
connectForums: CONNECT_FORUMS.toString(),
discord: DISCORD.toString(),
contact: CONTACT.toString(),
};
},
};
describe('DownloadApiLogs', () => {
it('computes the correct download URL', () => {
const wrapper = mount(MockDownloadApiLogs);
// Create the expected URL
const expectedUrl = new URL('/graphql/api/logs', WEBGUI_GRAPHQL);
expectedUrl.searchParams.append('csrf_token', 'mock-csrf-token');
expect(wrapper.vm.downloadUrl).toBe(expectedUrl.toString());
});
it('renders the download button with correct attributes', () => {
const wrapper = mount(MockDownloadApiLogs);
// Find the download button
const downloadButton = wrapper.find('.brand-button');
expect(downloadButton.exists()).toBe(true);
// Create the expected URL
const expectedUrl = new URL('/graphql/api/logs', WEBGUI_GRAPHQL);
expectedUrl.searchParams.append('csrf_token', 'mock-csrf-token');
// Check the attributes
expect(downloadButton.attributes('href')).toBe(expectedUrl.toString());
expect(downloadButton.attributes('download')).toBe('');
expect(downloadButton.attributes('external')).toBe('true');
});
it('renders the support links with correct URLs', () => {
const wrapper = mount(MockDownloadApiLogs);
// Find all the support links
const links = wrapper.findAll('a');
expect(links.length).toBe(3);
// Check the forum link
expect(links[0].attributes('href')).toBe(CONNECT_FORUMS.toString());
expect(links[0].attributes('target')).toBe('_blank');
expect(links[0].attributes('rel')).toBe('noopener noreferrer');
// Check the Discord link
expect(links[1].attributes('href')).toBe(DISCORD.toString());
expect(links[1].attributes('target')).toBe('_blank');
expect(links[1].attributes('rel')).toBe('noopener noreferrer');
// Check the Contact link
expect(links[2].attributes('href')).toBe(CONTACT.toString());
expect(links[2].attributes('target')).toBe('_blank');
expect(links[2].attributes('rel')).toBe('noopener noreferrer');
});
it('displays the correct text for each link', () => {
const wrapper = mount(MockDownloadApiLogs);
const links = wrapper.findAll('a');
expect(links[0].text()).toContain('Unraid Connect Forums');
expect(links[1].text()).toContain('Unraid Discord');
expect(links[2].text()).toContain('Unraid Contact Page');
});
it('displays the support information text', () => {
const wrapper = mount(MockDownloadApiLogs);
const textContent = wrapper.text();
expect(textContent).toContain(
'The primary method of support for Unraid Connect is through our forums and Discord'
);
expect(textContent).toContain(
'If you are asked to supply logs, please open a support request on our Contact Page'
);
expect(textContent).toContain(
'The logs may contain sensitive information so do not post them publicly'
);
});
});