Newer
Older
import { criterionToString, stringToCriterion, getPrettyCriterion, getPrettyOperator } from './';
import {
FieldCriterion,
JsonCriterion,
SelectMultipleCriterion,
BetweenCriterion,
Criterion,
ListCriterion
} from '.';
describe('CriterionModel', () => {
it('#criterionToString should convert criterion object to string', () => {
let betweenCriterion: BetweenCriterion = { id: 1, type: 'between', min: 'un', max: null };
let expected = '1::gte::un';
expect(criterionToString(betweenCriterion)).toEqual(expected);
betweenCriterion = { id: 1, type: 'between', min: null, max: 'deux' };
expected = '1::lte::deux';
expect(criterionToString(betweenCriterion)).toEqual(expected);
betweenCriterion = { id: 1, type: 'between', min: 'un', max: 'deux' };
expected = '1::bw::un|deux';
expect(criterionToString(betweenCriterion)).toEqual(expected);
const fieldCriterion = { id: 1, type: 'field', operator: 'eq', value: 'value' } as FieldCriterion;
expected = '1::eq::value';
expect(criterionToString(fieldCriterion)).toEqual(expected);
const listCriterion = { id: 1, type: 'list', values: ['un', 'deux'] } as ListCriterion;
expected = '1::in::un|deux';
expect(criterionToString(listCriterion)).toEqual(expected);
const jsonCriterion = { id: 1, type: 'json', path: 'path', operator: 'eq', value: 'value' } as JsonCriterion;
expected = '1::js::path|eq|value';
expect(criterionToString(jsonCriterion)).toEqual(expected);
const selectMultipleCriterion = { id: 1, type: 'multiple', options: [{ label: 'un', value: '1', display: 1 }, { label: 'deux', value: '2', display: 2 }] } as SelectMultipleCriterion;
expected = '1::in::1|2';
expect(criterionToString(selectMultipleCriterion)).toEqual(expected);
});
it('#stringToCriterion should convert string criterion into object', () => {
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
let attribute: Attribute = {
id: 1,
name: 'myAttribute',
label: 'my attribute',
form_label: 'My Attribute',
output_display: 1,
criteria_display: 1,
search_type: 'field',
operator: 'eq',
type: 'field',
display_detail: 1
};
let expected: Criterion = { id: 1, type: 'field', operator: 'neq', value: 'otherValue' } as FieldCriterion;
expect(stringToCriterion(attribute, ['', 'neq', 'otherValue'])).toEqual(expected);
attribute = {
id: 1,
name: 'myAttribute',
label: 'my attribute',
form_label: 'My Attribute',
output_display: 1,
criteria_display: 1,
search_type: 'field',
operator: 'eq',
type: 'field',
min: 'value',
display_detail: 1
};
expected = { id: 1, type: 'field', operator: 'eq', value: 'value' } as FieldCriterion;
expect(stringToCriterion(attribute)).toEqual(expected);
attribute = {
id: 1,
name: 'myAttribute',
label: 'my attribute',
form_label: 'My Attribute',
output_display: 1,
criteria_display: 1,
search_type: 'list',
operator: 'eq',
type: 'field',
display_detail: 1
};
expected = { id: 1, type: 'list', values: ['one', 'two'] } as ListCriterion;
expect(stringToCriterion(attribute, ['', '', 'one|two'])).toEqual(expected);
attribute = {
id: 1,
name: 'myAttribute',
label: 'my attribute',
form_label: 'My Attribute',
output_display: 1,
criteria_display: 1,
search_type: 'list',
operator: 'eq',
type: 'field',
min: 'valueA|valueB',
display_detail: 1
};
expected = { id: 1, type: 'list', values: ['valueA', 'valueB'] } as ListCriterion;
expect(stringToCriterion(attribute)).toEqual(expected);
attribute = {
id: 1,
name: 'myAttribute',
label: 'my attribute',
form_label: 'My Attribute',
output_display: 1,
criteria_display: 1,
search_type: 'between',
operator: 'eq',
type: 'field',
min: 'one',
max: 'two',
display_detail: 1
};
expected = { id: 1, type: 'between', min: 'valueA', max: 'valueB' } as BetweenCriterion;
expect(stringToCriterion(attribute, ['', 'bw', 'valueA|valueB'])).toEqual(expected);
expected = { id: 1, type: 'between', min: 'valueA', max: null } as BetweenCriterion;
expect(stringToCriterion(attribute, ['', 'gte', 'valueA'])).toEqual(expected);
expected = { id: 1, type: 'between', min: null, max: 'valueB' } as BetweenCriterion;
expect(stringToCriterion(attribute, ['', 'lte', 'valueB'])).toEqual(expected);
expected = { id: 1, type: 'between', min: 'one', max: 'two' } as BetweenCriterion;
expect(stringToCriterion(attribute)).toEqual(expected);
attribute = {
id: 1,
name: 'myAttribute',
label: 'my attribute',
form_label: 'My Attribute',
output_display: 1,
criteria_display: 1,
search_type: 'select-multiple',
operator: 'eq',
type: 'field',
min: '1|2',
display_detail: 1,
options: [{ label: 'one', value: '1', display: 1 }, { label: 'two', value: '2', display: 2 }, { label: 'three', value: '3', display: 3 }]
};
expected = { id: 1, type: 'multiple', options: [{ label: 'one', value: '1', display: 1 }, { label: 'three', value: '3', display: 3 }] } as SelectMultipleCriterion;
expect(stringToCriterion(attribute, ['', '', '1|3'])).toEqual(expected);
attribute = {
id: 1,
name: 'myAttribute',
label: 'my attribute',
form_label: 'My Attribute',
output_display: 1,
criteria_display: 1,
search_type: 'select-multiple',
operator: 'eq',
type: 'field',
min: '1|2',
display_detail: 1,
options: [{ label: 'one', value: '1', display: 1 }, { label: 'two', value: '2', display: 2 }, { label: 'three', value: '3', display: 3 }]
};
expected = { id: 1, type: 'multiple', options: [{ label: 'one', value: '1', display: 1 }, { label: 'two', value: '2', display: 2 }] } as SelectMultipleCriterion;
expect(stringToCriterion(attribute)).toEqual(expected);
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
attribute = {
id: 1,
name: 'myAttribute',
label: 'my attribute',
form_label: 'My Attribute',
output_display: 1,
criteria_display: 1,
search_type: 'json',
operator: 'eq',
type: 'field',
display_detail: 1
};
expected = { id: 1, type: 'json', path: 'path', operator: 'op', value: 'value' } as JsonCriterion;
expect(stringToCriterion(attribute, ['', '', 'path|op|value'])).toEqual(expected);
attribute = {
id: 1,
name: 'myAttribute',
label: 'my attribute',
form_label: 'My Attribute',
output_display: 1,
criteria_display: 1,
search_type: 'json',
operator: 'eq',
type: 'field',
min: 'path|op|value',
display_detail: 1
};
expect(stringToCriterion(attribute)).toEqual(expected);
attribute = {
id: 1,
name: 'myAttribute',
label: 'my attribute',
form_label: 'My Attribute',
output_display: 1,
criteria_display: 1,
search_type: '',
operator: 'eq',
type: 'field',
display_detail: 1
};
expect(stringToCriterion(attribute)).toBeNull();
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
it('#getPrettyCriterion should print criterion correctly', () => {
const fieldCriterion = { id: 1, type: 'field', operator: 'eq', value: 'value' } as FieldCriterion;
let expectedPrintedCriterion = '= value';
expect(getPrettyCriterion(fieldCriterion)).toEqual(expectedPrintedCriterion);
const jsonCriterion = { id: 1, type: 'json', path: 'path', operator: 'eq', value: 'value' } as JsonCriterion;
expectedPrintedCriterion = 'path eq value';
expect(getPrettyCriterion(jsonCriterion)).toEqual(expectedPrintedCriterion);
const selectMultipleCriterion = { id: 1, type: 'multiple', options: [{label: 'un', value: '1', display: 1}, {label: 'deux', value: '2', display: 2}] } as SelectMultipleCriterion;
expectedPrintedCriterion = '[un,deux]';
expect(getPrettyCriterion(selectMultipleCriterion)).toEqual(expectedPrintedCriterion);
let betweenCriterion = { id: 1, type: 'between', min: 'un', max: null } as BetweenCriterion;
expectedPrintedCriterion = '>= un';
expect(getPrettyCriterion(betweenCriterion)).toEqual(expectedPrintedCriterion);
betweenCriterion = { id: 1, type: 'between', min: null, max: 'deux' } as BetweenCriterion;
expectedPrintedCriterion = '<= deux';
expect(getPrettyCriterion(betweenCriterion)).toEqual(expectedPrintedCriterion);
betweenCriterion = { id: 1, type: 'between', min: 'un', max: 'deux' } as BetweenCriterion;
expectedPrintedCriterion = '∈ [un;deux]';
expect(getPrettyCriterion(betweenCriterion)).toEqual(expectedPrintedCriterion);
const listCriterion = { id: 1, type: 'list', values: ['un', 'deux'] } as ListCriterion;
expectedPrintedCriterion = '= [un,deux]';
expect(getPrettyCriterion(listCriterion)).toEqual(expectedPrintedCriterion);
const notCriterion = {id: 1, type: null} as Criterion;
expectedPrintedCriterion = 'Criterion type not valid!';
expect(getPrettyCriterion(notCriterion)).toEqual(expectedPrintedCriterion);
});
it('#getPrettyOperator() should prettify operator', () => {
expect(getPrettyOperator('eq')).toEqual('=');
expect(getPrettyOperator('neq')).toEqual('≠');
expect(getPrettyOperator('gt')).toEqual('>');
expect(getPrettyOperator('gte')).toEqual('>=');
expect(getPrettyOperator('lt')).toEqual('<');
expect(getPrettyOperator('lte')).toEqual('<=');
expect(getPrettyOperator('lk')).toEqual('like');
expect(getPrettyOperator('nlk')).toEqual('not like');
expect(getPrettyOperator('in')).toEqual('in');
expect(getPrettyOperator('nin')).toEqual('not in');
expect(getPrettyOperator('toto')).toEqual('toto');
expect(getPrettyOperator('')).toEqual('');
expect(getPrettyOperator('')).toEqual('');
});