2024-03-29 14:40:54 +02:00
|
|
|
import * as React from 'react';
|
|
|
|
import RepositoryApi from '@joplin/lib/services/plugins/RepositoryApi';
|
2024-05-02 18:05:25 +02:00
|
|
|
import { createTempDir, mockMobilePlatform, setupDatabaseAndSynchronizer, supportDir, switchClient } from '@joplin/lib/testing/test-utils';
|
2024-03-29 14:40:54 +02:00
|
|
|
|
2024-05-02 18:05:25 +02:00
|
|
|
import { act, render, screen } from '@testing-library/react-native';
|
2024-03-29 14:40:54 +02:00
|
|
|
import '@testing-library/react-native/extend-expect';
|
|
|
|
|
|
|
|
import Setting from '@joplin/lib/models/Setting';
|
|
|
|
import PluginService, { PluginSettings, defaultPluginSetting } from '@joplin/lib/services/plugins/PluginService';
|
|
|
|
import { useCallback, useState } from 'react';
|
|
|
|
import pluginServiceSetup from './testUtils/pluginServiceSetup';
|
|
|
|
import PluginStates from './PluginStates';
|
|
|
|
import configScreenStyles from '../configScreenStyles';
|
|
|
|
import { remove, writeFile } from 'fs-extra';
|
|
|
|
import { join } from 'path';
|
|
|
|
import shim from '@joplin/lib/shim';
|
|
|
|
import { resetRepoApi } from './utils/useRepoApi';
|
2024-05-02 18:05:25 +02:00
|
|
|
import { Store } from 'redux';
|
|
|
|
import { AppState } from '../../../../utils/types';
|
|
|
|
import createMockReduxStore from '../../../../utils/testing/createMockReduxStore';
|
2024-03-29 14:40:54 +02:00
|
|
|
|
|
|
|
interface WrapperProps {
|
|
|
|
initialPluginSettings: PluginSettings;
|
|
|
|
}
|
|
|
|
|
2024-05-02 18:05:25 +02:00
|
|
|
let reduxStore: Store<AppState> = null;
|
|
|
|
|
2024-03-29 14:40:54 +02:00
|
|
|
const shouldShowBasedOnSettingSearchQuery = ()=>true;
|
|
|
|
const PluginStatesWrapper = (props: WrapperProps) => {
|
|
|
|
const styles = configScreenStyles(Setting.THEME_LIGHT);
|
|
|
|
|
2024-04-27 12:45:39 +02:00
|
|
|
const [pluginSettings, setPluginSettings] = useState(() => {
|
|
|
|
return props.initialPluginSettings ?? {};
|
2024-03-29 14:40:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const updatePluginStates = useCallback((newStates: PluginSettings) => {
|
2024-04-27 12:45:39 +02:00
|
|
|
setPluginSettings(newStates);
|
2024-03-29 14:40:54 +02:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PluginStates
|
|
|
|
styles={styles}
|
2024-05-02 18:05:25 +02:00
|
|
|
themeId={Setting.THEME_LIGHT}
|
2024-03-29 14:40:54 +02:00
|
|
|
updatePluginStates={updatePluginStates}
|
2024-04-27 12:45:39 +02:00
|
|
|
pluginSettings={pluginSettings}
|
2024-03-29 14:40:54 +02:00
|
|
|
shouldShowBasedOnSearchQuery={shouldShowBasedOnSettingSearchQuery}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
let repoTempDir: string|null = null;
|
|
|
|
const mockRepositoryApiConstructor = async () => {
|
|
|
|
if (repoTempDir) {
|
|
|
|
await remove(repoTempDir);
|
|
|
|
}
|
|
|
|
repoTempDir = await createTempDir();
|
|
|
|
|
2024-04-03 19:51:09 +02:00
|
|
|
RepositoryApi.ofDefaultJoplinRepo = jest.fn((_tempDirPath: string, appType, installMode) => {
|
|
|
|
return new RepositoryApi(`${supportDir}/pluginRepo`, repoTempDir, appType, installMode);
|
2024-03-29 14:40:54 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const loadMockPlugin = async (id: string, name: string, version: string, pluginSettings: PluginSettings) => {
|
|
|
|
const service = PluginService.instance();
|
|
|
|
const pluginSource = `
|
|
|
|
/* joplin-manifest:
|
|
|
|
${JSON.stringify({
|
|
|
|
id,
|
|
|
|
manifest_version: 1,
|
|
|
|
app_min_version: '1.4',
|
|
|
|
name,
|
|
|
|
description: 'Test plugin',
|
2024-05-02 18:05:25 +02:00
|
|
|
platforms: ['mobile', 'desktop'],
|
2024-03-29 14:40:54 +02:00
|
|
|
version,
|
|
|
|
homepage_url: 'https://joplinapp.org',
|
|
|
|
})}
|
|
|
|
*/
|
|
|
|
|
|
|
|
joplin.plugins.register({
|
|
|
|
onStart: async function() { },
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
const pluginPath = join(await createTempDir(), 'plugin.js');
|
|
|
|
await writeFile(pluginPath, pluginSource, 'utf-8');
|
2024-05-02 18:05:25 +02:00
|
|
|
await act(async () => {
|
|
|
|
await service.loadAndRunPlugins([pluginPath], pluginSettings);
|
|
|
|
});
|
2024-03-29 14:40:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
describe('PluginStates', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await setupDatabaseAndSynchronizer(0);
|
|
|
|
await switchClient(0);
|
2024-05-02 18:05:25 +02:00
|
|
|
reduxStore = createMockReduxStore();
|
|
|
|
pluginServiceSetup(reduxStore);
|
2024-03-29 14:40:54 +02:00
|
|
|
resetRepoApi();
|
2024-04-10 12:39:18 +02:00
|
|
|
|
|
|
|
await mockMobilePlatform('android');
|
|
|
|
await mockRepositoryApiConstructor();
|
2024-03-29 14:40:54 +02:00
|
|
|
});
|
|
|
|
afterEach(async () => {
|
|
|
|
for (const pluginId of PluginService.instance().pluginIds) {
|
2024-05-02 18:05:25 +02:00
|
|
|
await act(() => PluginService.instance().unloadPlugin(pluginId));
|
2024-03-29 14:40:54 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
'android',
|
|
|
|
'ios',
|
|
|
|
])('should not allow updating a plugin that is not recommended on iOS, but should on Android (on %s)', async (platform) => {
|
|
|
|
await mockMobilePlatform(platform);
|
|
|
|
expect(shim.mobilePlatform()).toBe(platform);
|
|
|
|
await mockRepositoryApiConstructor();
|
|
|
|
|
|
|
|
const abcPluginId = 'org.joplinapp.plugins.AbcSheetMusic';
|
|
|
|
const backlinksPluginId = 'joplin.plugin.ambrt.backlinksToNote';
|
|
|
|
|
|
|
|
const defaultPluginSettings: PluginSettings = {
|
|
|
|
[abcPluginId]: defaultPluginSetting(),
|
|
|
|
[backlinksPluginId]: defaultPluginSetting(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Load an outdated recommended plugin
|
|
|
|
await loadMockPlugin(abcPluginId, 'ABC Sheet Music', '0.0.1', defaultPluginSettings);
|
|
|
|
expect(PluginService.instance().plugins[abcPluginId]).toBeTruthy();
|
|
|
|
|
|
|
|
// Load a plugin not marked as recommended
|
|
|
|
await loadMockPlugin(backlinksPluginId, 'Backlinks to note', '0.0.1', defaultPluginSettings);
|
|
|
|
expect(PluginService.instance().plugins[backlinksPluginId]).toBeTruthy();
|
|
|
|
|
|
|
|
render(
|
|
|
|
<PluginStatesWrapper
|
|
|
|
initialPluginSettings={defaultPluginSettings}
|
|
|
|
/>,
|
|
|
|
);
|
2024-04-11 09:37:20 +02:00
|
|
|
expect(await screen.findByText(/^ABC Sheet Music/)).toBeVisible();
|
|
|
|
expect(await screen.findByText(/^Backlinks to note/)).toBeVisible();
|
2024-03-29 14:40:54 +02:00
|
|
|
|
2024-04-08 13:36:40 +02:00
|
|
|
expect(await screen.findByRole('button', { name: 'Update ABC Sheet Music', disabled: false })).toBeVisible();
|
2024-03-29 14:40:54 +02:00
|
|
|
|
2024-04-08 13:36:40 +02:00
|
|
|
// Backlinks to note should not be updatable on iOS (it's not _recommended).
|
|
|
|
const backlinksToNoteQuery = { name: 'Update Backlinks to note', disabled: false };
|
|
|
|
if (platform === 'android') {
|
|
|
|
expect(await screen.findByRole('button', backlinksToNoteQuery)).toBeVisible();
|
|
|
|
} else {
|
|
|
|
expect(await screen.queryByRole('button', backlinksToNoteQuery)).toBeNull();
|
2024-03-29 14:40:54 +02:00
|
|
|
}
|
|
|
|
});
|
2024-04-10 12:39:18 +02:00
|
|
|
|
|
|
|
it('should show the current plugin version on updatable plugins', async () => {
|
|
|
|
const abcPluginId = 'org.joplinapp.plugins.AbcSheetMusic';
|
|
|
|
const defaultPluginSettings: PluginSettings = { [abcPluginId]: defaultPluginSetting() };
|
|
|
|
|
|
|
|
const outdatedVersion = '0.0.1';
|
|
|
|
await loadMockPlugin(abcPluginId, 'ABC Sheet Music', outdatedVersion, defaultPluginSettings);
|
|
|
|
expect(PluginService.instance().plugins[abcPluginId]).toBeTruthy();
|
|
|
|
|
|
|
|
render(
|
|
|
|
<PluginStatesWrapper
|
|
|
|
initialPluginSettings={defaultPluginSettings}
|
|
|
|
/>,
|
|
|
|
);
|
2024-04-11 09:37:20 +02:00
|
|
|
expect(await screen.findByText(/^ABC Sheet Music/)).toBeVisible();
|
2024-04-10 12:39:18 +02:00
|
|
|
expect(await screen.findByRole('button', { name: 'Update ABC Sheet Music', disabled: false })).toBeVisible();
|
|
|
|
expect(await screen.findByText(`v${outdatedVersion}`)).toBeVisible();
|
|
|
|
});
|
2024-05-02 18:05:25 +02:00
|
|
|
|
|
|
|
it('should update the list of installed plugins when a plugin is installed and uninstalled', async () => {
|
|
|
|
const pluginSettings: PluginSettings = { };
|
|
|
|
|
|
|
|
render(
|
|
|
|
<PluginStatesWrapper
|
|
|
|
initialPluginSettings={pluginSettings}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Initially, no plugins should be visible.
|
|
|
|
expect(screen.queryByText(/^ABC Sheet Music/)).toBeNull();
|
|
|
|
|
|
|
|
const testPluginId1 = 'org.joplinapp.plugins.AbcSheetMusic';
|
|
|
|
const testPluginId2 = 'org.joplinapp.plugins.test.plugin.id';
|
|
|
|
await act(() => loadMockPlugin(testPluginId1, 'ABC Sheet Music', '1.2.3', pluginSettings));
|
|
|
|
await act(() => loadMockPlugin(testPluginId2, 'A test plugin', '1.0.0', pluginSettings));
|
|
|
|
expect(PluginService.instance().plugins[testPluginId1]).toBeTruthy();
|
|
|
|
|
|
|
|
// Should update the list of installed plugins even though the plugin settings didn't change.
|
|
|
|
expect(await screen.findByText(/^ABC Sheet Music/)).toBeVisible();
|
|
|
|
expect(await screen.findByText(/^A test plugin/)).toBeVisible();
|
|
|
|
|
|
|
|
// Uninstalling one plugin should keep the other in the list
|
|
|
|
await act(() => PluginService.instance().uninstallPlugin(testPluginId1));
|
|
|
|
expect(await screen.findByText(/^A test plugin/)).toBeVisible();
|
|
|
|
expect(screen.queryByText(/^ABC Sheet Music/)).toBeNull();
|
|
|
|
});
|
2024-03-29 14:40:54 +02:00
|
|
|
});
|