-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathgridster-directive.js
More file actions
175 lines (138 loc) · 3.83 KB
/
gridster-directive.js
File metadata and controls
175 lines (138 loc) · 3.83 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
'use strict';
describe('gridster directive', function() {
beforeEach(module('gridster'));
var $scope;
var GridsterCtrl;
var $el;
var startCount;
var resizeCount;
var stopCount;
var broadcastOnRootScope;
var dragHelper = function(el, dx, dy) {
el.simulate('mouseover').simulate('drag', {
moves: 1,
dx: dx,
dy: dy
});
};
beforeEach(inject(function($rootScope, $compile) {
broadcastOnRootScope = spyOn($rootScope, '$broadcast').and.callThrough();
$scope = $rootScope.$new();
startCount = resizeCount = stopCount = 0;
$scope.opts = {
minRows: 3,
resizable: {
enabled: true,
handles: ['n', 'e', 's', 'w', 'se', 'sw'],
start: function() {
startCount++;
},
resize: function() {
resizeCount++;
},
stop: function() {
stopCount++;
}
}
};
$scope.dashboard = {
widgets: [{
id: 1,
row: 0,
col: 0,
sizeX: 1,
sizeY: 1
}, {
id: 2,
row: 0,
col: 3,
sizeX: 2,
sizeY: 1
}, {
id: 3,
row: 1,
col: 3,
sizeX: 2,
sizeY: 2
}]
};
$el = angular.element('<div gridster="opts" style="width: 1000px;">' +
'<ul><li gridster-item="widget" ng-repeat="widget in dashboard.widgets">' +
'</div>');
$el.appendTo(document.body); // append to body so jquery-simulate works
$compile($el)($scope);
$scope.$digest();
GridsterCtrl = $el.controller('gridster');
}));
it('should add a class of gridster', function() {
expect($el.hasClass('gridster')).toBe(true);
});
it('should override options', function() {
expect(GridsterCtrl.minRows).toBe($scope.opts.minRows);
});
it('should add widgets to DOM', function() {
expect($el.find('li').length).toBe($scope.dashboard.widgets.length);
});
it('should initialize resizable', function() {
var $widget = $el.find('li:first-child');
expect($widget.find('.handle-s').length).toBe(1);
});
it('should update widget dimensions on resize & trigger custom resize events', function() {
var $widget = $el.find('li:first-child');
var handle = $widget.find('.handle-e');
expect($widget.width()).toBe(155);
expect($scope.dashboard.widgets[0].sizeX).toBe(1);
expect(startCount).toBe(0);
expect(resizeCount).toBe(0);
expect(stopCount).toBe(0);
dragHelper(handle, 50); // should resize to next width step
expect($widget.width()).toBe(320);
expect($scope.dashboard.widgets[0].sizeX).toBe(2);
expect(startCount).toBe(1);
expect(resizeCount).toBe(1);
expect(stopCount).toBe(1);
});
it('should broadcast "gridster-item-resized" event on resize', function() {
// arrange
var eHandle = $el.find('li:first-child').find('.handle-e');
var sHandle = $el.find('li:first-child').find('.handle-s');
broadcastOnRootScope.calls.reset();
// act
dragHelper(eHandle, 50);
// assert
expect(broadcastOnRootScope).toHaveBeenCalledWith('gridster-item-resized', jasmine.objectContaining({
sizeX: 2,
sizeY: 1
}));
// arrange
broadcastOnRootScope.calls.reset();
// act
dragHelper(sHandle, 0, 50);
// assert
expect(broadcastOnRootScope).toHaveBeenCalledWith('gridster-item-resized', jasmine.objectContaining({
sizeX: 2,
sizeY: 2
}));
});
it('should broadcast "gridster-columns-changed" on columns change', function() {
// arrange
broadcastOnRootScope.calls.reset();
// act
GridsterCtrl.columns = 10;
$scope.$digest();
// assert
expect(broadcastOnRootScope).toHaveBeenCalledWith('gridster-columns-changed', jasmine.objectContaining({
columns: 10
}));
});
it('number of columns should be different when enabling dynamicColumns and setting minWidthToAddANewColumn to a lower value', function() {
//precondition
expect(GridsterCtrl.columns).toBe(6);
// act
GridsterCtrl.dynamicColumns = true;
GridsterCtrl.minWidthToAddANewColumn = 50;
$scope.$digest();
// assert
expect(GridsterCtrl.columns).not.toBe(6);
});
});