1
1
import { TABLE_DATA_TYPE } from '../table/constants' ;
2
- import { TABLE_FIXED_TYPE } from './constants' ;
3
- import { getFixedColumnKeys , isFixedTableRow } from './helpers' ;
2
+ import { FIXED_COLUMN_LEFT_SIDE , FIXED_COLUMN_RIGHT_SIDE , TABLE_FIXED_TYPE } from './constants' ;
3
+ import { getFixedColumnKeys , isFixedTableRow , calculateFixedColumnProps } from './helpers' ;
4
4
5
5
describe ( 'TableFixedColumns Plugin helpers' , ( ) => {
6
+ const sampleType = Symbol ( 'sample' ) ;
7
+ const createColumn = ( name , fixed ) => ( {
8
+ key : `key_${ name } ` , type : TABLE_DATA_TYPE , column : { name } , ...fixed && { fixed } ,
9
+ } ) ;
10
+ const tableColumns = [
11
+ createColumn ( 'a' , FIXED_COLUMN_LEFT_SIDE ) ,
12
+ createColumn ( 'b' , FIXED_COLUMN_LEFT_SIDE ) ,
13
+ {
14
+ key : 'key_type1' , type : sampleType ,
15
+ } ,
16
+ createColumn ( 'c' , FIXED_COLUMN_RIGHT_SIDE ) ,
17
+ createColumn ( 'd' , FIXED_COLUMN_RIGHT_SIDE ) ,
18
+ ] ;
19
+ const findColumnByNameCore = ( name , columns ) => (
20
+ columns . find ( c => c . column && c . column . name === name )
21
+ ) ;
22
+ const tableColumnDimensions = {
23
+ key_a : 20 ,
24
+ key_b : 30 ,
25
+ key_type1 : 40 ,
26
+ key_c : 70 ,
27
+ key_d : 150 ,
28
+ } ;
29
+
6
30
describe ( '#getFixedColumnKeys' , ( ) => {
7
31
it ( 'should return the correct array of column keys' , ( ) => {
8
- const sampleType = Symbol ( 'sample' ) ;
9
-
10
- const tableColumns = [
11
- { key : 'key_a' , type : TABLE_DATA_TYPE , column : { name : 'a' } } ,
12
- { key : 'key_b' , type : TABLE_DATA_TYPE , column : { name : 'b' } } ,
13
- { key : 'key_type1' , type : sampleType } ,
14
- { key : 'key_c' , type : TABLE_DATA_TYPE , column : { name : 'c' } } ,
15
- { key : 'key_d' , type : TABLE_DATA_TYPE , column : { name : 'd' } } ,
16
- ] ;
17
32
const fixedNames = [ 'a' , 'd' , sampleType ] ;
18
33
19
34
expect ( getFixedColumnKeys ( tableColumns , fixedNames ) )
@@ -27,4 +42,113 @@ describe('TableFixedColumns Plugin helpers', () => {
27
42
expect ( isFixedTableRow ( { type : 'undefined' } ) ) . toBeFalsy ( ) ;
28
43
} ) ;
29
44
} ) ;
45
+
46
+ describe ( '#calculateFixedColumnProps' , ( ) => {
47
+ describe ( 'position' , ( ) => {
48
+ const calculatePosition = ( fixedColumns , column ) => {
49
+ const { position } = calculateFixedColumnProps (
50
+ { tableColumn : column } ,
51
+ fixedColumns ,
52
+ tableColumns ,
53
+ tableColumnDimensions ,
54
+ ) ;
55
+ return position ;
56
+ } ;
57
+ const findColumnByName = name => findColumnByNameCore ( name , tableColumns ) ;
58
+
59
+ it ( 'should calculate position of columns fixed at the right side' , ( ) => {
60
+ const fixedColumns = { leftColumns : [ 'a' ] , rightColumns : [ 'c' , 'd' ] } ;
61
+
62
+ expect ( calculatePosition ( fixedColumns , findColumnByName ( 'c' ) ) ) . toBe ( 150 ) ;
63
+ expect ( calculatePosition ( fixedColumns , findColumnByName ( 'd' ) ) ) . toBe ( 0 ) ;
64
+ } ) ;
65
+
66
+ it ( 'should calculate position of columns fixed at the left side' , ( ) => {
67
+ const fixedColumns = { leftColumns : [ 'a' , 'b' ] , rightColumns : [ 'c' ] } ;
68
+
69
+ expect ( calculatePosition ( fixedColumns , findColumnByName ( 'a' ) ) ) . toBe ( 0 ) ;
70
+ expect ( calculatePosition ( fixedColumns , findColumnByName ( 'b' ) ) ) . toBe ( 20 ) ;
71
+ } ) ;
72
+ } ) ;
73
+
74
+ describe ( 'dividers visibility' , ( ) => {
75
+ const extendedTableColumns = [
76
+ createColumn ( 'a' , FIXED_COLUMN_LEFT_SIDE ) ,
77
+ createColumn ( 'col0' ) ,
78
+ createColumn ( 'b0' , FIXED_COLUMN_LEFT_SIDE ) ,
79
+ createColumn ( 'b1' , FIXED_COLUMN_LEFT_SIDE ) ,
80
+ createColumn ( 'b2' , FIXED_COLUMN_LEFT_SIDE ) ,
81
+ createColumn ( 'col1' ) ,
82
+ createColumn ( 'c' , FIXED_COLUMN_LEFT_SIDE ) ,
83
+ createColumn ( 'col2' ) ,
84
+ createColumn ( 'd' , FIXED_COLUMN_RIGHT_SIDE ) ,
85
+ createColumn ( 'col2' ) ,
86
+ createColumn ( 'e0' , FIXED_COLUMN_RIGHT_SIDE ) ,
87
+ createColumn ( 'e1' , FIXED_COLUMN_RIGHT_SIDE ) ,
88
+ createColumn ( 'e2' , FIXED_COLUMN_RIGHT_SIDE ) ,
89
+ createColumn ( 'col3' ) ,
90
+ createColumn ( 'f' , FIXED_COLUMN_RIGHT_SIDE ) ,
91
+ ] ;
92
+ const findColumnByName = name => findColumnByNameCore ( name , extendedTableColumns ) ;
93
+
94
+ const calculateDividers = column => ( calculateFixedColumnProps (
95
+ { tableColumn : column } ,
96
+ { leftColumns : [ 'a' , 'b0' , 'b1' , 'b2' , 'c' ] , rightColumns : [ 'd' , 'e0' , 'e1' , 'e2' , 'f' ] } ,
97
+ extendedTableColumns ,
98
+ { } ,
99
+ ) ) ;
100
+
101
+ it ( 'should be visible for standalone left column' , ( ) => {
102
+ const { showLeftDivider, showRightDivider } = calculateDividers ( findColumnByName ( 'c' ) ) ;
103
+
104
+ expect ( showLeftDivider ) . toBeTruthy ( ) ;
105
+ expect ( showRightDivider ) . toBeTruthy ( ) ;
106
+ } ) ;
107
+ it ( 'should be visible for standalone right column' , ( ) => {
108
+ const { showLeftDivider, showRightDivider } = calculateDividers ( findColumnByName ( 'd' ) ) ;
109
+
110
+ expect ( showLeftDivider ) . toBeTruthy ( ) ;
111
+ expect ( showRightDivider ) . toBeTruthy ( ) ;
112
+ } ) ;
113
+
114
+ it ( 'should show only right divider for standalone leftmost column' , ( ) => {
115
+ const { showLeftDivider, showRightDivider } = calculateDividers ( findColumnByName ( 'a' ) ) ;
116
+
117
+ expect ( showLeftDivider ) . toBeFalsy ( ) ;
118
+ expect ( showRightDivider ) . toBeTruthy ( ) ;
119
+ } ) ;
120
+ it ( 'should show only left divider for standalone rightmost column' , ( ) => {
121
+ const { showLeftDivider, showRightDivider } = calculateDividers ( findColumnByName ( 'f' ) ) ;
122
+
123
+ expect ( showLeftDivider ) . toBeTruthy ( ) ;
124
+ expect ( showRightDivider ) . toBeFalsy ( ) ;
125
+ } ) ;
126
+
127
+ it ( 'should show only left divider for first left column in a group' , ( ) => {
128
+ const { showLeftDivider, showRightDivider } = calculateDividers ( findColumnByName ( 'b0' ) ) ;
129
+
130
+ expect ( showLeftDivider ) . toBeTruthy ( ) ;
131
+ expect ( showRightDivider ) . toBeFalsy ( ) ;
132
+ } ) ;
133
+ it ( 'should show only right divider for first right column in a group' , ( ) => {
134
+ const { showLeftDivider, showRightDivider } = calculateDividers ( findColumnByName ( 'e2' ) ) ;
135
+
136
+ expect ( showLeftDivider ) . toBeFalsy ( ) ;
137
+ expect ( showRightDivider ) . toBeTruthy ( ) ;
138
+ } ) ;
139
+
140
+ it ( 'should not be visible for consecutive left columns' , ( ) => {
141
+ const { showLeftDivider, showRightDivider } = calculateDividers ( findColumnByName ( 'b1' ) ) ;
142
+
143
+ expect ( showLeftDivider ) . toBeFalsy ( ) ;
144
+ expect ( showRightDivider ) . toBeFalsy ( ) ;
145
+ } ) ;
146
+ it ( 'should not be visible for consecutive right columns' , ( ) => {
147
+ const { showLeftDivider, showRightDivider } = calculateDividers ( findColumnByName ( 'e1' ) ) ;
148
+
149
+ expect ( showLeftDivider ) . toBeFalsy ( ) ;
150
+ expect ( showRightDivider ) . toBeFalsy ( ) ;
151
+ } ) ;
152
+ } ) ;
153
+ } ) ;
30
154
} ) ;
0 commit comments