Dispatcharr/frontend/src/utils/pages/__tests__/PluginsUtils.test.js
Nick Sandstrom 93f74c9d91 Squashed commit of the following:
commit df18a89d0562edc8fd8fb5bc4cac702aefb5272c
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Sat Jan 10 19:18:23 2026 -0800

    Updated tests

commit 90240344b89717fbad0e16fe209dbf00c567b1a8
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Sun Jan 4 03:18:41 2026 -0800

    Updated tests

commit 525b7cb32bc8d235613706d6795795a0177ea24b
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Sun Jan 4 03:18:31 2026 -0800

    Extracted component and util logic

commit e54ea2c3173c0ce3cfb0a2d70d76fdd0a66accc8
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Wed Dec 31 11:55:40 2025 -0800

    Updated tests

commit 5cbe164cb9818d8eab607af037da5faee2c1556f
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Wed Dec 31 11:55:14 2025 -0800

    Minor changes

    Exporting UiSettingsForm as default
    Reverted admin level type check

commit f9ab0d2a06091a2eed3ee6f34268c81bfd746f1e
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Tue Dec 30 23:31:29 2025 -0800

    Extracted component and util logic

commit a705a4db4a32d0851d087a984111837a0a83f722
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Sun Dec 28 00:47:29 2025 -0800

    Updated tests

commit a72c6720a3980d0f279edf050b6b51eaae11cdbd
Merge: e8dcab6f 43525ca3
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Sun Dec 28 00:04:24 2025 -0800

    Merge branch 'enhancement/component-cleanup' into test/component-cleanup

commit e8dcab6f832570cb986f114cfa574db4994b3aab
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Sat Dec 27 22:35:59 2025 -0800

    Updated tests

commit 0fd230503844fba0c418ab0a03c46dc878697a55
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Sat Dec 27 22:35:53 2025 -0800

    Added plugins store

commit d987f2de72272f24e26b1ed5bc04bb5c83033868
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Sat Dec 27 22:35:43 2025 -0800

    Extracted component and util logic

commit 5a3138370a468a99c9f1ed0a36709a173656d809
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Wed Dec 24 23:13:07 2025 -0800

    Lazy-loading button modals

commit ac6945b5b55e0e16d050d4412a20c82f19250c4b
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Wed Dec 24 22:41:51 2025 -0800

    Extracted notification util

commit befe159fc06b67ee415f7498b5400fee0dc82528
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Wed Dec 24 22:28:12 2025 -0800

    Extracted component and util logic

commit ec10a3a4200a0c94cae29691a9fe06e5c4317bb7
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Wed Dec 24 22:22:09 2025 -0800

    Updated tests

commit c1c7214c8589c0ce7645ea24418d9dd978ac8c1f
Merge: eba6dce7 9c9cbab9
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Tue Dec 23 12:41:25 2025 -0800

    Merge branch 'enhancement/component-cleanup' into test/component-cleanup

commit eba6dce786495e352d4696030500db41d028036e
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Sun Dec 21 10:12:19 2025 -0800

    Updated style props

commit 2024b0b267b849a5f100e5543b9188e8ad6dd3d9
Merge: b3700956 1029eb5b
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Sun Dec 21 09:27:21 2025 -0800

    Merge branch 'enhancement/component-cleanup' into test/component-cleanup

commit b3700956a4c2f473f1e977826f9537d27ea018ae
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Thu Dec 18 07:45:36 2025 -0800

    Reverted Channels change

commit 137cbb02473b7f2f41488601e3b64e5ff45ac656
Merge: 644ed001 2a0df81c
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Wed Dec 17 13:36:05 2025 -0800

    Merge branch 'enhancement/component-cleanup' into test/component-cleanup

commit 644ed00196c41eaa44df1b98236b7e5cc3124d82
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Wed Dec 17 13:29:13 2025 -0800

    Updated tests

commit c62d1bd0534aa19be99b8f87232ba872420111a0
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Tue Dec 16 14:12:31 2025 -0800

    Updated tests

commit 0cc0ee31d5ad84c59d8eba9fc4424f118f5e0ee2
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Tue Dec 16 13:44:55 2025 -0800

    Extracted component and util logic

commit 25d1b112af250b5ccebb1006511bff8e4387fc76
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Tue Dec 16 13:44:11 2025 -0800

    Added correct import for Text component

commit d8a04c6c09edf158220d3073939c9fb60069745c
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Tue Dec 16 13:43:55 2025 -0800

    Fixed component syntax

commit 59e35d3a4d0da8ed8476560cedacadf76162ea43
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Tue Dec 16 13:43:39 2025 -0800

    Fixed cache_url fallback

commit d2a170d2efd3d2b0e6078c9eebeb8dcea237be3b
Merge: b8f7e435 6c1b0f9a
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Tue Dec 16 12:00:45 2025 -0800

    Merge branch 'enhancement/component-cleanup' into test/component-cleanup

commit b8f7e4358a23f2e3a902929b57ab7a7d115241c5
Merge: 5b12c68a d97f0c90
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Mon Dec 15 07:42:06 2025 -0800

    Merge branch 'enhancement/component-cleanup' into test/component-cleanup

commit 5b12c68ab8ce429adc8d1355632aa411007d365b
Merge: eff58126 c63cb75b
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Mon Dec 8 16:56:14 2025 -0800

    Merge branch 'enhancement/unit-tests' into stage

commit eff58126fb6aba4ebe9a0c67eee65773bffb8ae9
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Mon Dec 8 16:49:43 2025 -0800

    Update .gitignore

commit c63cb75b8cad204d48a392a28d8a5bdf8c270496
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Mon Dec 8 16:28:03 2025 -0800

    Added unit tests for pages

commit 75306a6181ddeb2eaeb306387ba2b44c7fcfd5e3
Author: Nick Sandstrom <32273437+nick4810@users.noreply.github.com>
Date:   Mon Dec 8 16:27:19 2025 -0800

    Added Actions workflow
2026-01-10 19:36:23 -08:00

269 lines
7.8 KiB
JavaScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
import * as PluginsUtils from '../PluginsUtils';
import API from '../../../api.js';
vi.mock('../../../api.js', () => ({
default: {
updatePluginSettings: vi.fn(),
runPluginAction: vi.fn(),
setPluginEnabled: vi.fn(),
importPlugin: vi.fn(),
deletePlugin: vi.fn()
}
}));
describe('PluginsUtils', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('updatePluginSettings', () => {
it('should call API updatePluginSettings with key and settings', async () => {
const key = 'test-plugin';
const settings = { option1: 'value1', option2: true };
await PluginsUtils.updatePluginSettings(key, settings);
expect(API.updatePluginSettings).toHaveBeenCalledWith(key, settings);
expect(API.updatePluginSettings).toHaveBeenCalledTimes(1);
});
it('should return API response', async () => {
const key = 'test-plugin';
const settings = { enabled: true };
const mockResponse = { success: true };
API.updatePluginSettings.mockResolvedValue(mockResponse);
const result = await PluginsUtils.updatePluginSettings(key, settings);
expect(result).toEqual(mockResponse);
});
it('should handle empty settings object', async () => {
const key = 'test-plugin';
const settings = {};
await PluginsUtils.updatePluginSettings(key, settings);
expect(API.updatePluginSettings).toHaveBeenCalledWith(key, {});
});
it('should handle null settings', async () => {
const key = 'test-plugin';
const settings = null;
await PluginsUtils.updatePluginSettings(key, settings);
expect(API.updatePluginSettings).toHaveBeenCalledWith(key, null);
});
it('should propagate API errors', async () => {
const key = 'test-plugin';
const settings = { enabled: true };
const error = new Error('API error');
API.updatePluginSettings.mockRejectedValue(error);
await expect(PluginsUtils.updatePluginSettings(key, settings)).rejects.toThrow('API error');
});
});
describe('runPluginAction', () => {
it('should call API runPluginAction with key and actionId', async () => {
const key = 'test-plugin';
const actionId = 'refresh-data';
await PluginsUtils.runPluginAction(key, actionId);
expect(API.runPluginAction).toHaveBeenCalledWith(key, actionId);
expect(API.runPluginAction).toHaveBeenCalledTimes(1);
});
it('should return API response', async () => {
const key = 'test-plugin';
const actionId = 'sync';
const mockResponse = { status: 'completed' };
API.runPluginAction.mockResolvedValue(mockResponse);
const result = await PluginsUtils.runPluginAction(key, actionId);
expect(result).toEqual(mockResponse);
});
it('should handle numeric actionId', async () => {
const key = 'test-plugin';
const actionId = 123;
await PluginsUtils.runPluginAction(key, actionId);
expect(API.runPluginAction).toHaveBeenCalledWith(key, 123);
});
it('should propagate API errors', async () => {
const key = 'test-plugin';
const actionId = 'invalid-action';
const error = new Error('Action not found');
API.runPluginAction.mockRejectedValue(error);
await expect(PluginsUtils.runPluginAction(key, actionId)).rejects.toThrow('Action not found');
});
});
describe('setPluginEnabled', () => {
it('should call API setPluginEnabled with key and next value', async () => {
const key = 'test-plugin';
const next = true;
await PluginsUtils.setPluginEnabled(key, next);
expect(API.setPluginEnabled).toHaveBeenCalledWith(key, true);
expect(API.setPluginEnabled).toHaveBeenCalledTimes(1);
});
it('should handle false value', async () => {
const key = 'test-plugin';
const next = false;
await PluginsUtils.setPluginEnabled(key, next);
expect(API.setPluginEnabled).toHaveBeenCalledWith(key, false);
});
it('should return API response', async () => {
const key = 'test-plugin';
const next = true;
const mockResponse = { enabled: true };
API.setPluginEnabled.mockResolvedValue(mockResponse);
const result = await PluginsUtils.setPluginEnabled(key, next);
expect(result).toEqual(mockResponse);
});
it('should handle truthy values', async () => {
const key = 'test-plugin';
const next = 'yes';
await PluginsUtils.setPluginEnabled(key, next);
expect(API.setPluginEnabled).toHaveBeenCalledWith(key, 'yes');
});
it('should handle falsy values', async () => {
const key = 'test-plugin';
const next = 0;
await PluginsUtils.setPluginEnabled(key, next);
expect(API.setPluginEnabled).toHaveBeenCalledWith(key, 0);
});
it('should propagate API errors', async () => {
const key = 'test-plugin';
const next = true;
const error = new Error('Plugin not found');
API.setPluginEnabled.mockRejectedValue(error);
await expect(PluginsUtils.setPluginEnabled(key, next)).rejects.toThrow('Plugin not found');
});
});
describe('importPlugin', () => {
it('should call API importPlugin with importFile', async () => {
const importFile = new File(['content'], 'plugin.zip', { type: 'application/zip' });
await PluginsUtils.importPlugin(importFile);
expect(API.importPlugin).toHaveBeenCalledWith(importFile);
expect(API.importPlugin).toHaveBeenCalledTimes(1);
});
it('should return API response', async () => {
const importFile = new File(['content'], 'plugin.zip', { type: 'application/zip' });
const mockResponse = { key: 'imported-plugin', success: true };
API.importPlugin.mockResolvedValue(mockResponse);
const result = await PluginsUtils.importPlugin(importFile);
expect(result).toEqual(mockResponse);
});
it('should handle string file path', async () => {
const importFile = '/path/to/plugin.zip';
await PluginsUtils.importPlugin(importFile);
expect(API.importPlugin).toHaveBeenCalledWith(importFile);
});
it('should handle FormData', async () => {
const formData = new FormData();
formData.append('file', new File(['content'], 'plugin.zip'));
await PluginsUtils.importPlugin(formData);
expect(API.importPlugin).toHaveBeenCalledWith(formData);
});
it('should propagate API errors', async () => {
const importFile = new File(['content'], 'plugin.zip', { type: 'application/zip' });
const error = new Error('Invalid plugin format');
API.importPlugin.mockRejectedValue(error);
await expect(PluginsUtils.importPlugin(importFile)).rejects.toThrow('Invalid plugin format');
});
});
describe('deletePluginByKey', () => {
it('should call API deletePlugin with key', () => {
const key = 'test-plugin';
PluginsUtils.deletePluginByKey(key);
expect(API.deletePlugin).toHaveBeenCalledWith(key);
expect(API.deletePlugin).toHaveBeenCalledTimes(1);
});
it('should return API response', () => {
const key = 'test-plugin';
const mockResponse = { success: true };
API.deletePlugin.mockReturnValue(mockResponse);
const result = PluginsUtils.deletePluginByKey(key);
expect(result).toEqual(mockResponse);
});
it('should handle numeric key', () => {
const key = 123;
PluginsUtils.deletePluginByKey(key);
expect(API.deletePlugin).toHaveBeenCalledWith(123);
});
it('should handle empty string key', () => {
const key = '';
PluginsUtils.deletePluginByKey(key);
expect(API.deletePlugin).toHaveBeenCalledWith('');
});
it('should handle null key', () => {
const key = null;
PluginsUtils.deletePluginByKey(key);
expect(API.deletePlugin).toHaveBeenCalledWith(null);
});
});
});