2021-01-04 18:49:59 +02:00
|
|
|
import Setting from '@joplin/lib/models/Setting';
|
2020-12-01 16:08:41 +02:00
|
|
|
import PluginService from '@joplin/lib/services/plugins/PluginService';
|
2021-02-08 17:36:36 +02:00
|
|
|
import { newPluginService, newPluginScript, setupDatabaseAndSynchronizer, switchClient, afterEachCleanUp } from '../../../test-utils';
|
2021-01-22 19:41:11 +02:00
|
|
|
import Note from '@joplin/lib/models/Note';
|
|
|
|
import Folder from '@joplin/lib/models/Folder';
|
|
|
|
import ItemChange from '@joplin/lib/models/ItemChange';
|
2020-12-01 16:08:41 +02:00
|
|
|
|
|
|
|
describe('JoplinWorkspace', () => {
|
|
|
|
|
|
|
|
beforeEach(async (done) => {
|
|
|
|
await setupDatabaseAndSynchronizer(1);
|
|
|
|
await switchClient(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
await afterEachCleanUp();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should listen to noteChange events', async () => {
|
2021-02-08 17:36:36 +02:00
|
|
|
const appState: Record<string, any> = {
|
|
|
|
selectedNoteIds: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
const service = newPluginService('1.4', {
|
|
|
|
getState: () => {
|
|
|
|
return appState;
|
|
|
|
},
|
|
|
|
});
|
2020-12-01 16:08:41 +02:00
|
|
|
|
|
|
|
const pluginScript = newPluginScript(`
|
|
|
|
joplin.plugins.register({
|
|
|
|
onStart: async function() {
|
|
|
|
await joplin.workspace.onNoteChange(async (event) => {
|
|
|
|
await joplin.data.post(['folders'], null, { title: JSON.stringify(event) });
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
`);
|
|
|
|
|
|
|
|
const note = await Note.save({});
|
2021-02-08 17:36:36 +02:00
|
|
|
appState.selectedNoteIds.push(note.id);
|
2020-12-01 16:08:41 +02:00
|
|
|
await ItemChange.waitForAllSaved();
|
|
|
|
|
|
|
|
const plugin = await service.loadPluginFromJsBundle('', pluginScript);
|
|
|
|
await service.runPlugin(plugin);
|
|
|
|
|
|
|
|
await Note.save({ id: note.id, body: 'testing' });
|
|
|
|
await ItemChange.waitForAllSaved();
|
|
|
|
|
|
|
|
const folder = (await Folder.all())[0];
|
|
|
|
|
|
|
|
const result: any = JSON.parse(folder.title);
|
|
|
|
|
|
|
|
expect(result.id).toBe(note.id);
|
|
|
|
expect(result.event).toBe(ItemChange.TYPE_UPDATE);
|
|
|
|
|
|
|
|
await service.destroy();
|
|
|
|
});
|
|
|
|
|
2021-01-04 18:49:59 +02:00
|
|
|
test('should return the selected folder', async () => {
|
|
|
|
const service = new newPluginService() as PluginService;
|
|
|
|
|
|
|
|
const pluginScript = newPluginScript(`
|
|
|
|
joplin.plugins.register({
|
|
|
|
onStart: async function() {
|
|
|
|
const folder = await joplin.workspace.selectedFolder();
|
|
|
|
await joplin.data.put(['folders', folder.id], null, { title: "changedtitle" });
|
|
|
|
},
|
|
|
|
});
|
|
|
|
`);
|
|
|
|
|
|
|
|
const folder = await Folder.save({ title: 'folder' });
|
|
|
|
Setting.setValue('activeFolderId', folder.id);
|
|
|
|
|
|
|
|
const plugin = await service.loadPluginFromJsBundle('', pluginScript);
|
|
|
|
await service.runPlugin(plugin);
|
|
|
|
|
|
|
|
const modFolder = await Folder.load(folder.id);
|
|
|
|
expect(modFolder.title).toBe('changedtitle');
|
|
|
|
});
|
|
|
|
|
2020-12-01 16:08:41 +02:00
|
|
|
});
|