2023-12-13 21:45:02 +02:00
|
|
|
import { ViewPlugin } from '@codemirror/view';
|
2024-02-22 17:36:27 +02:00
|
|
|
import createEditorControl from './testUtil/createEditorControl';
|
2023-09-21 10:12:40 +02:00
|
|
|
|
|
|
|
describe('CodeMirrorControl', () => {
|
|
|
|
it('clearHistory should clear the undo/redo history', () => {
|
2023-12-13 21:45:02 +02:00
|
|
|
const controls = createEditorControl('');
|
2023-09-21 10:12:40 +02:00
|
|
|
|
|
|
|
const insertedText = 'Testing... This is a test...';
|
|
|
|
controls.insertText(insertedText);
|
|
|
|
|
|
|
|
const fullInsertedText = insertedText;
|
|
|
|
expect(controls.getValue()).toBe(fullInsertedText);
|
|
|
|
|
|
|
|
// Undo should work before clearing history
|
|
|
|
controls.undo();
|
|
|
|
expect(controls.getValue()).toBe('');
|
|
|
|
|
|
|
|
controls.redo();
|
|
|
|
controls.clearHistory();
|
|
|
|
|
|
|
|
expect(controls.getValue()).toBe(fullInsertedText);
|
|
|
|
|
|
|
|
// Should not be able to undo cleared changes
|
|
|
|
controls.undo();
|
|
|
|
expect(controls.getValue()).toBe(fullInsertedText);
|
|
|
|
|
|
|
|
// Should be able to undo new changes
|
|
|
|
controls.insertText('!!!');
|
|
|
|
expect(controls.getValue()).toBe(`${fullInsertedText}!!!`);
|
|
|
|
|
|
|
|
controls.undo();
|
|
|
|
expect(controls.getValue()).toBe(fullInsertedText);
|
|
|
|
});
|
2023-12-13 21:45:02 +02:00
|
|
|
|
|
|
|
it('should support adding CodeMirror 6 extensions', () => {
|
|
|
|
const control = createEditorControl('');
|
|
|
|
|
|
|
|
const updateFn = jest.fn();
|
|
|
|
control.addExtension([
|
|
|
|
ViewPlugin.fromClass(class {
|
|
|
|
public update = updateFn;
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Verify that the extension has been loaded
|
|
|
|
updateFn.mockReset();
|
|
|
|
control.insertText('Test...');
|
|
|
|
expect(updateFn).toHaveBeenCalled();
|
|
|
|
});
|
2024-02-19 12:04:20 +02:00
|
|
|
|
|
|
|
it('should support adding custom editor commands', () => {
|
|
|
|
const control = createEditorControl('');
|
|
|
|
const command = jest.fn(() => 'test');
|
|
|
|
control.registerCommand('myTestCommand', command);
|
|
|
|
|
|
|
|
expect(control.supportsCommand('myTestCommand')).toBe(true);
|
|
|
|
expect(control.execCommand('myTestCommand')).toBe('test');
|
|
|
|
expect(command).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
2023-09-21 10:12:40 +02:00
|
|
|
});
|