|
| 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 { |
| 11 | + resolveDashboardFilterDefs, |
| 12 | + dashboardFilterVariableDefs, |
| 13 | + buildFilterCondition, |
| 14 | + buildWidgetScopedFilter, |
| 15 | + DATE_RANGE_FILTER_NAME, |
| 16 | + type DashboardFilterDef, |
| 17 | +} from '../dashboard-filters'; |
| 18 | +import { mergeFilters } from '../merge-filters'; |
| 19 | + |
| 20 | +const regionDef: DashboardFilterDef = { |
| 21 | + name: 'region', |
| 22 | + field: 'region', |
| 23 | + type: 'select', |
| 24 | + options: ['EMEA', 'APAC', 'AMER'], |
| 25 | +}; |
| 26 | + |
| 27 | +const dateDef: DashboardFilterDef = { |
| 28 | + name: DATE_RANGE_FILTER_NAME, |
| 29 | + field: 'created_at', |
| 30 | + type: 'dateRange', |
| 31 | +}; |
| 32 | + |
| 33 | +describe('resolveDashboardFilterDefs', () => { |
| 34 | + it('maps dateRange to the reserved name with a created_at field default', () => { |
| 35 | + const defs = resolveDashboardFilterDefs({ |
| 36 | + dateRange: { defaultRange: 'last_30_days' }, |
| 37 | + }); |
| 38 | + expect(defs).toHaveLength(1); |
| 39 | + expect(defs[0]).toMatchObject({ |
| 40 | + name: DATE_RANGE_FILTER_NAME, |
| 41 | + field: 'created_at', |
| 42 | + type: 'dateRange', |
| 43 | + defaultValue: { preset: 'last_30_days' }, |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('honors an explicit dateRange.field and skips a custom default preset', () => { |
| 48 | + const defs = resolveDashboardFilterDefs({ |
| 49 | + dateRange: { field: 'closed_at', defaultRange: 'custom' }, |
| 50 | + }); |
| 51 | + expect(defs[0].field).toBe('closed_at'); |
| 52 | + expect(defs[0].defaultValue).toBeUndefined(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('defaults a global filter name to its field and preserves declared names', () => { |
| 56 | + const defs = resolveDashboardFilterDefs({ |
| 57 | + globalFilters: [ |
| 58 | + { field: 'region' }, |
| 59 | + { name: 'owner', field: 'owner_id', type: 'lookup' }, |
| 60 | + ], |
| 61 | + }); |
| 62 | + expect(defs.map((d) => d.name)).toEqual(['region', 'owner']); |
| 63 | + expect(defs[0].type).toBe('text'); |
| 64 | + expect(defs[1].field).toBe('owner_id'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('skips entries without a field and lets duplicate names win last', () => { |
| 68 | + const defs = resolveDashboardFilterDefs({ |
| 69 | + globalFilters: [ |
| 70 | + { field: '' } as any, |
| 71 | + { name: 'region', field: 'region' }, |
| 72 | + { name: 'region', field: 'sales_region' }, |
| 73 | + ], |
| 74 | + }); |
| 75 | + expect(defs).toHaveLength(1); |
| 76 | + expect(defs[0].field).toBe('sales_region'); |
| 77 | + }); |
| 78 | +}); |
| 79 | + |
| 80 | +describe('dashboardFilterVariableDefs', () => { |
| 81 | + it('produces page-variable definitions keyed by filter name', () => { |
| 82 | + const vars = dashboardFilterVariableDefs([dateDef, regionDef]); |
| 83 | + expect(vars).toEqual([ |
| 84 | + { name: DATE_RANGE_FILTER_NAME, type: 'object', defaultValue: undefined }, |
| 85 | + { name: 'region', type: 'string', defaultValue: undefined }, |
| 86 | + ]); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +describe('buildFilterCondition', () => { |
| 91 | + it('maps a date preset to symbolic macro-token bounds', () => { |
| 92 | + expect(buildFilterCondition(dateDef, { preset: 'last_30_days' })).toEqual({ |
| 93 | + $gte: '{30_days_ago}', |
| 94 | + $lte: '{today}', |
| 95 | + }); |
| 96 | + expect(buildFilterCondition(dateDef, { preset: 'this_month' })).toEqual({ |
| 97 | + $gte: '{current_month_start}', |
| 98 | + $lte: '{current_month_end}', |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + it('passes custom ISO bounds through and omits a missing bound', () => { |
| 103 | + expect(buildFilterCondition(dateDef, { from: '2026-01-01', to: '2026-06-30' })).toEqual({ |
| 104 | + $gte: '2026-01-01', |
| 105 | + $lte: '2026-06-30', |
| 106 | + }); |
| 107 | + expect(buildFilterCondition(dateDef, { from: '2026-01-01' })).toEqual({ |
| 108 | + $gte: '2026-01-01', |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it('maps select values to equality and arrays to $in', () => { |
| 113 | + expect(buildFilterCondition(regionDef, 'EMEA')).toBe('EMEA'); |
| 114 | + expect(buildFilterCondition(regionDef, ['EMEA', 'APAC'])).toEqual({ $in: ['EMEA', 'APAC'] }); |
| 115 | + }); |
| 116 | + |
| 117 | + it('maps text to $contains and numbers to equality', () => { |
| 118 | + expect(buildFilterCondition({ name: 'q', field: 'name', type: 'text' }, 'acme')).toEqual({ |
| 119 | + $contains: 'acme', |
| 120 | + }); |
| 121 | + expect(buildFilterCondition({ name: 'n', field: 'amount', type: 'number' }, 42)).toBe(42); |
| 122 | + }); |
| 123 | + |
| 124 | + it('returns undefined for empty values', () => { |
| 125 | + expect(buildFilterCondition(regionDef, undefined)).toBeUndefined(); |
| 126 | + expect(buildFilterCondition(regionDef, '')).toBeUndefined(); |
| 127 | + expect(buildFilterCondition(regionDef, [])).toBeUndefined(); |
| 128 | + expect(buildFilterCondition(dateDef, {})).toBeUndefined(); |
| 129 | + expect(buildFilterCondition(dateDef, { preset: undefined })).toBeUndefined(); |
| 130 | + }); |
| 131 | +}); |
| 132 | + |
| 133 | +describe('buildWidgetScopedFilter', () => { |
| 134 | + const defs = [dateDef, regionDef]; |
| 135 | + |
| 136 | + it('applies the default binding (the filter\'s own field)', () => { |
| 137 | + const scoped = buildWidgetScopedFilter({ id: 'w1' }, defs, { region: 'EMEA' }); |
| 138 | + expect(scoped).toEqual({ region: 'EMEA' }); |
| 139 | + }); |
| 140 | + |
| 141 | + it('lets filterBindings override the target field per widget', () => { |
| 142 | + const scoped = buildWidgetScopedFilter( |
| 143 | + { id: 'w1', filterBindings: { dateRange: 'signed_at', region: 'sales_region' } }, |
| 144 | + defs, |
| 145 | + { dateRange: { preset: 'last_7_days' }, region: 'APAC' }, |
| 146 | + ); |
| 147 | + expect(scoped).toEqual({ |
| 148 | + $and: [ |
| 149 | + { signed_at: { $gte: '{7_days_ago}', $lte: '{today}' } }, |
| 150 | + { sales_region: 'APAC' }, |
| 151 | + ], |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + it('opts a widget out with filterBindings: false', () => { |
| 156 | + const scoped = buildWidgetScopedFilter( |
| 157 | + { id: 'w1', filterBindings: { region: false } }, |
| 158 | + defs, |
| 159 | + { region: 'EMEA' }, |
| 160 | + ); |
| 161 | + expect(scoped).toBeUndefined(); |
| 162 | + }); |
| 163 | + |
| 164 | + it('honors the legacy targetWidgets allow-list for default bindings', () => { |
| 165 | + const gated: DashboardFilterDef = { ...regionDef, targetWidgets: ['w2'] }; |
| 166 | + expect(buildWidgetScopedFilter({ id: 'w1' }, [gated], { region: 'EMEA' })).toBeUndefined(); |
| 167 | + expect(buildWidgetScopedFilter({ id: 'w2' }, [gated], { region: 'EMEA' })).toEqual({ |
| 168 | + region: 'EMEA', |
| 169 | + }); |
| 170 | + // Explicit binding wins over the allow-list. |
| 171 | + expect( |
| 172 | + buildWidgetScopedFilter({ id: 'w1', filterBindings: { region: 'area' } }, [gated], { |
| 173 | + region: 'EMEA', |
| 174 | + }), |
| 175 | + ).toEqual({ area: 'EMEA' }); |
| 176 | + }); |
| 177 | + |
| 178 | + it('combines several active filters with $and and returns undefined when none apply', () => { |
| 179 | + const scoped = buildWidgetScopedFilter({ id: 'w1' }, defs, { |
| 180 | + dateRange: { preset: 'today' }, |
| 181 | + region: 'EMEA', |
| 182 | + }); |
| 183 | + expect(scoped).toEqual({ |
| 184 | + $and: [ |
| 185 | + { created_at: { $gte: '{today}', $lte: '{today}' } }, |
| 186 | + { region: 'EMEA' }, |
| 187 | + ], |
| 188 | + }); |
| 189 | + expect(buildWidgetScopedFilter({ id: 'w1' }, defs, {})).toBeUndefined(); |
| 190 | + }); |
| 191 | +}); |
| 192 | + |
| 193 | +describe('mergeFilters', () => { |
| 194 | + it('ANDs two non-empty filters, passes single ones through, drops empties', () => { |
| 195 | + expect(mergeFilters({ a: 1 }, { b: 2 })).toEqual({ $and: [{ a: 1 }, { b: 2 }] }); |
| 196 | + expect(mergeFilters({ a: 1 }, undefined)).toEqual({ a: 1 }); |
| 197 | + expect(mergeFilters(undefined, { b: 2 })).toEqual({ b: 2 }); |
| 198 | + expect(mergeFilters({}, undefined)).toBeUndefined(); |
| 199 | + }); |
| 200 | +}); |
0 commit comments