2021-03-23 11:01:34 +02:00
|
|
|
import useOnInstallHandler from './useOnInstallHandler';
|
|
|
|
import { renderHook } from '@testing-library/react-hooks';
|
|
|
|
|
2024-03-09 13:03:57 +02:00
|
|
|
import PluginService, { defaultPluginSetting } from '../../../../services/plugins/PluginService';
|
|
|
|
import { ItemEvent } from './types';
|
2021-03-23 11:01:34 +02:00
|
|
|
|
2024-03-09 13:03:57 +02:00
|
|
|
jest.mock('../../../../services/plugins/PluginService');
|
2021-03-23 11:01:34 +02:00
|
|
|
|
|
|
|
const pluginServiceInstance = {
|
|
|
|
updatePluginFromRepo: jest.fn(),
|
|
|
|
installPluginFromRepo: jest.fn(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const pluginId = 'test.plugin';
|
|
|
|
const setInstallingPluginIds = jest.fn();
|
|
|
|
const repoApi = jest.fn();
|
|
|
|
const onPluginSettingsChange = jest.fn();
|
|
|
|
const itemEvent = ({
|
|
|
|
item: { manifest: { id: pluginId } },
|
|
|
|
} as ItemEvent);
|
2021-03-24 16:15:49 +02:00
|
|
|
const callHook = (isUpdate: boolean, pluginEnabled = true, pluginInstalledViaGUI = true) => () => useOnInstallHandler(
|
2021-03-23 11:01:34 +02:00
|
|
|
setInstallingPluginIds,
|
2024-06-14 20:36:44 +02:00
|
|
|
{ current: {
|
2021-03-24 16:15:49 +02:00
|
|
|
[pluginId]: pluginInstalledViaGUI ? {
|
2021-03-23 11:01:34 +02:00
|
|
|
enabled: pluginEnabled,
|
|
|
|
deleted: false,
|
|
|
|
hasBeenUpdated: false,
|
2021-03-24 16:15:49 +02:00
|
|
|
} : undefined,
|
2024-06-14 20:36:44 +02:00
|
|
|
} },
|
2021-03-23 11:01:34 +02:00
|
|
|
repoApi,
|
|
|
|
onPluginSettingsChange,
|
2023-08-22 12:58:53 +02:00
|
|
|
isUpdate,
|
2021-03-23 11:01:34 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
describe('useOnInstallHandler', () => {
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
(PluginService.instance as jest.Mock).mockReturnValue(pluginServiceInstance);
|
|
|
|
(defaultPluginSetting as jest.Mock).mockImplementation(
|
2024-03-09 13:03:57 +02:00
|
|
|
jest.requireActual('../../../../services/plugins/PluginService').defaultPluginSetting,
|
2021-03-23 11:01:34 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should report that the plugin is being updated', async () => {
|
|
|
|
const { result: { current: onUpdate } } = renderHook(callHook(true));
|
|
|
|
await onUpdate(itemEvent);
|
|
|
|
|
|
|
|
expect(setInstallingPluginIds).toHaveBeenCalledTimes(2);
|
|
|
|
expect(setInstallingPluginIds.mock.calls[0][0]({})).toMatchObject({ [pluginId]: true });
|
|
|
|
expect(setInstallingPluginIds.mock.calls[1][0]({})).toMatchObject({ [pluginId]: false });
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should update the plugin when there is an update', async () => {
|
|
|
|
const { result: { current: onUpdate } } = renderHook(callHook(true));
|
|
|
|
await onUpdate(itemEvent);
|
|
|
|
|
|
|
|
expect(pluginServiceInstance.updatePluginFromRepo).toHaveBeenCalledWith(undefined, pluginId);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should install the plugin when it is not yet installed', async () => {
|
|
|
|
const { result: { current: onInstall } } = renderHook(callHook(false));
|
|
|
|
await onInstall(itemEvent);
|
|
|
|
|
|
|
|
expect(pluginServiceInstance.installPluginFromRepo).toHaveBeenCalledWith(undefined, pluginId);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should preserve the enabled flag when plugin is updated', async () => {
|
|
|
|
const { result: { current: onUpdate } } = renderHook(callHook(true, false));
|
|
|
|
await onUpdate(itemEvent);
|
|
|
|
|
|
|
|
const newSettings = onPluginSettingsChange.mock.calls[0][0].value;
|
|
|
|
expect(newSettings[pluginId].enabled).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should indicate it when plugin has been updated', async () => {
|
|
|
|
const { result: { current: onUpdate } } = renderHook(callHook(true));
|
|
|
|
await onUpdate(itemEvent);
|
|
|
|
|
|
|
|
const newSettings = onPluginSettingsChange.mock.calls[0][0].value;
|
|
|
|
expect(newSettings[pluginId].hasBeenUpdated).toBe(true);
|
|
|
|
});
|
2021-03-24 16:15:49 +02:00
|
|
|
|
|
|
|
test('should not fail when plugin was not installed through the GUI', async () => {
|
|
|
|
const { result: { current: onUpdate } } = renderHook(callHook(true, true, false));
|
|
|
|
await onUpdate(itemEvent);
|
|
|
|
});
|
2021-03-23 11:01:34 +02:00
|
|
|
});
|