diff --git a/src/js/core/factories/GridColumn.js b/src/js/core/factories/GridColumn.js index 608fd37f71..3039c707ed 100644 --- a/src/js/core/factories/GridColumn.js +++ b/src/js/core/factories/GridColumn.js @@ -464,8 +464,23 @@ angular.module('ui.grid') } } - self.minWidth = !colDef.minWidth ? 30 : colDef.minWidth; - self.maxWidth = !colDef.maxWidth ? 9000 : colDef.maxWidth; + ['minWidth', 'maxWidth'].forEach(function (name) { + var minOrMaxWidth = colDef[name]; + var parseErrorMsg = "Cannot parse column " + name + " '" + minOrMaxWidth + "' for column named '" + colDef.name + "'"; + + if (!angular.isString(minOrMaxWidth) && !angular.isNumber(minOrMaxWidth)) { + //Sets default minWidth and maxWidth values + self[name] = ((name === 'minWidth') ? 30 : 9000); + } else if (angular.isString(minOrMaxWidth)) { + if (minOrMaxWidth.match(/^(\d+)$/)) { + self[name] = parseInt(minOrMaxWidth.match(/^(\d+)$/)[1], 10); + } else { + throw new Error(parseErrorMsg); + } + } else { + self[name] = minOrMaxWidth; + } + }); //use field if it is defined; name if it is not self.field = (colDef.field === undefined) ? colDef.name : colDef.field; diff --git a/test/unit/core/factories/GridColumn.spec.js b/test/unit/core/factories/GridColumn.spec.js index 55980a59d2..6caeb5c3af 100644 --- a/test/unit/core/factories/GridColumn.spec.js +++ b/test/unit/core/factories/GridColumn.spec.js @@ -560,6 +560,52 @@ describe('GridColumn factory', function () { expect(updateCol(colDef.width)).toThrow(); }); + it ('should set the value of minWidth to 30 when colDef.minWidth is undefined', invalidMinOrMaxWidthDef(undefined, 'minWidth')); + it ('should set the value of minWidth to 30 when colDef.minWidth is null', invalidMinOrMaxWidthDef(null, 'minWidth')); + it ('should set the value of minWidth to 30 when colDef.minWidth is an object', invalidMinOrMaxWidthDef({}, 'minWidth')); + + it ('should set the value of minWidth to the parsed integer colDef.minWidth when it is a string', function () { + colDef.minWidth = '90'; + col.updateColumnDef(colDef); + expect(col.minWidth).toBe(90); + }); + + it ('should set the value of minWidth to colDef.minWidth when it is a number', function () { + colDef.minWidth = 90; + col.updateColumnDef(colDef); + expect(col.minWidth).toBe(90); + }); + + it ('should throw when colDef.minWidth is an invalid string', function () { + colDef.minWidth = 'e1%'; + expect(updateCol(col, colDef)).toThrow(); + colDef.minWidth = '#FFF'; + expect(updateCol(col, colDef)).toThrow(); + }); + + it ('should set the value of maxWidth to 9000 when colDef.maxWidth is undefined', invalidMinOrMaxWidthDef(undefined, 'maxWidth')); + it ('should set the value of maxWidth to 9000 when colDef.maxWidth is null', invalidMinOrMaxWidthDef(null, 'maxWidth')); + it ('should set the value of maxWidth to 9000 when colDef.maxWidth is an object', invalidMinOrMaxWidthDef({}, 'maxWidth')); + + it ('should set the value of maxWidth to the parsed integer colDef.maxWidth when it is a string', function () { + colDef.maxWidth = '200'; + col.updateColumnDef(colDef); + expect(col.maxWidth).toBe(200); + }); + + it ('should set the value of maxWidth to colDef.maxWidth when it is a number', function () { + colDef.maxWidth = 200; + col.updateColumnDef(colDef); + expect(col.maxWidth).toBe(200); + }); + + it ('should throw when colDef.maxWidth is an invalid string', function () { + colDef.maxWidth = 'e1%'; + expect(updateCol(col, colDef)).toThrow(); + colDef.maxWidth = '#FFF'; + expect(updateCol(col, colDef)).toThrow(); + }); + function widthEqualsColDefWidth(expected) { return function () { colDef.width = expected; @@ -581,5 +627,13 @@ describe('GridColumn factory', function () { col.updateColumnDef(colDef); }; } + + function invalidMinOrMaxWidthDef(width, minOrMax) { + return function () { + colDef[minOrMax] = width; + col.updateColumnDef(colDef); + expect(col[minOrMax]).toBe(minOrMax === 'minWidth' ? 30 : 9000); + }; + } }); }); \ No newline at end of file