@@ -89,10 +89,122 @@ describe('filterData function', () => {
89
89
} ) ;
90
90
91
91
it ( 'should return unfiltered data if no filters are provided' , ( ) => {
92
- const filters = [ ] ;
92
+ const filters : CrudFilters = [ ] ;
93
93
const filteredData = filterData ( filters , unstructuredData ) ;
94
94
expect ( filteredData ) . toEqual ( unstructuredData ) ;
95
95
} ) ;
96
+
97
+ it ( 'filters with nested structure' , ( ) => {
98
+ const filters : CrudFilters = [
99
+ {
100
+ field : 'status.state' , operator : 'eq' , value : 'Normal' ,
101
+ } ,
102
+ {
103
+ field : 'status.state' , operator : 'in' , value : [
104
+ 'InUpdate' ,
105
+ 'InRollback' ,
106
+ ] ,
107
+ } ,
108
+ {
109
+ operator : 'and' , value : [
110
+ {
111
+ field : 'metadata.deletionTimestamp' , operator : 'nnull' , value : true ,
112
+ } ,
113
+ {
114
+ field : 'status.state' , operator : 'ne' , value : 'DeleteFailed' ,
115
+ } ,
116
+ ] ,
117
+ } ,
118
+ ] ;
119
+ const data : ( Unstructured & { status : { state : string ; } } ) [ ] = [
120
+ {
121
+ apiVersion : 'v1' ,
122
+ kind : 'Pod' ,
123
+ metadata : { name : 'Pod A' , namespace : 'Namespace A' } ,
124
+ status : {
125
+ state : 'Normal' ,
126
+ } ,
127
+ } ,
128
+ {
129
+ apiVersion : 'v1' ,
130
+ kind : 'Pod' ,
131
+ metadata : { name : 'Pod B' , namespace : 'Namespace B' } ,
132
+ status : {
133
+ state : 'InRollback' ,
134
+ } ,
135
+ } ,
136
+ {
137
+ apiVersion : 'v1' ,
138
+ kind : 'Pod' ,
139
+ metadata : { name : 'Pod C' , namespace : 'Namespace A' , deletionTimestamp : 'mock time' } ,
140
+ status : {
141
+ state : 'InUpgrade' ,
142
+ } ,
143
+ } ,
144
+ ] ;
145
+
146
+ expect ( filterData ( [
147
+ {
148
+ operator : 'and' ,
149
+ value : [
150
+ filters [ 0 ] ,
151
+ ] ,
152
+ } ,
153
+ ] , data ) ) . toEqual ( [ {
154
+ apiVersion : 'v1' ,
155
+ kind : 'Pod' ,
156
+ metadata : { name : 'Pod A' , namespace : 'Namespace A' } ,
157
+ status : {
158
+ state : 'Normal' ,
159
+ } ,
160
+ } ] ) ;
161
+
162
+ expect ( filterData ( [
163
+ {
164
+ operator : 'and' ,
165
+ value : [
166
+ filters [ 1 ] ,
167
+ ] ,
168
+ } ,
169
+ ] , data ) ) . toEqual ( [ {
170
+ apiVersion : 'v1' ,
171
+ kind : 'Pod' ,
172
+ metadata : { name : 'Pod B' , namespace : 'Namespace B' } ,
173
+ status : {
174
+ state : 'InRollback' ,
175
+ } ,
176
+ } ] ) ;
177
+
178
+ expect ( filterData ( [
179
+ {
180
+ operator : 'and' ,
181
+ value : [
182
+ filters [ 2 ] ,
183
+ ] ,
184
+ } ,
185
+ ] , data ) ) . toEqual ( [ {
186
+ apiVersion : 'v1' ,
187
+ kind : 'Pod' ,
188
+ metadata : { name : 'Pod C' , namespace : 'Namespace A' , deletionTimestamp : 'mock time' } ,
189
+ status : {
190
+ state : 'InUpgrade' ,
191
+ } ,
192
+ } , ] ) ;
193
+
194
+ expect ( filterData ( [
195
+ {
196
+ operator : 'and' ,
197
+ value : filters ,
198
+ } ,
199
+ ] , data ) ) . toEqual ( [ ] ) ;
200
+
201
+ expect ( filterData ( [
202
+ {
203
+ operator : 'or' ,
204
+ value : filters ,
205
+ } ,
206
+ ] , data ) ) . toEqual ( data ) ;
207
+ } ) ;
96
208
} ) ;
97
209
98
210
describe ( 'evaluateFilter function' , ( ) => {
@@ -107,8 +219,8 @@ describe('evaluateFilter function', () => {
107
219
total : 10 ,
108
220
labels : [ 'label-1' , 'label-2' ] ,
109
221
description : null ,
110
- type : 'type-1'
111
- }
222
+ type : 'type-1' ,
223
+ } ,
112
224
} as Unstructured ;
113
225
114
226
test ( 'handles "eq" operator' , ( ) => {
@@ -190,13 +302,13 @@ describe('evaluateFilter function', () => {
190
302
} ) ;
191
303
192
304
test ( 'handles "null" operator' , ( ) => {
193
- const result = evaluateFilter ( mockItem , 'spec.description' , 'null' , null ) ;
194
- expect ( result ) . toBeTruthy ( ) ;
305
+ expect ( evaluateFilter ( mockItem , 'spec.description' , 'null' , null ) ) . toBeTruthy ( ) ;
306
+ expect ( evaluateFilter ( mockItem , 'spec.non_exist_path' , 'null' , null ) ) . toBeFalsy ( ) ;
195
307
} ) ;
196
308
197
309
test ( 'handles "nnull" operator' , ( ) => {
198
- const result = evaluateFilter ( mockItem , 'spec.total' , 'nnull' , null ) ;
199
- expect ( result ) . toBeTruthy ( ) ;
310
+ expect ( evaluateFilter ( mockItem , 'spec.total' , 'nnull' , null ) ) . toBeTruthy ( ) ;
311
+ expect ( evaluateFilter ( mockItem , 'spec.non_exist_path' , 'null' , null ) ) . toBeFalsy ( ) ;
200
312
} ) ;
201
313
202
314
test ( 'handles "startswith" operator' , ( ) => {
@@ -238,4 +350,4 @@ describe('evaluateFilter function', () => {
238
350
const result = evaluateFilter ( mockItem , 'metadata.name' , 'nendswiths' , 'SERVICE' ) ;
239
351
expect ( result ) . toBeTruthy ( ) ;
240
352
} ) ;
241
- } )
353
+ } ) ;
0 commit comments