|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect } from 'vitest'; |
| 10 | +import { normalizeQuickFilter, normalizeQuickFilters } from '../normalize-quick-filter'; |
| 11 | + |
| 12 | +describe('normalizeQuickFilter', () => { |
| 13 | + it('should pass through ObjectUI format unchanged', () => { |
| 14 | + const input = { id: 'active', label: 'Active', filters: [['status', '=', 'active']] }; |
| 15 | + const result = normalizeQuickFilter(input); |
| 16 | + expect(result).toBe(input); // same reference |
| 17 | + expect(result.id).toBe('active'); |
| 18 | + expect(result.label).toBe('Active'); |
| 19 | + expect(result.filters).toEqual([['status', '=', 'active']]); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should convert spec format { field, operator, value } to ObjectUI format', () => { |
| 23 | + const result = normalizeQuickFilter({ |
| 24 | + field: 'status', |
| 25 | + operator: 'eq', |
| 26 | + value: 'active', |
| 27 | + label: 'Active', |
| 28 | + }); |
| 29 | + expect(result.id).toBe('status-eq-active'); |
| 30 | + expect(result.label).toBe('Active'); |
| 31 | + expect(result.filters).toEqual([['status', '=', 'active']]); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should auto-generate label when omitted', () => { |
| 35 | + const result = normalizeQuickFilter({ |
| 36 | + field: 'status', |
| 37 | + operator: 'eq', |
| 38 | + value: 'active', |
| 39 | + }); |
| 40 | + expect(result.label).toBe('status eq active'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should handle null value', () => { |
| 44 | + const result = normalizeQuickFilter({ |
| 45 | + field: 'archived', |
| 46 | + operator: 'eq', |
| 47 | + value: null, |
| 48 | + label: 'Not Archived', |
| 49 | + }); |
| 50 | + expect(result.id).toBe('archived-eq-'); |
| 51 | + expect(result.filters).toEqual([['archived', '=', null]]); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should map "equals" operator to "="', () => { |
| 55 | + const result = normalizeQuickFilter({ |
| 56 | + field: 'status', |
| 57 | + operator: 'equals', |
| 58 | + value: 'active', |
| 59 | + }); |
| 60 | + expect(result.filters).toEqual([['status', '=', 'active']]); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should map "gt" operator to ">"', () => { |
| 64 | + const result = normalizeQuickFilter({ |
| 65 | + field: 'amount', |
| 66 | + operator: 'gt', |
| 67 | + value: 100, |
| 68 | + }); |
| 69 | + expect(result.filters).toEqual([['amount', '>', 100]]); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should map "lte" operator to "<="', () => { |
| 73 | + const result = normalizeQuickFilter({ |
| 74 | + field: 'age', |
| 75 | + operator: 'lte', |
| 76 | + value: 18, |
| 77 | + }); |
| 78 | + expect(result.filters).toEqual([['age', '<=', 18]]); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should pass through unknown operators', () => { |
| 82 | + const result = normalizeQuickFilter({ |
| 83 | + field: 'status', |
| 84 | + operator: 'custom_op', |
| 85 | + value: 'x', |
| 86 | + }); |
| 87 | + expect(result.filters).toEqual([['status', 'custom_op', 'x']]); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should preserve icon and defaultActive', () => { |
| 91 | + const result = normalizeQuickFilter({ |
| 92 | + field: 'status', |
| 93 | + operator: 'eq', |
| 94 | + value: 'active', |
| 95 | + label: 'Active', |
| 96 | + icon: 'check', |
| 97 | + defaultActive: true, |
| 98 | + }); |
| 99 | + expect(result.icon).toBe('check'); |
| 100 | + expect(result.defaultActive).toBe(true); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +describe('normalizeQuickFilters', () => { |
| 105 | + it('should return undefined for undefined input', () => { |
| 106 | + expect(normalizeQuickFilters(undefined)).toBeUndefined(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should return undefined for empty array', () => { |
| 110 | + expect(normalizeQuickFilters([])).toBeUndefined(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should normalize mixed format arrays', () => { |
| 114 | + const result = normalizeQuickFilters([ |
| 115 | + { id: 'vip', label: 'VIP', filters: [['vip', '=', true]] }, |
| 116 | + { field: 'status', operator: 'eq', value: 'active', label: 'Active' }, |
| 117 | + ]); |
| 118 | + expect(result).toHaveLength(2); |
| 119 | + expect(result![0].id).toBe('vip'); |
| 120 | + expect(result![1].id).toBe('status-eq-active'); |
| 121 | + expect(result![1].filters).toEqual([['status', '=', 'active']]); |
| 122 | + }); |
| 123 | +}); |
0 commit comments