Skip to content

Commit 3ff49df

Browse files
author
Tim Streicher
committed
test: split and add tests
1 parent e3947f2 commit 3ff49df

File tree

2 files changed

+85
-54
lines changed

2 files changed

+85
-54
lines changed

src/test/conversion.test.ts src/test/conversion-basic.test.ts

+1-54
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {applyDRFInterceptor, convertFilterSetConfig} from '../middleware'
21
import type {FilterSetConfig} from '../types'
2+
import {convertFilterSetConfig} from '../middleware'
33

44
interface Data {
55
number: number
@@ -74,56 +74,3 @@ test('it should map multiple filters', () => {
7474
// eslint-disable-next-line camelcase
7575
expect(converted).toEqual({number__gt: 123, number__lt: 987, text__lt: 'string', text__gt: 'foo'})
7676
})
77-
78-
interface FilterSetMapping {
79-
number: 'exact' | 'lte' | 'lt' | 'gt'
80-
text: 'lt' | 'exact'
81-
}
82-
83-
test('it should accept a KeyConfig ', () => {
84-
const simpleConfig: FilterSetConfig<Data, FilterSetMapping> = {
85-
number: {lt: 987, gt: 123},
86-
text: {exact: 'string'},
87-
}
88-
const converted = convertFilterSetConfig(simpleConfig)
89-
// eslint-disable-next-line camelcase
90-
expect(converted).toEqual({number__gt: 123, number__lt: 987, text__exact: 'string'})
91-
})
92-
93-
interface CustomObject {
94-
attribute: string
95-
}
96-
97-
interface CustomFilter {
98-
99-
custom1: any[]
100-
custom2: CustomObject
101-
102-
}
103-
104-
interface CustomFilterSetMapping {
105-
number: 'exact' | 'lte' | 'lt' | 'gt' | 'custom1'
106-
text: 'lt' | 'exact' | 'custom2'
107-
}
108-
109-
test('it should accept a CustomKeyConfig ', () => {
110-
const customObject = {attribute: ''}
111-
const simpleConfig: FilterSetConfig<Data, CustomFilterSetMapping, CustomFilter> = {
112-
number: {custom1: ['']},
113-
text: {custom2: customObject},
114-
}
115-
const converted = convertFilterSetConfig(simpleConfig)
116-
// eslint-disable-next-line camelcase
117-
expect(converted).toEqual({number__custom1: [''], text__custom2: customObject})
118-
})
119-
120-
test('it should accept a CustomKeyConfig and standard DRF filters', () => {
121-
const customObject = {attribute: ''}
122-
const simpleConfig: FilterSetConfig<Data, CustomFilterSetMapping, CustomFilter> = {
123-
number: {custom1: [''], exact: 123},
124-
text: {custom2: customObject, lt: 'foo'},
125-
}
126-
const converted = convertFilterSetConfig(simpleConfig)
127-
// eslint-disable-next-line camelcase
128-
expect(converted).toEqual({number__custom1: [''], number__exact: 123, text__custom2: customObject, text__lt: 'foo'})
129-
})

src/test/conversion-custom.test.ts

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)