2024-07-26 13:35:50 +02:00
|
|
|
import * as React from 'react';
|
|
|
|
|
|
|
|
import { describe, it, beforeEach } from '@jest/globals';
|
2024-09-28 17:20:46 +02:00
|
|
|
import { act, fireEvent, render, screen, userEvent, waitFor } from '@testing-library/react-native';
|
2024-07-26 13:35:50 +02:00
|
|
|
import '@testing-library/jest-native/extend-expect';
|
|
|
|
import { Provider } from 'react-redux';
|
|
|
|
|
|
|
|
import NoteScreen from './Note';
|
|
|
|
import { MenuProvider } from 'react-native-popup-menu';
|
2024-10-11 23:14:18 +02:00
|
|
|
import { setupDatabaseAndSynchronizer, switchClient, simulateReadOnlyShareEnv, supportDir, synchronizerStart, resourceFetcher, runWithFakeTimers } from '@joplin/lib/testing/test-utils';
|
2024-09-28 17:20:46 +02:00
|
|
|
import { waitFor as waitForWithRealTimers } from '@joplin/lib/testing/test-utils';
|
2024-07-26 13:35:50 +02:00
|
|
|
import Note from '@joplin/lib/models/Note';
|
|
|
|
import { AppState } from '../../utils/types';
|
|
|
|
import { Store } from 'redux';
|
|
|
|
import createMockReduxStore from '../../utils/testing/createMockReduxStore';
|
|
|
|
import initializeCommandService from '../../utils/initializeCommandService';
|
|
|
|
import { PaperProvider } from 'react-native-paper';
|
|
|
|
import getWebViewDomById from '../../utils/testing/getWebViewDomById';
|
|
|
|
import { NoteEntity } from '@joplin/lib/services/database/types';
|
|
|
|
import Folder from '@joplin/lib/models/Folder';
|
|
|
|
import BaseItem from '@joplin/lib/models/BaseItem';
|
|
|
|
import { ModelType } from '@joplin/lib/BaseModel';
|
|
|
|
import ItemChange from '@joplin/lib/models/ItemChange';
|
|
|
|
import { getDisplayParentId } from '@joplin/lib/services/trash';
|
|
|
|
import { itemIsReadOnlySync, ItemSlice } from '@joplin/lib/models/utils/readOnly';
|
|
|
|
import { LayoutChangeEvent } from 'react-native';
|
2024-09-21 14:05:27 +02:00
|
|
|
import shim from '@joplin/lib/shim';
|
2024-09-27 16:23:02 +02:00
|
|
|
import getWebViewWindowById from '../../utils/testing/getWebViewWindowById';
|
|
|
|
import CodeMirrorControl from '@joplin/editor/CodeMirror/CodeMirrorControl';
|
2024-10-11 23:14:18 +02:00
|
|
|
import Setting from '@joplin/lib/models/Setting';
|
|
|
|
import Resource from '@joplin/lib/models/Resource';
|
2024-07-26 13:35:50 +02:00
|
|
|
|
|
|
|
interface WrapperProps {
|
|
|
|
}
|
|
|
|
|
|
|
|
let store: Store<AppState>;
|
|
|
|
|
|
|
|
const WrappedNoteScreen: React.FC<WrapperProps> = _props => {
|
|
|
|
return <MenuProvider>
|
|
|
|
<PaperProvider>
|
|
|
|
<Provider store={store}>
|
|
|
|
<NoteScreen />
|
|
|
|
</Provider>
|
|
|
|
</PaperProvider>
|
|
|
|
</MenuProvider>;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getNoteViewerDom = async () => {
|
|
|
|
return await getWebViewDomById('NoteBodyViewer');
|
|
|
|
};
|
|
|
|
|
2024-09-27 16:23:02 +02:00
|
|
|
const getNoteEditorControl = async () => {
|
|
|
|
const noteEditor = await getWebViewWindowById('NoteEditor');
|
|
|
|
const getEditorControl = () => {
|
|
|
|
if ('cm' in noteEditor.window && noteEditor.window.cm) {
|
|
|
|
return noteEditor.window.cm as CodeMirrorControl;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
await waitFor(async () => {
|
|
|
|
expect(getEditorControl()).toBeTruthy();
|
|
|
|
});
|
|
|
|
return getEditorControl();
|
|
|
|
};
|
|
|
|
|
|
|
|
const waitForNoteToMatch = async (noteId: string, note: Partial<NoteEntity>) => {
|
2024-09-28 17:20:46 +02:00
|
|
|
await act(() => waitForWithRealTimers(async () => {
|
2024-09-27 16:23:02 +02:00
|
|
|
const loadedNote = await Note.load(noteId);
|
|
|
|
expect(loadedNote).toMatchObject(note);
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2024-10-11 23:14:18 +02:00
|
|
|
const openExistingNote = async (noteId: string) => {
|
|
|
|
const note = await Note.load(noteId);
|
2024-07-26 13:35:50 +02:00
|
|
|
const displayParentId = getDisplayParentId(note, await Folder.load(note.parent_id));
|
|
|
|
|
|
|
|
store.dispatch({
|
|
|
|
type: 'NOTE_UPDATE_ALL',
|
|
|
|
notes: await Note.previews(displayParentId),
|
|
|
|
});
|
|
|
|
|
|
|
|
store.dispatch({
|
|
|
|
type: 'FOLDER_AND_NOTE_SELECT',
|
|
|
|
id: note.id,
|
|
|
|
folderId: displayParentId,
|
|
|
|
});
|
2024-10-11 23:14:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const openNewNote = async (noteProperties: NoteEntity) => {
|
|
|
|
const note = await Note.save({
|
|
|
|
parent_id: (await Folder.defaultFolder()).id,
|
|
|
|
...noteProperties,
|
|
|
|
});
|
2024-09-27 16:23:02 +02:00
|
|
|
|
2024-10-11 23:14:18 +02:00
|
|
|
await openExistingNote(note.id);
|
2024-09-27 16:23:02 +02:00
|
|
|
await waitForNoteToMatch(note.id, { parent_id: note.parent_id, title: note.title, body: note.body });
|
|
|
|
|
2024-07-26 13:35:50 +02:00
|
|
|
return note.id;
|
|
|
|
};
|
|
|
|
|
|
|
|
const openNoteActionsMenu = async () => {
|
|
|
|
// It doesn't seem possible to find the menu trigger with role/label.
|
|
|
|
const actionMenuButton = await screen.findByTestId('screen-header-menu-trigger');
|
|
|
|
|
|
|
|
// react-native-action-menu only shows the menu content after receiving onLayout
|
|
|
|
// events from various components (including a View that wraps the screen).
|
|
|
|
let cursor = actionMenuButton;
|
|
|
|
while (cursor.parent) {
|
|
|
|
if (cursor.props.onLayout) {
|
|
|
|
const mockedEvent = { nativeEvent: { layout: { x: 0, y: 0, width: 120, height: 100 } } };
|
|
|
|
cursor.props.onLayout(mockedEvent as LayoutChangeEvent);
|
|
|
|
}
|
|
|
|
cursor = cursor.parent;
|
|
|
|
}
|
|
|
|
|
2024-09-28 17:20:46 +02:00
|
|
|
// Wrap in act(...) -- this tells the test library that component state is intended to update (prevents
|
|
|
|
// warnings).
|
|
|
|
await act(async () => {
|
|
|
|
await runWithFakeTimers(async () => {
|
|
|
|
await userEvent.press(actionMenuButton);
|
|
|
|
});
|
|
|
|
|
|
|
|
// State can update until the menu content is marked as in the process of refocusing (part of the
|
|
|
|
// menu transition).
|
|
|
|
await waitFor(async () => {
|
|
|
|
expect(await screen.findByTestId('menu-content-refocusing')).toBeVisible();
|
|
|
|
});
|
|
|
|
});
|
2024-09-27 16:23:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const openEditor = async () => {
|
|
|
|
const editButton = await screen.findByLabelText('Edit');
|
2024-07-26 13:35:50 +02:00
|
|
|
|
2024-09-28 17:20:46 +02:00
|
|
|
fireEvent.press(editButton);
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(screen.queryByLabelText('Edit')).toBeNull();
|
2024-09-27 16:23:02 +02:00
|
|
|
});
|
2024-09-28 17:20:46 +02:00
|
|
|
};
|
2024-09-27 16:23:02 +02:00
|
|
|
|
2024-09-28 17:20:46 +02:00
|
|
|
describe('screens/Note', () => {
|
2024-07-26 13:35:50 +02:00
|
|
|
beforeEach(async () => {
|
2024-10-11 23:14:18 +02:00
|
|
|
await setupDatabaseAndSynchronizer(1);
|
2024-07-26 13:35:50 +02:00
|
|
|
await setupDatabaseAndSynchronizer(0);
|
|
|
|
await switchClient(0);
|
|
|
|
|
|
|
|
store = createMockReduxStore();
|
|
|
|
initializeCommandService(store);
|
|
|
|
|
|
|
|
// In order for note changes to be saved, note-screen-shared requires
|
|
|
|
// that at least one folder exist.
|
|
|
|
await Folder.save({ title: 'test', parent_id: '' });
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
screen.unmount();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show the currently selected note', async () => {
|
|
|
|
await openNewNote({ title: 'Test note (title)', body: '# Testing...' });
|
|
|
|
render(<WrappedNoteScreen />);
|
|
|
|
|
|
|
|
const titleInput = await screen.findByDisplayValue('Test note (title)');
|
|
|
|
expect(titleInput).toBeVisible();
|
|
|
|
|
|
|
|
const renderedNote = await getNoteViewerDom();
|
|
|
|
expect(renderedNote.querySelector('h1')).toMatchObject({ textContent: 'Testing...' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('changing the note title input should update the note\'s title', async () => {
|
|
|
|
const noteId = await openNewNote({ title: 'Change me!', body: 'Unchanged body' });
|
2024-09-27 16:23:02 +02:00
|
|
|
|
2024-07-26 13:35:50 +02:00
|
|
|
render(<WrappedNoteScreen />);
|
|
|
|
|
|
|
|
const titleInput = await screen.findByDisplayValue('Change me!');
|
|
|
|
|
2024-09-27 16:23:02 +02:00
|
|
|
const user = userEvent.setup();
|
|
|
|
await user.clear(titleInput);
|
|
|
|
await user.type(titleInput, 'New title');
|
|
|
|
|
|
|
|
await waitForNoteToMatch(noteId, { title: 'New title', body: 'Unchanged body' });
|
|
|
|
|
2024-09-28 17:20:46 +02:00
|
|
|
// Use fake timers to allow advancing timers without pausing the test
|
|
|
|
await runWithFakeTimers(async () => {
|
|
|
|
let expectedTitle = 'New title';
|
|
|
|
for (let i = 0; i <= 10; i++) {
|
|
|
|
for (const chunk of ['!', ' test', '!!!', ' Testing']) {
|
|
|
|
jest.advanceTimersByTime(i % 5);
|
|
|
|
await user.type(titleInput, chunk);
|
|
|
|
expectedTitle += chunk;
|
|
|
|
|
|
|
|
// Don't verify after each input event -- this allows the save action queue to fill.
|
|
|
|
if (i % 4 === 0) {
|
|
|
|
await waitForNoteToMatch(noteId, { title: expectedTitle });
|
|
|
|
}
|
2024-09-27 16:23:02 +02:00
|
|
|
}
|
2024-09-28 17:20:46 +02:00
|
|
|
await waitForNoteToMatch(noteId, { title: expectedTitle });
|
2024-09-27 16:23:02 +02:00
|
|
|
}
|
2024-09-28 17:20:46 +02:00
|
|
|
});
|
2024-09-27 16:23:02 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('changing the note body in the editor should update the note\'s body', async () => {
|
|
|
|
const defaultBody = 'Change me!';
|
|
|
|
const noteId = await openNewNote({ title: 'Unchanged title', body: defaultBody });
|
|
|
|
|
|
|
|
const noteScreen = render(<WrappedNoteScreen />);
|
2024-09-28 17:20:46 +02:00
|
|
|
await act(async () => await runWithFakeTimers(async () => {
|
|
|
|
await openEditor();
|
|
|
|
const editor = await getNoteEditorControl();
|
|
|
|
editor.select(defaultBody.length, defaultBody.length);
|
2024-09-27 16:23:02 +02:00
|
|
|
|
2024-09-28 17:20:46 +02:00
|
|
|
editor.insertText(' Testing!!!');
|
|
|
|
await waitForNoteToMatch(noteId, { body: 'Change me! Testing!!!' });
|
2024-09-27 16:23:02 +02:00
|
|
|
|
2024-09-28 17:20:46 +02:00
|
|
|
editor.insertText(' This is a test.');
|
|
|
|
await waitForNoteToMatch(noteId, { body: 'Change me! Testing!!! This is a test.' });
|
2024-09-27 16:23:02 +02:00
|
|
|
|
2024-09-28 17:20:46 +02:00
|
|
|
// should also save changes made shortly before unmounting
|
|
|
|
editor.insertText(' Test!');
|
2024-09-27 16:23:02 +02:00
|
|
|
|
2024-09-28 17:20:46 +02:00
|
|
|
// TODO: Decreasing this below 100 causes the test to fail.
|
|
|
|
// See issue #11125.
|
|
|
|
await jest.advanceTimersByTimeAsync(450);
|
2024-09-27 16:23:02 +02:00
|
|
|
|
2024-09-28 17:20:46 +02:00
|
|
|
noteScreen.unmount();
|
|
|
|
await waitForNoteToMatch(noteId, { body: 'Change me! Testing!!! This is a test. Test!' });
|
|
|
|
}));
|
2024-07-26 13:35:50 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('pressing "delete" should move the note to the trash', async () => {
|
|
|
|
const noteId = await openNewNote({ title: 'To be deleted', body: '...' });
|
|
|
|
render(<WrappedNoteScreen />);
|
|
|
|
|
|
|
|
await openNoteActionsMenu();
|
|
|
|
const deleteButton = await screen.findByText('Delete');
|
|
|
|
fireEvent.press(deleteButton);
|
|
|
|
|
2024-09-28 17:20:46 +02:00
|
|
|
await waitFor(async () => {
|
2024-07-26 13:35:50 +02:00
|
|
|
expect((await Note.load(noteId)).deleted_time).toBeGreaterThan(0);
|
2024-09-28 17:20:46 +02:00
|
|
|
});
|
2024-07-26 13:35:50 +02:00
|
|
|
});
|
|
|
|
|
2024-09-21 14:05:27 +02:00
|
|
|
it('pressing "delete permanently" should permanently delete a note', async () => {
|
|
|
|
const noteId = await openNewNote({ title: 'To be deleted', body: '...', deleted_time: Date.now() });
|
|
|
|
render(<WrappedNoteScreen />);
|
|
|
|
|
|
|
|
// Permanently delete note shows a confirmation dialog -- mock it.
|
|
|
|
const deleteId = 0;
|
|
|
|
shim.showMessageBox = jest.fn(async () => deleteId);
|
|
|
|
|
|
|
|
await openNoteActionsMenu();
|
|
|
|
const deleteButton = await screen.findByText('Permanently delete note');
|
|
|
|
fireEvent.press(deleteButton);
|
|
|
|
|
2024-09-28 17:20:46 +02:00
|
|
|
await waitFor(async () => {
|
2024-09-21 14:05:27 +02:00
|
|
|
expect(await Note.load(noteId)).toBeUndefined();
|
2024-09-28 17:20:46 +02:00
|
|
|
});
|
2024-09-21 14:05:27 +02:00
|
|
|
expect(shim.showMessageBox).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2024-07-26 13:35:50 +02:00
|
|
|
it('delete should be disabled in a read-only note', async () => {
|
|
|
|
const shareId = 'testShare';
|
|
|
|
const noteId = await openNewNote({
|
|
|
|
title: 'Title: Read-only note',
|
|
|
|
body: 'A **read-only** note.',
|
|
|
|
share_id: shareId,
|
|
|
|
});
|
|
|
|
const cleanup = simulateReadOnlyShareEnv(shareId, store);
|
|
|
|
expect(
|
|
|
|
itemIsReadOnlySync(
|
|
|
|
ModelType.Note,
|
|
|
|
ItemChange.SOURCE_UNSPECIFIED,
|
|
|
|
await Note.load(noteId) as ItemSlice,
|
|
|
|
'',
|
|
|
|
BaseItem.syncShareCache,
|
|
|
|
),
|
|
|
|
).toBe(true);
|
|
|
|
|
|
|
|
render(<WrappedNoteScreen />);
|
|
|
|
|
|
|
|
const titleInput = await screen.findByDisplayValue('Title: Read-only note');
|
|
|
|
expect(titleInput).toBeVisible();
|
|
|
|
expect(titleInput).toBeDisabled();
|
|
|
|
|
|
|
|
await openNoteActionsMenu();
|
|
|
|
const deleteButton = await screen.findByText('Delete');
|
|
|
|
expect(deleteButton).toBeDisabled();
|
|
|
|
|
|
|
|
cleanup();
|
|
|
|
});
|
2024-10-11 23:14:18 +02:00
|
|
|
|
|
|
|
it.each([
|
|
|
|
'auto',
|
|
|
|
'manual',
|
|
|
|
])('should correctly auto-download or not auto-download resources in %j mode', async (downloadMode) => {
|
|
|
|
let note = await Note.save({ title: 'Note 1', parent_id: (await Folder.defaultFolder()).id });
|
|
|
|
note = await shim.attachFileToNote(note, `${supportDir}/photo.jpg`);
|
|
|
|
|
|
|
|
await synchronizerStart();
|
|
|
|
await switchClient(1);
|
|
|
|
Setting.setValue('sync.resourceDownloadMode', downloadMode);
|
|
|
|
await synchronizerStart();
|
|
|
|
|
|
|
|
// Before opening the note, the resource should not be marked for download
|
|
|
|
const allResources = await Resource.all();
|
|
|
|
expect(allResources.length).toBe(1);
|
|
|
|
const resource = allResources[0];
|
|
|
|
expect(await Resource.localState(resource)).toMatchObject({ fetch_status: Resource.FETCH_STATUS_IDLE });
|
|
|
|
|
|
|
|
await openExistingNote(note.id);
|
|
|
|
|
|
|
|
render(<WrappedNoteScreen />);
|
|
|
|
|
|
|
|
// Note should render
|
|
|
|
const titleInput = await screen.findByDisplayValue('Note 1');
|
|
|
|
expect(titleInput).toBeVisible();
|
|
|
|
|
|
|
|
// Wrap in act() -- the component may update in the background during this.
|
|
|
|
await act(async () => {
|
|
|
|
await resourceFetcher().waitForAllFinished();
|
|
|
|
|
|
|
|
// After opening the note, the resource should be marked for download only in automatic mode
|
|
|
|
if (downloadMode === 'auto') {
|
|
|
|
await waitFor(async () => {
|
|
|
|
expect(await Resource.localState(resource.id)).toMatchObject({ fetch_status: Resource.FETCH_STATUS_DONE });
|
|
|
|
});
|
|
|
|
} else if (downloadMode === 'manual') {
|
|
|
|
// In manual mode, should not mark for download
|
|
|
|
expect(await Resource.localState(resource)).toMatchObject({ fetch_status: Resource.FETCH_STATUS_IDLE });
|
|
|
|
} else {
|
|
|
|
throw new Error(`Should not be testing downloadMode: ${downloadMode}.`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2024-07-26 13:35:50 +02:00
|
|
|
});
|