-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
200 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type {FilterSetConfig} from '../types' | ||
import {convertFilterSetConfig} from '../middleware' | ||
|
||
interface ComplexData { | ||
id: number | ||
} | ||
|
||
interface Data { | ||
complex?: ComplexData | ||
number?: number | ||
text?: string | ||
new?: boolean | ||
} | ||
test('Error when type of attribute is passed without filter', () => { | ||
const simpleConfig: FilterSetConfig<Data> = { | ||
// @ts-expect-error plain passing is not allowed | ||
text: 'string', | ||
} | ||
expect(() => convertFilterSetConfig(simpleConfig)).toThrow('Cannot use \'in\' operator to search for \'value\' in string') | ||
}) | ||
|
||
test('Error when attribute is not in data type', () => { | ||
const simpleConfig: FilterSetConfig<Data> = { | ||
// @ts-expect-error number is not part of data type | ||
foo: {value: 123}, | ||
} | ||
const converted = convertFilterSetConfig(simpleConfig) | ||
expect(converted).toEqual({foo: 123}) | ||
}) | ||
|
||
test('Error when unknown filter is used', () => { | ||
const simpleConfig: FilterSetConfig<Data> = { | ||
// @ts-expect-error foo is not defined as a filter | ||
number: {foo: 123}, | ||
} | ||
const converted = convertFilterSetConfig(simpleConfig) | ||
// eslint-disable-next-line camelcase | ||
expect(converted).toEqual({number__foo: 123}) | ||
}) | ||
|
||
test('Error when value has the wrong type', () => { | ||
const simpleConfig: FilterSetConfig<Data> = { | ||
// @ts-expect-error text is of type string | ||
text: {value: 123}, | ||
} | ||
const converted = convertFilterSetConfig(simpleConfig) | ||
expect(converted).toEqual({text: 123}) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type {FilterSetConfig} from '../types' | ||
import {convertFilterSetConfig} from '../middleware' | ||
|
||
interface ComplexData { | ||
id: number | ||
} | ||
|
||
interface Data { | ||
complex?: ComplexData | ||
number?: number | ||
text?: string | ||
new?: boolean | ||
} | ||
|
||
interface FilterSetMappingCustom { | ||
data: 'lt' | 'gt' | 'custom1' | ||
text: 'startswith' | 'exact' | 'custom2' | ||
} | ||
|
||
interface CustomFilter { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
custom1: any[] | ||
custom2: number | ||
} | ||
|
||
test('error when custom filter key is not allowed', () => { | ||
const config: FilterSetConfig<Data, FilterSetMappingCustom, CustomFilter> = { | ||
text: { | ||
// @ts-expect-error custom filter is not allowed | ||
custom1: '', | ||
}, | ||
} | ||
const converted = convertFilterSetConfig(config) | ||
// eslint-disable-next-line camelcase | ||
expect(converted).toEqual({text__custom1: ''}) | ||
}) | ||
|
||
test('error when custom has the wrong type', () => { | ||
const config: FilterSetConfig<Data, FilterSetMappingCustom, CustomFilter> = { | ||
text: { | ||
// @ts-expect-error custom filter has wrong type | ||
custom2: '', | ||
}, | ||
} | ||
const converted = convertFilterSetConfig(config) | ||
// eslint-disable-next-line camelcase | ||
expect(converted).toEqual({text__custom2: ''}) | ||
}) | ||
|
||
test('error when custom has the wrong type', () => { | ||
const simpleConfig: FilterSetConfig<Data, FilterSetMappingCustom, CustomFilter> = { | ||
text: { | ||
// @ts-expect-error custom filter has wrong type | ||
custom2: '', | ||
}, | ||
} | ||
const converted = convertFilterSetConfig(simpleConfig) | ||
// eslint-disable-next-line camelcase | ||
expect(converted).toEqual({text__custom2: ''}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import type {FilterSetConfig} from '../types' | ||
import {convertFilterSetConfig} from '../middleware' | ||
|
||
interface ComplexData { | ||
id: number | ||
} | ||
|
||
interface Data { | ||
complex?: ComplexData | ||
number?: number | ||
text?: string | ||
new?: boolean | ||
} | ||
|
||
interface FilterSetKeyConfig { | ||
complex: { | ||
id: 'in' | ||
} | ||
number: 'lt' | 'gt' | 'in' | ||
text: 'startswith' | 'exact' | ||
} | ||
|
||
test('it should accept a KeyConfig', () => { | ||
const numberArray = [1, 2, 3] | ||
const config: FilterSetConfig<Data, FilterSetKeyConfig> = { | ||
number: { | ||
in: numberArray, | ||
lt: 5, | ||
}, | ||
} | ||
const converted = convertFilterSetConfig(config) | ||
// eslint-disable-next-line camelcase | ||
expect(converted).toEqual({number__in: numberArray, number__lt: 5}) | ||
}) | ||
|
||
test('error when filter key is disallowed in mapping', () => { | ||
const numberData = {id: 123} | ||
const config: FilterSetConfig<Data, FilterSetKeyConfig> = { | ||
number: { | ||
// @ts-expect-error lte is not allowed | ||
lte: numberData, | ||
}, | ||
} | ||
const converted = convertFilterSetConfig(config) | ||
// eslint-disable-next-line camelcase | ||
expect(converted).toEqual({number__lte: numberData}) | ||
}) | ||
|
||
test('should handle nested key configs', () => { | ||
const numberArray = [1, 2, 3] | ||
const config: FilterSetConfig<Data, FilterSetKeyConfig> = { | ||
complex: { | ||
id: { | ||
in: numberArray, | ||
}, | ||
}, | ||
} | ||
const converted = convertFilterSetConfig(config) | ||
// eslint-disable-next-line camelcase | ||
expect(converted).toEqual({complex__id__in: numberArray}) | ||
}) | ||
|
||
test('error when filter key is disallowed in nested mapping', () => { | ||
const config: FilterSetConfig<Data, FilterSetKeyConfig> = { | ||
complex: { | ||
id: { | ||
// @ts-expect-error lt is not allowed for nested property | ||
lt: 1, | ||
}, | ||
}, | ||
} | ||
const converted = convertFilterSetConfig(config) | ||
// eslint-disable-next-line camelcase | ||
expect(converted).toEqual({complex__id__lt: 1}) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters