Skip to content

Commit c5d9152

Browse files
committed
feat: add unit tests
1 parent 11245d9 commit c5d9152

3 files changed

Lines changed: 49 additions & 10 deletions

File tree

src/web/EnrichedTextInput.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,7 @@ import {
8787
sanitizeMentionAttributes,
8888
} from './sanitization/htmlSanitizer';
8989
import { assertBrowserEnvironment } from './utils/assertBrowserEnvironment';
90-
91-
function runSafelyInEditor<T>(
92-
editor: Editor | null,
93-
callback: (editor: Editor) => T
94-
): T | null {
95-
if (editor && !editor.isDestroyed) {
96-
return callback(editor);
97-
}
98-
return null;
99-
}
90+
import { runSafelyInEditor } from './utils/runSafelyInEditor';
10091

10192
function runFocused(
10293
editor: Editor,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type { Editor } from '@tiptap/react';
2+
import { runSafelyInEditor } from '../utils/runSafelyInEditor';
3+
4+
function makeEditor(isDestroyed: boolean): Editor {
5+
return { isDestroyed } as Editor;
6+
}
7+
8+
describe('runSafelyInEditor', () => {
9+
test('runs the callback and returns its result when editor is alive', () => {
10+
const editor = makeEditor(false);
11+
const callback = jest.fn((e: Editor) => e);
12+
13+
const result = runSafelyInEditor(editor, callback);
14+
15+
expect(callback).toHaveBeenCalledWith(editor);
16+
expect(result).toBe(editor);
17+
});
18+
19+
test('does not run the callback and returns null when editor is destroyed', () => {
20+
const editor = makeEditor(true);
21+
const callback = jest.fn();
22+
23+
const result = runSafelyInEditor(editor, callback);
24+
25+
expect(callback).not.toHaveBeenCalled();
26+
expect(result).toBeNull();
27+
});
28+
29+
test('does not run the callback and returns null when editor is null', () => {
30+
const callback = jest.fn();
31+
32+
const result = runSafelyInEditor(null, callback);
33+
34+
expect(callback).not.toHaveBeenCalled();
35+
expect(result).toBeNull();
36+
});
37+
});

src/web/utils/runSafelyInEditor.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { Editor } from '@tiptap/react';
2+
3+
export function runSafelyInEditor<T>(
4+
editor: Editor | null,
5+
callback: (editor: Editor) => T
6+
): T | null {
7+
if (editor && !editor.isDestroyed) {
8+
return callback(editor);
9+
}
10+
return null;
11+
}

0 commit comments

Comments
 (0)