mirror of
https://github.com/unraid/api.git
synced 2025-12-21 08:39:38 -06:00
This is batch 2 of the web store tests. I started implementing the recommended approach in the Pinia docs for the store tests and was able to eliminate most of the mocking files. The server.test.ts file still uses `pinia/testing` for now since I was having trouble with some of the dependencies in the store due to it's complexity. I also updated the `web-testing-rules`for Cursor in an effort to streamline the AI's approach when helping with tests. There's some things it still struggles with and seems like it doesn't always take the rules into consideration until after it hits a snag. It likes to try and create a mock for the store it's actually testing even though there's a rule in place and has to be reminded. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Summary by CodeRabbit - **Tests** - Added comprehensive test suites for account management and activation flows to ensure smoother user interactions. - Introduced new test coverage for callback actions, validating state management and action handling. - Added a new test file for the account store, covering various actions and their interactions. - Introduced a new test file for the activation code store, verifying state management and modal visibility. - Established a new test file for dropdown functionality, ensuring accurate state management and action methods. - Added a new test suite for the Errors store, covering error handling and modal interactions. - Enhanced test guidelines for Vue components and Pinia stores, providing clearer documentation and best practices. - Introduced a new test file for the InstallKey store, validating key installation processes and error handling. - Added a new test file for the dropdown store, ensuring accurate visibility state management and action methods. - **Refactor** - Streamlined the structure of account action payloads for consistent behavior. - Improved code formatting and reactivity setups to enhance overall operational stability. - Enhanced reactivity capabilities in various stores by introducing new Vue composition API functions. - Improved code readability and organization in the installKey store and other related files. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: mdatelle <mike@datelle.net>
153 lines
4.4 KiB
TypeScript
153 lines
4.4 KiB
TypeScript
/**
|
||
* Errors store test coverage
|
||
*/
|
||
|
||
import { nextTick } from 'vue';
|
||
import { createPinia, setActivePinia } from 'pinia';
|
||
|
||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||
|
||
import type { Error } from '~/store/errors';
|
||
|
||
import { useErrorsStore } from '~/store/errors';
|
||
|
||
const mockFeedbackButton = vi.fn();
|
||
|
||
// Mock OBJ_TO_STR function
|
||
vi.mock('~/helpers/functions', () => ({
|
||
OBJ_TO_STR: (obj: unknown) => JSON.stringify(obj),
|
||
}));
|
||
|
||
// Mock FeedbackButton global
|
||
vi.stubGlobal('FeedbackButton', mockFeedbackButton);
|
||
|
||
describe('Errors Store', () => {
|
||
let store: ReturnType<typeof useErrorsStore>;
|
||
const originalConsoleError = console.error;
|
||
|
||
const mockError: Error = {
|
||
heading: 'Test Error',
|
||
level: 'error',
|
||
message: 'Test message',
|
||
type: 'request',
|
||
ref: 'test-ref',
|
||
};
|
||
|
||
beforeEach(() => {
|
||
// Silence console.error during tests
|
||
console.error = vi.fn();
|
||
|
||
const pinia = createPinia();
|
||
setActivePinia(pinia);
|
||
store = useErrorsStore();
|
||
vi.clearAllMocks();
|
||
});
|
||
|
||
afterEach(() => {
|
||
console.error = originalConsoleError;
|
||
vi.resetAllMocks();
|
||
});
|
||
|
||
describe('State and Actions', () => {
|
||
it('should initialize with empty errors array', () => {
|
||
expect(store.errors).toEqual([]);
|
||
});
|
||
|
||
it('should add error', () => {
|
||
store.setError(mockError);
|
||
expect(store.errors).toHaveLength(1);
|
||
expect(store.errors[0]).toEqual(mockError);
|
||
});
|
||
|
||
it('should remove error by index', async () => {
|
||
store.setError(mockError);
|
||
store.setError({ ...mockError, ref: 'test-ref-2' });
|
||
expect(store.errors).toHaveLength(2);
|
||
|
||
store.removeErrorByIndex(0);
|
||
await nextTick();
|
||
|
||
expect(store.errors).toHaveLength(1);
|
||
expect(store.errors[0].ref).toBe('test-ref-2');
|
||
});
|
||
|
||
it('should remove error by ref', async () => {
|
||
store.setError(mockError);
|
||
store.setError({ ...mockError, ref: 'test-ref-2' });
|
||
expect(store.errors).toHaveLength(2);
|
||
|
||
store.removeErrorByRef('test-ref');
|
||
await nextTick();
|
||
|
||
expect(store.errors).toHaveLength(1);
|
||
expect(store.errors[0].ref).toBe('test-ref-2');
|
||
});
|
||
|
||
it('should reset errors', async () => {
|
||
store.setError(mockError);
|
||
store.setError({ ...mockError, ref: 'test-ref-2' });
|
||
expect(store.errors).toHaveLength(2);
|
||
|
||
store.resetErrors();
|
||
await nextTick();
|
||
|
||
expect(store.errors).toHaveLength(0);
|
||
});
|
||
});
|
||
|
||
describe('Troubleshoot Feature', () => {
|
||
beforeEach(() => {
|
||
// Mock the DOM elements needed for troubleshoot
|
||
const mockModal = document.createElement('div');
|
||
mockModal.className = 'sweet-alert visible';
|
||
|
||
const mockTextarea = document.createElement('textarea');
|
||
mockTextarea.id = 'troubleshootDetails';
|
||
mockModal.appendChild(mockTextarea);
|
||
|
||
const mockEmailInput = document.createElement('input');
|
||
mockEmailInput.id = 'troubleshootEmail';
|
||
mockModal.appendChild(mockEmailInput);
|
||
|
||
const mockRadio = document.createElement('input');
|
||
mockRadio.id = 'optTroubleshoot';
|
||
mockRadio.type = 'radio';
|
||
mockModal.appendChild(mockRadio);
|
||
|
||
const mockPanels = document.createElement('div');
|
||
mockPanels.className = 'allpanels';
|
||
mockPanels.id = 'troubleshoot_panel';
|
||
mockModal.appendChild(mockPanels);
|
||
|
||
document.body.appendChild(mockModal);
|
||
});
|
||
|
||
afterEach(() => {
|
||
document.body.innerHTML = '';
|
||
});
|
||
|
||
it('should open troubleshoot with error details', async () => {
|
||
store.setError(mockError);
|
||
await nextTick();
|
||
|
||
await store.openTroubleshoot({
|
||
email: 'test@example.com',
|
||
includeUnraidApiLogs: true,
|
||
});
|
||
|
||
const textarea = document.querySelector('#troubleshootDetails') as HTMLTextAreaElement;
|
||
const emailInput = document.querySelector('#troubleshootEmail') as HTMLInputElement;
|
||
const radio = document.querySelector('#optTroubleshoot') as HTMLInputElement;
|
||
const panel = document.querySelector('#troubleshoot_panel') as HTMLElement;
|
||
|
||
expect(mockFeedbackButton).toHaveBeenCalled();
|
||
expect(textarea.value).toContain('Debug Details – Component Errors 1');
|
||
expect(textarea.value).toContain('Error 1: Test Error');
|
||
expect(textarea.value).toContain('Error 1 Message: Test message');
|
||
expect(emailInput.value).toBe('test@example.com');
|
||
expect(radio.checked).toBe(true);
|
||
expect(panel.style.display).toBe('block');
|
||
});
|
||
});
|
||
});
|