Repository navigation
Expand file tree
/
Copy pathgridstack-engine-spec.ts
More file actions
383 lines (335 loc) · 14.3 KB
/
Copy pathgridstack-engine-spec.ts
File metadata and controls
383 lines (335 loc) · 14.3 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
import { GridStackEngine } from '../src/gridstack-engine';
import { GridStackNode } from '../src/types';
describe('gridstack engine', function() {
'use strict';
let engine: GridStackEngine;
// old hacky JS code that's not happy in TS. just cast to `any` and skip warnings
let e: any = GridStackEngine;
let w: any = window;
let findNode = function(engine, id) {
return engine.nodes.find((i) => i.id === id);
};
it('should exist setup function.', function() {
expect(e).not.toBeNull();
expect(typeof e).toBe('function');
});
describe('test constructor', function() {
it('should be setup properly', function() {
engine = new GridStackEngine();
expect(engine.column).toEqual(12);
expect(engine.float).toEqual(false);
expect(engine.maxRow).toEqual(undefined);
expect(engine.nodes).toEqual([]);
expect(engine.batchMode).toEqual(undefined);
expect((engine as any).onChange).toEqual(undefined);
});
it('should set params correctly.', function() {
let fkt = function() { };
let arr: any = [1,2,3];
engine = new GridStackEngine({column: 1, onChange:fkt, float:true, maxRow:2, nodes:arr});
expect(engine.column).toEqual(1);
expect(engine.float).toBe(true);
expect(engine.maxRow).toEqual(2);
expect(engine.nodes).toEqual(arr);
expect(engine.batchMode).toEqual(undefined);
expect((engine as any).onChange).toEqual(fkt);
});
});
describe('batch update', function() {
it('should set float and batchMode when calling batchUpdate.', function() {
engine = new GridStackEngine({float: true});
engine.batchUpdate();
expect(engine.float).toBe(true);
expect(engine.batchMode).toBeTrue();
});
});
describe('test prepareNode', function() {
beforeAll(function() {
engine = new GridStackEngine();
});
it('should prepare a node', function() {
expect(engine.prepareNode({}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, h: 1}));
expect(engine.prepareNode({x: 10}, false)).toEqual(jasmine.objectContaining({x: 10, y: 0, h: 1}));
expect(engine.prepareNode({x: -10}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, h: 1}));
expect(engine.prepareNode({y: 10}, false)).toEqual(jasmine.objectContaining({x: 0, y: 10, h: 1}));
expect(engine.prepareNode({y: -10}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, h: 1}));
expect(engine.prepareNode({w: 3}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, w: 3, h: 1}));
expect(engine.prepareNode({w: 100}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, w: 12, h: 1}));
expect(engine.prepareNode({w: 0}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, h: 1}));
expect(engine.prepareNode({w: -190}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, h: 1}));
expect(engine.prepareNode({h: 3}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, h: 3}));
expect(engine.prepareNode({h: 0}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, h: 1}));
expect(engine.prepareNode({h: -10}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, h: 1}));
expect(engine.prepareNode({x: 4, w: 10}, false)).toEqual(jasmine.objectContaining({x: 2, y: 0, w: 10, h: 1}));
expect(engine.prepareNode({x: 4, w: 10}, true)).toEqual(jasmine.objectContaining({x: 4, y: 0, w: 8, h: 1}));
});
});
describe('sorting of nodes', function() {
// Note: legacy weird call on global window to hold data
it('should sort ascending with 12 columns.', function() {
w.column = 12;
w.nodes = [{x: 7, y: 0}, {x: 4, y: 4}, {x: 9, y: 0}, {x: 0, y: 1}];
engine.sortNodes.call(w, 1);
expect(w.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.', function() {
w.column = 12;
w.nodes = [{x: 7, y: 0}, {x: 4, y: 4}, {x: 9, y: 0}, {x: 0, y: 1}];
engine.sortNodes.call(w, -1);
expect(w.nodes).toEqual([{x: 4, y: 4}, {x: 0, y: 1}, {x: 9, y: 0}, {x: 7, y: 0}]);
});
it('should sort ascending with 1 columns.', function() {
w.column = 1;
w.nodes = [{x: 7, y: 0}, {x: 4, y: 4}, {x: 9, y: 0}, {x: 0, y: 1}];
engine.sortNodes.call(w, 1);
expect(w.nodes).toEqual([{x: 0, y: 1}, {x: 7, y: 0}, {x: 4, y: 4}, {x: 9, y: 0}]);
});
it('should sort descending with 1 columns.', function() {
w.column = 1;
w.nodes = [{x: 7, y: 0}, {x: 4, y: 4}, {x: 9, y: 0}, {x: 0, y: 1}];
engine.sortNodes.call(w, -1);
expect(w.nodes).toEqual([{x: 9, y: 0}, {x: 4, y: 4}, {x: 7, y: 0}, {x: 0, y: 1}]);
});
it('should sort ascending without columns.', function() {
w.column = undefined;
w.nodes = [{x: 7, y: 0, w: 1}, {x: 4, y: 4, w: 1}, {x: 9, y: 0, w: 1}, {x: 0, y: 1, w: 1}];
engine.sortNodes.call(w, 1);
expect(w.nodes).toEqual([{x: 7, y: 0, w: 1}, {x: 9, y: 0, w: 1}, {x: 0, y: 1, w: 1}, {x: 4, y: 4, w: 1}]);
});
it('should sort descending without columns.', function() {
w.column = undefined;
w.nodes = [{x: 7, y: 0, w: 1}, {x: 4, y: 4, w: 1}, {x: 9, y: 0, w: 1}, {x: 0, y: 1, w: 1}];
engine.sortNodes.call(w, -1);
expect(w.nodes).toEqual([{x: 4, y: 4, w: 1}, {x: 0, y: 1, w: 1}, {x: 9, y: 0, w: 1}, {x: 7, y: 0, w: 1}]);
});
});
describe('test isAreaEmpty', function() {
beforeAll(function() {
engine = new GridStackEngine({float:true});
engine.nodes = [
engine.prepareNode({x: 3, y: 2, w: 3, h: 2})
];
});
it('should be true', function() {
expect(engine.isAreaEmpty(0, 0, 3, 2)).toEqual(true);
expect(engine.isAreaEmpty(3, 4, 3, 2)).toEqual(true);
});
it('should be false', function() {
expect(engine.isAreaEmpty(1, 1, 3, 2)).toEqual(false);
expect(engine.isAreaEmpty(2, 3, 3, 2)).toEqual(false);
});
});
describe('test cleanNodes/getDirtyNodes', function() {
beforeAll(function() {
engine = new GridStackEngine({float:true});
engine.nodes = [
engine.prepareNode({x: 0, y: 0, id: 1, _dirty: true}),
engine.prepareNode({x: 3, y: 2, w: 3, h: 2, id: 2, _dirty: true}),
engine.prepareNode({x: 3, y: 7, w: 3, h: 2, id: 3})
];
});
beforeEach(function() {
delete engine.batchMode;
});
it('should return all dirty nodes', function() {
let nodes = engine.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', function() {
engine.batchMode = true;
engine.cleanNodes();
expect(engine.getDirtyNodes().length).toBeGreaterThan(0);
});
it('should clean all dirty nodes', function() {
engine.cleanNodes();
expect(engine.getDirtyNodes().length).toEqual(0);
});
});
describe('test batchUpdate/commit', function() {
beforeAll(function() {
engine = new GridStackEngine();
});
it('should work on not float grids', function() {
expect(engine.float).toEqual(false);
engine.batchUpdate();
engine.batchUpdate(); // double for code coverage
expect(engine.batchMode).toBeTrue();
expect(engine.float).toEqual(true);
engine.batchUpdate(false);
engine.batchUpdate(false);
expect(engine.batchMode).not.toBeTrue();
expect(engine.float).not.toBeTrue;
});
it('should work on float grids', function() {
engine.float = true;
engine.batchUpdate();
expect(engine.batchMode).toBeTrue();
expect(engine.float).toEqual(true);
engine.batchUpdate(false);
expect(engine.batchMode).not.toBeTrue();
expect(engine.float).toEqual(true);
});
});
describe('test batchUpdate/commit', function() {
beforeAll(function() {
engine = new GridStackEngine({float:true});
});
it('should work on float grids', function() {
expect(engine.float).toEqual(true);
engine.batchUpdate();
expect(engine.batchMode).toBeTrue();
expect(engine.float).toEqual(true);
engine.batchUpdate(false);
expect(engine.batchMode).not.toBeTrue();
expect(engine.float).toEqual(true);
});
});
describe('test _notify', function() {
let spy;
beforeEach(function() {
spy = {
callback: function() {}
};
spyOn(spy, 'callback');
engine = new GridStackEngine({float:true, onChange: spy.callback});
engine.nodes = [
engine.prepareNode({x: 0, y: 0, id: 1, _dirty: true}),
engine.prepareNode({x: 3, y: 2, w: 3, h: 2, id: 2, _dirty: true}),
engine.prepareNode({x: 3, y: 7, w: 3, h: 2, id: 3})
];
});
it('should\'n be called if batchMode true', function() {
engine.batchMode = true;
(engine as any)._notify();
expect(spy.callback).not.toHaveBeenCalled();
});
it('should by called with dirty nodes', function() {
(engine as any)._notify();
expect(spy.callback).toHaveBeenCalledWith([engine.nodes[0], engine.nodes[1]]);
});
it('should by called with extra passed node to be removed', function() {
let n1 = {id: -1};
(engine as any)._notify([n1]);
expect(spy.callback).toHaveBeenCalledWith([n1, engine.nodes[0], engine.nodes[1]]);
});
});
describe('test _packNodes', function() {
describe('using not float mode', function() {
beforeEach(function() {
engine = new GridStackEngine({float:false});
});
it('shouldn\'t pack one node with y coord eq 0', function() {
engine.nodes = [
{x: 0, y: 0, w:1, h:1, id: 1},
];
(engine as any)._packNodes();
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 0, h: 1}));
expect(findNode(engine, 1)._dirty).toBeFalsy();
});
it('should pack one node correctly', function() {
engine.nodes = [
{x: 0, y: 1, w:1, h:1, id: 1},
];
(engine as any)._packNodes();
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 0, _dirty: true}));
});
it('should pack nodes correctly', function() {
engine.nodes = [
{x: 0, y: 1, w:1, h:1, id: 1},
{x: 0, y: 5, w:1, h:1, id: 2},
];
(engine as any)._packNodes();
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 0, _dirty: true}));
expect(findNode(engine, 2)).toEqual(jasmine.objectContaining({x: 0, y: 1, _dirty: true}));
});
it('should pack nodes correctly', function() {
engine.nodes = [
{x: 0, y: 5, w:1, h:1, id: 1},
{x: 0, y: 1, w:1, h:1, id: 2},
];
(engine as any)._packNodes();
expect(findNode(engine, 2)).toEqual(jasmine.objectContaining({x: 0, y: 0, _dirty: true}));
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 1, _dirty: true}));
});
it('should respect locked nodes', function() {
engine.nodes = [
{x: 0, y: 1, w:1, h:1, id: 1, locked: true},
{x: 0, y: 5, w:1, h:1, id: 2},
];
(engine as any)._packNodes();
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 1, h: 1}));
expect(findNode(engine, 1)._dirty).toBeFalsy();
expect(findNode(engine, 2)).toEqual(jasmine.objectContaining({x: 0, y: 2, _dirty: true}));
});
});
});
describe('test changedPos', function() {
beforeAll(function() {
engine = new GridStackEngine();
});
it('should return true for changed x', function() {
let widget = { x: 1, y: 2, w: 3, h: 4 };
expect(engine.changedPosConstrain(widget, {x:2, y:2})).toEqual(true);
});
it('should return true for changed y', function() {
let widget = { x: 1, y: 2, w: 3, h: 4 };
expect(engine.changedPosConstrain(widget, {x:1, y:1})).toEqual(true);
});
it('should return true for changed width', function() {
let widget = { x: 1, y: 2, w: 3, h: 4 };
expect(engine.changedPosConstrain(widget, {x:2, y:2, w:4, h:4})).toEqual(true);
});
it('should return true for changed height', function() {
let widget = { x: 1, y: 2, w: 3, h: 4 };
expect(engine.changedPosConstrain(widget, {x:1, y:2, w:3, h:3})).toEqual(true);
});
it('should return false for unchanged position', function() {
let widget = { x: 1, y: 2, w: 3, h: 4 };
expect(engine.changedPosConstrain(widget, {x:1, y:2, w:3, h:4})).toEqual(false);
});
});
describe('test locked widget', function() {
beforeAll(function() {
engine = new GridStackEngine();
});
it('should add widgets around locked one', function() {
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
engine.addNode(nodes[0])
expect(findNode(engine, 0)).toEqual(jasmine.objectContaining({x: 0, y: 1, w: 12, h: 1, locked: true}));
// add item that moves past locked one
engine.addNode(nodes[1])
expect(findNode(engine, 0)).toEqual(jasmine.objectContaining({x: 0, y: 1, w: 12, h: 1, locked: true}));
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 1, y: 2, h: 3}));
// locked item can still be moved directly (what user does)
let node0 = findNode(engine, 0);
expect(engine.moveNode(node0, {y:6})).toEqual(true);
expect(findNode(engine, 0)).toEqual(jasmine.objectContaining({x: 0, y: 6, h: 1, locked: true}));
// but moves regular one past it
let node1 = findNode(engine, 1);
expect(engine.moveNode(node1, {x:6, y:6})).toEqual(true);
expect(node1).toEqual(jasmine.objectContaining({x: 6, y: 7, w: 2, h: 3}));
// but moves regular one before (gravity ON)
engine.float = false;
expect(engine.moveNode(node1, {x:7, y:3})).toEqual(true);
expect(node1).toEqual(jasmine.objectContaining({x: 7, y: 0, w: 2, h: 3}));
// but moves regular one before (gravity OFF)
engine.float = true;
expect(engine.moveNode(node1, {x:7, y:3})).toEqual(true);
expect(node1).toEqual(jasmine.objectContaining({x: 7, y: 3, w: 2, h: 3}));
});
});
describe('test compact', function() {
beforeAll(function() {
engine = new GridStackEngine();
});
it('do nothing', function() {
engine.compact();
});
});
});