Skip to content

Commit 9041dc3

Browse files
authored
- Fixed test compilation (#2850)
* - Fixed test compilation * - Fixed ID being now forced to be a string * - Fixed two cases * - Added infinite loop guard, tests was blocked due to this
1 parent 1ece758 commit 9041dc3

File tree

6 files changed

+58
-54
lines changed

6 files changed

+58
-54
lines changed

karma.conf.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ module.exports = function(config) {
1414
// }
1515
// }
1616
// },
17-
exclude: ["demo", "dist/ng"] // ignore dummy demo .ts files
17+
exclude: ["demo", "dist/ng"], // ignore dummy demo .ts files
18+
include: [
19+
"./spec/**/*-spec.ts"
20+
]
1821
},
1922

2023
// base path that will be used to resolve all patterns (eg. files, exclude)

spec/gridstack-engine-spec.ts

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ describe('gridstack engine', function() {
88
let e: any = GridStackEngine;
99
let w: any = window;
1010

11-
let findNode = function(engine, id) {
12-
return engine.nodes.find((i) => i.id === id);
11+
let findNode = function(engine: GridStackEngine, id: string) {
12+
return engine.nodes.find(i => i.id === id);
1313
};
1414

1515
it('should exist setup function.', function() {
@@ -23,9 +23,9 @@ describe('gridstack engine', function() {
2323
engine = new GridStackEngine();
2424
expect(engine.column).toEqual(12);
2525
expect(engine.float).toEqual(false);
26-
expect(engine.maxRow).toEqual(undefined);
26+
expect(engine.maxRow).toEqual(undefined!);
2727
expect(engine.nodes).toEqual([]);
28-
expect(engine.batchMode).toEqual(undefined);
28+
expect(engine.batchMode).toEqual(undefined!);
2929
expect((engine as any).onChange).toEqual(undefined);
3030
});
3131

@@ -146,21 +146,21 @@ describe('gridstack engine', function() {
146146
beforeAll(function() {
147147
engine = new GridStackEngine({float:true});
148148
engine.nodes = [
149-
engine.prepareNode({x: 0, y: 0, id: 1, _dirty: true}),
150-
engine.prepareNode({x: 3, y: 2, w: 3, h: 2, id: 2, _dirty: true}),
151-
engine.prepareNode({x: 3, y: 7, w: 3, h: 2, id: 3})
149+
engine.prepareNode({x: 0, y: 0, id: "1", _dirty: true}),
150+
engine.prepareNode({x: 3, y: 2, w: 3, h: 2, id: "2", _dirty: true}),
151+
engine.prepareNode({x: 3, y: 7, w: 3, h: 2, id: "3"})
152152
];
153153
});
154154

155155
beforeEach(function() {
156-
delete engine.batchMode;
156+
delete (engine as any).batchMode;
157157
});
158158

159159
it('should return all dirty nodes', function() {
160160
let nodes = engine.getDirtyNodes();
161161
expect(nodes.length).toEqual(2);
162-
expect(nodes[0].id).toEqual(1);
163-
expect(nodes[1].id).toEqual(2);
162+
expect(nodes[0].id).toEqual("1");
163+
expect(nodes[1].id).toEqual("2");
164164
});
165165

166166
it('should\'n clean nodes if batchMode true', function() {
@@ -230,9 +230,9 @@ describe('gridstack engine', function() {
230230
spyOn(spy, 'callback');
231231
engine = new GridStackEngine({float:true, onChange: spy.callback});
232232
engine.nodes = [
233-
engine.prepareNode({x: 0, y: 0, id: 1, _dirty: true}),
234-
engine.prepareNode({x: 3, y: 2, w: 3, h: 2, id: 2, _dirty: true}),
235-
engine.prepareNode({x: 3, y: 7, w: 3, h: 2, id: 3})
233+
engine.prepareNode({x: 0, y: 0, id: "1", _dirty: true}),
234+
engine.prepareNode({x: 3, y: 2, w: 3, h: 2, id: "2", _dirty: true}),
235+
engine.prepareNode({x: 3, y: 7, w: 3, h: 2, id: "3"})
236236
];
237237
});
238238

@@ -262,50 +262,50 @@ describe('gridstack engine', function() {
262262

263263
it('shouldn\'t pack one node with y coord eq 0', function() {
264264
engine.nodes = [
265-
{x: 0, y: 0, w:1, h:1, id: 1},
265+
{x: 0, y: 0, w:1, h:1, id: "1"},
266266
];
267267
(engine as any)._packNodes();
268-
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 0, h: 1}));
269-
expect(findNode(engine, 1)._dirty).toBeFalsy();
268+
expect(findNode(engine, "1")).toEqual(jasmine.objectContaining({x: 0, y: 0, h: 1}));
269+
expect(findNode(engine, "1")!._dirty).toBeFalsy();
270270
});
271271

272272
it('should pack one node correctly', function() {
273273
engine.nodes = [
274-
{x: 0, y: 1, w:1, h:1, id: 1},
274+
{x: 0, y: 1, w:1, h:1, id: "1"},
275275
];
276276
(engine as any)._packNodes();
277-
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 0, _dirty: true}));
277+
expect(findNode(engine, "1")).toEqual(jasmine.objectContaining({x: 0, y: 0, _dirty: true}));
278278
});
279279

280280
it('should pack nodes correctly', function() {
281281
engine.nodes = [
282-
{x: 0, y: 1, w:1, h:1, id: 1},
283-
{x: 0, y: 5, w:1, h:1, id: 2},
282+
{x: 0, y: 1, w:1, h:1, id: "1"},
283+
{x: 0, y: 5, w:1, h:1, id: "2"},
284284
];
285285
(engine as any)._packNodes();
286-
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 0, _dirty: true}));
287-
expect(findNode(engine, 2)).toEqual(jasmine.objectContaining({x: 0, y: 1, _dirty: true}));
286+
expect(findNode(engine, "1")).toEqual(jasmine.objectContaining({x: 0, y: 0, _dirty: true}));
287+
expect(findNode(engine, "2")).toEqual(jasmine.objectContaining({x: 0, y: 1, _dirty: true}));
288288
});
289289

290290
it('should pack nodes correctly', function() {
291291
engine.nodes = [
292-
{x: 0, y: 5, w:1, h:1, id: 1},
293-
{x: 0, y: 1, w:1, h:1, id: 2},
292+
{x: 0, y: 5, w:1, h:1, id: "1"},
293+
{x: 0, y: 1, w:1, h:1, id: "2"},
294294
];
295295
(engine as any)._packNodes();
296-
expect(findNode(engine, 2)).toEqual(jasmine.objectContaining({x: 0, y: 0, _dirty: true}));
297-
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 1, _dirty: true}));
296+
expect(findNode(engine, "2")).toEqual(jasmine.objectContaining({x: 0, y: 0, _dirty: true}));
297+
expect(findNode(engine, "1")).toEqual(jasmine.objectContaining({x: 0, y: 1, _dirty: true}));
298298
});
299299

300300
it('should respect locked nodes', function() {
301301
engine.nodes = [
302-
{x: 0, y: 1, w:1, h:1, id: 1, locked: true},
303-
{x: 0, y: 5, w:1, h:1, id: 2},
302+
{x: 0, y: 1, w:1, h:1, id: "1", locked: true},
303+
{x: 0, y: 5, w:1, h:1, id: "2"},
304304
];
305305
(engine as any)._packNodes();
306-
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 1, h: 1}));
307-
expect(findNode(engine, 1)._dirty).toBeFalsy();
308-
expect(findNode(engine, 2)).toEqual(jasmine.objectContaining({x: 0, y: 2, _dirty: true}));
306+
expect(findNode(engine, "1")).toEqual(jasmine.objectContaining({x: 0, y: 1, h: 1}));
307+
expect(findNode(engine, "1")!._dirty).toBeFalsy();
308+
expect(findNode(engine, "2")).toEqual(jasmine.objectContaining({x: 0, y: 2, _dirty: true}));
309309
});
310310
});
311311
});
@@ -342,31 +342,31 @@ describe('gridstack engine', function() {
342342
});
343343
it('should add widgets around locked one', function() {
344344
let nodes: GridStackNode[] = [
345-
{x: 0, y: 1, w: 12, h: 1, locked: true, noMove: true, noResize: true, id: 0},
346-
{x: 1, y: 0, w: 2, h: 3, id: 1}
345+
{x: 0, y: 1, w: 12, h: 1, locked: true, noMove: true, noResize: true, id: "0"},
346+
{x: 1, y: 0, w: 2, h: 3, id: "1"}
347347
];
348348
// add locked item
349349
engine.addNode(nodes[0])
350-
expect(findNode(engine, 0)).toEqual(jasmine.objectContaining({x: 0, y: 1, w: 12, h: 1, locked: true}));
350+
expect(findNode(engine, "0")).toEqual(jasmine.objectContaining({x: 0, y: 1, w: 12, h: 1, locked: true}));
351351
// add item that moves past locked one
352352
engine.addNode(nodes[1])
353-
expect(findNode(engine, 0)).toEqual(jasmine.objectContaining({x: 0, y: 1, w: 12, h: 1, locked: true}));
354-
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 1, y: 2, h: 3}));
353+
expect(findNode(engine, "0")).toEqual(jasmine.objectContaining({x: 0, y: 1, w: 12, h: 1, locked: true}));
354+
expect(findNode(engine, "1")).toEqual(jasmine.objectContaining({x: 1, y: 2, h: 3}));
355355
// locked item can still be moved directly (what user does)
356-
let node0 = findNode(engine, 0);
357-
expect(engine.moveNode(node0, {y:6})).toEqual(true);
358-
expect(findNode(engine, 0)).toEqual(jasmine.objectContaining({x: 0, y: 6, h: 1, locked: true}));
356+
let node0 = findNode(engine, "0");
357+
expect(engine.moveNode(node0!, {y:6})).toEqual(true);
358+
expect(findNode(engine, "0")).toEqual(jasmine.objectContaining({x: 0, y: 6, h: 1, locked: true}));
359359
// but moves regular one past it
360-
let node1 = findNode(engine, 1);
361-
expect(engine.moveNode(node1, {x:6, y:6})).toEqual(true);
360+
let node1 = findNode(engine, "1");
361+
expect(engine.moveNode(node1!, {x:6, y:6})).toEqual(true);
362362
expect(node1).toEqual(jasmine.objectContaining({x: 6, y: 7, w: 2, h: 3}));
363363
// but moves regular one before (gravity ON)
364364
engine.float = false;
365-
expect(engine.moveNode(node1, {x:7, y:3})).toEqual(true);
365+
expect(engine.moveNode(node1!, {x:7, y:3})).toEqual(true);
366366
expect(node1).toEqual(jasmine.objectContaining({x: 7, y: 0, w: 2, h: 3}));
367367
// but moves regular one before (gravity OFF)
368368
engine.float = true;
369-
expect(engine.moveNode(node1, {x:7, y:3})).toEqual(true);
369+
expect(engine.moveNode(node1!, {x:7, y:3})).toEqual(true);
370370
expect(node1).toEqual(jasmine.objectContaining({x: 7, y: 3, w: 2, h: 3}));
371371
});
372372
});

spec/gridstack-spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ describe('gridstack', function() {
330330
expect(parseInt(el2.getAttribute('gs-h'))).toBe(4);
331331

332332
// add default 1x1 item to the end (1 column)
333-
let el3 = grid.addWidget();
333+
let el3 = grid.addWidget({ });
334334
expect(el3).not.toBe(null);
335335
expect(parseInt(el3.getAttribute('gs-x'))).toBe(0);
336336
expect(parseInt(el3.getAttribute('gs-y'))).toBe(6);
@@ -747,7 +747,7 @@ describe('gridstack', function() {
747747
expect(grid.engine.nodes.length).toEqual(0);
748748
expect(document.getElementById('item2')).toBe(null);
749749

750-
let el3 = grid.addWidget(widgetHTML);
750+
let el3 = grid.makeWidget(widgetHTML);
751751
expect(el3).not.toBe(null);
752752
grid.removeWidget(el3, false);
753753
expect(grid.engine.nodes.length).toEqual(0);
@@ -903,7 +903,7 @@ describe('gridstack', function() {
903903
});
904904
it('should autoPosition (empty options)', function() {
905905
let grid = GridStack.init();
906-
let widget = grid.addWidget();
906+
let widget = grid.addWidget({ });
907907

908908
expect(parseInt(widget.getAttribute('gs-x'), 10)).toBe(8);
909909
expect(parseInt(widget.getAttribute('gs-y'), 10)).toBe(0);
@@ -933,15 +933,15 @@ describe('gridstack', function() {
933933
it('null options should clear x position', function() {
934934
let grid = GridStack.init({float: true});
935935
let HTML = '<div class="grid-stack-item" gs-x="9"><div class="grid-stack-item-content"></div></div>';
936-
let widget = grid.addWidget(HTML, {x:null, y:null, w:undefined});
936+
let widget = grid.makeWidget(HTML, {x:null, y:null, w:undefined});
937937

938938
expect(parseInt(widget.getAttribute('gs-x'), 10)).toBe(8);
939939
expect(parseInt(widget.getAttribute('gs-y'), 10)).toBe(0);
940940
});
941941
it('width attr should be retained', function() { // #1276
942942
let grid = GridStack.init({float: true});
943943
let HTML = '<div class="grid-stack-item" gs-w="3" gs-max-w="4" gs-id="foo"><div class="grid-stack-item-content"></div></div>';
944-
let widget = grid.addWidget(HTML, {x: 1, y: 5});
944+
let widget = grid.makeWidget(HTML, {x: 1, y: 5});
945945
expect(parseInt(widget.getAttribute('gs-x'), 10)).toBe(1);
946946
expect(parseInt(widget.getAttribute('gs-y'), 10)).toBe(5);
947947
expect(parseInt(widget.getAttribute('gs-w'), 10)).toBe(3);

spec/utils-spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ describe('gridstack utils', function() {
7979
expect(Utils.parseHeight('12.5cm')).toEqual(jasmine.objectContaining({h: 12.5, unit: 'cm'}));
8080
expect(Utils.parseHeight('12.5mm')).toEqual(jasmine.objectContaining({h: 12.5, unit: 'mm'}));
8181
expect(Utils.parseHeight('12.5')).toEqual(jasmine.objectContaining({h: 12.5, unit: 'px'}));
82-
expect(function() { Utils.parseHeight('12.5 df'); }).toThrowError('Invalid height');
82+
expect(function() { Utils.parseHeight('12.5 df'); }).toThrowError('Invalid height val = 12.5 df');
8383
});
8484

8585
it('should parse negative height value', function() {
@@ -94,7 +94,7 @@ describe('gridstack utils', function() {
9494
expect(Utils.parseHeight('-12.3cm')).toEqual(jasmine.objectContaining({h: -12.3, unit: 'cm'}));
9595
expect(Utils.parseHeight('-12.3mm')).toEqual(jasmine.objectContaining({h: -12.3, unit: 'mm'}));
9696
expect(Utils.parseHeight('-12.5')).toEqual(jasmine.objectContaining({h: -12.5, unit: 'px'}));
97-
expect(function() { Utils.parseHeight('-12.5 df'); }).toThrowError('Invalid height');
97+
expect(function() { Utils.parseHeight('-12.5 df'); }).toThrowError('Invalid height val = -12.5 df');
9898
});
9999
});
100100

src/gridstack-engine.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ export class GridStackEngine {
100100

101101
let didMove = false;
102102
const newOpt: GridStackMoveOpts = {nested: true, pack: false};
103+
let counter = 0;
103104
while (collide = collide || this.collide(node, area, opt.skip)) { // could collide with more than 1 item... so repeat for each
105+
if (counter++ > this.nodes.length * 2) {
106+
throw new Error("Infinite collide check");
107+
}
104108
let moved: boolean;
105109
// if colliding with a locked item OR loading (move after) OR moving down with top gravity (and collide could move up) -> skip past the collide,
106110
// but remember that skip down so we only do this once (and push others otherwise).

tsconfig.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
"strict": false,
1616
"target": "ES2020"
1717
},
18-
"exclude": [
19-
"./src/**/*.spec.ts",
20-
],
2118
"include": [
2219
"./src/**/*.ts"
2320
],

0 commit comments

Comments
 (0)