Skip to content

Commit b9e892f

Browse files
Portugal, Marcelovishalnarewade
authored andcommitted
test(uiGridSelectionDirective.spec): Updating selection unit tests.
Also, preping selection for future performance improvement.
1 parent c378da3 commit b9e892f

File tree

2 files changed

+65
-37
lines changed

2 files changed

+65
-37
lines changed

src/features/selection/js/selection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@
664664
allowCellFocus: true
665665
};
666666

667-
uiGridCtrl.grid.addRowHeaderColumn(selectionRowHeaderDef, 0);
667+
uiGridCtrl.grid.addRowHeaderColumn(selectionRowHeaderDef, 0, true);
668668
}
669669

670670
var processorSet = false;

src/features/selection/test/uiGridSelectionDirective.spec.js

Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,56 @@ describe('ui.grid.selection uiGridSelectionDirective', function() {
55
gridCtrl,
66
$compile,
77
$rootScope,
8+
$timeout,
89
uiGridConstants;
910

10-
beforeEach(module('ui.grid.selection'));
11+
/*
12+
NOTES
13+
- We have to flush $timeout because the header calculations are done post-$timeout, as that's when the header has been fully rendered.
14+
- We have to actually attach the grid element to the document body, otherwise it will not have a rendered height.
15+
*/
16+
function compileUiGridSelectionDirective(parentScope) {
17+
var elm = angular.element('<div style="width: 300px; height: 500px" ui-grid="options" ui-grid-selection></div>');
18+
19+
document.body.appendChild(elm[0]);
20+
$compile(elm)(parentScope);
21+
$timeout.flush();
22+
parentScope.$digest();
1123

12-
beforeEach(inject(function(_$rootScope_, _$compile_, _uiGridConstants_) {
13-
$compile = _$compile_;
14-
$rootScope = _$rootScope_;
15-
uiGridConstants = _uiGridConstants_;
24+
return elm;
25+
}
1626

17-
parentScope = $rootScope.$new();
27+
beforeEach(function() {
28+
module('ui.grid.selection');
1829

19-
parentScope.options = {
20-
columnDefs : [{field: 'id'}]
21-
};
30+
inject(function(_$compile_, _$rootScope_, _$timeout_, _uiGridConstants_) {
31+
$compile = _$compile_;
32+
$rootScope = _$rootScope_;
33+
$timeout = _$timeout_;
34+
uiGridConstants = _uiGridConstants_;
35+
});
2236

23-
parentScope.options.isRowSelectable = function(gridRow) {
24-
return gridRow.entity.id % 2 === 0;
37+
parentScope = $rootScope.$new();
38+
parentScope.options = {
39+
columnDefs : [{field: 'id'}],
40+
data: [],
41+
isRowSelectable: function(gridRow) {
42+
return gridRow.entity.id % 2 === 0;
43+
}
2544
};
2645

27-
parentScope.options.data = [];
2846
for (var i = 0; i < 10; i++) {
2947
parentScope.options.data.push({id: i});
3048
}
3149

32-
var tpl = '<div ui-grid="options" ui-grid-selection options="options"></div>';
33-
elm = $compile(tpl)(parentScope);
34-
35-
parentScope.$digest();
50+
elm = compileUiGridSelectionDirective(parentScope);
3651
scope = elm.scope();
37-
3852
gridCtrl = elm.controller('uiGrid');
53+
});
3954

40-
}));
55+
it('should add the row header selection buttons', function() {
56+
expect($(elm).find('.ui-grid-header .ui-grid-selection-row-header-buttons').length).toEqual(1);
57+
});
4158

4259
it('should set the "enableSelection" field of the row using the function specified in "isRowSelectable"', function() {
4360
for (var i = 0; i < gridCtrl.grid.rows.length; i++) {
@@ -56,25 +73,10 @@ describe('ui.grid.selection uiGridSelectionDirective', function() {
5673
});
5774

5875
describe('with filtering turned on', function () {
59-
var elm, $timeout;
60-
61-
/*
62-
NOTES
63-
- We have to flush $timeout because the header calculations are done post-$timeout, as that's when the header has been fully rendered.
64-
- We have to actually attach the grid element to the document body, otherwise it will not have a rendered height.
65-
*/
66-
67-
beforeEach(inject(function (_$timeout_) {
68-
$timeout = _$timeout_;
69-
76+
beforeEach(function () {
7077
parentScope.options.enableFiltering = true;
71-
72-
elm = angular.element('<div style="width: 300px; height: 500px" ui-grid="options" ui-grid-selection></div>');
73-
document.body.appendChild(elm[0]);
74-
$compile(elm)(parentScope);
75-
$timeout.flush();
76-
parentScope.$digest();
77-
}));
78+
elm = compileUiGridSelectionDirective(parentScope);
79+
});
7880

7981
afterEach(function () {
8082
$(elm).remove();
@@ -95,4 +97,30 @@ describe('ui.grid.selection uiGridSelectionDirective', function() {
9597
expect(noFilteringHeight < filteringHeight).toBe(true);
9698
});
9799
});
100+
101+
describe('when row header selection is turned off', function() {
102+
beforeEach(function () {
103+
parentScope.options.enableRowHeaderSelection = false;
104+
elm = compileUiGridSelectionDirective(parentScope);
105+
});
106+
107+
it('should not add the row header selection buttons', function() {
108+
expect($(elm).find('.ui-grid-header .ui-grid-selection-row-header-buttons').length).toEqual(0);
109+
});
110+
});
111+
112+
describe('when isRowSelectable is not defined', function() {
113+
beforeEach(function () {
114+
delete parentScope.options.isRowSelectable;
115+
elm = compileUiGridSelectionDirective(parentScope);
116+
gridCtrl = elm.controller('uiGrid');
117+
});
118+
119+
it('should not define enableSelection', function() {
120+
for (var i = 0; i < gridCtrl.grid.rows.length; i++) {
121+
var currentRow = gridCtrl.grid.rows[i];
122+
expect(currentRow.enableSelection).toBeUndefined();
123+
}
124+
});
125+
});
98126
});

0 commit comments

Comments
 (0)