Skip to content

Commit

Permalink
fix(core): fix #4180 by adding validity check for minWidth and maxWidth
Browse files Browse the repository at this point in the history
- String and number are both allowed.

- Valid String value will parse into number properly.

- Invalid String value will throw an error.
  • Loading branch information
merlinchen committed Aug 22, 2015
1 parent ff9de89 commit 5b03cb2
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/js/core/factories/GridColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
54 changes: 54 additions & 0 deletions test/unit/core/factories/GridColumn.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
};
}
});
});

0 comments on commit 5b03cb2

Please sign in to comment.