-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathuseEditor.spec.ts
More file actions
269 lines (214 loc) · 7.03 KB
/
Copy pathuseEditor.spec.ts
File metadata and controls
269 lines (214 loc) · 7.03 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { Document } from '@tiptap/extension-document'
import { Paragraph } from '@tiptap/extension-paragraph'
import { Text } from '@tiptap/extension-text'
import { render } from '@testing-library/react'
import React from 'react'
import { afterEach, describe, expect, it } from 'vitest'
import type { Editor } from '@tiptap/core'
import { useEditor } from './useEditor.js'
function flushTimers(ms: number) {
return new Promise<void>(resolve => {
setTimeout(resolve, ms)
})
}
describe('useEditor', () => {
afterEach(() => {
document.body.innerHTML = ''
})
it('does not fire onCreate more than once for a single mount under StrictMode', async () => {
// onBeforeCreate legitimately still fires for both the kept and the
// discarded StrictMode-duplicate instance (it fires synchronously as
// part of construction itself, before there's any way to know which one
// survives). onCreate is what's actually fixed here: it's deferred until
// the instance is confirmed mounted, so only the kept instance's onCreate
// is ever forwarded to the user's callback.
let beforeCreateCount = 0
let createCount = 0
const createdEditors = new Set<Editor>()
let latestEditor: Editor | null = null
function TestComponent() {
const editor = useEditor({
extensions: [Document, Text, Paragraph],
onBeforeCreate: () => {
beforeCreateCount += 1
},
onCreate: ({ editor: createdEditor }) => {
createCount += 1
createdEditors.add(createdEditor)
},
})
latestEditor = editor
return null
}
const { unmount } = render(
React.createElement(React.StrictMode, null, React.createElement(TestComponent)),
)
// The editor's own 'create' event fires via an internal setTimeout(0);
// give it real wall-clock time to flush.
await flushTimers(100)
expect(beforeCreateCount).toBeGreaterThanOrEqual(1)
expect(createCount).toBe(1)
expect(createdEditors.size).toBe(1)
expect(latestEditor?.isDestroyed).toBe(false)
unmount()
})
it('still fires onCreate exactly once when mounted without StrictMode', async () => {
let createCount = 0
function TestComponent() {
useEditor({
extensions: [Document, Text, Paragraph],
onCreate: () => {
createCount += 1
},
})
return null
}
const { unmount } = render(React.createElement(TestComponent))
await flushTimers(100)
expect(createCount).toBe(1)
unmount()
})
it('keeps two sibling editors independent under StrictMode', async () => {
let createCountA = 0
let createCountB = 0
const createdA = new Set<Editor>()
const createdB = new Set<Editor>()
let editorA: Editor | null = null
let editorB: Editor | null = null
function ComponentA() {
const editor = useEditor({
extensions: [Document, Text, Paragraph],
onCreate: ({ editor: e }) => {
createCountA += 1
createdA.add(e)
},
})
editorA = editor
return null
}
function ComponentB() {
const editor = useEditor({
extensions: [Document, Text, Paragraph],
onCreate: ({ editor: e }) => {
createCountB += 1
createdB.add(e)
},
})
editorB = editor
return null
}
const { unmount } = render(
React.createElement(
React.StrictMode,
null,
React.createElement(ComponentA),
React.createElement(ComponentB),
),
)
await flushTimers(100)
expect(createCountA).toBe(1)
expect(createCountB).toBe(1)
expect(createdA.size).toBe(1)
expect(createdB.size).toBe(1)
expect(editorA?.isDestroyed).toBe(false)
expect(editorB?.isDestroyed).toBe(false)
expect(editorA).not.toBe(editorB)
unmount()
})
it('does not throw and still creates exactly one editor when immediatelyRender is false under StrictMode', async () => {
let createCount = 0
function TestComponent() {
useEditor({
immediatelyRender: false,
extensions: [Document, Text, Paragraph],
onCreate: () => {
createCount += 1
},
})
return null
}
let unmount: () => void = () => {}
expect(() => {
;({ unmount } = render(
React.createElement(React.StrictMode, null, React.createElement(TestComponent)),
))
}).not.toThrow()
await flushTimers(100)
expect(createCount).toBe(1)
unmount()
})
it('handles a full unmount and remount cycle correctly under StrictMode', async () => {
// Tracks *deltas* around the real show/hide toggle, rather than an
// absolute destroyCount, so this stays correct regardless of exactly
// when the guard's ref is (re-)initialized across mounts.
let createCount = 0
let destroyCount = 0
function TestComponent() {
useEditor({
extensions: [Document, Text, Paragraph],
onCreate: () => {
createCount += 1
},
onDestroy: () => {
destroyCount += 1
},
})
return null
}
function Wrapper({ show }: { show: boolean }) {
return show ? React.createElement(TestComponent) : null
}
const { rerender, unmount } = render(
React.createElement(React.StrictMode, null, React.createElement(Wrapper, { show: true })),
)
await flushTimers(100)
expect(createCount).toBe(1)
const destroyCountBeforeHiding = destroyCount
rerender(
React.createElement(React.StrictMode, null, React.createElement(Wrapper, { show: false })),
)
await flushTimers(100)
expect(destroyCount - destroyCountBeforeHiding).toBe(1)
rerender(
React.createElement(React.StrictMode, null, React.createElement(Wrapper, { show: true })),
)
await flushTimers(100)
expect(createCount).toBe(2)
unmount()
})
it('does not crash the render if a user-provided onCreate throws', async () => {
function TestComponent() {
useEditor({
extensions: [Document, Text, Paragraph],
onCreate: () => {
throw new Error('boom from onCreate')
},
})
return null
}
const caughtAsync: unknown[] = []
const onUnhandled = (error: unknown) => {
caughtAsync.push(error)
}
process.on('uncaughtException', onUnhandled)
let unmount: () => void = () => {}
try {
expect(() => {
;({ unmount } = render(
React.createElement(React.StrictMode, null, React.createElement(TestComponent)),
))
}).not.toThrow()
await flushTimers(500)
} finally {
unmount()
await flushTimers(100)
process.off('uncaughtException', onUnhandled)
}
// A throwing onCreate should surface asynchronously (same as it would
// have before this fix, since it fires from a 0ms timer or a React
// effect, never synchronously inside the render call itself) - not
// crash the render.
expect(caughtAsync.length).toBeGreaterThanOrEqual(1)
expect(caughtAsync.every(error => error instanceof Error)).toBe(true)
})
})