|
| 1 | +import {applyDRFInterceptor, convertFilterSetConfig} from '../middleware' |
| 2 | +import type {FilterSetConfig} from '../types' |
| 3 | + |
| 4 | +interface Data { |
| 5 | + number: number |
| 6 | + text?: string |
| 7 | + new?: boolean |
| 8 | +} |
| 9 | + |
| 10 | +interface FilterSetMapping { |
| 11 | + number: 'exact' | 'lte' | 'lt' | 'gt' |
| 12 | + text: 'lt' | 'exact' |
| 13 | +} |
| 14 | + |
| 15 | +interface CustomObject { |
| 16 | + attribute: string |
| 17 | +} |
| 18 | + |
| 19 | +interface CustomFilter { |
| 20 | + |
| 21 | + custom1: any[] |
| 22 | + custom2: CustomObject |
| 23 | + |
| 24 | +} |
| 25 | + |
| 26 | +interface CustomFilterSetMapping { |
| 27 | + number: 'exact' | 'lte' | 'lt' | 'gt' | 'custom1' |
| 28 | + text: 'lt' | 'exact' | 'custom2' |
| 29 | +} |
| 30 | + |
| 31 | +test('it should accept a KeyConfig ', () => { |
| 32 | + const config: FilterSetConfig<Data, FilterSetMapping> = { |
| 33 | + number: {lt: 987, gt: 123}, |
| 34 | + text: {exact: 'string'}, |
| 35 | + } |
| 36 | + const converted = convertFilterSetConfig(config) |
| 37 | + // eslint-disable-next-line camelcase |
| 38 | + expect(converted).toEqual({number__gt: 123, number__lt: 987, text__exact: 'string'}) |
| 39 | +}) |
| 40 | + |
| 41 | +test('it should accept a CustomKeyConfig ', () => { |
| 42 | + const customObject: CustomObject = {attribute: ''} |
| 43 | + const simpleConfig: FilterSetConfig<Data, CustomFilterSetMapping, CustomFilter> = { |
| 44 | + number: {custom1: ['']}, |
| 45 | + text: {custom2: customObject}, |
| 46 | + } |
| 47 | + const converted = convertFilterSetConfig(simpleConfig) |
| 48 | + // eslint-disable-next-line camelcase |
| 49 | + expect(converted).toEqual({number__custom1: [''], text__custom2: customObject}) |
| 50 | +}) |
| 51 | + |
| 52 | +test('it should accept a CustomKeyConfig and standard DRF filters', () => { |
| 53 | + const customObject = {attribute: ''} |
| 54 | + const simpleConfig: FilterSetConfig<Data, CustomFilterSetMapping, CustomFilter> = { |
| 55 | + number: {custom1: [''], exact: 123}, |
| 56 | + text: {custom2: customObject, lt: 'foo'}, |
| 57 | + } |
| 58 | + const converted = convertFilterSetConfig(simpleConfig) |
| 59 | + // eslint-disable-next-line camelcase |
| 60 | + expect(converted).toEqual({number__custom1: [''], number__exact: 123, text__custom2: customObject, text__lt: 'foo'}) |
| 61 | +}) |
| 62 | + |
| 63 | +test('it should convert the filter defined with a custom filter handler', () => { |
| 64 | + const customObject = {attribute: ''} |
| 65 | + const simpleConfig: FilterSetConfig<Data, CustomFilterSetMapping, CustomFilter> = { |
| 66 | + number: {custom1: [''], exact: 123}, |
| 67 | + text: {custom2: customObject, lt: 'foo'}, |
| 68 | + } |
| 69 | + const converted = convertFilterSetConfig(simpleConfig, { |
| 70 | + custom1: (key, data) => { |
| 71 | + return [{key: 'handler', value: 987}] |
| 72 | + }, |
| 73 | + custom2: (key, data) => { |
| 74 | + const list = [] |
| 75 | + if (data && 'attribute' in data) |
| 76 | + list.push({key: 'custom', value: data.attribute}) |
| 77 | + |
| 78 | + return list |
| 79 | + }, |
| 80 | + }) |
| 81 | + // eslint-disable-next-line camelcase |
| 82 | + expect(converted).toEqual({number__handler: 987, number__exact: 123, text__custom: '', text__lt: 'foo'}) |
| 83 | +}) |
| 84 | + |
0 commit comments