/
transformQuery.test.ts
153 lines (140 loc) · 3.95 KB
/
transformQuery.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import type { RuleGroupType, RuleGroupTypeIC, RuleType } from '../types/index.noReact';
import { transformQuery } from './transformQuery';
const query: RuleGroupType = {
combinator: 'and',
rules: [{ field: 'f1', operator: '=', value: 'v1' }],
};
const queryIC: RuleGroupTypeIC = {
rules: [{ rules: [] }, 'and', { rules: [] }],
};
const combinatorMap = { and: '&&', or: '||' } as const;
it('only adds path by default', () => {
expect(transformQuery(query)).toEqual({
...query,
path: [],
rules: [{ ...query.rules[0], path: [0] }],
});
expect(transformQuery(queryIC)).toEqual({
path: [],
rules: [
{ ...queryIC.rules[0], path: [0] },
queryIC.rules[1],
{ ...queryIC.rules[2], path: [2] },
],
});
});
it('respects the ruleProcessor option', () => {
expect(
transformQuery(query, {
ruleProcessor: r => `${r.field} ${r.operator} ${r.value}`,
})
).toEqual({ combinator: 'and', rules: ['f1 = v1'], path: [] });
});
it('respects the ruleGroupProcessor option', () => {
expect(
transformQuery(query, {
ruleGroupProcessor: rg => ({
text: `rules.length == ${rg.rules.length}`,
}),
})
).toEqual({
text: 'rules.length == 1',
rules: query.rules.map((r, idx) => ({ ...r, path: [idx] })),
});
});
it('respects the propertyMap option', () => {
expect(
transformQuery(query, {
propertyMap: { combinator: 'AndOr', rules: 'filters', value: 'val', operator: false },
})
).toEqual({
AndOr: 'and',
path: [],
filters: [{ field: 'f1', val: 'v1', path: [0] }],
});
});
it('avoids recursion for { rules: false } propertyMap', () => {
expect(
transformQuery(query, {
propertyMap: { rules: false },
ruleProcessor: () => {
throw 'should never see this';
},
})
).toEqual({ combinator: 'and', path: [] });
});
it('does not set operator property if it is missing', () => {
expect(
transformQuery({ combinator: 'and', rules: [{ field: 'f1', value: 'v1' } as RuleType] })
).toEqual({
combinator: 'and',
path: [],
rules: [{ field: 'f1', value: 'v1', path: [0] }],
});
});
it('respects the combinatorMap option', () => {
expect(transformQuery(query, { combinatorMap })).toEqual({
...query,
path: [],
combinator: '&&',
rules: query.rules.map((r, idx) => ({ ...r, path: [idx] })),
});
});
it('respects the operatorMap option', () => {
expect(transformQuery(query, { operatorMap: { '=': '==' } })).toEqual({
...query,
path: [],
rules: [{ ...query.rules[0], operator: '==', path: [0] }],
});
});
it('respects the omitPath option', () => {
expect(transformQuery(query, { omitPath: true })).toEqual({
...query,
rules: [{ ...query.rules[0], operator: '=' }],
});
});
it('respects the deleteRemappedProperties option', () => {
expect(
transformQuery(query, {
deleteRemappedProperties: false,
propertyMap: {
combinator: 'AndOr',
value: 'val',
operator: 'operator',
unknownProperty: 'unknown',
},
})
).toEqual({
combinator: 'and',
path: [],
AndOr: 'and',
rules: [{ field: 'f1', operator: '=', val: 'v1', value: 'v1', path: [0] }],
});
});
it('handles independent combinators and nested groups', () => {
expect(
transformQuery(
{
rules: [
{
rules: [
{ field: 'f1', operator: '=', value: 'v1' },
'or',
{ field: 'f2', operator: '=', value: 'v2' },
],
},
],
},
{ ruleProcessor: () => 'Rule!', combinatorMap }
)
).toEqual({
path: [],
rules: [{ rules: ['Rule!', '||', 'Rule!'], path: [0] }],
});
// This next test is really just to check that ruleGroupProcessor inherits
// the type RuleGroupTypeIC from the first parameter.
expect(transformQuery(queryIC, { ruleGroupProcessor: rg => rg })).toEqual({
path: [],
rules: [{ rules: [], path: [0] }, queryIC.rules[1], { rules: [], path: [2] }],
});
});