Skip to content

Commit f46960d

Browse files
committed
add unit tests for number filter fns
1 parent 927523a commit f46960d

File tree

3 files changed

+306
-20
lines changed

3 files changed

+306
-20
lines changed

packages/table-core/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
"react",
1919
"vue",
2020
"solid",
21+
"svelte",
22+
"qwik",
23+
"lit",
24+
"angular",
2125
"table",
2226
"table-core",
2327
"datagrid"

packages/table-core/src/fns/filterFns.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,7 @@ export const filterFn_lessThan: FilterFn<any, any> = <
173173
columnId: string,
174174
filterValue: Date | number | string,
175175
) => {
176-
const rowValue = row.getValue(columnId)
177-
const numericRowValue =
178-
rowValue === null || rowValue === undefined ? 0 : +rowValue
179-
const numericFilterValue = +filterValue
180-
181-
if (!isNaN(numericFilterValue) && !isNaN(numericRowValue)) {
182-
return numericRowValue < numericFilterValue
183-
}
184-
185-
const stringValue = (rowValue ?? '').toString().toLowerCase().trim()
186-
const stringFilterValue = filterValue.toString().toLowerCase().trim()
187-
return stringValue < stringFilterValue
176+
return !filterFn_greaterThanOrEqualTo(row as any, columnId, filterValue)
188177
}
189178

190179
filterFn_lessThan.resolveFilterValue = (val: any) => testFalsy(val)
@@ -200,10 +189,7 @@ export const filterFn_lessThanOrEqualTo: FilterFn<any, any> = <
200189
columnId: string,
201190
filterValue: Date | number | string,
202191
) => {
203-
return (
204-
filterFn_lessThan(row as any, columnId, filterValue) ||
205-
filterFn_equals(row as any, columnId, filterValue)
206-
)
192+
return !filterFn_greaterThan(row as any, columnId, filterValue)
207193
}
208194

209195
filterFn_lessThanOrEqualTo.resolveFilterValue = (val: any) => testFalsy(val)

packages/table-core/tests/unit/fns/filterFns.test.ts

Lines changed: 300 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import {
55
filterFn_equals,
66
filterFn_equalsString,
77
filterFn_equalsStringSensitive,
8+
filterFn_greaterThan,
9+
filterFn_greaterThanOrEqualTo,
810
filterFn_includesString,
911
filterFn_includesStringSensitive,
12+
filterFn_lessThan,
13+
filterFn_lessThanOrEqualTo,
1014
filterFn_weakEquals,
1115
} from '../../../src'
1216
import { getStaticTestData } from '../../fixtures/data/generateTestData'
@@ -99,7 +103,7 @@ describe('Filter Functions', () => {
99103
it('should not match different case substrings', () => {
100104
const row = mockRows[0]!
101105
const columnId = 'firstName'
102-
const filterValue = 'john'
106+
const filterValue = 'john' // lowercase
103107
const result = filterFn_includesStringSensitive(
104108
row as any,
105109
columnId,
@@ -124,7 +128,7 @@ describe('Filter Functions', () => {
124128
it('should match case-insensitive substrings', () => {
125129
const row = mockRows[0]!
126130
const columnId = 'firstName'
127-
const filterValue = 'john'
131+
const filterValue = 'John'
128132
const result = filterFn_includesString(
129133
row as any,
130134
columnId,
@@ -135,7 +139,7 @@ describe('Filter Functions', () => {
135139
it('should match different case substrings', () => {
136140
const row = mockRows[0]!
137141
const columnId = 'firstName'
138-
const filterValue = 'john'
142+
const filterValue = 'john' // lowercase
139143
const result = filterFn_includesString(
140144
row as any,
141145
columnId,
@@ -167,7 +171,7 @@ describe('Filter Functions', () => {
167171
it('should match case-insensitive exact strings', () => {
168172
const row = mockRows[0]!
169173
const columnId = 'firstName'
170-
const filterValue = 'john'
174+
const filterValue = 'john' // lowercase
171175
const result = filterFn_equalsString(row as any, columnId, filterValue)
172176
expect(result).toBe(true)
173177
})
@@ -216,4 +220,296 @@ describe('Filter Functions', () => {
216220
})
217221
})
218222
})
223+
224+
describe('Number Filters', () => {
225+
describe('filterFn_greaterThan', () => {
226+
it('should match greater than values', () => {
227+
const row = mockRows[0]!
228+
const columnId = 'age' // number value 30
229+
const filterValue = 29
230+
const result = filterFn_greaterThan(row as any, columnId, filterValue)
231+
expect(result).toBe(true)
232+
})
233+
it('should not match equal values', () => {
234+
const row = mockRows[0]!
235+
const columnId = 'age' // number value 30
236+
const filterValue = 30
237+
const result = filterFn_greaterThan(row as any, columnId, filterValue)
238+
expect(result).toBe(false)
239+
})
240+
it('should not match less than values', () => {
241+
const row = mockRows[0]!
242+
const columnId = 'age' // number value 30
243+
const filterValue = 31
244+
const result = filterFn_greaterThan(row as any, columnId, filterValue)
245+
expect(result).toBe(false)
246+
})
247+
it('should match strings greater than numbers', () => {
248+
const row = mockRows[0]!
249+
const columnId = 'age' // number value 30
250+
const filterValue = '29'
251+
const result = filterFn_greaterThan(row as any, columnId, filterValue)
252+
expect(result).toBe(true)
253+
})
254+
it('should not match strings less than numbers', () => {
255+
const row = mockRows[0]!
256+
const columnId = 'age' // number value 30
257+
const filterValue = '31'
258+
const result = filterFn_greaterThan(row as any, columnId, filterValue)
259+
expect(result).toBe(false)
260+
})
261+
it('should match strings greater than other strings', () => {
262+
const row = mockRows[0]!
263+
const columnId = 'firstName' // 'John'
264+
const filterValue = 'a'
265+
const result = filterFn_greaterThan(row as any, columnId, filterValue)
266+
expect(result).toBe(true)
267+
})
268+
it('should not match strings less than other strings', () => {
269+
const row = mockRows[0]!
270+
const columnId = 'firstName' // 'John'
271+
const filterValue = 'z'
272+
const result = filterFn_greaterThan(row as any, columnId, filterValue)
273+
expect(result).toBe(false)
274+
})
275+
it('should not match strings equal to other strings', () => {
276+
const row = mockRows[0]!
277+
const columnId = 'firstName' // 'John'
278+
const filterValue = 'John'
279+
const result = filterFn_greaterThan(row as any, columnId, filterValue)
280+
expect(result).toBe(false)
281+
})
282+
})
283+
describe('filterFn_greaterThanOrEqualTo', () => {
284+
it('should match greater than values', () => {
285+
const row = mockRows[0]!
286+
const columnId = 'age' // number value 30
287+
const filterValue = 29
288+
const result = filterFn_greaterThanOrEqualTo(
289+
row as any,
290+
columnId,
291+
filterValue,
292+
)
293+
expect(result).toBe(true)
294+
})
295+
it('should match equal values', () => {
296+
const row = mockRows[0]!
297+
const columnId = 'age' // number value 30
298+
const filterValue = 30
299+
const result = filterFn_greaterThanOrEqualTo(
300+
row as any,
301+
columnId,
302+
filterValue,
303+
)
304+
expect(result).toBe(true)
305+
})
306+
it('should not match less than values', () => {
307+
const row = mockRows[0]!
308+
const columnId = 'age' // number value 30
309+
const filterValue = 31
310+
const result = filterFn_greaterThanOrEqualTo(
311+
row as any,
312+
columnId,
313+
filterValue,
314+
)
315+
expect(result).toBe(false)
316+
})
317+
it('should match strings greater than to numbers', () => {
318+
const row = mockRows[0]!
319+
const columnId = 'age' // number value 30
320+
const filterValue = '29'
321+
const result = filterFn_greaterThanOrEqualTo(
322+
row as any,
323+
columnId,
324+
filterValue,
325+
)
326+
expect(result).toBe(true)
327+
})
328+
it('should not match strings less than numbers', () => {
329+
const row = mockRows[0]!
330+
const columnId = 'age' // number value 30
331+
const filterValue = '31'
332+
const result = filterFn_greaterThanOrEqualTo(
333+
row as any,
334+
columnId,
335+
filterValue,
336+
)
337+
expect(result).toBe(false)
338+
})
339+
it('should match strings greater than other strings', () => {
340+
const row = mockRows[0]!
341+
const columnId = 'firstName' // 'John'
342+
const filterValue = 'a'
343+
const result = filterFn_greaterThanOrEqualTo(
344+
row as any,
345+
columnId,
346+
filterValue,
347+
)
348+
expect(result).toBe(true)
349+
})
350+
it('should not match strings less than other strings', () => {
351+
const row = mockRows[0]!
352+
const columnId = 'firstName' // 'John'
353+
const filterValue = 'z'
354+
const result = filterFn_greaterThanOrEqualTo(
355+
row as any,
356+
columnId,
357+
filterValue,
358+
)
359+
expect(result).toBe(false)
360+
})
361+
it('should match strings equal to other strings', () => {
362+
const row = mockRows[0]!
363+
const columnId = 'firstName' // 'John'
364+
const filterValue = 'John'
365+
const result = filterFn_greaterThanOrEqualTo(
366+
row as any,
367+
columnId,
368+
filterValue,
369+
)
370+
expect(result).toBe(true)
371+
})
372+
})
373+
describe('filterFn_lessThan', () => {
374+
it('should match less than values', () => {
375+
const row = mockRows[0]!
376+
const columnId = 'age' // number value 30
377+
const filterValue = 31
378+
const result = filterFn_lessThan(row as any, columnId, filterValue)
379+
expect(result).toBe(true)
380+
})
381+
it('should not match equal values', () => {
382+
const row = mockRows[0]!
383+
const columnId = 'age' // number value 30
384+
const filterValue = 30
385+
const result = filterFn_lessThan(row as any, columnId, filterValue)
386+
expect(result).toBe(false)
387+
})
388+
it('should not match greater than values', () => {
389+
const row = mockRows[0]!
390+
const columnId = 'age' // number value 30
391+
const filterValue = 29
392+
const result = filterFn_lessThan(row as any, columnId, filterValue)
393+
expect(result).toBe(false)
394+
})
395+
it('should match strings less than numbers', () => {
396+
const row = mockRows[0]!
397+
const columnId = 'age' // number value 30
398+
const filterValue = '31'
399+
const result = filterFn_lessThan(row as any, columnId, filterValue)
400+
expect(result).toBe(true)
401+
})
402+
it('should match strings less than other strings', () => {
403+
const row = mockRows[0]!
404+
const columnId = 'firstName' // 'John'
405+
const filterValue = 'z'
406+
const result = filterFn_lessThan(row as any, columnId, filterValue)
407+
expect(result).toBe(true)
408+
})
409+
it('should not match strings equal to other strings', () => {
410+
const row = mockRows[0]!
411+
const columnId = 'firstName' // 'John'
412+
const filterValue = 'John'
413+
const result = filterFn_lessThan(row as any, columnId, filterValue)
414+
expect(result).toBe(false)
415+
})
416+
it('should not match strings greater than other strings', () => {
417+
const row = mockRows[0]!
418+
const columnId = 'firstName' // 'John'
419+
const filterValue = 'a'
420+
const result = filterFn_lessThan(row as any, columnId, filterValue)
421+
expect(result).toBe(false)
422+
})
423+
})
424+
describe('filterFn_lessThanOrEqualTo', () => {
425+
it('should match less than values', () => {
426+
const row = mockRows[0]!
427+
const columnId = 'age' // number value 30
428+
const filterValue = 31
429+
const result = filterFn_lessThanOrEqualTo(
430+
row as any,
431+
columnId,
432+
filterValue,
433+
)
434+
expect(result).toBe(true)
435+
})
436+
it('should match equal values', () => {
437+
const row = mockRows[0]!
438+
const columnId = 'age' // number value 30
439+
const filterValue = 30
440+
const result = filterFn_lessThanOrEqualTo(
441+
row as any,
442+
columnId,
443+
filterValue,
444+
)
445+
expect(result).toBe(true)
446+
})
447+
it('should not match greater than values', () => {
448+
const row = mockRows[0]!
449+
const columnId = 'age' // number value 30
450+
const filterValue = 29
451+
const result = filterFn_lessThanOrEqualTo(
452+
row as any,
453+
columnId,
454+
filterValue,
455+
)
456+
expect(result).toBe(false)
457+
})
458+
it('should match strings less than to numbers', () => {
459+
const row = mockRows[0]!
460+
const columnId = 'age' // number value 30
461+
const filterValue = '31'
462+
const result = filterFn_lessThanOrEqualTo(
463+
row as any,
464+
columnId,
465+
filterValue,
466+
)
467+
expect(result).toBe(true)
468+
})
469+
it('should not match strings greater than numbers', () => {
470+
const row = mockRows[0]!
471+
const columnId = 'age' // number value 30
472+
const filterValue = '29'
473+
const result = filterFn_lessThanOrEqualTo(
474+
row as any,
475+
columnId,
476+
filterValue,
477+
)
478+
expect(result).toBe(false)
479+
})
480+
it('should match strings less than to other strings', () => {
481+
const row = mockRows[0]!
482+
const columnId = 'firstName' // 'John'
483+
const filterValue = 'z'
484+
const result = filterFn_lessThanOrEqualTo(
485+
row as any,
486+
columnId,
487+
filterValue,
488+
)
489+
expect(result).toBe(true)
490+
})
491+
it('should not match strings greater than other strings', () => {
492+
const row = mockRows[0]!
493+
const columnId = 'firstName' // 'John'
494+
const filterValue = 'a'
495+
const result = filterFn_lessThanOrEqualTo(
496+
row as any,
497+
columnId,
498+
filterValue,
499+
)
500+
expect(result).toBe(false)
501+
})
502+
it('should match strings equal to other strings', () => {
503+
const row = mockRows[0]!
504+
const columnId = 'firstName' // 'John'
505+
const filterValue = 'John'
506+
const result = filterFn_lessThanOrEqualTo(
507+
row as any,
508+
columnId,
509+
filterValue,
510+
)
511+
expect(result).toBe(true)
512+
})
513+
})
514+
})
219515
})

0 commit comments

Comments
 (0)