-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathcolumnCalculator.js
More file actions
120 lines (97 loc) · 3.9 KB
/
Copy pathcolumnCalculator.js
File metadata and controls
120 lines (97 loc) · 3.9 KB
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
/* jslint node: true */
'use strict';
var assert = require('assert');
var ColumnCalculator = require('../src/columnCalculator');
describe('ColumnCalculator', function () {
describe('buildColumnWidths', function () {
it('should set calcWidth to specified width for fixed columns', function () {
var columns = [
{width: 50, _minWidth: 30, _maxWidth: 80},
{width: 35, _minWidth: 30, _maxWidth: 80},
{width: 20, _minWidth: 30, _maxWidth: 80}
];
ColumnCalculator.buildColumnWidths(columns, 320);
columns.forEach(function (col) {
assert.equal(col._calcWidth, col.width);
});
});
it('should set calcWidth to minWidth for fixed columns with elasticWidth set to true', function () {
var columns = [
{width: 50, _minWidth: 30, _maxWidth: 80},
{width: 35, _minWidth: 30, _maxWidth: 80},
{width: 20, _minWidth: 30, _maxWidth: 80, elasticWidth: true}
];
ColumnCalculator.buildColumnWidths(columns, 320);
assert.equal(columns[0]._calcWidth, columns[0].width);
assert.equal(columns[1]._calcWidth, columns[1].width);
assert.equal(columns[2]._calcWidth, columns[2]._minWidth);
});
it('should set auto to maxWidth if there is enough space for all columns', function () {
var columns = [
{width: 'auto', _minWidth: 30, _maxWidth: 41},
{width: 'auto', _minWidth: 30, _maxWidth: 42},
{width: 'auto', _minWidth: 30, _maxWidth: 43}
];
ColumnCalculator.buildColumnWidths(columns, 320);
columns.forEach(function (col) {
assert.equal(col._calcWidth, col._maxWidth);
});
});
it('should equally divide availableSpace to star columns', function () {
var columns = [
{width: '*', _minWidth: 30, _maxWidth: 41},
{width: 'star', _minWidth: 30, _maxWidth: 42},
{_minWidth: 30, _maxWidth: 43}
];
ColumnCalculator.buildColumnWidths(columns, 320);
columns.forEach(function (col) {
assert.equal(col._calcWidth, 320 / 3);
});
});
it('should set calcWidth to minWidth if there is not enough space for the table', function () {
var columns = [
{width: 'auto', _minWidth: 300, _maxWidth: 410},
{width: 'auto', _minWidth: 301, _maxWidth: 420},
{width: 'auto', _minWidth: 303, _maxWidth: 421},
];
ColumnCalculator.buildColumnWidths(columns, 320);
columns.forEach(function (col) {
assert.equal(col._calcWidth, col._minWidth);
});
});
it('should set calcWidth of star columns to availableSpace distributed across star'+
'columns if there is not enough space for the table', function () {
var columns = [
{width: 'auto', _minWidth: 300, _maxWidth: 410},
{width: '*', _minWidth: 301, _maxWidth: 420},
{width: 'star', _minWidth: 303, _maxWidth: 421},
];
ColumnCalculator.buildColumnWidths(columns, 320);
assert.equal(columns[0]._calcWidth, columns[0]._minWidth);
assert.equal(columns[1]._calcWidth, 10);
assert.equal(columns[2]._calcWidth, 10);
});
it('should make columns wider proportionally if table can fit within the available space', function () {
var columns = [
{width: 'auto', _minWidth: 30, _maxWidth: 41},
{width: 'auto', _minWidth: 31, _maxWidth: 42},
{width: 'auto', _minWidth: 33, _maxWidth: 421},
];
ColumnCalculator.buildColumnWidths(columns, 320);
assert(columns[0]._calcWidth > 30);
assert(columns[1]._calcWidth > 31);
assert(columns[2]._calcWidth > 220);
});
it('should first take into account auto columns and then divide remaining space equally between all star if there is enough space for the table', function () {
var columns = [
{width: '*', _minWidth: 30, _maxWidth: 41},
{width: 'auto', _minWidth: 31, _maxWidth: 42},
{width: '*', _minWidth: 33, _maxWidth: 421},
];
ColumnCalculator.buildColumnWidths(columns, 320);
assert(columns[1]._calcWidth > 31);
assert.equal(columns[0]._calcWidth, columns[0]._calcWidth);
assert.equal(columns[0]._calcWidth + columns[1]._calcWidth + columns[2]._calcWidth, 320);
});
});
});