Skip to content

Commit 8d4d238

Browse files
cybermerlinmportuga
authored andcommitted
chore(ui-grid.core): Clean up and more tests
- Cleaning up spacing in many files, with some refactoring - Improve code coverage of GridColumn.js
1 parent 1bd060c commit 8d4d238

File tree

5 files changed

+212
-23
lines changed

5 files changed

+212
-23
lines changed

src/js/core/directives/ui-pinned-container.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
(function(){
22
'use strict';
33

4-
// TODO: rename this file to ui-grid-pinned-container.js
5-
64
angular.module('ui.grid').directive('uiGridPinnedContainer', ['gridUtil', function (gridUtil) {
75
return {
86
restrict: 'EA',
97
replace: true,
10-
template: '<div class="ui-grid-pinned-container"><div ui-grid-render-container container-id="side" row-container-name="\'body\'" col-container-name="side" bind-scroll-vertical="true" class="{{ side }} ui-grid-render-container-{{ side }}"></div></div>',
8+
template: '<div class="ui-grid-pinned-container">'
9+
+ '<div ui-grid-render-container container-id="side"'
10+
+ ' row-container-name="\'body\'" col-container-name="side"'
11+
+ ' bind-scroll-vertical="true"'
12+
+ ' class="{{ side }} ui-grid-render-container-{{ side }}"></div>'
13+
+ '</div>',
1114
scope: {
1215
side: '=uiGridPinnedContainer'
1316
},

src/js/core/factories/GridColumn.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ angular.module('ui.grid')
4242
* @ngdoc property
4343
* @name displayName
4444
* @propertyOf ui.grid.class:GridColumn
45-
* @description Column name that will be shown in the header. If displayName is not
45+
* @description Column name that will be shown in the header. If displayName is not
4646
* provided then one is generated using the name.
4747
*
4848
*/
@@ -51,7 +51,7 @@ angular.module('ui.grid')
5151
* @ngdoc property
5252
* @name displayName
5353
* @propertyOf ui.grid.class:GridOptions.columnDef
54-
* @description Column name that will be shown in the header. If displayName is not
54+
* @description Column name that will be shown in the header. If displayName is not
5555
* provided then one is generated using the name.
5656
*
5757
*/
@@ -444,6 +444,11 @@ angular.module('ui.grid')
444444
*
445445
*/
446446

447+
// Use colDef.displayName as long as it's not undefined, otherwise default to the field name
448+
function getDisplayName(colDef) {
449+
return colDef.displayName || gridUtil.readableColumnName(colDef.name);
450+
}
451+
447452
/**
448453
* @ngdoc method
449454
* @methodOf ui.grid.class:GridColumn
@@ -464,7 +469,7 @@ angular.module('ui.grid')
464469
throw new Error('colDef.name is required for column at index ' + self.grid.options.columnDefs.indexOf(colDef));
465470
}
466471

467-
self.displayName = (colDef.displayName === undefined) ? gridUtil.readableColumnName(colDef.name) : colDef.displayName;
472+
self.displayName = getDisplayName(colDef);
468473

469474
if (!angular.isNumber(self.width) || !self.hasCustomWidth || colDef.allowCustomWidthOverride) {
470475
var colDefWidth = colDef.width;
@@ -538,9 +543,7 @@ angular.module('ui.grid')
538543
}
539544

540545
self.name = colDef.name;
541-
542-
// Use colDef.displayName as long as it's not undefined, otherwise default to the field name
543-
self.displayName = (colDef.displayName === undefined) ? gridUtil.readableColumnName(colDef.name) : colDef.displayName;
546+
self.displayName = getDisplayName(colDef);
544547

545548
//self.originalIndex = index;
546549

src/js/core/services/gridClassFactory.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
if (grid.options.rowTemplate) {
2929
var rowTemplateFnPromise = $q.defer();
3030
grid.getRowTemplateFn = rowTemplateFnPromise.promise;
31-
31+
3232
gridUtil.getTemplate(grid.options.rowTemplate)
3333
.then(
3434
function (template) {
@@ -102,10 +102,12 @@
102102
.then(
103103
function (template) {
104104
if ( angular.isFunction(template) ) { template = template(); }
105-
var tooltipCall = ( tooltipType === 'cellTooltip' ) ? 'col.cellTooltip(row,col)' : 'col.headerTooltip(col)';
105+
var tooltipCall = ( tooltipType === 'cellTooltip' )
106+
? 'col.cellTooltip(row,col)'
107+
: 'col.headerTooltip(col)';
106108
if ( tooltipType && col[tooltipType] === false ){
107109
template = template.replace(uiGridConstants.TOOLTIP, '');
108-
} else if ( tooltipType && col[tooltipType] ){
110+
} else if ( tooltipType && col[tooltipType] ) {
109111
template = template.replace(uiGridConstants.TOOLTIP, 'title="{{' + tooltipCall + ' CUSTOM_FILTERS }}"');
110112
}
111113

@@ -195,7 +197,7 @@
195197
.then(function (template) {
196198
// Compile the template
197199
var rowTemplateFn = $compile(template);
198-
200+
199201
// Resolve the compiled template function promise
200202
perRowTemplateFnPromise.resolve(rowTemplateFn);
201203
},

src/templates/ui-grid/uiGridViewport.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<div
2-
role="rowgroup"
3-
class="ui-grid-viewport"
4-
ng-style="colContainer.getViewportStyle()"><!-- tbody -->
2+
role="rowgroup"
3+
class="ui-grid-viewport"
4+
ng-style="colContainer.getViewportStyle()"><!-- tbody -->
55
<div
6-
class="ui-grid-canvas">
6+
class="ui-grid-canvas">
77
<div
8-
ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index"
9-
class="ui-grid-row"
10-
ng-style="Viewport.rowStyle(rowRenderIndex)">
8+
ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index"
9+
class="ui-grid-row"
10+
ng-style="Viewport.rowStyle(rowRenderIndex)">
1111
<div
12-
role="row"
13-
ui-grid-row="row"
14-
row-render-index="rowRenderIndex">
12+
role="row"
13+
ui-grid-row="row"
14+
row-render-index="rowRenderIndex">
1515
</div>
1616
</div>
1717
</div>

test/unit/core/factories/GridColumn.spec.js

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,149 @@ describe('GridColumn factory', function () {
622622
expect(updateCol(col, colDef)).toThrow();
623623
});
624624

625+
describe('cellTooltip', function() {
626+
it('should be set to false when it is undefined', function() {
627+
colDef.cellTooltip = undefined;
628+
col.updateColumnDef(colDef);
629+
expect(col.cellTooltip).toBe(false);
630+
});
631+
it('should stay false when the user passed false', function() {
632+
colDef.cellTooltip = false;
633+
col.updateColumnDef(colDef);
634+
expect(col.cellTooltip).toBe(false);
635+
});
636+
it('should be set to a function that calls grid.getCellValue when it is set to true', function() {
637+
colDef.cellTooltip = true;
638+
col.updateColumnDef(colDef);
639+
expect(angular.isFunction(col.cellTooltip)).toBe(true);
640+
641+
spyOn(col.grid, 'getCellValue').and.callFake(angular.noop);
642+
col.cellTooltip(null, col);
643+
644+
expect(col.grid.getCellValue).toHaveBeenCalled();
645+
646+
col.grid.getCellValue.calls.reset();
647+
});
648+
it('should be assigned to whatever function is passed to it', function() {
649+
colDef.cellTooltip = angular.noop;
650+
col.updateColumnDef(colDef);
651+
expect(angular.isFunction(col.cellTooltip)).toBe(true);
652+
expect(col.cellTooltip).toEqual(colDef.cellTooltip);
653+
});
654+
it('should be set to a function that returns the string passed into it when it is set to a string', function() {
655+
colDef.cellTooltip = 'MockHeaderTooltip';
656+
col.updateColumnDef(colDef);
657+
expect(angular.isFunction(col.cellTooltip)).toBe(true);
658+
expect(col.cellTooltip(null, col)).toEqual(colDef.cellTooltip);
659+
});
660+
});
661+
662+
describe('headerTooltip', function() {
663+
it('should be set to false when it is undefined', function() {
664+
colDef.headerTooltip = undefined;
665+
col.updateColumnDef(colDef);
666+
expect(col.headerTooltip).toBe(false);
667+
});
668+
it('should stay false when the user passed false', function() {
669+
colDef.headerTooltip = false;
670+
col.updateColumnDef(colDef);
671+
expect(col.headerTooltip).toBe(false);
672+
});
673+
it('should be set to a function that returns the display name when it is set to true', function() {
674+
colDef.headerTooltip = true;
675+
col.updateColumnDef(colDef);
676+
expect(angular.isFunction(col.headerTooltip)).toBe(true);
677+
expect(col.headerTooltip(col)).toEqual(col.displayName);
678+
});
679+
it('should be assigned to whatever function is passed to it', function() {
680+
colDef.headerTooltip = angular.noop;
681+
col.updateColumnDef(colDef);
682+
expect(angular.isFunction(col.headerTooltip)).toBe(true);
683+
expect(col.headerTooltip).toEqual(colDef.headerTooltip);
684+
});
685+
it('should be set to a function that returns the string passed into it when it is set to a string', function() {
686+
colDef.headerTooltip = 'MockHeaderTooltip';
687+
col.updateColumnDef(colDef);
688+
expect(angular.isFunction(col.headerTooltip)).toBe(true);
689+
expect(col.headerTooltip(col)).toEqual(colDef.headerTooltip);
690+
});
691+
});
692+
693+
describe('sortCellFiltered', function() {
694+
it('should set sortCellFiltered to true when the user passes in a truthy value', function() {
695+
colDef.sortCellFiltered = 1;
696+
col.updateColumnDef(colDef);
697+
698+
expect(col.sortCellFiltered).toBe(true);
699+
});
700+
it('should set sortCellFiltered to false when the user passes in a falsy value', function() {
701+
colDef.sortCellFiltered = 0;
702+
col.updateColumnDef(colDef);
703+
704+
expect(col.sortCellFiltered).toBe(false);
705+
});
706+
});
707+
708+
describe('filterCellFiltered', function() {
709+
it('should set filterCellFiltered to true when the user passes in a truthy value', function() {
710+
colDef.filterCellFiltered = 'yes';
711+
col.updateColumnDef(colDef);
712+
713+
expect(col.filterCellFiltered).toBe(true);
714+
});
715+
it('should set filterCellFiltered to false when the user passes in a falsy value', function() {
716+
colDef.filterCellFiltered = null;
717+
col.updateColumnDef(colDef);
718+
719+
expect(col.filterCellFiltered).toBe(false);
720+
});
721+
});
722+
723+
describe('footerCellFilter', function() {
724+
it('should set footerCellFilter to the value passed in by the user when the user passes in a truthy value', function() {
725+
colDef.footerCellFilter = 'date';
726+
col.updateColumnDef(colDef);
727+
728+
expect(col.footerCellFilter).toEqual(colDef.footerCellFilter);
729+
});
730+
it('should set footerCellFilter an empty string when the user passes in a falsy value', function() {
731+
colDef.footerCellFilter = null;
732+
col.updateColumnDef(colDef);
733+
734+
expect(col.footerCellFilter).toEqual('');
735+
});
736+
});
737+
738+
describe('sortDirectionCycle', function() {
739+
it('should set sortDirectionCycle to the value passed in by the user when the user passes in a truthy value', function() {
740+
colDef.sortDirectionCycle = [uiGridConstants.ASC, uiGridConstants.DESC];
741+
col.updateColumnDef(colDef);
742+
743+
expect(col.sortDirectionCycle).toEqual(colDef.sortDirectionCycle);
744+
});
745+
it('should set sortDirectionCycle the default value when the user passes in undefined', function() {
746+
colDef.sortDirectionCycle = undefined;
747+
col.updateColumnDef(colDef);
748+
749+
expect(col.sortDirectionCycle).toEqual([null, uiGridConstants.ASC, uiGridConstants.DESC]);
750+
});
751+
});
752+
753+
describe('enableFiltering', function() {
754+
it('should set enableFiltering to the value passed by the user when that value is not undefined', function() {
755+
colDef.enableFiltering = false;
756+
col.updateColumnDef(colDef);
757+
758+
expect(col.enableFiltering).toEqual(colDef.enableFiltering);
759+
});
760+
it('should set enableFiltering to true when the user passes in undefined', function() {
761+
colDef.enableFiltering = undefined;
762+
col.updateColumnDef(colDef);
763+
764+
expect(col.enableFiltering).toBe(true);
765+
});
766+
});
767+
625768
function widthEqualsColDefWidth(expected) {
626769
return function () {
627770
colDef.width = expected;
@@ -653,4 +796,42 @@ describe('GridColumn factory', function () {
653796
};
654797
}
655798
});
799+
800+
describe('isPinnedLeft', function() {
801+
var col;
802+
803+
beforeEach(function () {
804+
col = grid.columns[0];
805+
});
806+
807+
it('should return true when the column is on the left render container', function() {
808+
col.renderContainer = 'left';
809+
810+
expect(col.isPinnedLeft()).toBe(true);
811+
});
812+
it('should return false when the column is not on the left render container', function() {
813+
col.renderContainer = 'right';
814+
815+
expect(col.isPinnedLeft()).toBe(false);
816+
});
817+
});
818+
819+
describe('isPinnedRight', function() {
820+
var col;
821+
822+
beforeEach(function () {
823+
col = grid.columns[0];
824+
});
825+
826+
it('should return true when the column is on the right render container', function() {
827+
col.renderContainer = 'right';
828+
829+
expect(col.isPinnedRight()).toBe(true);
830+
});
831+
it('should return false when the column is not on the right render container', function() {
832+
col.renderContainer = 'left';
833+
834+
expect(col.isPinnedRight()).toBe(false);
835+
});
836+
});
656837
});

0 commit comments

Comments
 (0)