|
| 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 | +}); |
0 commit comments