-
-
Notifications
You must be signed in to change notification settings - Fork 627
Expand file tree
/
Copy pathFieldConditionsConverter.test.js
More file actions
148 lines (120 loc) · 4.33 KB
/
FieldConditionsConverter.test.js
File metadata and controls
148 lines (120 loc) · 4.33 KB
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
import { test, expect } from 'vitest';
import Converter from '../components/field-conditions/Converter.js';
const FieldConditionsConverter = new Converter();
test('it converts from blueprint format', () => {
let converted = FieldConditionsConverter.fromBlueprint({
name: 'isnt joe',
age: 13,
email: 'equals san@holo.com',
});
let expected = [
{ field: 'name', operator: 'not', value: 'joe' },
{ field: 'age', operator: 'equals', value: '13' },
{ field: 'email', operator: 'equals', value: 'san@holo.com' },
];
expect(converted).toEqual(expected);
});
test('it converts from blueprint format and applies prefixes', () => {
let converted = FieldConditionsConverter.fromBlueprint(
{
name: 'isnt joe',
age: 13,
email: 'equals san@holo.com',
},
'nested_',
);
let expected = [
{ field: 'nested_name', operator: 'not', value: 'joe' },
{ field: 'nested_age', operator: 'equals', value: '13' },
{ field: 'nested_email', operator: 'equals', value: 'san@holo.com' },
];
expect(converted).toEqual(expected);
});
test('it converts from blueprint format when a field has multiple conditions', () => {
let converted = FieldConditionsConverter.fromBlueprint({
status: ['published', 'archived'],
audience: 'members',
});
let expected = [
{ field: 'status', operator: 'equals', value: 'published' },
{ field: 'status', operator: 'equals', value: 'archived' },
{ field: 'audience', operator: 'equals', value: 'members' },
];
expect(converted).toEqual(expected);
});
test('it converts from blueprint format and does not apply prefix to field conditions with root syntax', () => {
let converted = FieldConditionsConverter.fromBlueprint(
{
name: 'isnt joe',
'$root.title': 'not empty',
},
'nested_',
);
let expected = [
{ field: 'nested_name', operator: 'not', value: 'joe' },
{ field: '$root.title', operator: 'not', value: 'empty' },
];
expect(converted).toEqual(expected);
});
test('it converts from blueprint format and does not apply prefix to field conditions with legacy root syntax for backwards compatibility', () => {
let converted = FieldConditionsConverter.fromBlueprint(
{
name: 'isnt joe',
'root.title': 'not empty',
},
'nested_',
);
let expected = [
{ field: 'nested_name', operator: 'not', value: 'joe' },
{ field: 'root.title', operator: 'not', value: 'empty' },
];
expect(converted).toEqual(expected);
});
test('it converts from blueprint format and does not apply prefix to field conditions with parent syntax', () => {
let converted = FieldConditionsConverter.fromBlueprint(
{
name: 'isnt joe',
'$parent.title': 'not empty',
},
'nested_',
);
let expected = [
{ field: 'nested_name', operator: 'not', value: 'joe' },
{ field: '$parent.title', operator: 'not', value: 'empty' },
];
expect(converted).toEqual(expected);
});
test('it converts to blueprint format', () => {
let converted = FieldConditionsConverter.toBlueprint([
{ field: 'name', operator: 'isnt', value: 'joe' },
{ field: 'age', operator: '==', value: '13' },
]);
let expected = {
name: 'isnt joe',
age: '== 13',
};
expect(converted).toEqual(expected);
});
test('it converts to blueprint format when a field has multiple conditions', () => {
let converted = FieldConditionsConverter.toBlueprint([
{ field: 'status', operator: 'is', value: 'published' },
{ field: 'status', operator: 'is', value: 'archived' },
{ field: 'audience', operator: 'is', value: 'members' },
]);
let expected = {
status: ['is published', 'is archived'],
audience: 'is members',
};
expect(converted).toEqual(expected);
});
test('it converts and trims properly with empty operators', () => {
let converted = FieldConditionsConverter.toBlueprint([
{ field: 'name', operator: '', value: 'joe' },
{ field: 'age', operator: null, value: '13' },
]);
let expected = {
name: 'joe',
age: '13',
};
expect(converted).toEqual(expected);
});