|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | +/* |
| 5 | + * Copyright (c) 2016-present, Parse, LLC |
| 6 | + * All rights reserved. |
| 7 | + * |
| 8 | + * This source code is licensed under the license found in the LICENSE file in |
| 9 | + * the root directory of this source tree. |
| 10 | + */ |
| 11 | + |
| 12 | +describe('BrowserFilter - Legacy Filter Normalization', () => { |
| 13 | + // Function to normalize filters (same logic as in BrowserFilter) |
| 14 | + function normalizeFilters(filters, className) { |
| 15 | + return filters.map(filter => { |
| 16 | + const normalizedFilter = { ...filter }; |
| 17 | + if (normalizedFilter.class === className) { |
| 18 | + delete normalizedFilter.class; |
| 19 | + } |
| 20 | + return normalizedFilter; |
| 21 | + }); |
| 22 | + } |
| 23 | + |
| 24 | + describe('filter normalization for legacy support', () => { |
| 25 | + it('should normalize filters by removing class property that matches current className', () => { |
| 26 | + const filtersWithClass = [ |
| 27 | + { field: 'fieldA', constraint: 'eq', compareTo: 'valueA', class: 'MyClass' }, |
| 28 | + { field: 'fieldB', constraint: 'eq', compareTo: 'valueB', class: 'MyClass' } |
| 29 | + ]; |
| 30 | + |
| 31 | + const filtersWithoutClass = [ |
| 32 | + { field: 'fieldA', constraint: 'eq', compareTo: 'valueA' }, |
| 33 | + { field: 'fieldB', constraint: 'eq', compareTo: 'valueB' } |
| 34 | + ]; |
| 35 | + |
| 36 | + const normalizedWithClass = normalizeFilters(filtersWithClass, 'MyClass'); |
| 37 | + const normalizedWithoutClass = normalizeFilters(filtersWithoutClass, 'MyClass'); |
| 38 | + |
| 39 | + expect(JSON.stringify(normalizedWithClass)).toBe(JSON.stringify(normalizedWithoutClass)); |
| 40 | + expect(JSON.stringify(normalizedWithClass)).toBe(JSON.stringify(filtersWithoutClass)); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should not remove class property that differs from current className', () => { |
| 44 | + const filtersWithDifferentClass = [ |
| 45 | + { field: 'name', constraint: 'eq', compareTo: 'test', class: '_User' } |
| 46 | + ]; |
| 47 | + |
| 48 | + const normalized = normalizeFilters(filtersWithDifferentClass, 'MyClass'); |
| 49 | + |
| 50 | + expect(normalized[0].class).toBe('_User'); |
| 51 | + expect(JSON.stringify(normalized)).toBe(JSON.stringify(filtersWithDifferentClass)); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should handle filters without class property', () => { |
| 55 | + const filtersWithoutClass = [ |
| 56 | + { field: 'fieldA', constraint: 'eq', compareTo: 'valueA' }, |
| 57 | + { field: 'fieldB', constraint: 'eq', compareTo: 'valueB' } |
| 58 | + ]; |
| 59 | + |
| 60 | + const normalized = normalizeFilters(filtersWithoutClass, 'MyClass'); |
| 61 | + |
| 62 | + expect(JSON.stringify(normalized)).toBe(JSON.stringify(filtersWithoutClass)); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should handle complex filters with dates', () => { |
| 66 | + const filtersWithDates = [ |
| 67 | + { |
| 68 | + field: 'createdAt', |
| 69 | + constraint: 'after', |
| 70 | + compareTo: { __type: 'Date', iso: '2023-11-18T00:00:00.000Z' }, |
| 71 | + class: 'MyClass' |
| 72 | + } |
| 73 | + ]; |
| 74 | + |
| 75 | + const expectedNormalized = [ |
| 76 | + { |
| 77 | + field: 'createdAt', |
| 78 | + constraint: 'after', |
| 79 | + compareTo: { __type: 'Date', iso: '2023-11-18T00:00:00.000Z' } |
| 80 | + } |
| 81 | + ]; |
| 82 | + |
| 83 | + const normalized = normalizeFilters(filtersWithDates, 'MyClass'); |
| 84 | + |
| 85 | + expect(JSON.stringify(normalized)).toBe(JSON.stringify(expectedNormalized)); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should match normalized filters', () => { |
| 89 | + // This is the exact filter from the broken URL |
| 90 | + const originalLegacyFilter = [ |
| 91 | + { field: 'fieldA', constraint: 'eq', compareTo: 'valueA' }, |
| 92 | + { field: 'fieldB', constraint: 'eq', compareTo: 'valueB' }, |
| 93 | + { |
| 94 | + field: 'createdAt', |
| 95 | + constraint: 'after', |
| 96 | + compareTo: { __type: 'Date', iso: '2023-11-18T00:00:00.000Z' } |
| 97 | + } |
| 98 | + ]; |
| 99 | + |
| 100 | + // This is what extractFiltersFromQuery creates (with class property added) |
| 101 | + const processedFilter = [ |
| 102 | + { field: 'fieldA', constraint: 'eq', compareTo: 'valueA', class: 'MyClass' }, |
| 103 | + { field: 'fieldB', constraint: 'eq', compareTo: 'valueB', class: 'MyClass' }, |
| 104 | + { |
| 105 | + field: 'createdAt', |
| 106 | + constraint: 'after', |
| 107 | + compareTo: { __type: 'Date', iso: '2023-11-18T00:00:00.000Z' }, |
| 108 | + class: 'MyClass' |
| 109 | + } |
| 110 | + ]; |
| 111 | + |
| 112 | + // Without normalization, these don't match |
| 113 | + expect(JSON.stringify(originalLegacyFilter)).not.toBe(JSON.stringify(processedFilter)); |
| 114 | + |
| 115 | + // With normalization, they should match |
| 116 | + const normalizedOriginal = normalizeFilters(originalLegacyFilter, 'MyClass'); |
| 117 | + const normalizedProcessed = normalizeFilters(processedFilter, 'MyClass'); |
| 118 | + |
| 119 | + expect(JSON.stringify(normalizedOriginal)).toBe(JSON.stringify(normalizedProcessed)); |
| 120 | + expect(JSON.stringify(normalizedOriginal)).toBe(JSON.stringify(originalLegacyFilter)); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should handle the working filter', () => { |
| 124 | + // This is the filter from the working URL (already has class property) |
| 125 | + const workingFilter = [ |
| 126 | + { class: 'MyClass', field: 'fieldA', constraint: 'eq', compareTo: 'valueA' } |
| 127 | + ]; |
| 128 | + |
| 129 | + // When processed, it should remain the same since it already has the correct class |
| 130 | + const processedWorkingFilter = [ |
| 131 | + { class: 'MyClass', field: 'fieldA', constraint: 'eq', compareTo: 'valueA' } |
| 132 | + ]; |
| 133 | + |
| 134 | + // With normalization, both should normalize to the same thing |
| 135 | + const normalizedWorking = normalizeFilters(workingFilter, 'MyClass'); |
| 136 | + const normalizedProcessed = normalizeFilters(processedWorkingFilter, 'MyClass'); |
| 137 | + |
| 138 | + expect(JSON.stringify(normalizedWorking)).toBe(JSON.stringify(normalizedProcessed)); |
| 139 | + |
| 140 | + // Both should normalize to the version without class property |
| 141 | + const expectedNormalized = [ |
| 142 | + { field: 'fieldA', constraint: 'eq', compareTo: 'valueA' } |
| 143 | + ]; |
| 144 | + |
| 145 | + expect(JSON.stringify(normalizedWorking)).toBe(JSON.stringify(expectedNormalized)); |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments