|
| 1 | +describe('gridstack engine', function() { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + var e; |
| 5 | + var w; |
| 6 | + |
| 7 | + beforeEach(function() { |
| 8 | + w = window; |
| 9 | + e = w.GridStackUI.Engine; |
| 10 | + }); |
| 11 | + |
| 12 | + describe('test constructor', function() { |
| 13 | + var engine; |
| 14 | + |
| 15 | + beforeAll(function() { |
| 16 | + engine = new GridStackUI.Engine(12); |
| 17 | + }) |
| 18 | + |
| 19 | + it('should be setup properly', function() { |
| 20 | + expect(engine.width).toEqual(12); |
| 21 | + expect(engine.float).toEqual(false); |
| 22 | + expect(engine.height).toEqual(0); |
| 23 | + expect(engine.nodes).toEqual([]); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('test _prepareNode', function() { |
| 28 | + var engine; |
| 29 | + |
| 30 | + beforeAll(function() { |
| 31 | + engine = new GridStackUI.Engine(12); |
| 32 | + }) |
| 33 | + |
| 34 | + it('should prepare a node', function() { |
| 35 | + expect(engine._prepareNode({}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 1})); |
| 36 | + expect(engine._prepareNode({x: 10}, false)).toEqual(jasmine.objectContaining({x: 10, y: 0, width: 1, height: 1})); |
| 37 | + expect(engine._prepareNode({x: -10}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 1})); |
| 38 | + expect(engine._prepareNode({y: 10}, false)).toEqual(jasmine.objectContaining({x: 0, y: 10, width: 1, height: 1})); |
| 39 | + expect(engine._prepareNode({y: -10}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 1})); |
| 40 | + expect(engine._prepareNode({width: 3}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 3, height: 1})); |
| 41 | + expect(engine._prepareNode({width: 100}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 12, height: 1})); |
| 42 | + expect(engine._prepareNode({width: 0}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 1})); |
| 43 | + expect(engine._prepareNode({width: -190}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 1})); |
| 44 | + expect(engine._prepareNode({height: 3}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 3})); |
| 45 | + expect(engine._prepareNode({height: 0}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 1})); |
| 46 | + expect(engine._prepareNode({height: -10}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 1})); |
| 47 | + expect(engine._prepareNode({x: 4, width: 10}, false)).toEqual(jasmine.objectContaining({x: 2, y: 0, width: 10, height: 1})); |
| 48 | + expect(engine._prepareNode({x: 4, width: 10}, true)).toEqual(jasmine.objectContaining({x: 4, y: 0, width: 8, height: 1})); |
| 49 | + }); |
| 50 | + }); |
| 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 | + }); |
| 160 | +}); |
0 commit comments