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
|
import { ContentEditor } from '~/content_editor/services/content_editor';
import eventHubFactory from '~/helpers/event_hub_factory';
import { createTestEditor, createDocBuilder } from '../test_utils';
describe('content_editor/services/content_editor', () => {
let contentEditor;
let serializer;
let deserializer;
let eventHub;
let doc;
let p;
const testMarkdown = '**bold text**';
beforeEach(() => {
const tiptapEditor = createTestEditor();
jest.spyOn(tiptapEditor, 'destroy');
({
builders: { doc, p },
} = createDocBuilder({
tiptapEditor,
}));
serializer = { serialize: jest.fn() };
deserializer = { deserialize: jest.fn() };
eventHub = eventHubFactory();
contentEditor = new ContentEditor({
tiptapEditor,
serializer,
deserializer,
eventHub,
});
});
const testDoc = () => doc(p('document'));
const testEmptyDoc = () => doc();
describe('.dispose', () => {
it('destroys the tiptapEditor', () => {
expect(contentEditor.tiptapEditor.destroy).not.toHaveBeenCalled();
contentEditor.dispose();
expect(contentEditor.tiptapEditor.destroy).toHaveBeenCalled();
});
});
describe('empty', () => {
it('returns true when tiptapEditor is empty', async () => {
deserializer.deserialize.mockResolvedValueOnce({ document: testEmptyDoc() });
await contentEditor.setSerializedContent(testMarkdown);
expect(contentEditor.empty).toBe(true);
});
it('returns false when tiptapEditor is not empty', async () => {
deserializer.deserialize.mockResolvedValueOnce({ document: testDoc() });
await contentEditor.setSerializedContent(testMarkdown);
expect(contentEditor.empty).toBe(false);
});
});
describe('editable', () => {
it('returns true when tiptapEditor is editable', async () => {
contentEditor.setEditable(true);
expect(contentEditor.editable).toBe(true);
});
it('returns false when tiptapEditor is readonly', async () => {
contentEditor.setEditable(false);
expect(contentEditor.editable).toBe(false);
});
});
describe('changed', () => {
it('returns true when the initial document changes', async () => {
deserializer.deserialize.mockResolvedValueOnce({ document: testDoc() });
await contentEditor.setSerializedContent(testMarkdown);
contentEditor.tiptapEditor.commands.insertContent(' new content');
expect(contentEditor.changed).toBe(true);
});
it('returns false when the initial document hasn’t changed', async () => {
deserializer.deserialize.mockResolvedValueOnce({ document: testDoc() });
await contentEditor.setSerializedContent(testMarkdown);
expect(contentEditor.changed).toBe(false);
});
it('returns false when an initial document is not set and the document is empty', () => {
expect(contentEditor.changed).toBe(false);
});
it('returns true when an initial document is not set and the document is not empty', () => {
contentEditor.tiptapEditor.commands.insertContent('new content');
expect(contentEditor.changed).toBe(true);
});
});
describe('when setSerializedContent succeeds', () => {
it('sets the deserialized document in the tiptap editor object', async () => {
const document = testDoc();
deserializer.deserialize.mockResolvedValueOnce({ document });
await contentEditor.setSerializedContent(testMarkdown);
expect(contentEditor.tiptapEditor.state.doc.toJSON()).toEqual(document.toJSON());
});
});
});
|