1- import { HEADER_PADDING , MAX_COLUMN_WIDTH , getColumnWidth } from '../getColumnWidth' ;
1+ import {
2+ HEADER_PADDING ,
3+ MAX_COLUMN_WIDTH ,
4+ PIXELS_PER_CHARACTER ,
5+ SORT_ICON_PADDING ,
6+ getColumnWidth ,
7+ } from '../getColumnWidth' ;
28
39describe ( 'getColumnWidth' , ( ) => {
410 it ( 'returns minimum width for empty data' , ( ) => {
511 const result = getColumnWidth ( { data : [ ] , name : 'test' } ) ;
6- expect ( result ) . toBe ( HEADER_PADDING + 'test' . length * 10 ) ;
12+ expect ( result ) . toBe ( HEADER_PADDING + 'test' . length * PIXELS_PER_CHARACTER ) ;
713 } ) ;
814
915 it ( 'calculates correct width for string columns' , ( ) => {
1016 const data = [ { test : 'short' } , { test : 'medium length' } , { test : 'this is a longer string' } ] ;
1117 const result = getColumnWidth ( { data, name : 'test' } ) ;
12- expect ( result ) . toBe ( HEADER_PADDING + 'this is a longer string' . length * 10 ) ;
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 + SORT_ICON_PADDING + 'test' . length * PIXELS_PER_CHARACTER ,
27+ ) ;
28+ } ) ;
29+
30+ it ( 'calculates correct width for columns with header' , ( ) => {
31+ const result = getColumnWidth ( { data : [ ] , name : 'test' , header : 'a' } ) ;
32+ expect ( result ) . toBe ( HEADER_PADDING + SORT_ICON_PADDING + 'a' . length * PIXELS_PER_CHARACTER ) ;
1333 } ) ;
1434
1535 it ( 'returns MAX_COLUMN_WIDTH when calculated width exceeds it' , ( ) => {
@@ -20,18 +40,54 @@ describe('getColumnWidth', () => {
2040
2141 it ( 'handles undefined data correctly' , ( ) => {
2242 const result = getColumnWidth ( { name : 'test' } ) ;
23- expect ( result ) . toBe ( HEADER_PADDING + 'test' . length * 10 ) ;
43+ expect ( result ) . toBe ( HEADER_PADDING + 'test' . length * PIXELS_PER_CHARACTER ) ;
2444 } ) ;
2545
2646 it ( 'handles missing values in data correctly' , ( ) => {
2747 const data = [ { test : 'short' } , { } , { test : 'longer string' } ] ;
2848 const result = getColumnWidth ( { data, name : 'test' } ) ;
29- expect ( result ) . toBe ( HEADER_PADDING + 'longer string' . length * 10 ) ;
49+ expect ( result ) . toBe ( HEADER_PADDING + 'longer string' . length * PIXELS_PER_CHARACTER ) ;
3050 } ) ;
3151
3252 it ( 'uses column name length when all values are shorter' , ( ) => {
3353 const data = [ { longColumnName : 'a' } , { longColumnName : 'bb' } ] ;
3454 const result = getColumnWidth ( { data, name : 'longColumnName' } ) ;
35- expect ( result ) . toBe ( HEADER_PADDING + 'longColumnName' . length * 10 ) ;
55+ expect ( result ) . toBe ( HEADER_PADDING + 'longColumnName' . length * PIXELS_PER_CHARACTER ) ;
56+ } ) ;
57+
58+ it ( 'handles null values in data correctly' , ( ) => {
59+ const data = [ { test : 'a' } , { test : null } ] ;
60+ const result = getColumnWidth ( { data, name : 'test' } ) ;
61+ expect ( result ) . toBe ( HEADER_PADDING + 'a' . length * PIXELS_PER_CHARACTER ) ;
62+ } ) ;
63+
64+ it ( 'handles undefined values in data correctly' , ( ) => {
65+ const data = [ { test : 'a' } , { test : undefined } ] ;
66+ const result = getColumnWidth ( { data, name : 'test' } ) ;
67+ expect ( result ) . toBe ( HEADER_PADDING + 'a' . length * PIXELS_PER_CHARACTER ) ;
68+ } ) ;
69+
70+ it ( 'handles empty string values in data correctly' , ( ) => {
71+ const data = [ { test : 'short' } , { test : '' } , { test : 'longer string' } ] ;
72+ const result = getColumnWidth ( { data, name : 'test' } ) ;
73+ expect ( result ) . toBe ( HEADER_PADDING + 'longer string' . length * PIXELS_PER_CHARACTER ) ;
74+ } ) ;
75+
76+ it ( 'handles an array of numbers correctly' , ( ) => {
77+ const data = [ { test : 1 } , { test : 123 } , { test : 12345 } ] ;
78+ const result = getColumnWidth ( { data, name : 'test' } ) ;
79+ expect ( result ) . toBe ( HEADER_PADDING + '12345' . length * PIXELS_PER_CHARACTER ) ;
80+ } ) ;
81+
82+ it ( 'handles an array of mixed data types correctly' , ( ) => {
83+ const data = [ { test : 'short' } , { test : 123 } , { test : null } , { test : 'longer string' } ] ;
84+ const result = getColumnWidth ( { data, name : 'test' } ) ;
85+ expect ( result ) . toBe ( HEADER_PADDING + 'longer string' . length * PIXELS_PER_CHARACTER ) ;
86+ } ) ;
87+
88+ it ( 'handles empty name correctly' , ( ) => {
89+ const data = [ { test : 'test' } ] ;
90+ const result = getColumnWidth ( { data, name : '' } ) ;
91+ expect ( result ) . toBe ( HEADER_PADDING ) ;
3692 } ) ;
3793} ) ;
0 commit comments