/
ui-grid.spec.js
209 lines (165 loc) · 6.55 KB
/
ui-grid.spec.js
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
describe('ui-grid', function() {
beforeEach(module('ui.grid'));
// beforeEach(module('ui.grid.body'));
// beforeEach(module('ui.grid.header'));
/*describe('ui-grid calculated columns', function() {
var element, scope;
beforeEach(inject(function($compile, $rootScope) {
element = angular.element('<div class="col-md-5" ui-grid="data" ui-grid-table-class="table"></div>');
scope = $rootScope;
scope.data = [{ col1: 'col1', col2: 'col2' }];
$compile(element)(scope);
scope.$digest();
}));
it('gets columns correctly', function() {
expect(element.isolateScope().gridOptions.columnDefs.length).toBe(2);
expect(element.isolateScope().gridOptions.columnDefs[0].name).toBe('Col1');
expect(element.isolateScope().gridOptions.columnDefs[0].field).toBe('col1');
});
});
describe('ui-grid declarative columns', function() {
var element, scope;
beforeEach(inject(function($compile, $rootScope) {
element = angular.element('<div class="col-md-5" ui-grid="data" ui-grid-columns="[{name:\'Decl Col 1\',field:\'declCol1\'}]" ui-grid-table-class="table"></div>');
scope = $rootScope;
scope.data = [{ declCol1: 'col1', declCol2: 'col2' }];
$compile(element)(scope);
scope.$digest();
}));
it('gets columns correctly', function() {
expect(element.isolateScope().gridOptions.columnDefs.length).toBe(1);
expect(element.isolateScope().gridOptions.columnDefs[0].name).toBe('Decl Col 1');
expect(element.isolateScope().gridOptions.columnDefs[0].field).toBe('declCol1');
});
});
describe('ui-grid imperative columns', function () {
var element, scope;
beforeEach(inject(function ($compile, $rootScope) {
element = angular.element('<div class="col-md-5" ui-grid="data" ui-grid-options="myGridOptions" ui-grid-columns="[{name:\'Decl Col 1\',field:\'declCol1\'}]" ui-grid-table-class="table"></div>');
scope = $rootScope;
scope.data = [{ impCol1: 'col1', impCol2: 'col2' }];
//specifying gridOptions on parent scope will override any attributes
scope.myGridOptions = {};
scope.myGridOptions.columnDefs = [{ name: 'Imp Col 1', field: 'impCol1' }];
$compile(element)(scope);
scope.$digest();
}));
it('gets columns correctly', function () {
expect(element.isolateScope().gridOptions.columnDefs.length).toBe(1);
expect(element.isolateScope().gridOptions.columnDefs[0].name).toBe('Imp Col 1');
expect(element.isolateScope().gridOptions.columnDefs[0].field).toBe('impCol1');
});
});*/
describe('minColumnsToRender', function() {
it('calculates the minimum number of columns to render, correctly', function() {
// TODO
});
});
describe('column width calculation', function () {
var element = null, gridApi = null;
var columnDefs = [
{ name: 'col1' },
{ name: 'col2' },
{ name: 'col3' },
{ name: 'col4' },
{ name: 'col5' },
{ name: 'col6' },
{ name: 'col7' }
];
beforeEach(inject(function ($compile, $rootScope, $document) {
var scope = $rootScope;
element = angular.element('<div style="width 333px; height: 150px" ui-grid="gridOptions"></div>');
scope.gridOptions = {
columnDefs: columnDefs,
data: [],
onRegisterApi: function( api ){ gridApi = api; }
};
$compile(element)(scope);
$document[0].body.appendChild(element[0]);
scope.$digest();
}));
afterEach(function() {
element.remove();
angular.forEach(columnDefs, function (c) {
delete c.width;
});
});
// ideally there should be tests for multiple column configurations here
// but need to figure out how to have separate columnDefs for each
// expect block below
it('should distribute extra width', function () {
var renderWidth = 0;
angular.forEach(gridApi.grid.columns, function (c) {
renderWidth += c.drawnWidth;
});
expect(renderWidth).toBe(gridApi.grid.getViewportWidth() - gridApi.grid.scrollbarWidth);
});
});
describe('watch for new pinned containers', function () {
var element, scope;
beforeEach(inject(function ($compile, $rootScope, $timeout) {
element = angular.element('<div class="col-md-5" ui-grid="gridOptions"></div>');
scope = $rootScope;
scope.gridOptions = {};
scope.gridOptions.data = [
{ col1: 'col1', col2: 'col2' }
];
scope.gridOptions.onRegisterApi = function(gridApi) {
scope.grid = gridApi.grid;
};
$timeout(function () {
$compile(element)(scope);
});
$timeout.flush();
}));
it('fires watch for left container', inject(function($timeout) {
spyOn(scope.grid, 'refreshCanvas');
expect(scope.grid.refreshCanvas.callCount).toEqual(0);
$timeout(function(){
scope.grid.createLeftContainer();
});
$timeout.flush();
expect(scope.grid.refreshCanvas).toHaveBeenCalledWith(true);
}));
it('fires watch for right container', inject(function($timeout) {
spyOn(scope.grid, 'refreshCanvas');
expect(scope.grid.refreshCanvas.callCount).toEqual(0);
$timeout(function(){
scope.grid.createRightContainer();
});
$timeout.flush();
expect(scope.grid.refreshCanvas).toHaveBeenCalledWith(true);
}));
});
describe('appScope is correctly assigned', function () {
var element, scope, gridApi;
it('should assign scope to grid.appScope', inject(function($compile, $rootScope, $timeout) {
element = angular.element('<div class="col-md-5" ui-grid="gridOptions"></div>');
scope = $rootScope;
scope.gridOptions = { onRegisterApi: function( api ){ gridApi = api; }};
scope.gridOptions.data = [
{ col1: 'col1', col2: 'col2' }
];
$timeout(function () {
$compile(element)(scope);
});
$timeout.flush();
expect(gridApi.grid.appScope).toBe(scope);
}));
it('should assign gridOptions.appScopeProvider to grid.appScope', inject(function($compile, $rootScope, $timeout) {
element = angular.element('<div class="col-md-5" ui-grid="gridOptions"></div>');
scope = $rootScope;
scope.gridOptions = {
appScopeProvider : 'someValue',
onRegisterApi: function( api ){ gridApi = api; }};
scope.gridOptions.data = [
{ col1: 'col1', col2: 'col2' }
];
$timeout(function () {
$compile(element)(scope);
});
$timeout.flush();
expect(gridApi.grid.appScope).toBe('someValue');
}));
});
});