-
-
Notifications
You must be signed in to change notification settings - Fork 232
/
jest-prosemirror-serializer.ts
40 lines (36 loc) 路 1.13 KB
/
jest-prosemirror-serializer.ts
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
import { keys } from '@remirror/core-helpers';
import { isEditorSchema, isEditorState, isProsemirrorNode } from '@remirror/core-utils';
/**
* Jest serializer for prosemirror nodes and the editor state.
*/
export const prosemirrorSerializer: jest.SnapshotSerializerPlugin = {
test: val => isProsemirrorNode(val) || isEditorState(val) || isEditorSchema(val),
print: val => {
if (isEditorState(val)) {
return `Prosemirror doc: ${JSON.stringify(
val.doc.toJSON(),
null,
2,
)}\nProsemirror selection: ${JSON.stringify(val.selection, null, 2)}`;
}
if (isEditorSchema(val)) {
const nodes = keys(val.nodes).reduce((acc, key) => {
const { spec } = val.nodes[key];
return { ...acc, [key]: spec };
}, {});
const marks = keys(val.marks).reduce((acc, key) => {
const { spec } = val.marks[key];
return { ...acc, [key]: spec };
}, {});
return `Prosemirror schema: ${JSON.stringify(
{
nodes,
marks,
},
null,
2,
)}`;
}
return `Prosemirror node: ${JSON.stringify(val, null, 2)}`;
},
};