|
1 |
| -import { forceFillColumnWidths } from './math'; |
| 1 | +import { adjustColumnWidths, forceFillColumnWidths } from './math'; |
2 | 2 |
|
3 | 3 | describe('Math function', () => {
|
4 | 4 | describe('forceFillColumnWidths', () => {
|
@@ -34,4 +34,51 @@ describe('Math function', () => {
|
34 | 34 | });
|
35 | 35 | });
|
36 | 36 | });
|
| 37 | + |
| 38 | + describe('adjustColumnWidths', () => { |
| 39 | + describe('flex mode', () => { |
| 40 | + it('should not go over/under compared to given max width', () => { |
| 41 | + const cols = [ |
| 42 | + { prop: 'id1', width: 287, maxWidth: undefined, minWidth: 175, flexGrow: 2, canAutoResize: true }, |
| 43 | + { prop: 'id2', width: 215, maxWidth: undefined, minWidth: 200, flexGrow: 1.5, canAutoResize: true }, |
| 44 | + { prop: 'id3', width: 287, maxWidth: undefined, minWidth: 150, flexGrow: 2, canAutoResize: true }, |
| 45 | + { prop: 'id4', width: 175, maxWidth: undefined, minWidth: 175, flexGrow: 1, canAutoResize: true }, |
| 46 | + { prop: 'id5', width: 143, maxWidth: undefined, minWidth: 120, flexGrow: 1, canAutoResize: true } |
| 47 | + ]; |
| 48 | + |
| 49 | + const givenTableWidth = 1180; |
| 50 | + |
| 51 | + adjustColumnWidths(cols, givenTableWidth); |
| 52 | + |
| 53 | + const totalAdjustedColumnWidths = cols.map(c => c.width).reduce((p, c) => p + c, 0); |
| 54 | + expect(totalAdjustedColumnWidths).toBeCloseTo(givenTableWidth, 0.001); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should overflow if the total of given min widths is bigger than given max width', () => { |
| 58 | + const cols = [ |
| 59 | + { prop: 'id1', width: 100, maxWidth: undefined, minWidth: 100, flexGrow: 1, canAutoResize: true }, |
| 60 | + { prop: 'id2', width: 100, maxWidth: undefined, minWidth: 100, flexGrow: 1, canAutoResize: true } |
| 61 | + ]; |
| 62 | + const maxWidth = 199; |
| 63 | + |
| 64 | + adjustColumnWidths(cols, maxWidth); |
| 65 | + |
| 66 | + const totalAdjustedColumnWidths = cols.map(c => c.width).reduce((p, c) => p + c, 0); |
| 67 | + expect(totalAdjustedColumnWidths).toBeGreaterThan(maxWidth); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should respect min widths', () => { |
| 71 | + const cols = [ |
| 72 | + { prop: 'id1', width: 0, maxWidth: undefined, minWidth: 10, flexGrow: 3.0000000000000075, canAutoResize: true }, |
| 73 | + { prop: 'id2', width: 0, maxWidth: undefined, minWidth: 10, flexGrow: 1, canAutoResize: true } |
| 74 | + ]; |
| 75 | + |
| 76 | + adjustColumnWidths(cols, 40); |
| 77 | + |
| 78 | + for (const col of cols) { |
| 79 | + expect(col.width - col.minWidth).toBeGreaterThanOrEqual(0); |
| 80 | + } |
| 81 | + }); |
| 82 | + }); |
| 83 | + }); |
37 | 84 | });
|
0 commit comments