|
| 1 | +import {MAX_COLUMN_WIDTH, getColumnWidth} from './getColumnWidth'; |
| 2 | + |
| 3 | +describe('getColumnWidth', () => { |
| 4 | + it('returns minimum width for empty data', () => { |
| 5 | + const result = getColumnWidth({data: [], name: 'test', columnType: 'string'}); |
| 6 | + expect(result).toBe(20 + 'test'.length * 10); |
| 7 | + }); |
| 8 | + |
| 9 | + it('calculates correct width for string columns', () => { |
| 10 | + const data = [{test: 'short'}, {test: 'medium length'}, {test: 'this is a longer string'}]; |
| 11 | + const result = getColumnWidth({data, name: 'test', columnType: 'string'}); |
| 12 | + expect(result).toBe(20 + 'this is a longer string'.length * 10); |
| 13 | + }); |
| 14 | + |
| 15 | + it('calculates correct width for number columns', () => { |
| 16 | + const data = [{test: 123}, {test: 456789}, {test: 0}]; |
| 17 | + const result = getColumnWidth({data, name: 'test', columnType: 'number'}); |
| 18 | + expect(result).toBe(40 + '456789'.length * 10); |
| 19 | + }); |
| 20 | + |
| 21 | + it('returns MAX_COLUMN_WIDTH when calculated width exceeds it', () => { |
| 22 | + const data = [{test: 'a'.repeat(100)}]; |
| 23 | + const result = getColumnWidth({data, name: 'test', columnType: 'string'}); |
| 24 | + expect(result).toBe(MAX_COLUMN_WIDTH); |
| 25 | + }); |
| 26 | + |
| 27 | + it('handles undefined data correctly', () => { |
| 28 | + const result = getColumnWidth({name: 'test', columnType: 'string'}); |
| 29 | + expect(result).toBe(20 + 'test'.length * 10); |
| 30 | + }); |
| 31 | + |
| 32 | + it('handles missing values in data correctly', () => { |
| 33 | + const data = [{test: 'short'}, {}, {test: 'longer string'}]; |
| 34 | + const result = getColumnWidth({data, name: 'test', columnType: 'string'}); |
| 35 | + expect(result).toBe(20 + 'longer string'.length * 10); |
| 36 | + }); |
| 37 | + |
| 38 | + it('uses column name length when all values are shorter', () => { |
| 39 | + const data = [{longColumnName: 'a'}, {longColumnName: 'bb'}]; |
| 40 | + const result = getColumnWidth({data, name: 'longColumnName', columnType: 'string'}); |
| 41 | + expect(result).toBe(20 + 'longColumnName'.length * 10); |
| 42 | + }); |
| 43 | +}); |
0 commit comments