|
| 1 | +import { computed } from 'vue' |
| 2 | +import { beforeEach, describe, expect, it, vi, expectTypeOf } from 'vitest' |
| 3 | +import type { SuggestionOptions } from '@tiptap/suggestion' |
| 4 | +import { PluginKey } from '@tiptap/pm/state' |
| 5 | +import { useEditorMenu } from '../../src/runtime/composables/useEditorMenu' |
| 6 | +import type { EditorMenuOptions } from '../../src/runtime/composables/useEditorMenu' |
| 7 | +import type { EditorSuggestionMenuProps } from '../../src/runtime/components/EditorSuggestionMenu.vue' |
| 8 | +import type { EditorMentionMenuProps } from '../../src/runtime/components/EditorMentionMenu.vue' |
| 9 | +import type { EditorEmojiMenuProps } from '../../src/runtime/components/EditorEmojiMenu.vue' |
| 10 | + |
| 11 | +const { suggestionMock } = vi.hoisted(() => ({ |
| 12 | + suggestionMock: vi.fn((config: any) => config) |
| 13 | +})) |
| 14 | + |
| 15 | +vi.mock('@tiptap/suggestion', () => ({ |
| 16 | + default: suggestionMock |
| 17 | +})) |
| 18 | + |
| 19 | +function createEditor() { |
| 20 | + const dom = document.createElement('div') |
| 21 | + const parent = document.createElement('div') |
| 22 | + parent.appendChild(dom) |
| 23 | + |
| 24 | + return { |
| 25 | + isDestroyed: false, |
| 26 | + view: { |
| 27 | + dom, |
| 28 | + state: { |
| 29 | + tr: { |
| 30 | + setMeta: vi.fn(() => ({})) |
| 31 | + } |
| 32 | + }, |
| 33 | + dispatch: vi.fn() |
| 34 | + } |
| 35 | + } as any |
| 36 | +} |
| 37 | + |
| 38 | +function createOptions(overrides: Partial<EditorMenuOptions<{ label: string }>> = {}): EditorMenuOptions<{ label: string }> { |
| 39 | + return { |
| 40 | + editor: createEditor(), |
| 41 | + char: ':', |
| 42 | + pluginKey: 'suggestion-menu', |
| 43 | + items: [{ label: 'Alpha' }, { label: 'Beta' }], |
| 44 | + onSelect: vi.fn(), |
| 45 | + renderItem: vi.fn(() => []), |
| 46 | + ui: computed(() => ({ |
| 47 | + content: () => '', |
| 48 | + viewport: () => '', |
| 49 | + group: () => '', |
| 50 | + label: () => '', |
| 51 | + separator: () => '', |
| 52 | + item: () => '', |
| 53 | + itemLeadingIcon: () => '', |
| 54 | + itemWrapper: () => '', |
| 55 | + itemLabel: () => '', |
| 56 | + itemDescription: () => '' |
| 57 | + })), |
| 58 | + ...overrides |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function getSuggestionConfig() { |
| 63 | + if (suggestionMock.mock.calls.length !== 1) { |
| 64 | + throw new Error(`Suggestion should be called exactly once, but was called ${suggestionMock.mock.calls.length} times`) |
| 65 | + } |
| 66 | + |
| 67 | + return suggestionMock.mock.calls[0]![0] |
| 68 | +} |
| 69 | + |
| 70 | +describe('useEditorMenu', () => { |
| 71 | + beforeEach(() => { |
| 72 | + suggestionMock.mockClear() |
| 73 | + }) |
| 74 | + |
| 75 | + it('forwards suggestion matching options', () => { |
| 76 | + useEditorMenu(createOptions({ |
| 77 | + suggestion: { |
| 78 | + allowedPrefixes: null, |
| 79 | + allowSpaces: true, |
| 80 | + startOfLine: true |
| 81 | + } |
| 82 | + })) |
| 83 | + |
| 84 | + const config = getSuggestionConfig() |
| 85 | + |
| 86 | + expect(config.allowedPrefixes).toBeNull() |
| 87 | + expect(config.allowSpaces).toBe(true) |
| 88 | + expect(config.startOfLine).toBe(true) |
| 89 | + expect(config.char).toBe(':') |
| 90 | + }) |
| 91 | + |
| 92 | + it('keeps existing defaults when suggestion is omitted', () => { |
| 93 | + useEditorMenu(createOptions()) |
| 94 | + |
| 95 | + const config = getSuggestionConfig() |
| 96 | + const items = config.items({ query: 'al' }) |
| 97 | + |
| 98 | + expect(config).not.toHaveProperty('allowedPrefixes') |
| 99 | + expect(items).toEqual([{ label: 'Alpha' }]) |
| 100 | + }) |
| 101 | + |
| 102 | + it('keeps plugin identity fields authoritative over suggestion overrides', () => { |
| 103 | + const wrongPluginKey = new PluginKey('wrong-plugin-key') |
| 104 | + const wrongEditor = createEditor() |
| 105 | + |
| 106 | + useEditorMenu(createOptions({ |
| 107 | + suggestion: { |
| 108 | + pluginKey: wrongPluginKey, |
| 109 | + editor: wrongEditor, |
| 110 | + char: '@' |
| 111 | + } as Partial<SuggestionOptions> |
| 112 | + })) |
| 113 | + |
| 114 | + const config = getSuggestionConfig() |
| 115 | + |
| 116 | + expect(config.pluginKey).not.toBe(wrongPluginKey) |
| 117 | + expect(config.editor).not.toBe(wrongEditor) |
| 118 | + expect(config.char).toBe(':') |
| 119 | + }) |
| 120 | + |
| 121 | + it('keeps menu callbacks authoritative over suggestion overrides', () => { |
| 122 | + const suggestionItems = vi.fn(() => []) |
| 123 | + const suggestionCommand = vi.fn() |
| 124 | + const suggestionRender = vi.fn() |
| 125 | + |
| 126 | + useEditorMenu(createOptions({ |
| 127 | + suggestion: { |
| 128 | + items: suggestionItems, |
| 129 | + command: suggestionCommand, |
| 130 | + render: suggestionRender |
| 131 | + } as Partial<SuggestionOptions> |
| 132 | + })) |
| 133 | + |
| 134 | + const config = getSuggestionConfig() |
| 135 | + |
| 136 | + expect(config.items).not.toBe(suggestionItems) |
| 137 | + expect(config.command).not.toBe(suggestionCommand) |
| 138 | + expect(config.render).not.toBe(suggestionRender) |
| 139 | + }) |
| 140 | + |
| 141 | + it('types suggestion options on the composable and component props', () => { |
| 142 | + type ExpectedType = Omit<Partial<SuggestionOptions>, 'pluginKey' | 'editor' | 'char' | 'items' | 'command' | 'render'> | undefined |
| 143 | + |
| 144 | + expectTypeOf<EditorMenuOptions<{ label: string }>['suggestion']>().toEqualTypeOf<ExpectedType>() |
| 145 | + expectTypeOf<EditorSuggestionMenuProps['suggestion']>().toEqualTypeOf<ExpectedType>() |
| 146 | + expectTypeOf<EditorMentionMenuProps['suggestion']>().toEqualTypeOf<ExpectedType>() |
| 147 | + expectTypeOf<EditorEmojiMenuProps['suggestion']>().toEqualTypeOf<ExpectedType>() |
| 148 | + }) |
| 149 | +}) |
0 commit comments