1
- import { compareSmart } from "../src/use-column-sort.js" ;
1
+ import { compareSmart , useColumnSort } from "../src/use-column-sort.js" ;
2
+ import { renderHook } from "@testing-library/react-hooks" ;
3
+ import { GridCellKind , type GridCell } from "@glideapps/glide-data-grid" ;
2
4
import { expect , describe , test } from "vitest" ;
3
5
4
6
describe ( "use-column-sort" , ( ) => {
@@ -24,4 +26,155 @@ describe("use-column-sort", () => {
24
26
] ) ;
25
27
} ) ;
26
28
} ) ;
29
+
30
+ test ( "multi column sort" , ( ) => {
31
+ const columns = [
32
+ { title : "A" , id : "A" } ,
33
+ { title : "B" , id : "B" } ,
34
+ ] ;
35
+ const data = [
36
+ [ "2" , "a" ] ,
37
+ [ "1" , "b" ] ,
38
+ [ "2" , "b" ] ,
39
+ [ "1" , "a" ] ,
40
+ ] ;
41
+
42
+ const getCellContent = ( [ col , row ] : readonly [ number , number ] ) : GridCell => ( {
43
+ kind : GridCellKind . Text ,
44
+ allowOverlay : false ,
45
+ data : data [ row ] [ col ] ,
46
+ displayData : data [ row ] [ col ] ,
47
+ } ) ;
48
+
49
+ const { result } = renderHook ( ( ) =>
50
+ useColumnSort ( {
51
+ columns,
52
+ rows : data . length ,
53
+ getCellContent,
54
+ sort : [
55
+ { column : columns [ 0 ] , direction : "asc" } ,
56
+ { column : columns [ 1 ] , direction : "asc" } ,
57
+ ] ,
58
+ } )
59
+ ) ;
60
+
61
+ const order = Array . from ( { length : data . length } , ( _ , i ) => result . current . getOriginalIndex ( i ) ) ;
62
+ expect ( order ) . toEqual ( [ 3 , 1 , 0 , 2 ] ) ;
63
+ } ) ;
64
+
65
+ test ( "multi column sort with desc" , ( ) => {
66
+ const columns = [
67
+ { title : "A" , id : "A" } ,
68
+ { title : "B" , id : "B" } ,
69
+ ] ;
70
+ const data = [
71
+ [ "2" , "a" ] ,
72
+ [ "1" , "b" ] ,
73
+ [ "2" , "b" ] ,
74
+ [ "1" , "a" ] ,
75
+ ] ;
76
+
77
+ const getCellContent = ( [ col , row ] : readonly [ number , number ] ) : GridCell => ( {
78
+ kind : GridCellKind . Text ,
79
+ allowOverlay : false ,
80
+ data : data [ row ] [ col ] ,
81
+ displayData : data [ row ] [ col ] ,
82
+ } ) ;
83
+
84
+ const { result } = renderHook ( ( ) =>
85
+ useColumnSort ( {
86
+ columns,
87
+ rows : data . length ,
88
+ getCellContent,
89
+ sort : [
90
+ { column : columns [ 0 ] , direction : "desc" } ,
91
+ { column : columns [ 1 ] , direction : "desc" } ,
92
+ ] ,
93
+ } )
94
+ ) ;
95
+
96
+ const order = Array . from ( { length : data . length } , ( _ , i ) => result . current . getOriginalIndex ( i ) ) ;
97
+ expect ( order ) . toEqual ( [ 2 , 0 , 1 , 3 ] ) ;
98
+ } ) ;
99
+
100
+ test ( "multi column sort with mixed directions" , ( ) => {
101
+ const columns = [
102
+ { title : "A" , id : "A" } ,
103
+ { title : "B" , id : "B" } ,
104
+ ] ;
105
+ const data = [
106
+ [ "2" , "a" ] ,
107
+ [ "1" , "b" ] ,
108
+ [ "2" , "b" ] ,
109
+ [ "1" , "a" ] ,
110
+ ] ;
111
+
112
+ const getCellContent = ( [ col , row ] : readonly [ number , number ] ) : GridCell => ( {
113
+ kind : GridCellKind . Text ,
114
+ allowOverlay : false ,
115
+ data : data [ row ] [ col ] ,
116
+ displayData : data [ row ] [ col ] ,
117
+ } ) ;
118
+
119
+ const { result } = renderHook ( ( ) =>
120
+ useColumnSort ( {
121
+ columns,
122
+ rows : data . length ,
123
+ getCellContent,
124
+ sort : [
125
+ { column : columns [ 0 ] , direction : "asc" } ,
126
+ { column : columns [ 1 ] , direction : "desc" } ,
127
+ ] ,
128
+ } )
129
+ ) ;
130
+
131
+ const order = Array . from ( { length : data . length } , ( _ , i ) => result . current . getOriginalIndex ( i ) ) ;
132
+ expect ( order ) . toEqual ( [ 1 , 3 , 2 , 0 ] ) ;
133
+ } ) ;
134
+
135
+ test ( "multi column sort with smart mode" , ( ) => {
136
+ const columns = [
137
+ { title : "A" , id : "A" } ,
138
+ { title : "B" , id : "B" } ,
139
+ ] ;
140
+ const data = [
141
+ [ 2 , "a" ] ,
142
+ [ 1 , "b" ] ,
143
+ [ 2 , "b" ] ,
144
+ [ 1 , "a" ] ,
145
+ ] ;
146
+
147
+ const getCellContent = ( [ col , row ] : readonly [ number , number ] ) : GridCell => {
148
+ const d = data [ row ] [ col ] ;
149
+ if ( typeof d === "number" ) {
150
+ return {
151
+ kind : GridCellKind . Number ,
152
+ allowOverlay : false ,
153
+ data : d ,
154
+ displayData : String ( d ) ,
155
+ } ;
156
+ }
157
+ return {
158
+ kind : GridCellKind . Text ,
159
+ allowOverlay : false ,
160
+ data : String ( d ) ,
161
+ displayData : String ( d ) ,
162
+ } ;
163
+ } ;
164
+
165
+ const { result } = renderHook ( ( ) =>
166
+ useColumnSort ( {
167
+ columns,
168
+ rows : data . length ,
169
+ getCellContent,
170
+ sort : [
171
+ { column : columns [ 0 ] , direction : "asc" , mode : "smart" } ,
172
+ { column : columns [ 1 ] , direction : "asc" , mode : "smart" } ,
173
+ ] ,
174
+ } )
175
+ ) ;
176
+
177
+ const order = Array . from ( { length : data . length } , ( _ , i ) => result . current . getOriginalIndex ( i ) ) ;
178
+ expect ( order ) . toEqual ( [ 3 , 1 , 0 , 2 ] ) ;
179
+ } ) ;
27
180
} ) ;
0 commit comments