|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + * |
| 4 | + * The rich editor uses TipTap's initial-content model: opening a file loads its markdown as the |
| 5 | + * editor's initial `content`, which must NOT emit an update — so a freshly opened file is never |
| 6 | + * marked dirty (no spurious autosave / "unsaved changes"). Only a genuine edit emits, which is what |
| 7 | + * flips the dirty/autosave state on. These two cases guard exactly that contract. |
| 8 | + */ |
| 9 | +import { Editor } from '@tiptap/core' |
| 10 | +import { afterEach, describe, expect, it } from 'vitest' |
| 11 | +import { createMarkdownContentExtensions } from './extensions' |
| 12 | + |
| 13 | +let editor: Editor | null = null |
| 14 | +afterEach(() => { |
| 15 | + editor?.destroy() |
| 16 | + editor = null |
| 17 | +}) |
| 18 | + |
| 19 | +function mount(content: string, onUpdate: () => void): Editor { |
| 20 | + return new Editor({ |
| 21 | + extensions: createMarkdownContentExtensions(), |
| 22 | + content, |
| 23 | + contentType: 'markdown', |
| 24 | + onUpdate, |
| 25 | + }) |
| 26 | +} |
| 27 | + |
| 28 | +describe('rich markdown editor — dirty signal', () => { |
| 29 | + it('opening a file emits no update (never dirty on open), including markdown that normalizes', () => { |
| 30 | + // A trailing newline and `_emphasis_` both normalize on serialization; opening must still be clean. |
| 31 | + let updates = 0 |
| 32 | + editor = mount('# Title\n\nsome _emphasis_ here\n', () => { |
| 33 | + updates++ |
| 34 | + }) |
| 35 | + expect(updates).toBe(0) |
| 36 | + expect(editor.isEmpty).toBe(false) |
| 37 | + }) |
| 38 | + |
| 39 | + it('opening an empty file emits no update and is editable', () => { |
| 40 | + let updates = 0 |
| 41 | + editor = mount('', () => { |
| 42 | + updates++ |
| 43 | + }) |
| 44 | + expect(updates).toBe(0) |
| 45 | + }) |
| 46 | + |
| 47 | + it('a genuine edit emits an update (marks dirty → triggers autosave)', () => { |
| 48 | + let updates = 0 |
| 49 | + editor = mount('hello', () => { |
| 50 | + updates++ |
| 51 | + }) |
| 52 | + editor.commands.insertContent(' world') |
| 53 | + expect(updates).toBeGreaterThan(0) |
| 54 | + }) |
| 55 | +}) |
0 commit comments