|
| 1 | +import TipTapEditor from '../TipTapEditor/TipTapEditor.vue'; |
| 2 | + |
| 3 | +function makeEditorStub({ markdownOut, htmlOut }) { |
| 4 | + return { |
| 5 | + storage: { |
| 6 | + markdown: { getMarkdown: () => markdownOut }, |
| 7 | + }, |
| 8 | + getHTML: () => htmlOut, |
| 9 | + }; |
| 10 | +} |
| 11 | + |
| 12 | +function getContent(editor, isReady, format) { |
| 13 | + if (!editor || !isReady) return ''; |
| 14 | + if (format === 'html') return editor.getHTML(); |
| 15 | + if (!editor.storage?.markdown) return ''; |
| 16 | + return editor.storage.markdown.getMarkdown(); |
| 17 | +} |
| 18 | +describe('TipTapEditor — format prop declaration', () => { |
| 19 | + const { format: formatProp } = TipTapEditor.props; |
| 20 | + |
| 21 | + it('exists on the component', () => { |
| 22 | + expect(formatProp).toBeDefined(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('defaults to markdown', () => { |
| 26 | + expect(formatProp.default).toBe('markdown'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('validator accepts markdown', () => { |
| 30 | + expect(formatProp.validator('markdown')).toBe(true); |
| 31 | + }); |
| 32 | + |
| 33 | + it('validator accepts html', () => { |
| 34 | + expect(formatProp.validator('html')).toBe(true); |
| 35 | + }); |
| 36 | + |
| 37 | + it('validator rejects anything else', () => { |
| 38 | + expect(formatProp.validator('xml')).toBe(false); |
| 39 | + expect(formatProp.validator('')).toBe(false); |
| 40 | + expect(formatProp.validator('JSON')).toBe(false); |
| 41 | + }); |
| 42 | +}); |
| 43 | + |
| 44 | +describe('TipTapEditor — getContent() logic', () => { |
| 45 | + const MARKDOWN = '**bold**'; |
| 46 | + const HTML = '<p><strong>bold</strong></p>'; |
| 47 | + |
| 48 | + describe('when editor is not ready', () => { |
| 49 | + it('returns empty string when editor is null', () => { |
| 50 | + expect(getContent(null, true, 'markdown')).toBe(''); |
| 51 | + }); |
| 52 | + |
| 53 | + it('returns empty string when isReady is false', () => { |
| 54 | + const editor = makeEditorStub({ markdownOut: MARKDOWN, htmlOut: HTML }); |
| 55 | + expect(getContent(editor, false, 'markdown')).toBe(''); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('format="markdown" (default)', () => { |
| 60 | + it('returns markdown from storage', () => { |
| 61 | + const editor = makeEditorStub({ markdownOut: MARKDOWN, htmlOut: HTML }); |
| 62 | + expect(getContent(editor, true, 'markdown')).toBe(MARKDOWN); |
| 63 | + }); |
| 64 | + |
| 65 | + it('returns empty string when markdown storage is absent', () => { |
| 66 | + const editor = { storage: {}, getHTML: () => HTML }; |
| 67 | + expect(getContent(editor, true, 'markdown')).toBe(''); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('format="html"', () => { |
| 72 | + it('returns HTML from editor.getHTML()', () => { |
| 73 | + const editor = makeEditorStub({ markdownOut: MARKDOWN, htmlOut: HTML }); |
| 74 | + expect(getContent(editor, true, 'html')).toBe(HTML); |
| 75 | + }); |
| 76 | + |
| 77 | + it('does not call getMarkdown() in html mode', () => { |
| 78 | + const getMarkdown = jest.fn(() => MARKDOWN); |
| 79 | + const editor = { storage: { markdown: { getMarkdown } }, getHTML: () => HTML }; |
| 80 | + getContent(editor, true, 'html'); |
| 81 | + expect(getMarkdown).not.toHaveBeenCalled(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('works even when markdown storage is absent', () => { |
| 85 | + const editor = { storage: {}, getHTML: () => HTML }; |
| 86 | + expect(getContent(editor, true, 'html')).toBe(HTML); |
| 87 | + }); |
| 88 | + }); |
| 89 | +}); |
0 commit comments