-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathsuggestion.test.ts
More file actions
190 lines (157 loc) · 4.8 KB
/
suggestion.test.ts
File metadata and controls
190 lines (157 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { Editor, Extension } from '@tiptap/core'
import { PluginKey } from '@tiptap/pm/state'
import StarterKit from '@tiptap/starter-kit'
import { describe, expect, it, vi } from 'vitest'
import { exitSuggestion, Suggestion } from '../suggestion.js'
describe('suggestion integration', () => {
it('should respect shouldShow returning false', async () => {
const shouldShow = vi.fn().mockReturnValue(false)
const items = vi.fn().mockReturnValue([])
const render = vi.fn()
const MentionExtension = Extension.create({
name: 'mention-false',
addProseMirrorPlugins() {
return [
Suggestion({
editor: this.editor,
char: '@',
shouldShow,
items,
render: () => ({
onStart: render,
onUpdate: render,
onExit: render,
}),
}),
]
},
})
const editor = new Editor({
extensions: [StarterKit, MentionExtension],
content: '<p></p>',
})
editor.chain().insertContent('@').run()
// Flush microtasks because plugin view update is async
await Promise.resolve()
expect(shouldShow).toHaveBeenCalled()
expect(render).not.toHaveBeenCalled()
editor.destroy()
})
it('should respect shouldShow returning true', async () => {
const shouldShow = vi.fn().mockReturnValue(true)
const render = vi.fn()
const items = vi.fn().mockReturnValue([])
const MentionExtension = Extension.create({
name: 'mention-true',
addProseMirrorPlugins() {
return [
Suggestion({
editor: this.editor,
char: '@',
shouldShow,
items,
render: () => ({
onStart: render,
onUpdate: render,
onExit: render,
}),
}),
]
},
})
const editor = new Editor({
extensions: [StarterKit, MentionExtension],
content: '<p></p>',
})
editor.chain().insertContent('@').run()
// Flush microtasks because plugin view update is async
await Promise.resolve()
expect(shouldShow).toHaveBeenCalled()
expect(render).toHaveBeenCalled()
editor.destroy()
})
it('should pass transaction to shouldShow', async () => {
let capturedProps: any = null
const shouldShow = vi.fn(props => {
capturedProps = props
return true
})
const MentionExtension = Extension.create({
name: 'mention-props',
addProseMirrorPlugins() {
return [
Suggestion({
editor: this.editor,
char: '@',
shouldShow,
}),
]
},
})
const editor = new Editor({
extensions: [StarterKit, MentionExtension],
content: '<p></p>',
})
editor.chain().insertContent('@').run()
// Flush microtasks
await Promise.resolve()
// The transaction is passed as a flat property
expect(capturedProps.transaction).toBeDefined()
expect(capturedProps.query).toBe('')
expect(capturedProps.text).toBe('@')
// Check that we receive the correct editor instance
expect(capturedProps.editor).toBe(editor)
editor.destroy()
})
it('should not activate another suggestion when exitSuggestion is called', async () => {
const pluginKeyA = new PluginKey('suggestionA')
const pluginKeyB = new PluginKey('suggestionB')
const onStartB = vi.fn()
const SuggestionA = Extension.create({
name: 'suggestion-a',
addProseMirrorPlugins() {
return [
Suggestion({
editor: this.editor,
pluginKey: pluginKeyA,
char: '@',
}),
]
},
})
const SuggestionB = Extension.create({
name: 'suggestion-b',
addProseMirrorPlugins() {
return [
Suggestion({
editor: this.editor,
pluginKey: pluginKeyB,
char: '#',
render: () => ({
onStart: onStartB,
}),
}),
]
},
})
const editor = new Editor({
extensions: [StarterKit, SuggestionA, SuggestionB],
content: '<p>#tag</p>',
})
// Place cursor at end of "#tag" — this activates suggestion B
editor.commands.setTextSelection(5)
await Promise.resolve()
expect(pluginKeyB.getState(editor.state).active).toBe(true)
// Exit suggestion B programmatically
exitSuggestion(editor.view, pluginKeyB)
await Promise.resolve()
expect(pluginKeyB.getState(editor.state).active).toBe(false)
onStartB.mockClear()
// Now exit suggestion A (not active) — this should NOT re-activate B
exitSuggestion(editor.view, pluginKeyA)
await Promise.resolve()
expect(pluginKeyB.getState(editor.state).active).toBe(false)
expect(onStartB).not.toHaveBeenCalled()
editor.destroy()
})
})