2023-08-18 10:31:45 +02:00
|
|
|
import Note from '@joplin/lib/models/Note';
|
|
|
|
import { setupDatabaseAndSynchronizer, switchClient } from '@joplin/lib/testing/test-utils';
|
2024-05-28 12:30:56 +02:00
|
|
|
import { act, renderHook } from '@testing-library/react-hooks';
|
2023-09-24 21:03:00 +02:00
|
|
|
import useFormNote, { HookDependencies } from './useFormNote';
|
2024-05-28 12:30:56 +02:00
|
|
|
import shim from '@joplin/lib/shim';
|
|
|
|
import Resource from '@joplin/lib/models/Resource';
|
2023-08-18 10:31:45 +02:00
|
|
|
|
2023-09-23 18:50:24 +02:00
|
|
|
const defaultFormNoteProps: HookDependencies = {
|
|
|
|
syncStarted: false,
|
|
|
|
decryptionStarted: false,
|
|
|
|
noteId: '',
|
|
|
|
isProvisional: false,
|
|
|
|
titleInputRef: null,
|
|
|
|
editorRef: null,
|
|
|
|
onBeforeLoad: ()=>{},
|
|
|
|
onAfterLoad: ()=>{},
|
|
|
|
};
|
2023-08-18 10:31:45 +02:00
|
|
|
|
|
|
|
describe('useFormNote', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await setupDatabaseAndSynchronizer(1);
|
|
|
|
await switchClient(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update note when decryption completes', async () => {
|
|
|
|
const testNote = await Note.save({ title: 'Test Note!' });
|
|
|
|
|
|
|
|
const makeFormNoteProps = (syncStarted: boolean, decryptionStarted: boolean): HookDependencies => {
|
|
|
|
return {
|
2023-09-23 18:50:24 +02:00
|
|
|
...defaultFormNoteProps,
|
2023-08-18 10:31:45 +02:00
|
|
|
syncStarted,
|
|
|
|
decryptionStarted,
|
|
|
|
noteId: testNote.id,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const formNote = renderHook(props => useFormNote(props), {
|
|
|
|
initialProps: makeFormNoteProps(true, false),
|
|
|
|
});
|
|
|
|
await formNote.waitFor(() => {
|
|
|
|
expect(formNote.result.current.formNote).toMatchObject({
|
|
|
|
encryption_applied: 0,
|
|
|
|
title: testNote.title,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
await Note.save({
|
|
|
|
id: testNote.id,
|
|
|
|
encryption_cipher_text: 'cipher_text',
|
|
|
|
encryption_applied: 1,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Sync starting should cause a re-render
|
|
|
|
formNote.rerender(makeFormNoteProps(false, false));
|
|
|
|
|
|
|
|
await formNote.waitFor(() => {
|
|
|
|
expect(formNote.result.current.formNote).toMatchObject({
|
|
|
|
encryption_applied: 1,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
formNote.rerender(makeFormNoteProps(false, true));
|
|
|
|
|
|
|
|
await Note.save({
|
|
|
|
id: testNote.id,
|
|
|
|
encryption_applied: 0,
|
|
|
|
title: 'Test Note!',
|
|
|
|
});
|
|
|
|
|
|
|
|
// Ending decryption should also cause a re-render
|
|
|
|
formNote.rerender(makeFormNoteProps(false, false));
|
|
|
|
|
|
|
|
await formNote.waitFor(() => {
|
|
|
|
expect(formNote.result.current.formNote).toMatchObject({
|
|
|
|
encryption_applied: 0,
|
|
|
|
title: 'Test Note!',
|
|
|
|
});
|
|
|
|
});
|
2024-05-28 12:30:56 +02:00
|
|
|
|
|
|
|
formNote.unmount();
|
2023-08-18 10:31:45 +02:00
|
|
|
});
|
2023-09-23 18:50:24 +02:00
|
|
|
|
2023-09-24 21:03:00 +02:00
|
|
|
// It seems this test is crashing the worker on CI (out of memory), so disabling it for now.
|
2023-09-23 18:50:24 +02:00
|
|
|
|
2023-09-24 21:03:00 +02:00
|
|
|
// it('should reload the note when it is changed outside of the editor', async () => {
|
|
|
|
// const note = await Note.save({ title: 'Test Note!' });
|
2023-09-23 18:50:24 +02:00
|
|
|
|
2023-09-24 21:03:00 +02:00
|
|
|
// const makeFormNoteProps = (dbNote: DbNote): HookDependencies => {
|
|
|
|
// return {
|
|
|
|
// ...defaultFormNoteProps,
|
|
|
|
// noteId: note.id,
|
|
|
|
// dbNote,
|
|
|
|
// };
|
|
|
|
// };
|
2023-09-23 18:50:24 +02:00
|
|
|
|
2023-09-24 21:03:00 +02:00
|
|
|
// const formNote = renderHook(props => useFormNote(props), {
|
|
|
|
// initialProps: makeFormNoteProps({ id: note.id, updated_time: note.updated_time }),
|
|
|
|
// });
|
2023-09-23 18:50:24 +02:00
|
|
|
|
2023-09-24 21:03:00 +02:00
|
|
|
// await formNote.waitFor(() => {
|
|
|
|
// expect(formNote.result.current.formNote.title).toBe('Test Note!');
|
|
|
|
// });
|
2023-09-23 18:50:24 +02:00
|
|
|
|
2023-09-24 21:03:00 +02:00
|
|
|
// // Simulate the note being modified outside the editor
|
|
|
|
// const modifiedNote = await Note.save({ id: note.id, title: 'Modified' });
|
2023-09-23 18:50:24 +02:00
|
|
|
|
2023-09-24 21:03:00 +02:00
|
|
|
// // NoteEditor then would update `dbNote`
|
|
|
|
// formNote.rerender(makeFormNoteProps({ id: note.id, updated_time: modifiedNote.updated_time }));
|
|
|
|
|
|
|
|
// await formNote.waitFor(() => {
|
|
|
|
// expect(formNote.result.current.formNote.title).toBe('Modified');
|
|
|
|
// });
|
|
|
|
// });
|
2023-09-23 18:50:24 +02:00
|
|
|
|
2024-05-28 12:30:56 +02:00
|
|
|
test('should refresh resource infos when changed outside the editor', async () => {
|
|
|
|
let note = await Note.save({});
|
|
|
|
note = await shim.attachFileToNote(note, __filename);
|
|
|
|
const resourceIds = Note.linkedItemIds(note.body);
|
|
|
|
const resource = await Resource.load(resourceIds[0]);
|
|
|
|
|
|
|
|
const makeFormNoteProps = (syncStarted: boolean, decryptionStarted: boolean): HookDependencies => {
|
|
|
|
return {
|
|
|
|
...defaultFormNoteProps,
|
|
|
|
syncStarted,
|
|
|
|
decryptionStarted,
|
|
|
|
noteId: note.id,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const formNote = renderHook(props => useFormNote(props), {
|
|
|
|
initialProps: makeFormNoteProps(true, false),
|
|
|
|
});
|
|
|
|
|
|
|
|
await formNote.waitFor(() => {
|
|
|
|
return Object.values(formNote.result.current.resourceInfos).length > 0;
|
|
|
|
});
|
|
|
|
const initialResourceInfos = formNote.result.current.resourceInfos;
|
|
|
|
expect(initialResourceInfos).toMatchObject({
|
|
|
|
[resource.id]: { item: { id: resource.id } },
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
await Resource.save({ ...resource, filename: 'test.ts' });
|
|
|
|
});
|
|
|
|
await formNote.waitFor(() => {
|
|
|
|
const resourceInfo = formNote.result.current.resourceInfos[resource.id];
|
|
|
|
expect(resourceInfo.item).toMatchObject({
|
|
|
|
id: resource.id, filename: 'test.ts',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
formNote.unmount();
|
|
|
|
});
|
2023-08-18 10:31:45 +02:00
|
|
|
});
|