UNPKG

15.1 kBJavaScriptView Raw
1$(function() {
2 var $b = $('#builder');
3
4 QUnit.module('data', {
5 afterEach: function() {
6 $b.queryBuilder('destroy');
7 }
8 });
9
10 /**
11 * Test filters values
12 */
13 QUnit.test('radio/checkbox/select values', function(assert) {
14 $b.queryBuilder({
15 filters: [{
16 id: '1',
17 type: 'string',
18 input: 'radio',
19 values: ['one', 'two', 'three']
20 }, {
21 id: '2',
22 type: 'string',
23 input: 'checkbox',
24 values: {
25 one: 'One',
26 two: 'Two',
27 three: 'Three'
28 }
29 }, {
30 id: '3',
31 type: 'string',
32 input: 'select',
33 values: [
34 { one: 'One' },
35 { two: 'Two' },
36 { three: 'Three' }
37 ]
38 }],
39 rules: {
40 rules: [{
41 id: '1',
42 value: 'one'
43 }, {
44 id: '2',
45 value: 'two'
46 }, {
47 id: '3',
48 value: 'three'
49 }]
50 }
51 });
52
53 assert.optionsMatch(
54 $('#builder_rule_0 .rule-value-container input'),
55 ['one', 'two', 'three'],
56 'Should take an array of values'
57 );
58
59 assert.optionsMatch(
60 $('#builder_rule_1 .rule-value-container input'),
61 ['one', 'two', 'three'],
62 'Should take an object of values'
63 );
64
65 assert.optionsMatch(
66 $('#builder_rule_2 .rule-value-container option'),
67 ['one', 'two', 'three'],
68 'Should take an array of objects of values'
69 );
70 });
71
72 /**
73 * Test data validation
74 */
75 QUnit.test('validation', function(assert) {
76 $b.queryBuilder({
77 filters: validation_filters
78 });
79
80 assert.validationError($b,
81 null,
82 /no_filter/
83 );
84
85 $b.queryBuilder('clear');
86 $b.queryBuilder('setRoot', false);
87
88 assert.validationError($b,
89 null,
90 /empty_group/
91 );
92
93 assert.validationError($b,
94 { id: 'radio' },
95 /radio_empty/
96 );
97
98 assert.validationError($b,
99 { id: 'checkbox' },
100 /checkbox_empty/
101 );
102
103 assert.validationError($b,
104 { id: 'checkbox', value: ['one', 'two'] },
105 /operator_not_multiple/
106 );
107
108 assert.validationError($b,
109 { id: 'select' },
110 /select_empty/
111 );
112
113 assert.validationError($b,
114 { id: 'select', value: -1 },
115 /select_empty/
116 );
117
118 assert.validationError($b,
119 { id: 'select_mult' },
120 /select_empty/
121 );
122
123 assert.validationError($b,
124 { id: 'select_mult', value: ['one', 'two'] },
125 /operator_not_multiple/
126 );
127
128 assert.validationError($b,
129 { id: 'string' },
130 /string_empty/
131 );
132
133 assert.validationError($b,
134 { id: 'string_val', value: 'aa' },
135 /string_exceed_min_length/
136 );
137
138 assert.validationError($b,
139 { id: 'string_val', value: 'aaaaaa' },
140 /string_exceed_max_length/
141 );
142
143 assert.validationError($b,
144 { id: 'string_val', value: '12345' },
145 /string_invalid_format/
146 );
147
148 assert.validationError($b,
149 { id: 'textarea' },
150 /string_empty/
151 );
152
153 assert.validationError($b,
154 { id: 'integer', value: 5.2 },
155 /number_not_integer/
156 );
157
158 assert.validationError($b,
159 { id: 'integer', value: -15 },
160 /number_exceed_min/
161 );
162
163 assert.validationError($b,
164 { id: 'integer', value: 15 },
165 /number_exceed_max/
166 );
167
168 assert.validationError($b,
169 { id: 'double', value: 0.05 },
170 /number_wrong_step/
171 );
172
173 assert.validationError($b,
174 { id: 'date' },
175 /datetime_empty/
176 );
177
178 assert.validationError($b,
179 { id: 'date', value: '2014/13/15' },
180 /datetime_invalid/
181 );
182
183 assert.validationError($b,
184 { id: 'time', value: '07:00' },
185 /datetime_exceed_min/
186 );
187
188 assert.validationError($b,
189 { id: 'time', value: '18:00' },
190 /datetime_exceed_max/
191 );
192
193 assert.validationError($b,
194 { id: 'boolean', value: 'oui' },
195 /boolean_not_valid/
196 );
197
198 assert.validationError($b,
199 { id: 'custom', value: '' },
200 /you_fool/
201 );
202 });
203
204 /**
205 * Test custom data
206 */
207 QUnit.test('custom data', function(assert) {
208 var rules = {
209 condition: 'AND',
210 data: [1, 2, 3],
211 rules: [{
212 id: 'name',
213 value: 'Mistic',
214 data: {
215 foo: 'bar'
216 }
217 }]
218 };
219
220 assert.expect(2);
221
222 $b.queryBuilder({
223 filters: basic_filters
224 });
225
226 $b.on('afterAddRule.queryBuilder', function(e, rule) {
227 assert.ok(
228 JSON.stringify(rule.data) === JSON.stringify(rules.rules[0].data),
229 'Custom data should be accessible in "afterAddRule" event'
230 );
231 });
232
233 $b.queryBuilder('setRules', rules);
234
235 assert.rulesMatch(
236 $b.queryBuilder('getRules'),
237 rules,
238 'Should keep custom data in "getRules"'
239 );
240 });
241
242 /**
243 * Set an empty rule
244 */
245 QUnit.test('set empty rule', function(assert) {
246 var rules = [{
247 id: 'name',
248 value: 'Mistic'
249 }, {
250 empty: true
251 }, {
252 condition: 'OR',
253 rules: []
254 }];
255
256 $b.queryBuilder({
257 filters: basic_filters,
258 rules: rules
259 });
260
261 assert.validationError($b,
262 null,
263 /no_filter/
264 );
265
266 assert.equal(
267 $b[0].queryBuilder.model.root.rules.length, 3,
268 'Should have two rules and one group'
269 );
270
271 assert.equal(
272 $b[0].queryBuilder.model.root.rules[2].rules.length, 0,
273 'Group should be empty'
274 );
275
276 assert.equal(
277 $('[name=builder_rule_1_filter]').val(), '-1',
278 'Second rule should be empty'
279 );
280 });
281
282 /**
283 * Test get flags with getRules
284 */
285 QUnit.test('get flags', function(assert) {
286 var rules_readonly = {
287 condition: 'AND',
288 flags: {
289 condition_readonly: true
290 },
291 rules: [{
292 id: 'price',
293 operator: 'less',
294 value: 10.25,
295 flags: {
296 no_delete: true
297 }
298 }, {
299 condition: 'OR',
300 readonly: true,
301 rules: [{
302 id: 'id',
303 operator: 'not_equal',
304 value: '1234-azer-5678',
305 readonly: true
306 }]
307 }]
308 };
309
310 $b.queryBuilder({
311 filters: basic_filters,
312 rules: rules_readonly
313 });
314
315 var rules_changed_flags = $.extend(true, {}, rules_readonly);
316 rules_changed_flags.rules[1].flags = {
317 condition_readonly: true,
318 no_add_rule: true,
319 no_add_group: true,
320 no_delete: true
321 };
322 rules_changed_flags.rules[1].rules[0].flags = {
323 filter_readonly: true,
324 operator_readonly: true,
325 value_readonly: true,
326 no_delete: true
327 };
328
329 var rules_all_flags = $.extend(true, {}, rules_changed_flags);
330 rules_all_flags.flags = {
331 condition_readonly: true,
332 no_add_rule: false,
333 no_add_group: false,
334 no_delete: false,
335 no_sortable: false,
336 no_drop: false
337 };
338 rules_all_flags.rules[0].flags = {
339 filter_readonly: false,
340 operator_readonly: false,
341 value_readonly: false,
342 no_delete: true,
343 no_sortable: false,
344 no_drop: false
345 };
346 rules_all_flags.rules[1].flags = {
347 condition_readonly: true,
348 no_add_rule: true,
349 no_add_group: true,
350 no_delete: true,
351 no_sortable: false,
352 no_drop: false
353 };
354 rules_all_flags.rules[1].rules[0].flags = {
355 filter_readonly: true,
356 operator_readonly: true,
357 value_readonly: true,
358 no_delete: true,
359 no_sortable: false,
360 no_drop: false
361 };
362
363 assert.rulesMatch(
364 $b.queryBuilder('getRules', { get_flags: true }),
365 rules_changed_flags,
366 'Should export rules with changed flags'
367 );
368
369 assert.rulesMatch(
370 $b.queryBuilder('getRules', { get_flags: 'all' }),
371 rules_all_flags,
372 'Should export rules with all flags'
373 );
374 });
375
376 /**
377 * Test value separator
378 */
379 QUnit.test('value separator', function(assert) {
380 $b.queryBuilder({
381 filters: basic_filters,
382 rules: [{
383 id: 'name',
384 operator: 'equal',
385 value: 'Mistic,Damien'
386 }, {
387 id: 'age',
388 operator: 'not_equal',
389 value: '16|17|18'
390 }]
391 });
392
393 $('[name=builder_rule_0_operator]').val('in').trigger('change');
394 $('[name=builder_rule_1_operator]').val('not_in').trigger('change');
395
396 assert.rulesMatch(
397 $b.queryBuilder('getRules'),
398 {
399 condition: 'AND',
400 rules: [{
401 id: 'name',
402 operator: 'in',
403 value: ['Mistic', 'Damien']
404 }, {
405 id: 'age',
406 operator: 'not_in',
407 value: ['16', '17', '18']
408 }]
409 },
410 'Should split values on comma and pipe'
411 );
412 });
413
414 /**
415 * Test allow_invalid option
416 */
417 QUnit.test('allow invalid', function(assert) {
418 $b.queryBuilder({
419 filters: basic_filters
420 });
421
422 $b.queryBuilder('setRules', {
423 condition: 'XOR',
424 rules: [{
425 id: 'name',
426 operator: 'unkown_ope',
427 value: 'Mistic'
428 }, {
429 id: 'unknown_id',
430 operator: 'equal',
431 value: 123
432 }]
433 }, {
434 allow_invalid: true
435 });
436
437 assert.rulesMatch(
438 $b.queryBuilder('getRules', {
439 allow_invalid: true
440 }),
441 {
442 valid: false,
443 condition: 'AND',
444 rules: [{
445 id: 'name',
446 operator: 'equal',
447 value: 'Mistic'
448 }, {
449 id: null,
450 operator: null,
451 value: null
452 }]
453 },
454 'Should allow invalid rules for setRules and getRules'
455 );
456 });
457
458 /**
459 * Test skip_empty option
460 */
461 QUnit.test('skip empty', function(assert) {
462 $b.queryBuilder({
463 filters: basic_filters
464 });
465
466 $b.queryBuilder('setRules', {
467 condition: 'AND',
468 rules: [{
469 id: 'name',
470 operator: 'equal',
471 value: 'Mistic'
472 }, {
473 empty: true
474 }]
475 });
476
477 assert.rulesMatch(
478 $b.queryBuilder('getRules', {
479 skip_empty: true
480 }),
481 {
482 condition: 'AND',
483 rules: [{
484 id: 'name',
485 operator: 'equal',
486 value: 'Mistic'
487 }]
488 },
489 'Should skip empty rules for getRules'
490 );
491 });
492
493 /**
494 * Test allow_empty_value option
495 */
496 QUnit.test('allow empty value', function(assert) {
497 var filters = $.extend(true, [], basic_filters);
498 filters.forEach(function(filter) {
499 filter.validation = $.extend({ allow_empty_value: true }, filter.validation);
500 });
501
502 $b.queryBuilder({
503 filters: filters,
504 rules: empty_rules
505 });
506
507 assert.rulesMatch(
508 $b.queryBuilder('getRules'),
509 empty_rules,
510 'Should allow empty value for all filters'
511 );
512 });
513
514
515 var validation_filters = [{
516 id: 'radio',
517 input: 'radio',
518 values: ['one', 'two']
519 }, {
520 id: 'checkbox',
521 input: 'checkbox',
522 values: ['one', 'two']
523 }, {
524 id: 'select',
525 input: 'select',
526 values: ['one', 'two'],
527 placeholder: '--',
528 placholder_value: -1
529 }, {
530 id: 'select_mult',
531 input: 'select',
532 multiple: true,
533 values: ['one', 'two']
534 }, {
535 id: 'string'
536 }, {
537 id: 'string_val',
538 validation: {
539 min: '4', max: '5',
540 format: '^[a-z]?$'
541 }
542 }, {
543 id: 'textarea',
544 input: 'textarea'
545 }, {
546 id: 'integer',
547 type: 'integer',
548 validation: {
549 min: -10, max: 10
550 }
551 }, {
552 id: 'double',
553 type: 'double',
554 validation: {
555 step: 0.1
556 }
557 }, {
558 id: 'date',
559 type: 'date',
560 validation: {
561 format: 'YYYY/MM/DD'
562 }
563 }, {
564 id: 'time',
565 type: 'time',
566 validation: {
567 format: 'HH:ss',
568 min: '08:00',
569 max: '17:00'
570 }
571 }, {
572 id: 'boolean',
573 type: 'boolean'
574 }, {
575 id: 'custom',
576 type: 'string',
577 validation: {
578 callback: function(value, rule) {
579 if (value == null || !value.length) {
580 return 'you_fool';
581 }
582 }
583 }
584 }];
585
586 var empty_rules = {
587 condition: 'AND',
588 rules: [{
589 id: 'name',
590 operator: 'equal',
591 value: ''
592 }, {
593 id: 'category',
594 operator: 'equal',
595 value: []
596 }, {
597 id: 'in_stock',
598 operator: 'equal',
599 value: undefined
600 }, {
601 id: 'price',
602 operator: 'equal',
603 value: ''
604 }]
605 };
606});