-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathuseEditor.spec.ts
More file actions
231 lines (184 loc) · 6.05 KB
/
Copy pathuseEditor.spec.ts
File metadata and controls
231 lines (184 loc) · 6.05 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
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 () => {
let createCount = 0
const createdEditors = new Set<Editor>()
let latestEditor: Editor | null = null
function TestComponent() {
const editor = useEditor({
extensions: [Document, Text, Paragraph],
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(createCount).toBe(1)
expect(createdEditors.size).toBe(1)
expect(latestEditor?.isDestroyed).toBe(false)
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 () => {
// Note: a StrictMode mount's discarded duplicate instance is destroyed
// (and so fires its own onDestroy) regardless of this fix - that already
// happened before this fix too, just 1ms later instead of synchronously.
// This test tracks *deltas* around the real show/hide toggle so that
// pre-existing, orthogonal onDestroy noise from StrictMode's duplicate
// doesn't get mistaken for a real unmount.
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 callback throws while evicting a discarded StrictMode duplicate', async () => {
function TestComponent() {
useEditor({
extensions: [Document, Text, Paragraph],
onDestroy: () => {
throw new Error('boom from onDestroy')
},
})
return null
}
let caughtAsync: unknown = null
const onUnhandled = (error: unknown) => {
caughtAsync = error
}
process.on('uncaughtException', onUnhandled)
try {
expect(() => {
render(React.createElement(React.StrictMode, null, React.createElement(TestComponent)))
}).not.toThrow()
await flushTimers(100)
} finally {
process.off('uncaughtException', onUnhandled)
}
// The throw is still surfaced (matching pre-existing behavior for a
// throwing onDestroy) - it just must not happen synchronously inside
// React's render phase.
expect(caughtAsync).toBeInstanceOf(Error)
expect((caughtAsync as Error).message).toBe('boom from onDestroy')
})
})