|
| 1 | +import type {FilterSetConfig} from '../types' |
| 2 | +import {convertFilterSetConfig} from '../middleware' |
| 3 | + |
| 4 | +interface NumberData { |
| 5 | + number: number |
| 6 | + noNumber?: Date |
| 7 | +} |
| 8 | + |
| 9 | +interface Data { |
| 10 | + data?: NumberData |
| 11 | + text?: string |
| 12 | + new?: boolean |
| 13 | +} |
| 14 | + |
| 15 | +test('Error when attribute is not in data type', () => { |
| 16 | + const simpleConfig: FilterSetConfig<Data> = { |
| 17 | + // @ts-expect-error number is not part of data type |
| 18 | + number: {value: 123}, |
| 19 | + } |
| 20 | + const converted = convertFilterSetConfig(simpleConfig) |
| 21 | + expect(converted).toEqual({number: 123}) |
| 22 | +}) |
| 23 | + |
| 24 | +test('Error when unknown filter is used', () => { |
| 25 | + const simpleConfig: FilterSetConfig<Data> = { |
| 26 | + // @ts-expect-error foo is not defined as a filter |
| 27 | + number: {foo: 123}, |
| 28 | + } |
| 29 | + const converted = convertFilterSetConfig(simpleConfig) |
| 30 | + // eslint-disable-next-line camelcase |
| 31 | + expect(converted).toEqual({number__foo: 123}) |
| 32 | +}) |
| 33 | + |
| 34 | +test('Error when attribute has the wrong type', () => { |
| 35 | + const simpleConfig: FilterSetConfig<Data> = { |
| 36 | + // @ts-expect-error text is of type string |
| 37 | + text: {value: 123}, |
| 38 | + } |
| 39 | + const converted = convertFilterSetConfig(simpleConfig) |
| 40 | + expect(converted).toEqual({text: 'string'}) |
| 41 | +}) |
| 42 | + |
| 43 | +interface FilterSetMapping { |
| 44 | + data: 'lt' | 'gt' |
| 45 | + text: 'startswith' | 'exact' |
| 46 | +} |
| 47 | + |
| 48 | +test('error when filter key is disallowed in mapping', () => { |
| 49 | + const numberData = {number: 123} |
| 50 | + const simpleConfig: FilterSetConfig<Data, FilterSetMapping> = { |
| 51 | + data: { |
| 52 | + // @ts-expect-error lte is not allowed |
| 53 | + lte: numberData, |
| 54 | + }, |
| 55 | + } |
| 56 | + const converted = convertFilterSetConfig(simpleConfig) |
| 57 | + // eslint-disable-next-line camelcase |
| 58 | + expect(converted).toEqual({data__lte: numberData}) |
| 59 | +}) |
| 60 | + |
| 61 | +interface CustomFilter { |
| 62 | + custom1: any[] |
| 63 | + custom2: number |
| 64 | +} |
| 65 | + |
| 66 | +interface FilterSetMappingCustom { |
| 67 | + data: 'lt' | 'gt' | 'custom1' |
| 68 | + text: 'startswith' | 'exact' | 'custom2' |
| 69 | +} |
| 70 | + |
| 71 | +test('error when custom filter key is not allowed', () => { |
| 72 | + const simpleConfig: FilterSetConfig<Data, FilterSetMappingCustom, CustomFilter> = { |
| 73 | + text: { |
| 74 | + // @ts-expect-error custom filter is not allowed |
| 75 | + custom1: '', |
| 76 | + }, |
| 77 | + } |
| 78 | + const converted = convertFilterSetConfig(simpleConfig) |
| 79 | + // eslint-disable-next-line camelcase |
| 80 | + expect(converted).toEqual({text__custom1: ''}) |
| 81 | +}) |
| 82 | + |
| 83 | +test('error when custom has the wrong type', () => { |
| 84 | + const simpleConfig: FilterSetConfig<Data, FilterSetMappingCustom, CustomFilter> = { |
| 85 | + text: { |
| 86 | + // @ts-expect-error custom filter has wrong type |
| 87 | + custom2: '', |
| 88 | + }, |
| 89 | + } |
| 90 | + const converted = convertFilterSetConfig(simpleConfig) |
| 91 | + // eslint-disable-next-line camelcase |
| 92 | + expect(converted).toEqual({text__custom2: ''}) |
| 93 | +}) |
0 commit comments