|
| 1 | +import { |
| 2 | + HEADER_PADDING, |
| 3 | + MAX_COLUMN_WIDTH, |
| 4 | + PIXELS_PER_CHARACTER, |
| 5 | + SORT_ICON_TO_CHARACTERS, |
| 6 | + getColumnWidth, |
| 7 | +} from '../getColumnWidth'; |
| 8 | + |
| 9 | +describe('getColumnWidth', () => { |
| 10 | + it('returns minimum width for empty data', () => { |
| 11 | + const result = getColumnWidth({data: [], name: 'test'}); |
| 12 | + expect(result).toBe(HEADER_PADDING + 'test'.length * PIXELS_PER_CHARACTER); |
| 13 | + }); |
| 14 | + |
| 15 | + it('calculates correct width for string columns', () => { |
| 16 | + const data = [{test: 'short'}, {test: 'medium length'}, {test: 'this is a longer string'}]; |
| 17 | + const result = getColumnWidth({data, name: 'test'}); |
| 18 | + expect(result).toBe( |
| 19 | + HEADER_PADDING + 'this is a longer string'.length * PIXELS_PER_CHARACTER, |
| 20 | + ); |
| 21 | + }); |
| 22 | + |
| 23 | + it('calculates correct width for columns with sorting', () => { |
| 24 | + const result = getColumnWidth({data: [], name: 'test', sortable: true}); |
| 25 | + expect(result).toBe( |
| 26 | + HEADER_PADDING + ('test'.length + SORT_ICON_TO_CHARACTERS) * PIXELS_PER_CHARACTER, |
| 27 | + ); |
| 28 | + }); |
| 29 | + it('calculates correct width for columns with sorting and column name wider than header', () => { |
| 30 | + const data = [{test: 'this is a longer string'}]; |
| 31 | + const result = getColumnWidth({data, name: 'test', sortable: true}); |
| 32 | + expect(result).toBe( |
| 33 | + HEADER_PADDING + 'this is a longer string'.length * PIXELS_PER_CHARACTER, |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + it('calculates correct width for columns with header', () => { |
| 38 | + const result = getColumnWidth({data: [], name: 'test', header: 'a'}); |
| 39 | + expect(result).toBe(HEADER_PADDING + 'a'.length * PIXELS_PER_CHARACTER); |
| 40 | + }); |
| 41 | + |
| 42 | + it('returns MAX_COLUMN_WIDTH when calculated width exceeds it', () => { |
| 43 | + const data = [{test: 'a'.repeat(100)}]; |
| 44 | + const result = getColumnWidth({data, name: 'test'}); |
| 45 | + expect(result).toBe(MAX_COLUMN_WIDTH); |
| 46 | + }); |
| 47 | + |
| 48 | + it('handles undefined data correctly', () => { |
| 49 | + const result = getColumnWidth({name: 'test'}); |
| 50 | + expect(result).toBe(HEADER_PADDING + 'test'.length * PIXELS_PER_CHARACTER); |
| 51 | + }); |
| 52 | + |
| 53 | + it('handles missing values in data correctly', () => { |
| 54 | + const data = [{test: 'short'}, {}, {test: 'longer string'}]; |
| 55 | + const result = getColumnWidth({data, name: 'test'}); |
| 56 | + expect(result).toBe(HEADER_PADDING + 'longer string'.length * PIXELS_PER_CHARACTER); |
| 57 | + }); |
| 58 | + |
| 59 | + it('uses column name length when all values are shorter', () => { |
| 60 | + const data = [{longColumnName: 'a'}, {longColumnName: 'bb'}]; |
| 61 | + const result = getColumnWidth({data, name: 'longColumnName'}); |
| 62 | + expect(result).toBe(HEADER_PADDING + 'longColumnName'.length * PIXELS_PER_CHARACTER); |
| 63 | + }); |
| 64 | + |
| 65 | + it('handles null values in data correctly', () => { |
| 66 | + const data = [{test: 'a'}, {test: null}]; |
| 67 | + const result = getColumnWidth({data, name: 'test'}); |
| 68 | + expect(result).toBe(HEADER_PADDING + 'test'.length * PIXELS_PER_CHARACTER); |
| 69 | + }); |
| 70 | + |
| 71 | + it('handles undefined values in data correctly', () => { |
| 72 | + const data = [{test: 'a'}, {test: undefined}]; |
| 73 | + const result = getColumnWidth({data, name: 'test'}); |
| 74 | + expect(result).toBe(HEADER_PADDING + 'test'.length * PIXELS_PER_CHARACTER); |
| 75 | + }); |
| 76 | + |
| 77 | + it('handles empty string values in data correctly', () => { |
| 78 | + const data = [{test: 'short'}, {test: ''}, {test: 'longer string'}]; |
| 79 | + const result = getColumnWidth({data, name: 'test'}); |
| 80 | + expect(result).toBe(HEADER_PADDING + 'longer string'.length * PIXELS_PER_CHARACTER); |
| 81 | + }); |
| 82 | + |
| 83 | + it('handles an array of numbers correctly', () => { |
| 84 | + const data = [{test: 1}, {test: 123}, {test: 12345}]; |
| 85 | + const result = getColumnWidth({data, name: 'test'}); |
| 86 | + expect(result).toBe(HEADER_PADDING + '12345'.length * PIXELS_PER_CHARACTER); |
| 87 | + }); |
| 88 | + |
| 89 | + it('handles an array of mixed data types correctly', () => { |
| 90 | + const data = [{test: 'short'}, {test: 123}, {test: null}, {test: 'longer string'}]; |
| 91 | + const result = getColumnWidth({data, name: 'test'}); |
| 92 | + expect(result).toBe(HEADER_PADDING + 'longer string'.length * PIXELS_PER_CHARACTER); |
| 93 | + }); |
| 94 | + |
| 95 | + it('handles empty name correctly', () => { |
| 96 | + const data = [{test: 'test'}]; |
| 97 | + const result = getColumnWidth({data, name: ''}); |
| 98 | + expect(result).toBe(HEADER_PADDING); |
| 99 | + }); |
| 100 | +}); |
0 commit comments