mirror of
https://github.com/unraid/api.git
synced 2026-01-07 09:10:05 -06:00
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Refactor** - Improved component tests by integrating Pinia's testing utilities for more reliable store mocking and state management. - Updated test setup to streamline plugin usage and remove unnecessary configuration. - Enhanced test clarity by relying on store state changes and Vue's reactivity instead of manual mock updates. - Simplified test cases by focusing on passed props and standardized store mocking. - **Chores** - Updated test directory structure for better organization. - Added additional test mocks for dependencies. - **New Features** - Added comprehensive tests for the ColorSwitcher component, verifying UI elements and theme store interactions. - Introduced tests for the DevSettings component, confirming UI rendering and interaction behavior. - Added detailed tests for the DowngradeOs component covering store interactions and conditional UI rendering. - Added new test suites for DummyServerSwitcher component, ensuring correct rendering and reactive state updates. - Added new test suites for HeaderOsVersion component, validating version badge rendering and error state handling. - Added new test suite for the I18nHost component, validating internationalization context provisioning and error handling. - Added a comprehensive test suite for the Modal component, covering UI behavior, styling, and event handling. - Added new test suite for the Registration component, verifying rendering based on server state. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: mdatelle <mike@datelle.net>
148 lines
4.4 KiB
TypeScript
148 lines
4.4 KiB
TypeScript
/**
|
|
* HeaderOsVersion Component Test Coverage
|
|
*/
|
|
|
|
import { nextTick } from 'vue';
|
|
import { setActivePinia } from 'pinia';
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
import { Badge } from '@unraid/ui';
|
|
import { createTestingPinia } from '@pinia/testing';
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import type { TestingPinia } from '@pinia/testing';
|
|
import type { VueWrapper } from '@vue/test-utils';
|
|
import type { Error as CustomApiError } from '~/store/errors';
|
|
import type { ServerUpdateOsResponse } from '~/types/server';
|
|
|
|
import HeaderOsVersion from '~/components/HeaderOsVersion.ce.vue';
|
|
import { useErrorsStore } from '~/store/errors';
|
|
import { useServerStore } from '~/store/server';
|
|
|
|
const testMockReleaseNotesUrl = 'http://mock.release.notes/v';
|
|
|
|
vi.mock('crypto-js/aes', () => ({ default: {} }));
|
|
vi.mock('@unraid/shared-callbacks', () => ({
|
|
useCallback: vi.fn(() => ({ send: vi.fn(), watcher: vi.fn() })),
|
|
}));
|
|
|
|
vi.mock('~/helpers/urls', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('~/helpers/urls')>();
|
|
const mockReleaseNotesUrl = 'http://mock.release.notes/v';
|
|
const mockWebGuiToolsUpdate = '/mock/Tools/Update';
|
|
const mockWebGuiToolsDowngrade = '/mock/Tools/Downgrade';
|
|
|
|
return {
|
|
...actual,
|
|
getReleaseNotesUrl: vi.fn((version: string) => `${mockReleaseNotesUrl}${version}`),
|
|
WEBGUI_TOOLS_UPDATE: mockWebGuiToolsUpdate,
|
|
WEBGUI_TOOLS_DOWNGRADE: mockWebGuiToolsDowngrade,
|
|
};
|
|
});
|
|
|
|
vi.mock('vue-i18n', () => ({
|
|
useI18n: () => ({
|
|
t: (key: string, params?: unknown) => {
|
|
if (params && Array.isArray(params)) {
|
|
let result = key;
|
|
params.forEach((val, index) => {
|
|
result = result.replace(`{${index}}`, String(val));
|
|
});
|
|
|
|
return result;
|
|
}
|
|
|
|
const keyMap: Record<string, string> = {
|
|
'Reboot Required for Update': 'Reboot Required for Update',
|
|
'Reboot Required for Downgrade': 'Reboot Required for Downgrade',
|
|
'Updating 3rd party drivers': 'Updating 3rd party drivers',
|
|
'Update Available': 'Update Available',
|
|
'Update Released': 'Update Released',
|
|
'View release notes': 'View release notes',
|
|
};
|
|
|
|
return keyMap[key] ?? key;
|
|
},
|
|
}),
|
|
}));
|
|
|
|
vi.mock('@unraid/ui', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('@unraid/ui')>();
|
|
return {
|
|
...actual,
|
|
Badge: actual.Badge,
|
|
};
|
|
});
|
|
|
|
describe('HeaderOsVersion', () => {
|
|
let wrapper: VueWrapper<unknown>;
|
|
let testingPinia: TestingPinia;
|
|
let serverStore: ReturnType<typeof useServerStore>;
|
|
let errorsStore: ReturnType<typeof useErrorsStore>;
|
|
|
|
const findUpdateStatusComponent = () => {
|
|
const statusElement = wrapper.find('a.group:not([title*="release notes"]), button.group');
|
|
return statusElement.exists() ? statusElement : null;
|
|
};
|
|
|
|
beforeEach(() => {
|
|
testingPinia = createTestingPinia({ createSpy: vi.fn });
|
|
setActivePinia(testingPinia);
|
|
|
|
serverStore = useServerStore();
|
|
errorsStore = useErrorsStore();
|
|
|
|
serverStore.osVersion = '6.12.0';
|
|
serverStore.rebootType = '';
|
|
serverStore.updateOsResponse = undefined;
|
|
serverStore.regExp = 0;
|
|
serverStore.updateOsIgnoredReleases = [];
|
|
errorsStore.errors = [];
|
|
|
|
wrapper = mount(HeaderOsVersion, {
|
|
global: {
|
|
plugins: [testingPinia],
|
|
components: { Badge },
|
|
},
|
|
});
|
|
});
|
|
|
|
afterEach(() => {
|
|
wrapper?.unmount();
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
it('renders OS version badge with correct link and no update status initially', () => {
|
|
const versionBadgeLink = wrapper.find('a[title*="release notes"]');
|
|
|
|
expect(versionBadgeLink.exists()).toBe(true);
|
|
expect(versionBadgeLink.attributes('href')).toBe(`${testMockReleaseNotesUrl}6.12.0`);
|
|
|
|
const badge = versionBadgeLink.findComponent(Badge);
|
|
|
|
expect(badge.exists()).toBe(true);
|
|
expect(badge.text()).toContain('6.12.0');
|
|
expect(findUpdateStatusComponent()).toBeNull();
|
|
});
|
|
|
|
it('does not render update status when stateDataError is present', async () => {
|
|
const mockError: CustomApiError = {
|
|
message: 'State data fetch failed',
|
|
heading: 'Fetch Error',
|
|
level: 'error',
|
|
type: 'serverState',
|
|
};
|
|
errorsStore.errors = [mockError];
|
|
serverStore.updateOsResponse = {
|
|
version: '6.13.0',
|
|
isNewer: true,
|
|
isEligible: true,
|
|
} as ServerUpdateOsResponse;
|
|
serverStore.rebootType = '';
|
|
|
|
await nextTick();
|
|
|
|
expect(findUpdateStatusComponent()).toBeNull();
|
|
});
|
|
});
|