2023-08-18 10:45:04 +02:00
|
|
|
import * as React from 'react';
|
|
|
|
|
|
|
|
import { describe, it, expect, beforeEach } from '@jest/globals';
|
|
|
|
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react-native';
|
|
|
|
import '@testing-library/jest-native';
|
|
|
|
|
|
|
|
import NoteEditor from './NoteEditor';
|
|
|
|
import Setting from '@joplin/lib/models/Setting';
|
|
|
|
import { _ } from '@joplin/lib/locale';
|
|
|
|
import { setupDatabaseAndSynchronizer, switchClient } from '@joplin/lib/testing/test-utils';
|
2024-03-11 17:02:15 +02:00
|
|
|
import commandDeclarations from './commandDeclarations';
|
2024-12-11 14:31:05 +02:00
|
|
|
import CommandService, { RegisteredRuntime } from '@joplin/lib/services/CommandService';
|
|
|
|
import TestProviderStack from '../testing/TestProviderStack';
|
|
|
|
import createMockReduxStore from '../../utils/testing/createMockReduxStore';
|
|
|
|
import mockCommandRuntimes from '../EditorToolbar/testing/mockCommandRuntimes';
|
|
|
|
import setupGlobalStore from '../../utils/testing/setupGlobalStore';
|
|
|
|
import { Store } from 'redux';
|
|
|
|
import { AppState } from '../../utils/types';
|
|
|
|
|
|
|
|
let store: Store<AppState>;
|
|
|
|
let registeredRuntime: RegisteredRuntime;
|
2023-08-18 10:45:04 +02:00
|
|
|
|
|
|
|
describe('NoteEditor', () => {
|
2024-03-11 17:02:15 +02:00
|
|
|
beforeAll(() => {
|
|
|
|
// This allows the NoteEditor test to register editor commands without errors.
|
|
|
|
for (const declaration of commandDeclarations) {
|
|
|
|
CommandService.instance().registerDeclaration(declaration);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-08-18 10:45:04 +02:00
|
|
|
beforeEach(async () => {
|
|
|
|
// Required to use ExtendedWebView
|
|
|
|
await setupDatabaseAndSynchronizer(0);
|
|
|
|
await switchClient(0);
|
2024-12-11 14:31:05 +02:00
|
|
|
|
|
|
|
store = createMockReduxStore();
|
|
|
|
setupGlobalStore(store);
|
|
|
|
registeredRuntime = mockCommandRuntimes(store);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
registeredRuntime.deregister();
|
2023-08-18 10:45:04 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should hide the markdown toolbar when the window is small', async () => {
|
|
|
|
const wrappedNoteEditor = render(
|
2024-12-11 14:31:05 +02:00
|
|
|
<TestProviderStack store={store}>
|
2023-08-18 10:45:04 +02:00
|
|
|
<NoteEditor
|
|
|
|
themeId={Setting.THEME_ARITIM_DARK}
|
|
|
|
initialText='Testing...'
|
|
|
|
style={{}}
|
|
|
|
toolbarEnabled={true}
|
|
|
|
readOnly={false}
|
|
|
|
onChange={()=>{}}
|
|
|
|
onSelectionChange={()=>{}}
|
|
|
|
onUndoRedoDepthChange={()=>{}}
|
2024-07-16 20:28:05 +02:00
|
|
|
onAttach={async ()=>{}}
|
2024-03-11 17:02:15 +02:00
|
|
|
plugins={{}}
|
2023-08-18 10:45:04 +02:00
|
|
|
/>
|
2024-12-11 14:31:05 +02:00
|
|
|
</TestProviderStack>,
|
2023-08-18 10:45:04 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Maps from screen height to whether the markdown toolbar should be visible.
|
|
|
|
const testCases: [number, boolean][] = [
|
|
|
|
[10, false],
|
|
|
|
[1000, true],
|
|
|
|
[100, false],
|
|
|
|
[80, false],
|
|
|
|
[600, true],
|
|
|
|
];
|
|
|
|
|
|
|
|
const noteEditorRoot = await wrappedNoteEditor.findByTestId('note-editor-root');
|
|
|
|
|
|
|
|
const setRootHeight = (height: number) => {
|
|
|
|
act(() => {
|
|
|
|
// See https://stackoverflow.com/a/61774123
|
|
|
|
fireEvent(noteEditorRoot, 'layout', {
|
|
|
|
nativeEvent: {
|
|
|
|
layout: { height },
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const [height, visible] of testCases) {
|
|
|
|
setRootHeight(height);
|
|
|
|
|
|
|
|
await waitFor(async () => {
|
2024-12-11 14:31:05 +02:00
|
|
|
const toolbarButton = await screen.queryByLabelText(_('Bold'));
|
2023-08-18 10:45:04 +02:00
|
|
|
if (visible) {
|
2024-12-11 14:31:05 +02:00
|
|
|
expect(toolbarButton).not.toBeNull();
|
2023-08-18 10:45:04 +02:00
|
|
|
} else {
|
2024-12-11 14:31:05 +02:00
|
|
|
expect(toolbarButton).toBeNull();
|
2023-08-18 10:45:04 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-07-18 10:44:13 +02:00
|
|
|
|
|
|
|
wrappedNoteEditor.unmount();
|
2023-08-18 10:45:04 +02:00
|
|
|
});
|
|
|
|
});
|