-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBlocksAPI.integration.spec.ts
More file actions
403 lines (317 loc) · 11.4 KB
/
BlocksAPI.integration.spec.ts
File metadata and controls
403 lines (317 loc) · 11.4 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* eslint-disable @typescript-eslint/no-magic-numbers, jsdoc/require-jsdoc,@typescript-eslint/naming-convention */
import { jest, beforeEach, afterEach, describe, it, expect } from '@jest/globals';
import type { CoreConfigValidated } from '@editorjs/sdk';
// @ts-expect-error - TS don't import types via import() so have to import them here as well
import type { BlocksManager } from '../components/BlockManager';
const USER_ID = 'integration-user';
const DOCUMENT_ID = 'integration-doc';
/**
* Mock console.error to suppress expected error logs
*/
console.error = jest.fn();
jest.unstable_mockModule('@editorjs/sdk', () => ({
BlockAddedCoreEvent: jest.fn(),
BlockRemovedCoreEvent: jest.fn(),
EventBus: jest.fn(() => ({
dispatchEvent: jest.fn(),
})),
}));
/**
* Mock DOM adapters — they require a real DOM environment
*/
jest.unstable_mockModule('@editorjs/dom-adapters', () => ({
BlockToolAdapter: jest.fn(() => ({})),
CaretAdapter: jest.fn(() => ({
attachBlock: jest.fn(),
userCaretIndex: undefined as { blockIndex: number } | undefined,
})),
FormattingAdapter: jest.fn(() => ({})),
}));
/**
* Mock ToolsManager — tools rendering is not part of this integration scope
*/
jest.unstable_mockModule('../tools/ToolsManager', () => ({
default: jest.fn(() => ({
blockTools: {
get: jest.fn((name: string) => ({
name,
create: jest.fn(() => ({
render: jest.fn(() => Promise.resolve(document.createElement('div'))),
})),
})),
},
})),
}));
// Import real model (no mock) and mocked adapters
const { EditorJSModel, EventType, BlockAddedEvent, BlockRemovedEvent } = await import('@editorjs/model');
const { EventBus } = await import('@editorjs/sdk');
const ToolsManager = (await import('../tools/ToolsManager')).default;
const { BlocksManager } = await import('../components/BlockManager.js');
const { BlocksAPI } = await import('./BlocksAPI.js');
describe('BlocksAPI integration (real model, mocked DOM adapters)', () => {
let model: InstanceType<typeof EditorJSModel>;
let eventBus: InstanceType<typeof EventBus>;
let blocksManager: BlocksManager;
let blocksAPI: InstanceType<typeof BlocksAPI>;
const config = {
defaultBlock: 'paragraph',
userId: USER_ID,
documentId: DOCUMENT_ID,
holder: {} as HTMLElement,
} as CoreConfigValidated;
beforeEach(() => {
model = new EditorJSModel(USER_ID, { identifier: DOCUMENT_ID });
eventBus = new EventBus();
// @ts-expect-error — mock constructor
const toolsManager = new ToolsManager();
blocksManager = new BlocksManager(
model,
eventBus,
toolsManager,
config
);
blocksAPI = new BlocksAPI(blocksManager, config);
});
afterEach(() => {
jest.restoreAllMocks();
});
describe('insert()', () => {
it('should add a block to an empty document and model.length becomes 1', () => {
blocksAPI.insert('paragraph', {});
expect(model.length).toBe(1);
expect(model.serialized.blocks[0]).toEqual(
expect.objectContaining({ name: 'paragraph' })
);
});
it('should insert a block at the specified index', () => {
blocksAPI.insert('paragraph');
blocksAPI.insert('paragraph');
blocksAPI.insert('header', { text: 'Title' }, 1);
expect(model.length).toBe(3);
expect(model.serialized.blocks[1]).toEqual(
expect.objectContaining({ name: 'header' })
);
});
it('should use the default block type when type is omitted', () => {
blocksAPI.insert();
expect(model.length).toBe(1);
expect(model.serialized.blocks[0]).toEqual(
expect.objectContaining({ name: 'paragraph' })
);
});
it('should replace a block at the given index when replace flag is set', () => {
blocksAPI.insert('paragraph');
blocksAPI.insert('paragraph');
blocksAPI.insert('header', {}, 0, undefined, true);
expect(model.length).toBe(2);
expect(model.serialized.blocks[0]).toEqual(
expect.objectContaining({ name: 'header' })
);
});
});
describe('insertMany()', () => {
it('should insert multiple blocks at the specified index', () => {
blocksAPI.insert('paragraph');
blocksAPI.insertMany(
[
{
name: 'header',
data: {},
},
{
name: 'image',
data: {},
},
],
0
);
expect(model.length).toBe(3);
expect(model.serialized.blocks[0]).toEqual(expect.objectContaining({ name: 'header' }));
expect(model.serialized.blocks[1]).toEqual(expect.objectContaining({ name: 'image' }));
expect(model.serialized.blocks[2]).toEqual(expect.objectContaining({ name: 'paragraph' }));
});
it('should append blocks at the end when index is omitted', () => {
blocksAPI.insert('paragraph');
blocksAPI.insertMany([
{
name: 'header',
data: {},
},
{
name: 'list',
data: {},
},
]);
expect(model.length).toBe(3);
expect(model.serialized.blocks[1]).toEqual(expect.objectContaining({ name: 'header' }));
expect(model.serialized.blocks[2]).toEqual(expect.objectContaining({ name: 'list' }));
});
});
describe('delete()', () => {
it('should remove a block at the given index', () => {
blocksAPI.insert('paragraph');
blocksAPI.insert('header', {}, 1);
blocksAPI.insert('list', {}, 2);
blocksAPI.delete(1);
expect(model.length).toBe(2);
expect(model.serialized.blocks[0]).toEqual(expect.objectContaining({ name: 'paragraph' }));
expect(model.serialized.blocks[1]).toEqual(expect.objectContaining({ name: 'list' }));
});
it('should remove the first block when index is 0', () => {
blocksAPI.insert('paragraph');
blocksAPI.insert('header', {}, 1);
blocksAPI.delete(0);
expect(model.length).toBe(1);
expect(model.serialized.blocks[0]).toEqual(expect.objectContaining({ name: 'header' }));
});
it('should throw when no index is provided and no caret is set', () => {
blocksAPI.insert('paragraph');
expect(() => blocksAPI.delete()).toThrow('No block selected to delete');
});
});
describe('move()', () => {
it('should move a block from fromIndex to toIndex (forward)', () => {
blocksAPI.insert('a');
blocksAPI.insert('b', {}, 1);
blocksAPI.insert('c', {}, 2);
// Move block 0 ("a") to index 2
blocksAPI.move(2, 0);
expect(model.serialized.blocks.map(b => b.name)).toEqual(['b', 'c', 'a']);
});
it('should move a block from fromIndex to toIndex (backward)', () => {
blocksAPI.insert('a');
blocksAPI.insert('b', {}, 1);
blocksAPI.insert('c', {}, 2);
blocksAPI.move(0, 2);
expect(model.serialized.blocks.map(b => b.name)).toEqual(['c', 'a', 'b']);
});
it('should not change anything when fromIndex equals toIndex', () => {
blocksAPI.insert('a');
blocksAPI.insert('b', {}, 1);
blocksAPI.move(0, 0);
expect(model.serialized.blocks.map(b => b.name)).toEqual(['a', 'b']);
});
it('should throw when no fromIndex is provided and no caret is set', () => {
blocksAPI.insert('paragraph');
expect(() => blocksAPI.move(0)).toThrow('No block selected to move');
});
});
describe('getBlocksCount()', () => {
it('should return 0 for an empty document', () => {
expect(blocksAPI.getBlocksCount()).toBe(0);
});
it('should return the correct count after insertions and deletions', () => {
blocksAPI.insert('paragraph');
blocksAPI.insert('paragraph');
blocksAPI.insert('paragraph');
expect(blocksAPI.getBlocksCount()).toBe(3);
blocksAPI.delete(0);
expect(blocksAPI.getBlocksCount()).toBe(2);
});
});
describe('render()', () => {
it('should replace document content with the provided serialized data', () => {
blocksAPI.insert('paragraph');
blocksAPI.insert('paragraph');
blocksAPI.render({
identifier: 'new-doc',
blocks: [
{
id: 'mock',
name: 'header',
data: {},
},
],
properties: {},
});
expect(model.length).toBe(1);
expect(model.serialized.blocks[0]).toEqual(expect.objectContaining({ name: 'header' }));
});
it('should result in an empty document when empty blocks array is passed', () => {
blocksAPI.insert('paragraph');
blocksAPI.render({
identifier: 'empty-doc',
blocks: [],
properties: {},
});
expect(model.length).toBe(0);
});
});
describe('clear()', () => {
it('should remove all blocks from the document', () => {
blocksAPI.insert('paragraph');
blocksAPI.insert('header', {}, 1);
blocksAPI.insert('list', {}, 2);
blocksAPI.clear();
expect(model.length).toBe(0);
expect(model.serialized.blocks).toEqual([]);
});
it('should be safe to call on an already empty document', () => {
blocksAPI.clear();
expect(model.length).toBe(0);
});
});
describe('model events', () => {
it('should emit BlockAddedEvent on model when insert is called', () => {
const handler = jest.fn();
model.addEventListener(EventType.Changed, handler);
blocksAPI.insert('paragraph');
expect(handler).toHaveBeenCalled();
expect(handler).toHaveBeenCalledWith(expect.any(BlockAddedEvent));
});
it('should emit BlockRemovedEvent on model when delete is called', async () => {
blocksAPI.insert('paragraph');
const handler = jest.fn();
model.addEventListener(EventType.Changed, handler);
blocksAPI.delete(0);
await Promise.resolve(); // flush queueMicrotask used by removeBlock
expect(handler).toHaveBeenCalledWith(expect.any(BlockRemovedEvent));
});
});
// ---------- combined operations ----------
describe('combined operations', () => {
it('should handle a sequence of insert, move, delete, and clear', () => {
// Insert 3 blocks: a, b, c
blocksAPI.insert('a');
blocksAPI.insert('b', {}, 1);
blocksAPI.insert('c', {}, 2);
expect(model.serialized.blocks.map(b => b.name)).toEqual(['a', 'b', 'c']);
// Move c to front: c, a, b
blocksAPI.move(0, 2);
expect(model.serialized.blocks.map(b => b.name)).toEqual(['c', 'a', 'b']);
// Delete middle block (a): c, b
blocksAPI.delete(1);
expect(model.serialized.blocks.map(b => b.name)).toEqual(['c', 'b']);
// Insert d at index 1: c, d, b
blocksAPI.insert('d', {}, 1);
expect(model.serialized.blocks.map(b => b.name)).toEqual(['c', 'd', 'b']);
// Clear everything
blocksAPI.clear();
expect(model.length).toBe(0);
});
it('should support render after clear and then further mutations', () => {
blocksAPI.insert('paragraph');
blocksAPI.clear();
blocksAPI.render({
identifier: 'doc-2',
blocks: [
{
id: 'mock-header',
name: 'header',
data: {},
},
{
id: 'mock-list',
name: 'list',
data: {},
},
],
properties: {},
});
expect(model.length).toBe(2);
blocksAPI.delete(0);
expect(model.length).toBe(1);
expect(model.serialized.blocks[0]).toEqual(expect.objectContaining({ name: 'list' }));
});
});
});