|
3 | 3 | anyFilters, |
4 | 4 | toErrorFilter, |
5 | 5 | ErrorFilter, |
| 6 | + noneFilters, |
6 | 7 | } from '../../src/filter/base'; |
7 | 8 |
|
8 | 9 | describe('filter/base', () => { |
@@ -123,6 +124,63 @@ describe('filter/base', () => { |
123 | 124 | }); |
124 | 125 | }); |
125 | 126 |
|
| 127 | + describe('noneFilters', () => { |
| 128 | + it('should handle array of functions', () => { |
| 129 | + const filter = noneFilters([() => false, () => false]); |
| 130 | + expect(filter.canHandle({}, 0, {})).toBe(true); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should handle array of ErrorFilters', () => { |
| 134 | + const filter = noneFilters([ |
| 135 | + { canHandle: () => false }, |
| 136 | + { canHandle: () => false }, |
| 137 | + ]); |
| 138 | + expect(filter.canHandle({}, 0, {})).toBe(true); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should handle mixed array of functions and ErrorFilters', () => { |
| 142 | + const filter = noneFilters([() => false, { canHandle: () => false }]); |
| 143 | + expect(filter.canHandle({}, 0, {})).toBe(true); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should return false if any filter returns true', () => { |
| 147 | + const filter = noneFilters([ |
| 148 | + () => false, |
| 149 | + () => true, |
| 150 | + { canHandle: () => false }, |
| 151 | + ]); |
| 152 | + expect(filter.canHandle({}, 0, {})).toBe(false); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should pass error, attempt and context to filters', () => { |
| 156 | + const error = new Error('test'); |
| 157 | + const attempt = 1; |
| 158 | + const context = { data: 'test' }; |
| 159 | + |
| 160 | + const functionSpy = jest.fn(() => false); |
| 161 | + const filterSpy = jest.fn(() => false); |
| 162 | + |
| 163 | + const filter = noneFilters([functionSpy, { canHandle: filterSpy }]); |
| 164 | + |
| 165 | + filter.canHandle(error, attempt, context); |
| 166 | + |
| 167 | + expect(functionSpy).toHaveBeenCalledWith(error, attempt, context); |
| 168 | + expect(filterSpy).toHaveBeenCalledWith(error, attempt, context); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should short-circuit evaluation when a filter returns true', () => { |
| 172 | + const functionSpy = jest.fn(() => true); |
| 173 | + const filterSpy = jest.fn(() => false); |
| 174 | + |
| 175 | + const filter = noneFilters([functionSpy, { canHandle: filterSpy }]); |
| 176 | + |
| 177 | + filter.canHandle({}, 0, {}); |
| 178 | + |
| 179 | + expect(functionSpy).toHaveBeenCalled(); |
| 180 | + expect(filterSpy).not.toHaveBeenCalled(); |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
126 | 184 | describe('complex filter combinations', () => { |
127 | 185 | it('should handle nested filters with allFilters', () => { |
128 | 186 | const filter = allFilters([ |
@@ -150,5 +208,13 @@ describe('filter/base', () => { |
150 | 208 | ]); |
151 | 209 | expect(filter.canHandle({}, 0, {})).toBe(true); |
152 | 210 | }); |
| 211 | + |
| 212 | + it('should handle combinations with noneFilters', () => { |
| 213 | + const filter = allFilters([ |
| 214 | + noneFilters([() => false, { canHandle: () => false }]), |
| 215 | + anyFilters([() => true, { canHandle: () => false }]), |
| 216 | + ]); |
| 217 | + expect(filter.canHandle({}, 0, {})).toBe(true); |
| 218 | + }); |
153 | 219 | }); |
154 | 220 | }); |
0 commit comments