-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBlockManager.spec.ts
More file actions
308 lines (259 loc) · 8.07 KB
/
BlockManager.spec.ts
File metadata and controls
308 lines (259 loc) · 8.07 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
/* eslint-disable @stylistic/comma-dangle,@typescript-eslint/naming-convention */
import { beforeEach, jest } from '@jest/globals';
import type { CoreConfigValidated } from '@editorjs/sdk';
const BLOCKS_COUNT = 7;
const USER_ID = 'user';
jest.unstable_mockModule('@editorjs/sdk', () => ({
EventBus: jest.fn(),
}));
// Register ESM mocks before importing the module under test
jest.unstable_mockModule('@editorjs/model', () => {
const EditorJSModel = jest.fn(() => ({
serialized: { blocks: [] },
addEventListener: jest.fn(),
addBlock: jest.fn(),
removeBlock: jest.fn(),
initializeDocument: jest.fn(),
clearBlocks: jest.fn(),
getCaret: jest.fn(),
getBlockSerialized: jest.fn(),
get length() {
return BLOCKS_COUNT;
},
}));
const EventBus = jest.fn(() => ({ dispatchEvent: jest.fn() }));
const EventType = { Changed: 'changed' };
return {
EditorJSModel,
EventBus,
EventType,
};
});
jest.unstable_mockModule('../tools/ToolsManager', () => ({
default: jest.fn(() => ({
blockTools: {
get: jest.fn(),
},
})),
}));
// Now import the modules (they will receive the mocks registered above)
const { EditorJSModel, EventBus } = await import('@editorjs/model');
const ToolsManager = (await import('../tools/ToolsManager')).default;
const { BlocksManager } = await import('./BlockManager.js');
describe('BlocksManager (unit, mocked deps)', () => {
// @ts-expect-error - mock object, dont need to pass any arguments
const model = new EditorJSModel();
const eventBus = new EventBus();
// @ts-expect-error - Mock instance
const toolsManager = new ToolsManager();
const defaultBlock = 'paragraph';
const blocksManager = new BlocksManager(
model,
eventBus,
toolsManager,
{ defaultBlock,
userId: USER_ID } as CoreConfigValidated
);
beforeEach(() => {
jest.resetAllMocks();
});
describe('.blocksCount', () => {
it('should proxy model.length', () => {
expect(blocksManager.blocksCount).toBe(BLOCKS_COUNT);
});
});
describe('.insert()', () => {
it('should call model.addBlock with default tool name and computed index', () => {
blocksManager.insert();
expect(model.addBlock).toHaveBeenCalledTimes(1);
expect(model.addBlock).toHaveBeenCalledWith(
USER_ID,
expect.objectContaining({
name: 'paragraph'
}),
BLOCKS_COUNT
);
expect(model.removeBlock).not.toHaveBeenCalled();
});
it('should use explicit index when provided', () => {
blocksManager.insert({
index: 2,
type: 'paragraph'
});
expect(model.addBlock).toHaveBeenCalledWith(
USER_ID,
expect.objectContaining({
name: 'paragraph'
}),
2
);
});
it('should call removeBlock then addBlock when replace is true and index is provided', () => {
blocksManager.insert({
type: 'new',
index: 0,
replace: true
});
expect(model.removeBlock).toHaveBeenCalledWith(USER_ID, 0);
expect(model.addBlock).toHaveBeenCalledWith(
USER_ID,
expect.objectContaining({
name: 'new'
}),
0
);
});
it('should call model.addBlock when focus is true', () => {
blocksManager.insert({ focus: true });
expect(model.addBlock).toHaveBeenCalledTimes(1);
expect(model.addBlock).toHaveBeenCalledWith(
USER_ID,
expect.objectContaining({ name: 'paragraph' }),
BLOCKS_COUNT
);
});
it('should use model.length as insertion/removal index when replace is true and index is omitted', () => {
blocksManager.insert({
replace: true
});
expect(model.removeBlock).toHaveBeenCalledWith(USER_ID, BLOCKS_COUNT - 1);
expect(model.addBlock).toHaveBeenCalledWith(
USER_ID,
expect.objectContaining({
name: 'paragraph'
}),
BLOCKS_COUNT - 1
);
});
});
describe('.insertMany()', () => {
it('should call model.addBlock for each block with increasing indexes', () => {
blocksManager.insertMany([
{
name: 'one',
data: {}
},
{
name: 'two',
data: {}
}
], 1);
expect(model.addBlock).toHaveBeenCalledTimes(2);
expect(model.addBlock).toHaveBeenNthCalledWith(
1,
USER_ID,
{
name: 'one',
data: {}
},
1
);
expect(model.addBlock).toHaveBeenNthCalledWith(
2,
USER_ID,
{
name: 'two',
data: {}
},
2
);
});
it('should use model.length as start index when index is omitted', () => {
blocksManager.insertMany([
{
name: 'first',
data: {}
},
{
name: 'second',
data: {}
}
]);
expect(model.addBlock).toHaveBeenNthCalledWith(
1,
USER_ID,
{
name: 'first',
data: {}
},
BLOCKS_COUNT
);
expect(model.addBlock).toHaveBeenNthCalledWith(
2,
USER_ID,
{
name: 'second',
data: {}
},
BLOCKS_COUNT + 1
);
});
});
describe('.render()', () => {
it('should call model.initializeDocument with provided document', () => {
const doc = {
identifier: 'doc',
blocks: [
{
id: 'mock',
name: 'x',
data: {}
}
],
properties: {}
};
blocksManager.render(doc);
expect(model.initializeDocument).toHaveBeenCalledWith(doc);
});
});
describe('.clear()', () => {
it('should call model.clearBlocks', () => {
blocksManager.clear();
expect(model.clearBlocks).toHaveBeenCalled();
});
});
describe('.deleteBlock()', () => {
it('should throw when no caret and no index is provided', () => {
model.getCaret = jest.fn(() => undefined);
expect(() => blocksManager.deleteBlock()).toThrow('No block selected to delete');
});
it('should call model.removeBlock with provided index', () => {
blocksManager.deleteBlock(0);
expect(model.removeBlock).toHaveBeenCalledWith(USER_ID, 0);
});
it('should call model.getCaret with the configured userId to resolve current block', () => {
// @ts-expect-error - mock return value does not need full Caret shape
model.getCaret = jest.fn(() => ({ index: { blockIndex: 2 } }));
blocksManager.deleteBlock();
expect(model.getCaret).toHaveBeenCalledWith(USER_ID);
expect(model.removeBlock).toHaveBeenCalledWith(USER_ID, 2);
});
});
describe('.move()', () => {
it('should call removeBlock and addBlock when moving current block forward', () => {
// @ts-expect-error - mock return value does not need full Caret shape
model.getCaret = jest.fn(() => ({ index: { blockIndex: 0 } }));
// @ts-expect-error - mock return value does not need full BlockNodeSerialized shape
model.getBlockSerialized = jest.fn(() => ({ name: 'a' }));
blocksManager.move(2);
expect(model.removeBlock).toHaveBeenCalledWith(USER_ID, 0);
expect(model.addBlock).toHaveBeenCalledWith(USER_ID, { name: 'a' }, 2);
});
it('should throw when there is no current block and no index provided', () => {
model.getCaret = jest.fn(() => undefined);
expect(() => blocksManager.move(1)).toThrow('No block selected to move');
});
it('should pass toIndex directly when toIndex is less than fromIndex', () => {
// @ts-expect-error - mock return value does not need full BlockNodeSerialized shape
model.getBlockSerialized = jest.fn(() => ({ name: 'c' }));
blocksManager.move(0, 2);
expect(model.removeBlock).toHaveBeenCalledWith(USER_ID, 2);
expect(model.addBlock).toHaveBeenCalledWith(USER_ID, { name: 'c' }, 0);
});
it('should do nothing when toIndex equals fromIndex', () => {
blocksManager.move(1, 1);
expect(model.removeBlock).not.toHaveBeenCalled();
expect(model.addBlock).not.toHaveBeenCalled();
});
});
});