|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + * |
| 4 | + * Converter unit tests for the table query builder. Cover the operator |
| 5 | + * round-trips — UI rule → Filter object → UI rule — with attention to the |
| 6 | + * valueless `$empty` operator that maps to two distinct UI operators. |
| 7 | + */ |
| 8 | +import { describe, expect, it } from 'vitest' |
| 9 | +import { filterRulesToFilter, filterToRules } from '@/lib/table/query-builder/converters' |
| 10 | +import type { FilterRule } from '@/lib/table/types' |
| 11 | + |
| 12 | +function rule(overrides: Partial<FilterRule>): FilterRule { |
| 13 | + return { |
| 14 | + id: 'rule-1', |
| 15 | + logicalOperator: 'and', |
| 16 | + column: 'name', |
| 17 | + operator: 'eq', |
| 18 | + value: '', |
| 19 | + ...overrides, |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +describe('filterRulesToFilter', () => { |
| 24 | + it('emits a bare value for eq (containment shorthand)', () => { |
| 25 | + expect(filterRulesToFilter([rule({ operator: 'eq', value: 'John' })])).toEqual({ name: 'John' }) |
| 26 | + }) |
| 27 | + |
| 28 | + it('wraps non-eq operators in a $-prefixed operator object', () => { |
| 29 | + expect( |
| 30 | + filterRulesToFilter([rule({ column: 'email', operator: 'startsWith', value: 'a' })]) |
| 31 | + ).toEqual({ email: { $startsWith: 'a' } }) |
| 32 | + expect( |
| 33 | + filterRulesToFilter([rule({ column: 'email', operator: 'ncontains', value: 'x' })]) |
| 34 | + ).toEqual({ email: { $ncontains: 'x' } }) |
| 35 | + }) |
| 36 | + |
| 37 | + it('parses comma-separated values into arrays for in / nin', () => { |
| 38 | + expect( |
| 39 | + filterRulesToFilter([rule({ column: 'status', operator: 'nin', value: 'a, b' })]) |
| 40 | + ).toEqual({ status: { $nin: ['a', 'b'] } }) |
| 41 | + }) |
| 42 | + |
| 43 | + it('serializes isEmpty / isNotEmpty to $empty without a value', () => { |
| 44 | + expect(filterRulesToFilter([rule({ column: 'phone', operator: 'isEmpty' })])).toEqual({ |
| 45 | + phone: { $empty: true }, |
| 46 | + }) |
| 47 | + expect(filterRulesToFilter([rule({ column: 'phone', operator: 'isNotEmpty' })])).toEqual({ |
| 48 | + phone: { $empty: false }, |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + it('merges two AND rules on the same column into one operator object', () => { |
| 53 | + const filter = filterRulesToFilter([ |
| 54 | + rule({ id: 'a', column: 'age', operator: 'gt', value: '18' }), |
| 55 | + rule({ id: 'b', column: 'age', operator: 'lt', value: '65' }), |
| 56 | + ]) |
| 57 | + expect(filter).toEqual({ age: { $gt: 18, $lt: 65 } }) |
| 58 | + }) |
| 59 | + |
| 60 | + it('normalizes a bare-equality shorthand when merging with an operator', () => { |
| 61 | + const filter = filterRulesToFilter([ |
| 62 | + rule({ id: 'a', column: 'name', operator: 'eq', value: 'John' }), |
| 63 | + rule({ id: 'b', column: 'name', operator: 'contains', value: 'oh' }), |
| 64 | + ]) |
| 65 | + expect(filter).toEqual({ name: { $eq: 'John', $contains: 'oh' } }) |
| 66 | + }) |
| 67 | + |
| 68 | + it('keeps same-column rules across an OR boundary in separate groups', () => { |
| 69 | + const filter = filterRulesToFilter([ |
| 70 | + rule({ id: 'a', column: 'age', operator: 'gt', value: '18' }), |
| 71 | + rule({ id: 'b', logicalOperator: 'or', column: 'age', operator: 'lt', value: '5' }), |
| 72 | + ]) |
| 73 | + expect(filter).toEqual({ $or: [{ age: { $gt: 18 } }, { age: { $lt: 5 } }] }) |
| 74 | + }) |
| 75 | +}) |
| 76 | + |
| 77 | +describe('filterToRules', () => { |
| 78 | + it('maps $empty: true back to isEmpty and $empty: false back to isNotEmpty', () => { |
| 79 | + const empty = filterToRules({ phone: { $empty: true } }) |
| 80 | + expect(empty).toHaveLength(1) |
| 81 | + expect(empty[0]).toMatchObject({ column: 'phone', operator: 'isEmpty', value: '' }) |
| 82 | + |
| 83 | + const notEmpty = filterToRules({ phone: { $empty: false } }) |
| 84 | + expect(notEmpty[0]).toMatchObject({ column: 'phone', operator: 'isNotEmpty', value: '' }) |
| 85 | + }) |
| 86 | + |
| 87 | + it("treats the string '$empty' operand the same as the boolean (no predicate flip)", () => { |
| 88 | + const empty = filterToRules({ phone: { $empty: 'true' } } as unknown as Parameters< |
| 89 | + typeof filterToRules |
| 90 | + >[0]) |
| 91 | + expect(empty[0]).toMatchObject({ column: 'phone', operator: 'isEmpty', value: '' }) |
| 92 | + |
| 93 | + const notEmpty = filterToRules({ phone: { $empty: 'false' } } as unknown as Parameters< |
| 94 | + typeof filterToRules |
| 95 | + >[0]) |
| 96 | + expect(notEmpty[0]).toMatchObject({ column: 'phone', operator: 'isNotEmpty', value: '' }) |
| 97 | + }) |
| 98 | + |
| 99 | + it('round-trips string-pattern operators', () => { |
| 100 | + for (const operator of ['contains', 'ncontains', 'startsWith', 'endsWith'] as const) { |
| 101 | + const filter = filterRulesToFilter([rule({ column: 'name', operator, value: 'abc' })]) |
| 102 | + const back = filterToRules(filter) |
| 103 | + expect(back[0]).toMatchObject({ column: 'name', operator, value: 'abc' }) |
| 104 | + } |
| 105 | + }) |
| 106 | + |
| 107 | + it('round-trips isEmpty through filterRulesToFilter', () => { |
| 108 | + const filter = filterRulesToFilter([rule({ column: 'name', operator: 'isEmpty' })]) |
| 109 | + const back = filterToRules(filter) |
| 110 | + expect(back[0]).toMatchObject({ column: 'name', operator: 'isEmpty', value: '' }) |
| 111 | + }) |
| 112 | + |
| 113 | + it('round-trips a multi-operator column (Filter → rules → Filter) without loss', () => { |
| 114 | + const original = { age: { $gte: 18, $lte: 65 } } |
| 115 | + const rules = filterToRules(original) |
| 116 | + expect(rules).toHaveLength(2) |
| 117 | + expect(filterRulesToFilter(rules)).toEqual(original) |
| 118 | + }) |
| 119 | +}) |
0 commit comments