-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathgridstack-engine-spec.ts
More file actions
405 lines (358 loc) · 14.5 KB
/
gridstack-engine-spec.ts
File metadata and controls
405 lines (358 loc) · 14.5 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
404
405
import { GridStackEngine } from'../src/gridstack-engine';
import { GridStackNode } from'../src/types';
describe('gridstack engine:', () => {
'use strict';
let e: GridStackEngine;
let ePriv: any; // cast engine for private vars access
let findNode = function(id: string) {
return e.nodes.find(n => n.id === id);
};
it('should exist setup function.', () => {
expect(GridStackEngine).not.toBeNull();
expect(typeof GridStackEngine).toBe('function');
});
describe('test constructor >', () => {
it('should be setup properly', () => {
ePriv = e = new GridStackEngine();
expect(e.column).toEqual(12);
expect(e.float).toEqual(false);
expect(e.maxRow).toEqual(undefined!);
expect(e.nodes).toEqual([]);
expect(e.batchMode).toEqual(undefined!);
expect(ePriv.onChange).toEqual(undefined);
});
it('should set params correctly.', () => {
let fkt = () => { };
let arr: any = [1,2,3];
ePriv = e = new GridStackEngine({column: 1, onChange:fkt, float:true, maxRow:2, nodes:arr});
expect(e.column).toEqual(1);
expect(e.float).toBe(true);
expect(e.maxRow).toEqual(2);
expect(e.nodes).toEqual(arr);
expect(e.batchMode).toEqual(undefined);
expect(ePriv.onChange).toEqual(fkt);
});
});
describe('batch update', () => {
it('should set float and batchMode when calling batchUpdate.', () => {
ePriv = e = new GridStackEngine({float: true});
e.batchUpdate();
expect(e.float).toBe(true);
expect(e.batchMode).toBe(true);
});
});
describe('test prepareNode >', () => {
beforeAll(() => {
ePriv = e = new GridStackEngine();
});
it('should prepare a node', () => {
expect(e.prepareNode({}, false)).toEqual(expect.objectContaining({x: 0, y: 0, h: 1}));
expect(e.prepareNode({x: 10}, false)).toEqual(expect.objectContaining({x: 10, y: 0, h: 1}));
expect(e.prepareNode({x: -10}, false)).toEqual(expect.objectContaining({x: 0, y: 0, h: 1}));
expect(e.prepareNode({y: 10}, false)).toEqual(expect.objectContaining({x: 0, y: 10, h: 1}));
expect(e.prepareNode({y: -10}, false)).toEqual(expect.objectContaining({x: 0, y: 0, h: 1}));
expect(e.prepareNode({w: 3}, false)).toEqual(expect.objectContaining({x: 0, y: 0, w: 3, h: 1}));
expect(e.prepareNode({w: 100}, false)).toEqual(expect.objectContaining({x: 0, y: 0, w: 12, h: 1}));
expect(e.prepareNode({w: 0}, false)).toEqual(expect.objectContaining({x: 0, y: 0, h: 1}));
expect(e.prepareNode({w: -190}, false)).toEqual(expect.objectContaining({x: 0, y: 0, h: 1}));
expect(e.prepareNode({h: 3}, false)).toEqual(expect.objectContaining({x: 0, y: 0, h: 3}));
expect(e.prepareNode({h: 0}, false)).toEqual(expect.objectContaining({x: 0, y: 0, h: 1}));
expect(e.prepareNode({h: -10}, false)).toEqual(expect.objectContaining({x: 0, y: 0, h: 1}));
expect(e.prepareNode({x: 4, w: 10}, false)).toEqual(expect.objectContaining({x: 2, y: 0, w: 10, h: 1}));
expect(e.prepareNode({x: 4, w: 10}, true)).toEqual(expect.objectContaining({x: 4, y: 0, w: 8, h: 1}));
});
});
describe('sorting of nodes >', () => {
beforeAll(() => {
ePriv = e = new GridStackEngine();
e.nodes = [{x: 7, y: 0}, {x: 4, y: 4}, {x: 9, y: 0}, {x: 0, y: 1}];
});
it('should sort ascending with 12 columns.', () => {
e.sortNodes(1);
expect(e.nodes).toEqual([{x: 7, y: 0}, {x: 9, y: 0}, {x: 0, y: 1}, {x: 4, y: 4}]);
});
it('should sort descending with 12 columns.', () => {
e.sortNodes(-1);
expect(e.nodes).toEqual([{x: 4, y: 4}, {x: 0, y: 1}, {x: 9, y: 0}, {x: 7, y: 0}]);
});
it('should sort ascending without columns.', () => {
ePriv.column = undefined;
e.sortNodes(1);
expect(e.nodes).toEqual([{x: 7, y: 0}, {x: 9, y: 0}, {x: 0, y: 1}, {x: 4, y: 4}]);
});
it('should sort descending without columns.', () => {
ePriv.column = undefined;
e.sortNodes(-1);
expect(e.nodes).toEqual([{x: 4, y: 4}, {x: 0, y: 1}, {x: 9, y: 0}, {x: 7, y: 0}]);
});
});
describe('test isAreaEmpty >', () => {
beforeAll(() => {
ePriv = e = new GridStackEngine({float:true});
e.nodes = [
e.prepareNode({x: 3, y: 2, w: 3, h: 2})
];
});
it('should be true', () => {
expect(e.isAreaEmpty(0, 0, 3, 2)).toEqual(true);
expect(e.isAreaEmpty(3, 4, 3, 2)).toEqual(true);
});
it('should be false', () => {
expect(e.isAreaEmpty(1, 1, 3, 2)).toEqual(false);
expect(e.isAreaEmpty(2, 3, 3, 2)).toEqual(false);
});
});
describe('test cleanNodes/getDirtyNodes >', () => {
beforeAll(() => {
ePriv = e = new GridStackEngine({float:true});
e.nodes = [
e.prepareNode({x: 0, y: 0, id: '1', _dirty: true}),
e.prepareNode({x: 3, y: 2, w: 3, h: 2, id: '2', _dirty: true}),
e.prepareNode({x: 3, y: 7, w: 3, h: 2, id: '3'})
];
});
beforeEach(() => {
delete ePriv.batchMode;
});
it('should return all dirty nodes', () => {
let nodes = e.getDirtyNodes();
expect(nodes.length).toEqual(2);
expect(nodes[0].id).toEqual('1');
expect(nodes[1].id).toEqual('2');
});
it('should\'n clean nodes if batchMode true', () => {
e.batchMode = true;
e.cleanNodes();
expect(e.getDirtyNodes().length).toBeGreaterThan(0);
});
it('should clean all dirty nodes', () => {
e.cleanNodes();
expect(e.getDirtyNodes().length).toEqual(0);
});
});
describe('test batchUpdate/commit >', () => {
beforeAll(() => {
ePriv = e = new GridStackEngine();
});
it('should work on not float grids', () => {
expect(e.float).toEqual(false);
e.batchUpdate();
e.batchUpdate(); // double for code coverage
expect(e.batchMode).toBe(true);
expect(e.float).toEqual(true);
e.batchUpdate(false);
e.batchUpdate(false);
expect(e.batchMode).not.toBe(true);
expect(e.float).not.toBe(true);
});
it('should work on float grids', () => {
e.float = true;
e.batchUpdate();
expect(e.batchMode).toBe(true);
expect(e.float).toEqual(true);
e.batchUpdate(false);
expect(e.batchMode).not.toBe(true);
expect(e.float).toEqual(true);
});
});
describe('test batchUpdate/commit >', () => {
beforeAll(() => {
ePriv = e = new GridStackEngine({float:true});
});
it('should work on float grids', () => {
expect(e.float).toEqual(true);
e.batchUpdate();
expect(e.batchMode).toBe(true);
expect(e.float).toEqual(true);
e.batchUpdate(false);
expect(e.batchMode).not.toBe(true);
expect(e.float).toEqual(true);
});
});
describe('test _notify >', () => {
let spy;
beforeEach(() => {
spy = {
callback: () => {}
};
vi.spyOn(spy,'callback');
ePriv = e = new GridStackEngine({float:true, onChange: spy.callback});
e.nodes = [
e.prepareNode({x: 0, y: 0, id: '1', _dirty: true}),
e.prepareNode({x: 3, y: 2, w: 3, h: 2, id: '2', _dirty: true}),
e.prepareNode({x: 3, y: 7, w: 3, h: 2, id: '3'})
];
});
it('should\'n be called if batchMode true', () => {
e.batchMode = true;
ePriv._notify();
expect(spy.callback).not.toHaveBeenCalled();
});
it('should by called with dirty nodes', () => {
ePriv._notify();
expect(spy.callback).toHaveBeenCalledWith([e.nodes[0], e.nodes[1]]);
});
it('should by called with extra passed node to be removed', () => {
let n1 = {id: -1};
ePriv._notify([n1]);
expect(spy.callback).toHaveBeenCalledWith([n1, e.nodes[0], e.nodes[1]]);
});
});
describe('test _packNodes >', () => {
describe('using float:false mode >', () => {
beforeEach(() => {
ePriv = e = new GridStackEngine({float:false});
});
it('shouldn\'t pack one node with y coord eq 0', () => {
e.nodes = [
e.prepareNode({x: 0, y: 0, w:1, h:1, id: '1'}),
];
ePriv._packNodes();
expect(findNode('1')).toEqual(expect.objectContaining({x: 0, y: 0, h: 1}));
expect(findNode('1')!._dirty).toBeFalsy();
});
it('should pack one node correctly', () => {
e.nodes = [
e.prepareNode({x: 0, y: 1, w:1, h:1, id: '1'}),
];
ePriv._packNodes();
expect(findNode('1')).toEqual(expect.objectContaining({x: 0, y: 0, _dirty: true}));
});
it('should pack nodes correctly', () => {
e.nodes = [
e.prepareNode({x: 0, y: 1, w:1, h:1, id: '1'}),
e.prepareNode({x: 0, y: 5, w:1, h:1, id: '2'}),
];
ePriv._packNodes();
expect(findNode('1')).toEqual(expect.objectContaining({x: 0, y: 0, _dirty: true}));
expect(findNode('2')).toEqual(expect.objectContaining({x: 0, y: 1, _dirty: true}));
});
it('should pack reverse nodes correctly', () => {
e.nodes = [
e.prepareNode({x: 0, y: 5, w:1, h:1, id: '1'}),
e.prepareNode({x: 0, y: 1, w:1, h:1, id: '2'}),
];
ePriv._packNodes();
expect(findNode('2')).toEqual(expect.objectContaining({x: 0, y: 0, _dirty: true}));
expect(findNode('1')).toEqual(expect.objectContaining({x: 0, y: 1, _dirty: true}));
});
it('should respect locked nodes', () => {
e.nodes = [
e.prepareNode({x: 0, y: 1, w:1, h:1, id: '1', locked: true}),
e.prepareNode({x: 0, y: 5, w:1, h:1, id: '2'}),
];
ePriv._packNodes();
expect(findNode('1')).toEqual(expect.objectContaining({x: 0, y: 1, h: 1}));
expect(findNode('1')!._dirty).toBeFalsy();
expect(findNode('2')).toEqual(expect.objectContaining({x: 0, y: 2, _dirty: true}));
});
});
});
describe('test changedPos >', () => {
beforeAll(() => {
ePriv = e = new GridStackEngine();
});
it('should return true for changed x', () => {
let widget = { x: 1, y: 2, w: 3, h: 4 };
expect(e.changedPosConstrain(widget, {x:2, y:2})).toEqual(true);
});
it('should return true for changed y', () => {
let widget = { x: 1, y: 2, w: 3, h: 4 };
expect(e.changedPosConstrain(widget, {x:1, y:1})).toEqual(true);
});
it('should return true for changed width', () => {
let widget = { x: 1, y: 2, w: 3, h: 4 };
expect(e.changedPosConstrain(widget, {x:2, y:2, w:4, h:4})).toEqual(true);
});
it('should return true for changed height', () => {
let widget = { x: 1, y: 2, w: 3, h: 4 };
expect(e.changedPosConstrain(widget, {x:1, y:2, w:3, h:3})).toEqual(true);
});
it('should return false for unchanged position', () => {
let widget = { x: 1, y: 2, w: 3, h: 4 };
expect(e.changedPosConstrain(widget, {x:1, y:2, w:3, h:4})).toEqual(false);
});
});
describe('test locked widget >', () => {
beforeAll(() => {
ePriv = e = new GridStackEngine();
});
it('should add widgets around locked one', () => {
let nodes: GridStackNode[] = [
{x: 0, y: 1, w: 12, h: 1, locked: true, noMove: true, noResize: true, id: '0'},
{x: 1, y: 0, w: 2, h: 3, id: '1'}
];
// add locked item
e.addNode(nodes[0])
expect(findNode('0')).toEqual(expect.objectContaining({x: 0, y: 1, w: 12, h: 1, locked: true}));
// add item that moves past locked one
e.addNode(nodes[1])
expect(findNode('0')).toEqual(expect.objectContaining({x: 0, y: 1, w: 12, h: 1, locked: true}));
expect(findNode('1')).toEqual(expect.objectContaining({x: 1, y: 2, h: 3}));
// locked item can still be moved directly (what user does)
let node0 = findNode('0');
expect(e.moveNode(node0!, {y:6})).toEqual(true);
expect(findNode('0')).toEqual(expect.objectContaining({x: 0, y: 6, h: 1, locked: true}));
// but moves regular one past it
let node1 = findNode('1');
expect(e.moveNode(node1!, {x:6, y:6})).toEqual(true);
expect(node1).toEqual(expect.objectContaining({x: 6, y: 7, w: 2, h: 3}));
// but moves regular one before (gravity ON)
e.float = false;
expect(e.moveNode(node1!, {x:7, y:3})).toEqual(true);
expect(node1).toEqual(expect.objectContaining({x: 7, y: 0, w: 2, h: 3}));
// but moves regular one before (gravity OFF)
e.float = true;
expect(e.moveNode(node1!, {x:7, y:3})).toEqual(true);
expect(node1).toEqual(expect.objectContaining({x: 7, y: 3, w: 2, h: 3}));
});
});
describe('test columnChanged >', () => {
beforeAll(() => {
});
it('12 to 1 and back', () => {
ePriv = e = new GridStackEngine({ column: 12 });
// Add two side-by-side components 6+6 = 12 columns
const left = e.addNode({ x: 0, y: 0, w: 6, h: 1, id: 'left' });
const right = e.addNode({ x: 6, y: 0, w: 6, h: 1, id: 'right' });
expect(left).toEqual(expect.objectContaining({x: 0, y: 0, w: 6, h: 1}));
expect(right).toEqual(expect.objectContaining({x: 6, y: 0, w: 6, h: 1}));
// Resize to 1 column
e.column = 1;
e.columnChanged(12, 1);
expect(left).toEqual(expect.objectContaining({x: 0, y: 0, w: 1, h: 1}));
expect(right).toEqual(expect.objectContaining({x: 0, y: 1, w: 1, h: 1}));
// Resize back to 12 column
e.column = 12;
e.columnChanged(1, 12);
expect(left).toEqual(expect.objectContaining({x: 0, y: 0, w: 6, h: 1}));
expect(right).toEqual(expect.objectContaining({x: 6, y: 0, w: 6, h: 1}));
});
it('24 column to 1 and back', () => {
ePriv = e = new GridStackEngine({ column: 24 });
// Add two side-by-side components 12+12 = 24 columns
const left = e.addNode({ x: 0, y: 0, w: 12, h: 1, id: 'left' });
const right = e.addNode({ x: 12, y: 0, w: 12, h: 1, id: 'right' });
expect(left).toEqual(expect.objectContaining({x: 0, y: 0, w: 12, h: 1}));
expect(right).toEqual(expect.objectContaining({x: 12, y: 0, w: 12, h: 1}));
// Resize to 1 column
e.column = 1;
e.columnChanged(24, 1);
expect(left).toEqual(expect.objectContaining({x: 0, y: 0, w: 1, h: 1}));
expect(right).toEqual(expect.objectContaining({x: 0, y: 1, w: 1, h: 1}));
// Resize back to 24 column
e.column = 24;
e.columnChanged(1, 24);
expect(left).toEqual(expect.objectContaining({x: 0, y: 0, w: 12, h: 1}));
expect(right).toEqual(expect.objectContaining({x: 12, y: 0, w: 12, h: 1}));
});
});
describe('test compact >', () => {
beforeAll(() => {
ePriv = e = new GridStackEngine();
});
it('do nothing', () => {
e.compact();
});
});
});