@handlewithcare/react-prosemirror 2.8.0-tiptap.10 → 2.8.0-tiptap.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/tiptap/tiptapNodeView.js +26 -13
- package/dist/esm/tiptap/tiptapNodeView.js +27 -14
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/constants.d.ts +1 -1
- package/dist/types/props.d.ts +26 -26
- package/package.json +1 -1
- package/dist/esm/@jest/pattern/src/__tests__/TestPathPatterns.test.js +0 -241
- package/dist/esm/@swc/cli/lib/swc/__tests__/compile.test.js +0 -19
- package/dist/esm/@swc/cli/lib/swc/__tests__/options.test.js +0 -315
- package/dist/esm/@swc/cli/lib/swc/__tests__/sources.test.js +0 -161
- package/dist/esm/@uiw/react-codemirror/src/__tests__/index.test.js +0 -111
- package/dist/esm/cheerio/src/__tests__/deprecated.spec.js +0 -249
- package/dist/esm/cheerio/src/__tests__/xml.spec.js +0 -55
- package/dist/esm/components/__tests__/ProseMirror.composition.test.js +0 -395
- package/dist/esm/components/__tests__/ProseMirror.domchange.test.js +0 -266
- package/dist/esm/components/__tests__/ProseMirror.draw-decoration.test.js +0 -968
- package/dist/esm/components/__tests__/ProseMirror.draw.test.js +0 -335
- package/dist/esm/components/__tests__/ProseMirror.mobile.test.js +0 -54
- package/dist/esm/components/__tests__/ProseMirror.node-view.test.js +0 -553
- package/dist/esm/components/__tests__/ProseMirror.selection.test.js +0 -440
- package/dist/esm/components/__tests__/ProseMirror.test.js +0 -324
- package/dist/esm/contexts/__tests__/DeferredLayoutEffects.test.js +0 -98
- package/dist/esm/hooks/__tests__/useEditorEffect.test.js +0 -134
- package/dist/esm/jsx-ast-utils/__tests__/helper.js +0 -83
- package/dist/esm/jsx-ast-utils/__tests__/src/elementType-test.js +0 -76
- package/dist/esm/jsx-ast-utils/__tests__/src/eventHandlers-test.js +0 -98
- package/dist/esm/jsx-ast-utils/__tests__/src/getProp-parser-test.js +0 -188
- package/dist/esm/jsx-ast-utils/__tests__/src/getProp-test.js +0 -119
- package/dist/esm/jsx-ast-utils/__tests__/src/getPropLiteralValue-babelparser-test.js +0 -393
- package/dist/esm/jsx-ast-utils/__tests__/src/getPropLiteralValue-flowparser-test.js +0 -379
- package/dist/esm/jsx-ast-utils/__tests__/src/getPropValue-babelparser-test.js +0 -925
- package/dist/esm/jsx-ast-utils/__tests__/src/getPropValue-flowparser-test.js +0 -700
- package/dist/esm/jsx-ast-utils/__tests__/src/hasProp-test.js +0 -350
- package/dist/esm/jsx-ast-utils/__tests__/src/index-test.js +0 -24
- package/dist/esm/jsx-ast-utils/__tests__/src/propName-test.js +0 -37
- package/dist/esm/plugins/__tests__/reactKeys.test.js +0 -77
- package/dist/esm/tsconfig-paths/lib/__tests__/config-loader.test.d.js +0 -1
- package/dist/esm/tsconfig-paths/lib/__tests__/config-loader.test.js +0 -97
- package/dist/esm/tsconfig-paths/lib/__tests__/data/match-path-data.d.js +0 -1
- package/dist/esm/tsconfig-paths/lib/__tests__/data/match-path-data.js +0 -319
- package/dist/esm/tsconfig-paths/lib/__tests__/filesystem.test.d.js +0 -1
- package/dist/esm/tsconfig-paths/lib/__tests__/filesystem.test.js +0 -62
- package/dist/esm/tsconfig-paths/lib/__tests__/mapping-entry.test.d.js +0 -1
- package/dist/esm/tsconfig-paths/lib/__tests__/mapping-entry.test.js +0 -80
- package/dist/esm/tsconfig-paths/lib/__tests__/match-path-async.test.d.js +0 -1
- package/dist/esm/tsconfig-paths/lib/__tests__/match-path-async.test.js +0 -22
- package/dist/esm/tsconfig-paths/lib/__tests__/match-path-sync.test.d.js +0 -1
- package/dist/esm/tsconfig-paths/lib/__tests__/match-path-sync.test.js +0 -20
- package/dist/esm/tsconfig-paths/lib/__tests__/try-path.test.d.js +0 -1
- package/dist/esm/tsconfig-paths/lib/__tests__/try-path.test.js +0 -219
- package/dist/esm/tsconfig-paths/lib/__tests__/tsconfig-loader.test.d.js +0 -1
- package/dist/esm/tsconfig-paths/lib/__tests__/tsconfig-loader.test.js +0 -323
- package/dist/esm/tsconfig-paths/src/__tests__/config-loader.test.js +0 -89
- package/dist/esm/tsconfig-paths/src/__tests__/data/match-path-data.js +0 -314
- package/dist/esm/tsconfig-paths/src/__tests__/filesystem.test.js +0 -58
- package/dist/esm/tsconfig-paths/src/__tests__/mapping-entry.test.js +0 -76
- package/dist/esm/tsconfig-paths/src/__tests__/match-path-async.test.js +0 -12
- package/dist/esm/tsconfig-paths/src/__tests__/match-path-sync.test.js +0 -10
- package/dist/esm/tsconfig-paths/src/__tests__/try-path.test.js +0 -214
- package/dist/esm/tsconfig-paths/src/__tests__/tsconfig-loader.test.js +0 -296
|
@@ -1,324 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-non-null-assertion */ import { render, screen } from "@testing-library/react";
|
|
2
|
-
import { Schema } from "prosemirror-model";
|
|
3
|
-
import { EditorState, Plugin } from "prosemirror-state";
|
|
4
|
-
import { doc, em, hr, li, p, schema, strong, ul } from "prosemirror-test-builder";
|
|
5
|
-
import React, { forwardRef, useEffect, useState } from "react";
|
|
6
|
-
import { useEditorEffect } from "../../hooks/useEditorEffect.js";
|
|
7
|
-
import { useStopEvent } from "../../hooks/useStopEvent.js";
|
|
8
|
-
import { reactKeys } from "../../plugins/reactKeys.js";
|
|
9
|
-
import { tempEditor } from "../../testing/editorViewTestHelpers.js";
|
|
10
|
-
import { ProseMirror } from "../ProseMirror.js";
|
|
11
|
-
import { ProseMirrorDoc } from "../ProseMirrorDoc.js";
|
|
12
|
-
describe("ProseMirror", ()=>{
|
|
13
|
-
it("renders a contenteditable", async ()=>{
|
|
14
|
-
const schema = new Schema({
|
|
15
|
-
nodes: {
|
|
16
|
-
text: {},
|
|
17
|
-
doc: {
|
|
18
|
-
content: "text*"
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
const editorState = EditorState.create({
|
|
23
|
-
schema
|
|
24
|
-
});
|
|
25
|
-
function TestEditor() {
|
|
26
|
-
return /*#__PURE__*/ React.createElement(ProseMirror, {
|
|
27
|
-
defaultState: editorState
|
|
28
|
-
}, /*#__PURE__*/ React.createElement(ProseMirrorDoc, {
|
|
29
|
-
"data-testid": "editor"
|
|
30
|
-
}));
|
|
31
|
-
}
|
|
32
|
-
render(/*#__PURE__*/ React.createElement(TestEditor, null));
|
|
33
|
-
const editor = screen.getByTestId("editor");
|
|
34
|
-
editor.focus();
|
|
35
|
-
await browser.keys("H");
|
|
36
|
-
await browser.keys("e");
|
|
37
|
-
await browser.keys("l");
|
|
38
|
-
await browser.keys("l");
|
|
39
|
-
await browser.keys("o");
|
|
40
|
-
await browser.keys(",");
|
|
41
|
-
await browser.keys(" ");
|
|
42
|
-
await browser.keys("w");
|
|
43
|
-
await browser.keys("o");
|
|
44
|
-
await browser.keys("r");
|
|
45
|
-
await browser.keys("l");
|
|
46
|
-
await browser.keys("d");
|
|
47
|
-
await browser.keys("!");
|
|
48
|
-
expect(editor.textContent).toBe("Hello, world!");
|
|
49
|
-
});
|
|
50
|
-
it("supports lifted editor state", async ()=>{
|
|
51
|
-
const schema = new Schema({
|
|
52
|
-
nodes: {
|
|
53
|
-
text: {},
|
|
54
|
-
doc: {
|
|
55
|
-
content: "text*"
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
let outerEditorState = EditorState.create({
|
|
60
|
-
schema
|
|
61
|
-
});
|
|
62
|
-
function TestEditor() {
|
|
63
|
-
const [editorState, setEditorState] = useState(outerEditorState);
|
|
64
|
-
useEffect(()=>{
|
|
65
|
-
outerEditorState = editorState;
|
|
66
|
-
}, [
|
|
67
|
-
editorState
|
|
68
|
-
]);
|
|
69
|
-
return /*#__PURE__*/ React.createElement(ProseMirror, {
|
|
70
|
-
state: editorState,
|
|
71
|
-
dispatchTransaction: (tr)=>setEditorState(editorState.apply(tr))
|
|
72
|
-
}, /*#__PURE__*/ React.createElement(ProseMirrorDoc, {
|
|
73
|
-
"data-testid": "editor"
|
|
74
|
-
}));
|
|
75
|
-
}
|
|
76
|
-
render(/*#__PURE__*/ React.createElement(TestEditor, null));
|
|
77
|
-
const editor = screen.getByTestId("editor");
|
|
78
|
-
editor.focus();
|
|
79
|
-
await browser.keys("H");
|
|
80
|
-
await browser.keys("e");
|
|
81
|
-
await browser.keys("l");
|
|
82
|
-
await browser.keys("l");
|
|
83
|
-
await browser.keys("o");
|
|
84
|
-
await browser.keys(",");
|
|
85
|
-
await browser.keys(" ");
|
|
86
|
-
await browser.keys("w");
|
|
87
|
-
await browser.keys("o");
|
|
88
|
-
await browser.keys("r");
|
|
89
|
-
await browser.keys("l");
|
|
90
|
-
await browser.keys("d");
|
|
91
|
-
await browser.keys("!");
|
|
92
|
-
expect(outerEditorState.doc.textContent).toBe("Hello, world!");
|
|
93
|
-
});
|
|
94
|
-
it("supports React NodeViews", async ()=>{
|
|
95
|
-
const schema = new Schema({
|
|
96
|
-
nodes: {
|
|
97
|
-
text: {},
|
|
98
|
-
paragraph: {
|
|
99
|
-
content: "text*",
|
|
100
|
-
toDOM () {
|
|
101
|
-
return [
|
|
102
|
-
"p",
|
|
103
|
-
0
|
|
104
|
-
];
|
|
105
|
-
}
|
|
106
|
-
},
|
|
107
|
-
doc: {
|
|
108
|
-
content: "paragraph+"
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
const editorState = EditorState.create({
|
|
113
|
-
schema
|
|
114
|
-
});
|
|
115
|
-
const Paragraph = /*#__PURE__*/ forwardRef(function Paragraph(param, ref) {
|
|
116
|
-
let { children } = param;
|
|
117
|
-
return /*#__PURE__*/ React.createElement("p", {
|
|
118
|
-
ref: ref,
|
|
119
|
-
"data-testid": "paragraph"
|
|
120
|
-
}, children);
|
|
121
|
-
});
|
|
122
|
-
const reactNodeViews = {
|
|
123
|
-
paragraph: Paragraph
|
|
124
|
-
};
|
|
125
|
-
function TestEditor() {
|
|
126
|
-
return /*#__PURE__*/ React.createElement(ProseMirror, {
|
|
127
|
-
defaultState: editorState,
|
|
128
|
-
nodeViews: reactNodeViews
|
|
129
|
-
}, /*#__PURE__*/ React.createElement(ProseMirrorDoc, {
|
|
130
|
-
"data-testid": "editor"
|
|
131
|
-
}));
|
|
132
|
-
}
|
|
133
|
-
render(/*#__PURE__*/ React.createElement(TestEditor, null));
|
|
134
|
-
const editor = screen.getByTestId("editor");
|
|
135
|
-
editor.focus();
|
|
136
|
-
await browser.keys("H");
|
|
137
|
-
await browser.keys("e");
|
|
138
|
-
await browser.keys("l");
|
|
139
|
-
await browser.keys("l");
|
|
140
|
-
await browser.keys("o");
|
|
141
|
-
await browser.keys(",");
|
|
142
|
-
await browser.keys(" ");
|
|
143
|
-
await browser.keys("w");
|
|
144
|
-
await browser.keys("o");
|
|
145
|
-
await browser.keys("r");
|
|
146
|
-
await browser.keys("l");
|
|
147
|
-
await browser.keys("d");
|
|
148
|
-
await browser.keys("!");
|
|
149
|
-
expect(editor.textContent).toBe("Hello, world!");
|
|
150
|
-
// Ensure that ProseMirror really rendered our Paragraph
|
|
151
|
-
// component, not just any old <p> tag
|
|
152
|
-
expect(screen.getAllByTestId("paragraph").length).toBeGreaterThanOrEqual(1);
|
|
153
|
-
});
|
|
154
|
-
it("reflects the current state in .props", async ()=>{
|
|
155
|
-
const { view } = tempEditor({
|
|
156
|
-
doc: doc(p())
|
|
157
|
-
});
|
|
158
|
-
expect(view.state).toBe(view.props.state);
|
|
159
|
-
});
|
|
160
|
-
it("calls handleScrollToSelection when appropriate", async ()=>{
|
|
161
|
-
let scrolled = 0;
|
|
162
|
-
const { view } = tempEditor({
|
|
163
|
-
doc: doc(p()),
|
|
164
|
-
handleScrollToSelection: ()=>{
|
|
165
|
-
scrolled++;
|
|
166
|
-
return false;
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
view.focus();
|
|
170
|
-
view.dispatch(view.state.tr.scrollIntoView());
|
|
171
|
-
expect(scrolled).toBe(1);
|
|
172
|
-
});
|
|
173
|
-
it("can be queried for the DOM position at a doc position", async ()=>{
|
|
174
|
-
const { view } = tempEditor({
|
|
175
|
-
doc: doc(ul(li(p(strong("foo")))))
|
|
176
|
-
});
|
|
177
|
-
const inText = view.domAtPos(4);
|
|
178
|
-
expect(inText.offset).toBe(1);
|
|
179
|
-
expect(inText.node.nodeValue).toBe("foo");
|
|
180
|
-
const beforeLI = view.domAtPos(1);
|
|
181
|
-
expect(beforeLI.offset).toBe(0);
|
|
182
|
-
expect(beforeLI.node.nodeName).toBe("UL");
|
|
183
|
-
const afterP = view.domAtPos(7);
|
|
184
|
-
expect(afterP.offset).toBe(1);
|
|
185
|
-
expect(afterP.node.nodeName).toBe("LI");
|
|
186
|
-
});
|
|
187
|
-
it("can bias DOM position queries to enter nodes", async ()=>{
|
|
188
|
-
const { view } = tempEditor({
|
|
189
|
-
doc: doc(p(em(strong("a"), "b"), "c"))
|
|
190
|
-
});
|
|
191
|
-
function get(pos, bias) {
|
|
192
|
-
const r = view.domAtPos(pos, bias);
|
|
193
|
-
return (r.node.nodeType == 1 ? r.node.nodeName : r.node.nodeValue) + "@" + r.offset;
|
|
194
|
-
}
|
|
195
|
-
expect(get(1, 0)).toBe("P@0");
|
|
196
|
-
expect(get(1, -1)).toBe("P@0");
|
|
197
|
-
expect(get(1, 1)).toBe("a@0");
|
|
198
|
-
expect(get(2, -1)).toBe("a@1");
|
|
199
|
-
expect(get(2, 0)).toBe("EM@1");
|
|
200
|
-
expect(get(2, 1)).toBe("b@0");
|
|
201
|
-
expect(get(3, -1)).toBe("b@1");
|
|
202
|
-
expect(get(3, 0)).toBe("P@1");
|
|
203
|
-
expect(get(3, 1)).toBe("c@0");
|
|
204
|
-
expect(get(4, -1)).toBe("c@1");
|
|
205
|
-
expect(get(4, 0)).toBe("P@2");
|
|
206
|
-
expect(get(4, 1)).toBe("P@2");
|
|
207
|
-
});
|
|
208
|
-
it("can be queried for a node's DOM representation", async ()=>{
|
|
209
|
-
const { view } = tempEditor({
|
|
210
|
-
doc: doc(p("foo"), hr())
|
|
211
|
-
});
|
|
212
|
-
expect(view.nodeDOM(0).nodeName).toBe("P");
|
|
213
|
-
expect(view.nodeDOM(5).nodeName).toBe("HR");
|
|
214
|
-
expect(view.nodeDOM(3)).toBeNull();
|
|
215
|
-
});
|
|
216
|
-
it("can map DOM positions to doc positions", async ()=>{
|
|
217
|
-
const { view } = tempEditor({
|
|
218
|
-
doc: doc(p("foo"), hr())
|
|
219
|
-
});
|
|
220
|
-
expect(view.posAtDOM(view.dom.firstChild.firstChild, 2)).toBe(3);
|
|
221
|
-
expect(view.posAtDOM(view.dom, 1)).toBe(5);
|
|
222
|
-
expect(view.posAtDOM(view.dom, 2)).toBe(6);
|
|
223
|
-
expect(view.posAtDOM(view.dom.lastChild, 0, -1)).toBe(5);
|
|
224
|
-
expect(view.posAtDOM(view.dom.lastChild, 0, 1)).toBe(6);
|
|
225
|
-
});
|
|
226
|
-
it("binds this to itself in dispatchTransaction prop", async ()=>{
|
|
227
|
-
let thisBinding;
|
|
228
|
-
const { view } = tempEditor({
|
|
229
|
-
doc: doc(p("foo"), hr()),
|
|
230
|
-
dispatchTransaction () {
|
|
231
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
232
|
-
thisBinding = this;
|
|
233
|
-
}
|
|
234
|
-
});
|
|
235
|
-
view.dispatch(view.state.tr.insertText("x"));
|
|
236
|
-
expect(view).toBe(thisBinding);
|
|
237
|
-
});
|
|
238
|
-
it("supports focusing interactive controls", async ()=>{
|
|
239
|
-
tempEditor({
|
|
240
|
-
doc: doc(hr()),
|
|
241
|
-
nodeViews: {
|
|
242
|
-
horizontal_rule: /*#__PURE__*/ forwardRef(function Button(param, ref) {
|
|
243
|
-
let { nodeProps, ...props } = param;
|
|
244
|
-
useStopEvent(()=>{
|
|
245
|
-
return true;
|
|
246
|
-
});
|
|
247
|
-
return /*#__PURE__*/ React.createElement("button", {
|
|
248
|
-
...props,
|
|
249
|
-
id: "button",
|
|
250
|
-
ref: ref,
|
|
251
|
-
type: "button"
|
|
252
|
-
}, "Click me");
|
|
253
|
-
})
|
|
254
|
-
}
|
|
255
|
-
});
|
|
256
|
-
const button = screen.getByText("Click me");
|
|
257
|
-
await $("#button").click();
|
|
258
|
-
expect(document.activeElement === button).toBeTruthy();
|
|
259
|
-
});
|
|
260
|
-
it("cleans up event listeners on reconfigure", async ()=>{
|
|
261
|
-
let firstFocusHandlerCalled = 0;
|
|
262
|
-
let secondFocusHandlerCalled = 0;
|
|
263
|
-
const viewPlugin = ()=>new Plugin({
|
|
264
|
-
props: {
|
|
265
|
-
nodeViews: {
|
|
266
|
-
horizontal_rule () {
|
|
267
|
-
const dom = document.createElement("hr");
|
|
268
|
-
return {
|
|
269
|
-
dom
|
|
270
|
-
};
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
});
|
|
275
|
-
const startDoc = doc(p("first"));
|
|
276
|
-
const firstState = EditorState.create({
|
|
277
|
-
doc: startDoc,
|
|
278
|
-
schema,
|
|
279
|
-
plugins: [
|
|
280
|
-
viewPlugin(),
|
|
281
|
-
reactKeys()
|
|
282
|
-
]
|
|
283
|
-
});
|
|
284
|
-
let firstView = null;
|
|
285
|
-
let secondView = null;
|
|
286
|
-
function Test() {
|
|
287
|
-
useEditorEffect((v)=>{
|
|
288
|
-
if (firstView === null) {
|
|
289
|
-
firstView = v;
|
|
290
|
-
} else {
|
|
291
|
-
secondView = v;
|
|
292
|
-
}
|
|
293
|
-
});
|
|
294
|
-
return null;
|
|
295
|
-
}
|
|
296
|
-
const { rerender } = render(/*#__PURE__*/ React.createElement(ProseMirror, {
|
|
297
|
-
state: firstState,
|
|
298
|
-
handleDOMEvents: {
|
|
299
|
-
focus: ()=>{
|
|
300
|
-
firstFocusHandlerCalled++;
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
}, /*#__PURE__*/ React.createElement(Test, null), /*#__PURE__*/ React.createElement(ProseMirrorDoc, null)));
|
|
304
|
-
const secondState = EditorState.create({
|
|
305
|
-
doc: doc(p("second")),
|
|
306
|
-
schema,
|
|
307
|
-
plugins: [
|
|
308
|
-
viewPlugin(),
|
|
309
|
-
reactKeys()
|
|
310
|
-
]
|
|
311
|
-
});
|
|
312
|
-
rerender(/*#__PURE__*/ React.createElement(ProseMirror, {
|
|
313
|
-
state: secondState,
|
|
314
|
-
handleDOMEvents: {
|
|
315
|
-
focus: ()=>{
|
|
316
|
-
secondFocusHandlerCalled++;
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
}, /*#__PURE__*/ React.createElement(Test, null), /*#__PURE__*/ React.createElement(ProseMirrorDoc, null)));
|
|
320
|
-
secondView.dom.focus();
|
|
321
|
-
expect(firstFocusHandlerCalled).toBe(0);
|
|
322
|
-
expect(secondFocusHandlerCalled).toBe(1);
|
|
323
|
-
});
|
|
324
|
-
});
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
/* Copyright (c) The New York Times Company */ import { act, render, screen } from "@testing-library/react";
|
|
2
|
-
import React, { useLayoutEffect, useState } from "react";
|
|
3
|
-
import { LayoutGroup } from "../../components/LayoutGroup.js";
|
|
4
|
-
import { useLayoutGroupEffect } from "../../hooks/useLayoutGroupEffect.js";
|
|
5
|
-
describe("DeferredLayoutEffects", ()=>{
|
|
6
|
-
jest.useFakeTimers("modern");
|
|
7
|
-
it("registers multiple effects and runs them", ()=>{
|
|
8
|
-
function Parent() {
|
|
9
|
-
return /*#__PURE__*/ React.createElement(LayoutGroup, null, /*#__PURE__*/ React.createElement(Child, null));
|
|
10
|
-
}
|
|
11
|
-
function Child() {
|
|
12
|
-
const [double, setDouble] = useState(1);
|
|
13
|
-
useLayoutEffect(()=>{
|
|
14
|
-
if (double === 2) {
|
|
15
|
-
setTimeout(()=>{
|
|
16
|
-
setDouble((d)=>d * 2.5);
|
|
17
|
-
}, 500);
|
|
18
|
-
}
|
|
19
|
-
if (double === 20) {
|
|
20
|
-
setDouble((d)=>d * 2.5);
|
|
21
|
-
}
|
|
22
|
-
}, [
|
|
23
|
-
double
|
|
24
|
-
]);
|
|
25
|
-
useLayoutGroupEffect(()=>{
|
|
26
|
-
const timeout = setTimeout(()=>{
|
|
27
|
-
setDouble((d)=>d * 2);
|
|
28
|
-
}, 1000);
|
|
29
|
-
return ()=>{
|
|
30
|
-
clearTimeout(timeout);
|
|
31
|
-
};
|
|
32
|
-
}, [
|
|
33
|
-
double
|
|
34
|
-
]);
|
|
35
|
-
return /*#__PURE__*/ React.createElement("div", null, /*#__PURE__*/ React.createElement("div", {
|
|
36
|
-
"data-testid": "double"
|
|
37
|
-
}, double));
|
|
38
|
-
}
|
|
39
|
-
// The component mounts ...
|
|
40
|
-
// ... the initial value should be 1
|
|
41
|
-
// ... there should be one timeout scheduled by the deferred effect
|
|
42
|
-
render(/*#__PURE__*/ React.createElement(Parent, null));
|
|
43
|
-
expect(screen.getByTestId("double").innerHTML).toBe("1");
|
|
44
|
-
// This block assert that deferred effects run.
|
|
45
|
-
// --------------------------------------------
|
|
46
|
-
// 1000 milliseconds go by ...
|
|
47
|
-
// ... the timeout set by the deferred effect should run
|
|
48
|
-
// ... the timeout should double the new value to 2
|
|
49
|
-
// ... the immediate effect should set a timeout
|
|
50
|
-
// ... the deferred effect should set a timeout
|
|
51
|
-
act(()=>{
|
|
52
|
-
jest.advanceTimersByTime(1000);
|
|
53
|
-
});
|
|
54
|
-
expect(screen.getByTestId("double").innerHTML).toBe("2");
|
|
55
|
-
// The next three blocks assert that cleanup of deferred effects run.
|
|
56
|
-
// ------------------------------------------------------------------
|
|
57
|
-
// 500 milliseconds go by ...
|
|
58
|
-
// ... the timeout set by the immediate effect should run
|
|
59
|
-
// ... the timeout should set the value to 5
|
|
60
|
-
// ... the old deferred effect should cancel its timeout
|
|
61
|
-
// ... the new deferred effect should set a new timeout
|
|
62
|
-
act(()=>{
|
|
63
|
-
jest.advanceTimersByTime(500);
|
|
64
|
-
});
|
|
65
|
-
expect(screen.getByTestId("double").innerHTML).toBe("5");
|
|
66
|
-
// ... 500 more milliseconds go by ...
|
|
67
|
-
// ... the canceled timeout should not run
|
|
68
|
-
// ... the rescheduled timoeut should not yet run
|
|
69
|
-
act(()=>{
|
|
70
|
-
jest.advanceTimersByTime(500);
|
|
71
|
-
});
|
|
72
|
-
expect(screen.getByTestId("double").innerHTML).toBe("5");
|
|
73
|
-
// ... 500 more milliseconds go by ...
|
|
74
|
-
// ... the rescheduled timeout should run
|
|
75
|
-
// ... the timeout should double the value to 10
|
|
76
|
-
// ... the deferred effect should set a new timeout
|
|
77
|
-
act(()=>{
|
|
78
|
-
jest.advanceTimersByTime(500);
|
|
79
|
-
});
|
|
80
|
-
expect(screen.getByTestId("double").innerHTML).toBe("10");
|
|
81
|
-
// The next block asserts that cancelation of deferred effects works.
|
|
82
|
-
// ------------------------------------------------------------------
|
|
83
|
-
// 1000 milliseconds go by ...
|
|
84
|
-
// ... the timeout set by the deferred effect should run
|
|
85
|
-
// ... the timeout should double the value to 20
|
|
86
|
-
// ... the immediate effect should then set the value to 50
|
|
87
|
-
// ... the deferred effect from the first render should not run
|
|
88
|
-
// ... the deferred effect from the second render should run
|
|
89
|
-
// ... the deferred effect that does run should set a new timeout
|
|
90
|
-
act(()=>{
|
|
91
|
-
jest.advanceTimersByTime(1000);
|
|
92
|
-
});
|
|
93
|
-
// For this assertion, we need to clear a timer from the React scheduler.
|
|
94
|
-
jest.advanceTimersByTime(1);
|
|
95
|
-
expect(screen.getByTestId("double").innerHTML).toBe("50");
|
|
96
|
-
expect(jest.getTimerCount()).toBe(1);
|
|
97
|
-
});
|
|
98
|
-
});
|
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
/* Copyright (c) The New York Times Company */ /* eslint-disable @typescript-eslint/no-empty-function */ import { render } from "@testing-library/react";
|
|
2
|
-
import React from "react";
|
|
3
|
-
import { ReactEditorView } from "../../ReactEditorView.js";
|
|
4
|
-
import { LayoutGroup } from "../../components/LayoutGroup.js";
|
|
5
|
-
import { EMPTY_STATE } from "../../constants.js";
|
|
6
|
-
import { EditorContext } from "../../contexts/EditorContext.js";
|
|
7
|
-
import { EditorStateContext } from "../../contexts/EditorStateContext.js";
|
|
8
|
-
import { useEditorEffect } from "../useEditorEffect.js";
|
|
9
|
-
function TestComponent(param) {
|
|
10
|
-
let { effect, dependencies = [] } = param;
|
|
11
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
12
|
-
useEditorEffect(effect, dependencies);
|
|
13
|
-
return null;
|
|
14
|
-
}
|
|
15
|
-
describe("useEditorEffect", ()=>{
|
|
16
|
-
it("should run the effect", ()=>{
|
|
17
|
-
const effect = jest.fn();
|
|
18
|
-
const editorState = EMPTY_STATE;
|
|
19
|
-
const editorView = new ReactEditorView({
|
|
20
|
-
mount: document.createElement("div")
|
|
21
|
-
}, {
|
|
22
|
-
state: editorState
|
|
23
|
-
});
|
|
24
|
-
const registerEventListener = ()=>{};
|
|
25
|
-
const unregisterEventListener = ()=>{};
|
|
26
|
-
const flushSyncRef = {
|
|
27
|
-
current: true
|
|
28
|
-
};
|
|
29
|
-
render(/*#__PURE__*/ React.createElement(LayoutGroup, null, /*#__PURE__*/ React.createElement(EditorContext.Provider, {
|
|
30
|
-
value: {
|
|
31
|
-
view: editorView,
|
|
32
|
-
cursorWrapper: null,
|
|
33
|
-
flushSyncRef,
|
|
34
|
-
registerEventListener,
|
|
35
|
-
unregisterEventListener,
|
|
36
|
-
isStatic: false
|
|
37
|
-
}
|
|
38
|
-
}, /*#__PURE__*/ React.createElement(EditorStateContext.Provider, {
|
|
39
|
-
value: editorState
|
|
40
|
-
}, /*#__PURE__*/ React.createElement(TestComponent, {
|
|
41
|
-
effect: effect
|
|
42
|
-
})))));
|
|
43
|
-
expect(effect).toHaveBeenCalled();
|
|
44
|
-
expect(effect).toHaveBeenCalledWith(editorView);
|
|
45
|
-
});
|
|
46
|
-
it("should not re-run the effect if no dependencies change", ()=>{
|
|
47
|
-
const effect = jest.fn();
|
|
48
|
-
const editorState = EMPTY_STATE;
|
|
49
|
-
const editorView = new ReactEditorView({
|
|
50
|
-
mount: document.createElement("div")
|
|
51
|
-
}, {
|
|
52
|
-
state: editorState
|
|
53
|
-
});
|
|
54
|
-
const registerEventListener = ()=>{};
|
|
55
|
-
const unregisterEventListener = ()=>{};
|
|
56
|
-
const contextValue = {
|
|
57
|
-
view: editorView,
|
|
58
|
-
cursorWrapper: null,
|
|
59
|
-
flushSyncRef: {
|
|
60
|
-
current: true
|
|
61
|
-
},
|
|
62
|
-
registerEventListener,
|
|
63
|
-
unregisterEventListener,
|
|
64
|
-
isStatic: false
|
|
65
|
-
};
|
|
66
|
-
const { rerender } = render(/*#__PURE__*/ React.createElement(LayoutGroup, null, /*#__PURE__*/ React.createElement(EditorContext.Provider, {
|
|
67
|
-
value: contextValue
|
|
68
|
-
}, /*#__PURE__*/ React.createElement(EditorStateContext.Provider, {
|
|
69
|
-
value: editorState
|
|
70
|
-
}, /*#__PURE__*/ React.createElement(TestComponent, {
|
|
71
|
-
effect: effect,
|
|
72
|
-
dependencies: []
|
|
73
|
-
})), " ")));
|
|
74
|
-
rerender(/*#__PURE__*/ React.createElement(LayoutGroup, null, /*#__PURE__*/ React.createElement(EditorContext.Provider, {
|
|
75
|
-
value: contextValue
|
|
76
|
-
}, /*#__PURE__*/ React.createElement(EditorStateContext.Provider, {
|
|
77
|
-
value: editorState
|
|
78
|
-
}, /*#__PURE__*/ React.createElement(TestComponent, {
|
|
79
|
-
effect: effect,
|
|
80
|
-
dependencies: []
|
|
81
|
-
})))));
|
|
82
|
-
expect(effect).toHaveBeenCalledTimes(1);
|
|
83
|
-
});
|
|
84
|
-
it("should re-run the effect if dependencies change", ()=>{
|
|
85
|
-
const effect = jest.fn();
|
|
86
|
-
const editorState = EMPTY_STATE;
|
|
87
|
-
const editorView = new ReactEditorView({
|
|
88
|
-
mount: document.createElement("div")
|
|
89
|
-
}, {
|
|
90
|
-
state: editorState
|
|
91
|
-
});
|
|
92
|
-
const registerEventListener = ()=>{};
|
|
93
|
-
const unregisterEventListener = ()=>{};
|
|
94
|
-
const { rerender } = render(/*#__PURE__*/ React.createElement(LayoutGroup, null, /*#__PURE__*/ React.createElement(EditorContext.Provider, {
|
|
95
|
-
value: {
|
|
96
|
-
view: editorView,
|
|
97
|
-
cursorWrapper: null,
|
|
98
|
-
flushSyncRef: {
|
|
99
|
-
current: true
|
|
100
|
-
},
|
|
101
|
-
registerEventListener,
|
|
102
|
-
unregisterEventListener,
|
|
103
|
-
isStatic: false
|
|
104
|
-
}
|
|
105
|
-
}, /*#__PURE__*/ React.createElement(EditorStateContext.Provider, {
|
|
106
|
-
value: editorState
|
|
107
|
-
}, /*#__PURE__*/ React.createElement(TestComponent, {
|
|
108
|
-
effect: effect,
|
|
109
|
-
dependencies: [
|
|
110
|
-
"one"
|
|
111
|
-
]
|
|
112
|
-
})))));
|
|
113
|
-
rerender(/*#__PURE__*/ React.createElement(LayoutGroup, null, /*#__PURE__*/ React.createElement(EditorContext.Provider, {
|
|
114
|
-
value: {
|
|
115
|
-
view: editorView,
|
|
116
|
-
registerEventListener,
|
|
117
|
-
unregisterEventListener,
|
|
118
|
-
cursorWrapper: null,
|
|
119
|
-
flushSyncRef: {
|
|
120
|
-
current: true
|
|
121
|
-
},
|
|
122
|
-
isStatic: false
|
|
123
|
-
}
|
|
124
|
-
}, /*#__PURE__*/ React.createElement(EditorStateContext.Provider, {
|
|
125
|
-
value: editorState
|
|
126
|
-
}, /*#__PURE__*/ React.createElement(TestComponent, {
|
|
127
|
-
effect: effect,
|
|
128
|
-
dependencies: [
|
|
129
|
-
"two"
|
|
130
|
-
]
|
|
131
|
-
})))));
|
|
132
|
-
expect(effect).toHaveBeenCalledTimes(2);
|
|
133
|
-
});
|
|
134
|
-
});
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
/* eslint-env jest */ import getProp from '../src/getProp';
|
|
2
|
-
const nodeVersion = parseInt(process.version.match(/^v(\d+)\./)[1], 10);
|
|
3
|
-
export const fallbackToBabylon = nodeVersion < 6;
|
|
4
|
-
let parserName;
|
|
5
|
-
const babelParser = fallbackToBabylon ? require('babylon') : require('@babel/parser');
|
|
6
|
-
const flowParser = require('flow-parser');
|
|
7
|
-
const defaultPlugins = [
|
|
8
|
-
'jsx',
|
|
9
|
-
'functionBind',
|
|
10
|
-
'estree',
|
|
11
|
-
'objectRestSpread',
|
|
12
|
-
'optionalChaining'
|
|
13
|
-
];
|
|
14
|
-
let plugins = [
|
|
15
|
-
...defaultPlugins
|
|
16
|
-
];
|
|
17
|
-
export function setParserName(name) {
|
|
18
|
-
parserName = name;
|
|
19
|
-
}
|
|
20
|
-
export function changePlugins(pluginOrFn) {
|
|
21
|
-
if (Array.isArray(pluginOrFn)) {
|
|
22
|
-
plugins = pluginOrFn;
|
|
23
|
-
} else if (typeof pluginOrFn === 'function') {
|
|
24
|
-
plugins = pluginOrFn(plugins);
|
|
25
|
-
} else {
|
|
26
|
-
throw new Error('changePlugins argument should be either an array or a function');
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
beforeEach(()=>{
|
|
30
|
-
plugins = [
|
|
31
|
-
...defaultPlugins
|
|
32
|
-
];
|
|
33
|
-
});
|
|
34
|
-
function parse(code) {
|
|
35
|
-
if (parserName === undefined) {
|
|
36
|
-
throw new Error('No parser specified');
|
|
37
|
-
}
|
|
38
|
-
if (parserName === 'babel') {
|
|
39
|
-
try {
|
|
40
|
-
return babelParser.parse(code, {
|
|
41
|
-
plugins,
|
|
42
|
-
sourceFilename: 'test.js'
|
|
43
|
-
});
|
|
44
|
-
} catch (_) {
|
|
45
|
-
// eslint-disable-next-line no-console
|
|
46
|
-
console.warn(`Failed to parse with ${fallbackToBabylon ? 'babylon' : 'Babel'} parser.`);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
if (parserName === 'flow') {
|
|
50
|
-
try {
|
|
51
|
-
return flowParser.parse(code, {
|
|
52
|
-
plugins
|
|
53
|
-
});
|
|
54
|
-
} catch (_) {
|
|
55
|
-
// eslint-disable-next-line no-console
|
|
56
|
-
console.warn('Failed to parse with the Flow parser');
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
throw new Error(`The parser ${parserName} is not yet supported for testing.`);
|
|
60
|
-
}
|
|
61
|
-
export function getOpeningElement(code) {
|
|
62
|
-
const parsedCode = parse(code);
|
|
63
|
-
let body;
|
|
64
|
-
if (parsedCode.program) {
|
|
65
|
-
// eslint-disable-next-line prefer-destructuring
|
|
66
|
-
body = parsedCode.program.body;
|
|
67
|
-
} else {
|
|
68
|
-
// eslint-disable-next-line prefer-destructuring
|
|
69
|
-
body = parsedCode.body;
|
|
70
|
-
}
|
|
71
|
-
if (Array.isArray(body) && body[0] != null) {
|
|
72
|
-
const [{ expression }] = body;
|
|
73
|
-
return expression.type === 'JSXFragment' ? expression.openingFragment : expression.openingElement;
|
|
74
|
-
}
|
|
75
|
-
return null;
|
|
76
|
-
}
|
|
77
|
-
export function extractProp(code) {
|
|
78
|
-
let prop = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 'foo';
|
|
79
|
-
const node = getOpeningElement(code);
|
|
80
|
-
const { attributes: props } = node;
|
|
81
|
-
return getProp(props, prop);
|
|
82
|
-
}
|
|
83
|
-
export const describeIfNotBabylon = fallbackToBabylon ? describe.skip : describe;
|