Skip to content

Commit 00d780a

Browse files
committed
GridStackEngine testing
1 parent 6cd5084 commit 00d780a

File tree

1 file changed

+121
-2
lines changed

1 file changed

+121
-2
lines changed

spec/gridstack-engine-spec.js

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ describe('gridstack engine', function() {
33

44
var e;
55
var w;
6-
var engine;
76

87
beforeEach(function() {
98
w = window;
109
e = w.GridStackUI.Engine;
11-
engine = new w.GridStackUI.Engine(12);
1210
});
1311

1412
describe('test constructor', function() {
13+
var engine;
14+
15+
beforeAll(function() {
16+
engine = new GridStackUI.Engine(12);
17+
})
18+
1519
it('should be setup properly', function() {
1620
expect(engine.width).toEqual(12);
1721
expect(engine.float).toEqual(false);
@@ -21,6 +25,12 @@ describe('gridstack engine', function() {
2125
});
2226

2327
describe('test _prepareNode', function() {
28+
var engine;
29+
30+
beforeAll(function() {
31+
engine = new GridStackUI.Engine(12);
32+
})
33+
2434
it('should prepare a node', function() {
2535
expect(engine._prepareNode({}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 1}));
2636
expect(engine._prepareNode({x: 10}, false)).toEqual(jasmine.objectContaining({x: 10, y: 0, width: 1, height: 1}));
@@ -38,4 +48,113 @@ describe('gridstack engine', function() {
3848
expect(engine._prepareNode({x: 4, width: 10}, true)).toEqual(jasmine.objectContaining({x: 4, y: 0, width: 8, height: 1}));
3949
});
4050
});
51+
52+
describe('test isAreaEmpty', function() {
53+
var engine;
54+
55+
beforeAll(function() {
56+
engine = new GridStackUI.Engine(12, null, true);
57+
engine.nodes = [
58+
engine._prepareNode({x: 3, y: 2, width: 3, height: 2})
59+
];
60+
})
61+
62+
it('should be true', function() {
63+
expect(engine.isAreaEmpty(0, 0, 3, 2)).toEqual(true);
64+
expect(engine.isAreaEmpty(3, 4, 3, 2)).toEqual(true);
65+
});
66+
67+
it('should be false', function() {
68+
expect(engine.isAreaEmpty(1, 1, 3, 2)).toEqual(false);
69+
expect(engine.isAreaEmpty(2, 3, 3, 2)).toEqual(false);
70+
});
71+
});
72+
73+
describe('test cleanNodes/getDirtyNodes', function() {
74+
var engine;
75+
76+
beforeAll(function() {
77+
engine = new GridStackUI.Engine(12, null, true);
78+
engine.nodes = [
79+
engine._prepareNode({x: 0, y: 0, width: 1, height: 1, idx: 1, _dirty: true}),
80+
engine._prepareNode({x: 3, y: 2, width: 3, height: 2, idx: 2, _dirty: true}),
81+
engine._prepareNode({x: 3, y: 7, width: 3, height: 2, idx: 3})
82+
];
83+
});
84+
85+
beforeEach(function() {
86+
engine._updateCounter = 0;
87+
});
88+
89+
it('should return all dirty nodes', function() {
90+
var nodes = engine.getDirtyNodes();
91+
92+
expect(nodes.length).toEqual(2);
93+
expect(nodes[0].idx).toEqual(1);
94+
expect(nodes[1].idx).toEqual(2);
95+
});
96+
97+
it('should\'n clean nodes if _updateCounter > 0', function() {
98+
engine._updateCounter = 1;
99+
engine.cleanNodes();
100+
101+
expect(engine.getDirtyNodes().length).toBeGreaterThan(0);
102+
});
103+
104+
it('should clean all dirty nodes', function() {
105+
engine.cleanNodes();
106+
107+
expect(engine.getDirtyNodes().length).toEqual(0);
108+
});
109+
});
110+
111+
describe('test _notify', function() {
112+
var engine;
113+
var spy;
114+
115+
beforeEach(function() {
116+
spy = {
117+
callback: function () {}
118+
}
119+
spyOn(spy, 'callback');
120+
121+
engine = new GridStackUI.Engine(12, spy.callback, true);
122+
123+
engine.nodes = [
124+
engine._prepareNode({x: 0, y: 0, width: 1, height: 1, idx: 1, _dirty: true}),
125+
engine._prepareNode({x: 3, y: 2, width: 3, height: 2, idx: 2, _dirty: true}),
126+
engine._prepareNode({x: 3, y: 7, width: 3, height: 2, idx: 3})
127+
];
128+
});
129+
130+
it('should\'n be called if _updateCounter > 0', function() {
131+
engine._updateCounter = 1;
132+
engine._notify();
133+
134+
expect(spy.callback).not.toHaveBeenCalled();
135+
});
136+
137+
it('should by called with dirty nodes', function() {
138+
engine._notify();
139+
140+
expect(spy.callback).toHaveBeenCalledWith([
141+
engine.nodes[0],
142+
engine.nodes[1]
143+
]);
144+
});
145+
146+
it('should by called with extra passed dirty nodes', function() {
147+
var n1 = {idx: -1},
148+
n2 = {idx: -2};
149+
150+
engine._notify(n1, n2);
151+
152+
expect(spy.callback).toHaveBeenCalledWith([
153+
n1,
154+
n2,
155+
engine.nodes[0],
156+
engine.nodes[1]
157+
]);
158+
});
159+
});
41160
});

0 commit comments

Comments
 (0)