-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathsplitBlock.test.js
More file actions
331 lines (279 loc) · 10.2 KB
/
splitBlock.test.js
File metadata and controls
331 lines (279 loc) · 10.2 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { splitBlock } from './splitBlock.js';
vi.mock('../Attribute.js', () => ({
Attribute: {
getSplittedAttributes: vi.fn((extensionAttrs, nodeName, nodeAttrs) => ({ ...nodeAttrs })),
},
}));
vi.mock('prosemirror-transform', () => ({
canSplit: vi.fn(() => true),
}));
/**
* Create a mock resolved position ($from/$to) compatible with ProseMirror
*/
function createMockResolvedPos(options = {}) {
const { pos = 5, parent = null, parentOffset = 0, depth = 0, marks = [], node = null } = options;
const resolved = {
pos,
parent: parent || { isBlock: true, content: { size: 10 }, type: { name: 'paragraph' }, inlineContent: true },
parentOffset,
depth,
marks: vi.fn(() => marks),
node: node || vi.fn(() => ({ type: { name: 'paragraph' }, attrs: {} })),
before: vi.fn(() => 0),
indexAfter: vi.fn(() => 0),
// Required for Selection constructor
min: vi.fn(function (other) {
return this.pos < other.pos ? this : other;
}),
max: vi.fn(function (other) {
return this.pos > other.pos ? this : other;
}),
};
return resolved;
}
describe('splitBlock', () => {
let mockEditor, mockState, mockTr, mockDispatch, mockSchema;
beforeEach(() => {
vi.clearAllMocks();
// Setup mock schema with mark types
mockSchema = {
marks: {
bold: {
create: vi.fn((attrs) => ({ type: { name: 'bold' }, attrs })),
},
italic: {
create: vi.fn((attrs) => ({ type: { name: 'italic' }, attrs })),
},
textStyle: {
create: vi.fn((attrs) => ({ type: { name: 'textStyle' }, attrs })),
},
underline: {
create: vi.fn((attrs) => ({ type: { name: 'underline' }, attrs })),
},
},
nodes: {
paragraph: {
name: 'paragraph',
},
},
};
// Setup mock state
mockState = {
schema: mockSchema,
selection: null,
storedMarks: null,
tr: null,
};
// Setup mock transaction
mockTr = {
selection: null,
doc: {
resolve: vi.fn(),
},
mapping: {
map: vi.fn((pos) => pos),
},
deleteSelection: vi.fn(),
split: vi.fn().mockReturnThis(),
setNodeMarkup: vi.fn().mockReturnThis(),
ensureMarks: vi.fn().mockReturnThis(),
scrollIntoView: vi.fn().mockReturnThis(),
};
mockState.tr = mockTr;
// Setup mock editor
mockEditor = {
extensionService: {
attributes: [],
splittableMarks: ['bold', 'italic', 'textStyle', 'underline'],
},
converter: null,
};
mockDispatch = vi.fn();
});
describe('basic split functionality', () => {
it('returns false if parent is not a block', () => {
const $from = createMockResolvedPos({
parent: { isBlock: false, content: { size: 10 }, type: { name: 'text' }, inlineContent: true },
});
const $to = createMockResolvedPos({ pos: 5 });
mockTr.selection = { $from, $to };
mockState.selection = mockTr.selection;
const command = splitBlock();
const result = command({ tr: mockTr, state: mockState, dispatch: mockDispatch, editor: mockEditor });
expect(result).toBe(false);
});
it('calls split and scrollIntoView when dispatching', () => {
const $from = createMockResolvedPos();
const $to = createMockResolvedPos({ pos: 10, parentOffset: 10 });
mockTr.selection = { $from, $to };
mockState.selection = mockTr.selection;
mockTr.doc = {
resolve: vi.fn(() => $from),
};
const command = splitBlock();
// Pass a non-null dispatch to trigger the actual logic
const result = command({ tr: mockTr, state: mockState, dispatch: () => {}, editor: mockEditor });
expect(result).toBe(true);
expect(mockTr.split).toHaveBeenCalled();
expect(mockTr.scrollIntoView).toHaveBeenCalled();
});
});
describe('mark merging behavior', () => {
it('filters marks by splittableMarks list', () => {
// Only bold and italic are splittable
mockEditor.extensionService.splittableMarks = ['bold', 'italic'];
const boldMark = { type: { name: 'bold' }, attrs: { value: true } };
const linkMark = { type: { name: 'link' }, attrs: { href: 'http://example.com' } };
const $from = createMockResolvedPos({
marks: [boldMark, linkMark],
node: vi.fn(() => ({
type: { name: 'paragraph' },
attrs: {},
})),
});
const $to = createMockResolvedPos({ pos: 5, parentOffset: 5 });
mockTr.selection = { $from, $to };
mockState.selection = mockTr.selection;
mockTr.doc = {
resolve: vi.fn(() => $from),
};
const command = splitBlock();
command({ tr: mockTr, state: mockState, dispatch: () => {}, editor: mockEditor });
// Verify ensureMarks was called with only the bold mark (link filtered out)
expect(mockTr.ensureMarks).toHaveBeenCalled();
const appliedMarks = mockTr.ensureMarks.mock.calls[0][0];
expect(appliedMarks).toContainEqual(boldMark);
expect(appliedMarks).not.toContainEqual(linkMark);
});
it('handles storedMarks from state', () => {
const storedBoldMark = { type: { name: 'bold' }, attrs: { value: true } };
mockState.storedMarks = [storedBoldMark];
const $from = createMockResolvedPos({
node: vi.fn(() => ({
type: { name: 'paragraph' },
attrs: {},
})),
});
const $to = createMockResolvedPos({ pos: 5, parentOffset: 5 });
mockTr.selection = { $from, $to };
mockState.selection = mockTr.selection;
mockTr.doc = {
resolve: vi.fn(() => $from),
};
const command = splitBlock();
command({ tr: mockTr, state: mockState, dispatch: () => {}, editor: mockEditor });
// Should use stored marks
expect(mockTr.ensureMarks).toHaveBeenCalled();
const appliedMarks = mockTr.ensureMarks.mock.calls[0][0];
expect(appliedMarks).toContainEqual(storedBoldMark);
});
});
describe('edge cases', () => {
it('does not call ensureMarks when keepMarks is false', () => {
const $from = createMockResolvedPos({
marks: [{ type: { name: 'bold' }, attrs: { value: true } }],
node: vi.fn(() => ({
type: { name: 'paragraph' },
attrs: {},
})),
});
const $to = createMockResolvedPos({ pos: 5, parentOffset: 5 });
mockTr.selection = { $from, $to };
mockState.selection = mockTr.selection;
mockTr.doc = {
resolve: vi.fn(() => $from),
};
const command = splitBlock({ keepMarks: false });
command({ tr: mockTr, state: mockState, dispatch: () => {}, editor: mockEditor });
// ensureMarks should NOT be called
expect(mockTr.ensureMarks).not.toHaveBeenCalled();
});
it('clears heading style on leading block when splitting at start of heading paragraph', () => {
const canReplaceWith = vi.fn(() => true);
const paragraphType = { name: 'paragraph', isTextblock: true, hasRequiredAttrs: vi.fn(() => false) };
const parentNode = {
contentMatchAt: vi.fn(() => ({
edgeCount: 1,
edge: vi.fn(() => ({ type: paragraphType })),
})),
canReplaceWith,
};
const headingAttrs = { paragraphProperties: { styleId: 'Heading2' } };
const $from = createMockResolvedPos({
depth: 1,
parent: {
isBlock: true,
content: { size: 10 },
type: { name: 'paragraph' },
inlineContent: true,
attrs: headingAttrs,
},
parentOffset: 0,
node: vi.fn((depth) => {
if (depth === -1) return parentNode;
return { type: { name: 'paragraph' }, attrs: headingAttrs };
}),
});
const $to = createMockResolvedPos({
pos: 5,
parent: { isBlock: true, content: { size: 10 }, type: { name: 'paragraph' }, inlineContent: true },
parentOffset: 0,
});
mockTr.selection = { $from, $to };
mockState.selection = mockTr.selection;
mockTr.doc = {
resolve: vi.fn(() => ({ index: vi.fn(() => 0) })),
};
const command = splitBlock();
command({ tr: mockTr, state: mockState, dispatch: () => {}, editor: mockEditor });
expect(mockTr.setNodeMarkup).toHaveBeenCalled();
const attrs = mockTr.setNodeMarkup.mock.calls[0][2];
expect(attrs.paragraphProperties?.styleId).toBeUndefined();
});
it('does not mutate source attrs when removing nested override attributes', () => {
const paragraphType = { name: 'paragraph', isTextblock: true, hasRequiredAttrs: vi.fn(() => false) };
const parentNode = {
contentMatchAt: vi.fn(() => ({
edgeCount: 1,
edge: vi.fn(() => ({ type: paragraphType })),
})),
};
const sourceAttrs = {
paragraphProperties: { styleId: 'Heading2', keep: true },
preserve: true,
};
const $from = createMockResolvedPos({
depth: 1,
parent: {
isBlock: true,
content: { size: 10 },
type: { name: 'paragraph' },
inlineContent: true,
attrs: sourceAttrs,
},
parentOffset: 0,
node: vi.fn((depth) => {
if (depth === -1) return parentNode;
return { type: { name: 'paragraph' }, attrs: sourceAttrs };
}),
});
const $to = createMockResolvedPos({
pos: 5,
parent: { isBlock: true, content: { size: 10 }, type: { name: 'paragraph' }, inlineContent: true },
parentOffset: 10,
});
mockTr.selection = { $from, $to };
mockState.selection = mockTr.selection;
mockTr.doc = {
resolve: vi.fn(() => ({ index: vi.fn(() => 0) })),
};
const command = splitBlock({ attrsToRemoveOverride: ['paragraphProperties.styleId'] });
command({ tr: mockTr, state: mockState, dispatch: () => {}, editor: mockEditor });
const splitTypes = mockTr.split.mock.calls[0][2];
expect(splitTypes?.[0]?.attrs?.paragraphProperties?.styleId).toBeUndefined();
expect(splitTypes?.[0]?.attrs?.paragraphProperties?.keep).toBe(true);
expect(sourceAttrs.paragraphProperties.styleId).toBe('Heading2');
});
});
});