Debian Codegraph @

tests/qunit/resources/mediawiki.rcfilters/dm.FiltersViewModel.test.js

raw symbols SCIP
1 /* eslint-disable camelcase */
2 ( function () {
3 const rcfilters = require( 'mediawiki.rcfilters.filters.ui' );
4 const filterDefinition = [ {
5 name: 'group1',
6 type: 'send_unselected_if_any',
7 filters: [
8 {
9 name: 'filter1', label: 'group1filter1-label', description: 'group1filter1-desc',
10 default: true,
11 cssClass: 'filter1class',
12 conflicts: [ { group: 'group2' } ],
13 subset: [
14 {
15 group: 'group1',
16 filter: 'filter2'
17 },
18 {
19 group: 'group1',
20 filter: 'filter3'
21 }
22 ]
23 },
24 {
25 name: 'filter2', label: 'group1filter2-label', description: 'group1filter2-desc',
26 conflicts: [ { group: 'group2', filter: 'filter6' } ],
27 cssClass: 'filter2class',
28 subset: [
29 {
30 group: 'group1',
31 filter: 'filter3'
32 }
33 ]
34 },
35 // NOTE: This filter has no highlight!
36 { name: 'filter3', label: 'group1filter3-label', description: 'group1filter3-desc', default: true }
37 ]
38 }, {
39 name: 'group2',
40 type: 'send_unselected_if_any',
41 fullCoverage: true,
42 conflicts: [ { group: 'group1', filter: 'filter1' } ],
43 filters: [
44 { name: 'filter4', label: 'group2filter4-label', description: 'group2filter4-desc', cssClass: 'filter4class' },
45 { name: 'filter5', label: 'group2filter5-label', description: 'group2filter5-desc', default: true, cssClass: 'filter5class' },
46 {
47 name: 'filter6', label: 'group2filter6-label', description: 'group2filter6-desc', cssClass: 'filter6class',
48 conflicts: [ { group: 'group1', filter: 'filter2' } ]
49 }
50 ]
51 }, {
52 name: 'group3',
53 type: 'string_options',
54 separator: ',',
55 default: 'filter8',
56 filters: [
57 { name: 'filter7', label: 'group3filter7-label', description: 'group3filter7-desc', cssClass: 'filter7class' },
58 { name: 'filter8', label: 'group3filter8-label', description: 'group3filter8-desc', cssClass: 'filter8class' },
59 { name: 'filter9', label: 'group3filter9-label', description: 'group3filter9-desc', cssClass: 'filter9class' }
60 ]
61 }, {
62 name: 'group4',
63 type: 'single_option',
64 hidden: true,
65 default: 'option2',
66 filters: [
67 // NOTE: The entire group has no highlight supported
68 { name: 'option1', label: 'group4option1-label', description: 'group4option1-desc' },
69 { name: 'option2', label: 'group4option2-label', description: 'group4option2-desc' },
70 { name: 'option3', label: 'group4option3-label', description: 'group4option3-desc' }
71 ]
72 }, {
73 name: 'group5',
74 type: 'single_option',
75 filters: [
76 { name: 'option1', label: 'group5option1-label', description: 'group5option1-desc', cssClass: 'group5opt1class' },
77 { name: 'option2', label: 'group5option2-label', description: 'group5option2-desc', cssClass: 'group5opt2class' },
78 { name: 'option3', label: 'group5option3-label', description: 'group5option3-desc', cssClass: 'group5opt3class' }
79 ]
80 }, {
81 name: 'group6',
82 type: 'boolean',
83 sticky: true,
84 filters: [
85 { name: 'group6option1', label: 'group6option1-label', description: 'group6option1-desc', cssClass: 'group6opt1class' },
86 { name: 'group6option2', label: 'group6option2-label', description: 'group6option2-desc', default: true, cssClass: 'group6opt2class' },
87 { name: 'group6option3', label: 'group6option3-label', description: 'group6option3-desc', default: true, cssClass: 'group6opt3class' }
88 ]
89 }, {
90 name: 'group7',
91 type: 'single_option',
92 sticky: true,
93 default: 'group7option2',
94 filters: [
95 { name: 'group7option1', label: 'group7option1-label', description: 'group7option1-desc', cssClass: 'group7opt1class' },
96 { name: 'group7option2', label: 'group7option2-label', description: 'group7option2-desc', cssClass: 'group7opt2class' },
97 { name: 'group7option3', label: 'group7option3-label', description: 'group7option3-desc', cssClass: 'group7opt3class' }
98 ]
99 } ],
100 shortFilterDefinition = [ {
101 name: 'group1',
102 type: 'send_unselected_if_any',
103 filters: [ { name: 'filter1' }, { name: 'filter2' } ]
104 }, {
105 name: 'group2',
106 type: 'boolean',
107 hidden: true,
108 filters: [ { name: 'filter3' }, { name: 'filter4' } ]
109 }, {
110 name: 'group3',
111 type: 'string_options',
112 sticky: true,
113 default: 'filter6',
114 filters: [ { name: 'filter5' }, { name: 'filter6' }, { name: 'filter7' } ]
115 } ],
116 viewsDefinition = {
117 namespaces: {
118 label: 'Namespaces',
119 trigger: ':',
120 groups: [ {
121 name: 'namespace',
122 label: 'Namespaces',
123 type: 'string_options',
124 separator: ';',
125 filters: [
126 { name: 0, label: 'Main', cssClass: 'namespace-0' },
127 { name: 1, label: 'Talk', cssClass: 'namespace-1' },
128 { name: 2, label: 'User', cssClass: 'namespace-2' },
129 { name: 3, label: 'User talk', cssClass: 'namespace-3' }
130 ]
131 } ]
132 }
133 },
134 defaultParameters = {
135 filter1: '1',
136 filter2: '0',
137 filter3: '1',
138 filter4: '0',
139 filter5: '1',
140 filter6: '0',
141 group3: 'filter8',
142 group4: 'option2',
143 group5: 'option1',
144 namespace: ''
145 },
146 baseParamRepresentation = {
147 filter1: '0',
148 filter2: '0',
149 filter3: '0',
150 filter4: '0',
151 filter5: '0',
152 filter6: '0',
153 group3: '',
154 group4: 'option2',
155 group5: 'option1',
156 group6option1: '0',
157 group6option2: '1',
158 group6option3: '1',
159 group7: 'group7option2',
160 namespace: ''
161 },
162 emptyParamRepresentation = {
163 filter1: '0',
164 filter2: '0',
165 filter3: '0',
166 filter4: '0',
167 filter5: '0',
168 filter6: '0',
169 group3: '',
170 group4: '',
171 group5: '',
172 group6option1: '0',
173 group6option2: '0',
174 group6option3: '0',
175 group7: '',
176 namespace: '',
177 // Null highlights
178 group1__filter1_color: null,
179 group1__filter2_color: null,
180 // group1__filter3_color: null, // Highlight isn't supported
181 group2__filter4_color: null,
182 group2__filter5_color: null,
183 group2__filter6_color: null,
184 group3__filter7_color: null,
185 group3__filter8_color: null,
186 group3__filter9_color: null,
187 // group4__option1_color: null, // Highlight isn't supported
188 // group4__option2_color: null, // Highlight isn't supported
189 // group4__option3_color: null, // Highlight isn't supported
190 group5__option1_color: null,
191 group5__option2_color: null,
192 group5__option3_color: null,
193 group6__group6option1_color: null,
194 group6__group6option2_color: null,
195 group6__group6option3_color: null,
196 group7__group7option1_color: null,
197 group7__group7option2_color: null,
198 group7__group7option3_color: null,
199 namespace__0_color: null,
200 namespace__1_color: null,
201 namespace__2_color: null,
202 namespace__3_color: null
203 },
204 baseFilterRepresentation = {
205 group1__filter1: false,
206 group1__filter2: false,
207 group1__filter3: false,
208 group2__filter4: false,
209 group2__filter5: false,
210 group2__filter6: false,
211 group3__filter7: false,
212 group3__filter8: false,
213 group3__filter9: false,
214 // The 'single_value' type of group can't have empty value; it's either
215 // the default given or the first item that will get the truthy value
216 group4__option1: false,
217 group4__option2: true, // Default
218 group4__option3: false,
219 group5__option1: true, // No default set, first item is default value
220 group5__option2: false,
221 group5__option3: false,
222 group6__group6option1: false,
223 group6__group6option2: true,
224 group6__group6option3: true,
225 group7__group7option1: false,
226 group7__group7option2: true,
227 group7__group7option3: false,
228 namespace__0: false,
229 namespace__1: false,
230 namespace__2: false,
231 namespace__3: false
232 },
233 baseFullFilterState = {
234 group1__filter1: { selected: false, conflicted: false, included: false },
235 group1__filter2: { selected: false, conflicted: false, included: false },
236 group1__filter3: { selected: false, conflicted: false, included: false },
237 group2__filter4: { selected: false, conflicted: false, included: false },
238 group2__filter5: { selected: false, conflicted: false, included: false },
239 group2__filter6: { selected: false, conflicted: false, included: false },
240 group3__filter7: { selected: false, conflicted: false, included: false },
241 group3__filter8: { selected: false, conflicted: false, included: false },
242 group3__filter9: { selected: false, conflicted: false, included: false },
243 group4__option1: { selected: false, conflicted: false, included: false },
244 group4__option2: { selected: true, conflicted: false, included: false },
245 group4__option3: { selected: false, conflicted: false, included: false },
246 group5__option1: { selected: true, conflicted: false, included: false },
247 group5__option2: { selected: false, conflicted: false, included: false },
248 group5__option3: { selected: false, conflicted: false, included: false },
249 group6__group6option1: { selected: false, conflicted: false, included: false },
250 group6__group6option2: { selected: true, conflicted: false, included: false },
251 group6__group6option3: { selected: true, conflicted: false, included: false },
252 group7__group7option1: { selected: false, conflicted: false, included: false },
253 group7__group7option2: { selected: true, conflicted: false, included: false },
254 group7__group7option3: { selected: false, conflicted: false, included: false },
255 namespace__0: { selected: false, conflicted: false, included: false },
256 namespace__1: { selected: false, conflicted: false, included: false },
257 namespace__2: { selected: false, conflicted: false, included: false },
258 namespace__3: { selected: false, conflicted: false, included: false }
259 };
260
261 QUnit.module( 'mediawiki.rcfilters - FiltersViewModel', QUnit.newMwEnvironment( {
262 messages: {
263 'group1filter1-label': 'Group 1: Filter 1 title',
264 'group1filter1-desc': 'Description of Filter 1 in Group 1',
265 'group1filter2-label': 'Group 1: Filter 2 title',
266 'group1filter2-desc': 'Description of Filter 2 in Group 1',
267 'group1filter3-label': 'Group 1: Filter 3',
268 'group1filter3-desc': 'Description of Filter 3 in Group 1',
269
270 'group2filter4-label': 'Group 2: Filter 4 title',
271 'group2filter4-desc': 'Description of Filter 4 in Group 2',
272 'group2filter5-label': 'Group 2: Filter 5',
273 'group2filter5-desc': 'Description of Filter 5 in Group 2',
274 'group2filter6-label': 'xGroup 2: Filter 6',
275 'group2filter6-desc': 'Description of Filter 6 in Group 2'
276 }
277 } ) );
278
279 QUnit.test( 'Setting up filters', ( assert ) => {
280 const model = new rcfilters.dm.FiltersViewModel();
281
282 model.initializeFilters( filterDefinition, viewsDefinition );
283
284 // Test that all items were created
285 assert.true(
286 Object.keys( baseFilterRepresentation ).every( ( filterName ) => model.getItemByName( filterName ) instanceof rcfilters.dm.FilterItem ),
287 'Filters instantiated and stored correctly'
288 );
289
290 assert.deepEqual(
291 model.getSelectedState(),
292 baseFilterRepresentation,
293 'Initial state of filters'
294 );
295
296 model.toggleFiltersSelected( {
297 group1__filter1: true,
298 group2__filter5: true,
299 group3__filter7: true
300 } );
301 assert.deepEqual(
302 model.getSelectedState(),
303 $.extend( true, {}, baseFilterRepresentation, {
304 group1__filter1: true,
305 group2__filter5: true,
306 group3__filter7: true
307 } ),
308 'Updating filter states correctly'
309 );
310 } );
311
312 QUnit.test( 'Default filters', ( assert ) => {
313 const model = new rcfilters.dm.FiltersViewModel();
314
315 model.initializeFilters( filterDefinition, viewsDefinition );
316
317 // Empty query = only default values
318 assert.deepEqual(
319 model.getDefaultParams(),
320 defaultParameters,
321 'Default parameters are stored properly per filter and group (sticky groups are ignored)'
322 );
323 } );
324
325 QUnit.test( 'Parameter minimal state', ( assert ) => {
326 const model = new rcfilters.dm.FiltersViewModel(),
327 cases = [
328 {
329 input: {},
330 result: {},
331 msg: 'Empty parameter representation produces an empty result'
332 },
333 {
334 input: {
335 filter1: '1',
336 filter2: '0',
337 filter3: '0',
338 group3: '',
339 group4: 'option2'
340 },
341 result: {
342 filter1: '1',
343 group4: 'option2'
344 },
345 msg: 'Mixed input results in only non-falsey values as result'
346 },
347 {
348 input: {
349 filter1: '0',
350 filter2: '0',
351 filter3: '0',
352 group3: '',
353 group4: '',
354 group1__filter1_color: null
355 },
356 result: {},
357 msg: 'An all-falsey input results in an empty result.'
358 },
359 {
360 input: {
361 filter1: '0',
362 filter2: '0',
363 filter3: '0',
364 group3: '',
365 group4: '',
366 group1__filter1_color: 'c1'
367 },
368 result: {
369 group1__filter1_color: 'c1'
370 },
371 msg: 'An all-falsey input with highlight params result in only the highlight param.'
372 },
373 {
374 input: {
375 group1__filter1_color: 'c1',
376 group1__filter3_color: 'c3' // Not supporting highlights
377 },
378 result: {
379 group1__filter1_color: 'c1'
380 },
381 msg: 'Unsupported highlights are removed.'
382 }
383 ];
384
385 model.initializeFilters( filterDefinition, viewsDefinition );
386
387 cases.forEach( ( test ) => {
388 assert.deepEqual(
389 model.getMinimizedParamRepresentation( test.input ),
390 test.result,
391 test.msg
392 );
393 } );
394 } );
395
396 QUnit.test( 'Parameter states', ( assert ) => {
397 // Some groups / params have their defaults immediately applied
398 // to their state. These include single_option which can never
399 // be empty, etc. These are these states:
400 const appliedDefaultParameters = {
401 group4: 'option2',
402 group5: 'option1',
403 // Sticky, their defaults apply immediately
404 group6option2: '1',
405 group6option3: '1',
406 group7: 'group7option2'
407 };
408 let model = new rcfilters.dm.FiltersViewModel();
409
410 model.initializeFilters( filterDefinition, viewsDefinition );
411 assert.deepEqual(
412 model.getEmptyParameterState(),
413 emptyParamRepresentation,
414 'Producing an empty parameter state'
415 );
416
417 model.toggleFiltersSelected( {
418 group1__filter1: true,
419 group3__filter7: true
420 } );
421
422 assert.deepEqual(
423 model.getCurrentParameterState(),
424 // appliedDefaultParams applies the default value to parameters
425 // who must have an initial value to begin with, so we have to
426 // take it into account in the current state
427 $.extend( true, {}, appliedDefaultParameters, {
428 filter2: '1',
429 filter3: '1',
430 group3: 'filter7'
431 } ),
432 'Producing a current parameter state'
433 );
434
435 // Reset
436 model = new rcfilters.dm.FiltersViewModel();
437 model.initializeFilters( filterDefinition, viewsDefinition );
438
439 const parametersWithoutExcluded = $.extend( true, {}, appliedDefaultParameters );
440 delete parametersWithoutExcluded.group7;
441 delete parametersWithoutExcluded.group6option2;
442 delete parametersWithoutExcluded.group6option3;
443
444 assert.deepEqual(
445 model.getCurrentParameterState( true ),
446 parametersWithoutExcluded,
447 'Producing a current clean parameter state without excluded filters'
448 );
449 } );
450
451 QUnit.test( 'Cleaning up parameter states', ( assert ) => {
452 const model = new rcfilters.dm.FiltersViewModel(),
453 cases = [
454 {
455 input: {},
456 result: {},
457 msg: 'Empty parameter representation produces an empty result'
458 },
459 {
460 input: {
461 filter1: '1', // Regular (do not strip)
462 group6option1: '1' // Sticky
463 },
464 result: { filter1: '1' },
465 msg: 'Valid input strips all sticky params regardless of value'
466 }
467 ];
468
469 model.initializeFilters( filterDefinition, viewsDefinition );
470
471 cases.forEach( ( test ) => {
472 assert.deepEqual(
473 model.removeStickyParams( test.input ),
474 test.result,
475 test.msg
476 );
477 } );
478
479 } );
480
481 QUnit.test( 'Finding matching filters', ( assert ) => {
482 const testCases = [
483 {
484 query: 'group',
485 expectedMatches: {
486 group1: [ 'group1__filter1', 'group1__filter2', 'group1__filter3' ],
487 group2: [ 'group2__filter4', 'group2__filter5' ]
488 },
489 reason: 'Finds filters starting with the query string'
490 },
491 {
492 query: 'in Group 2',
493 expectedMatches: {
494 group2: [ 'group2__filter4', 'group2__filter5', 'group2__filter6' ]
495 },
496 reason: 'Finds filters containing the query string in their description'
497 },
498 {
499 query: 'title',
500 expectedMatches: {
501 group1: [ 'group1__filter1', 'group1__filter2' ],
502 group2: [ 'group2__filter4' ]
503 },
504 reason: 'Finds filters containing the query string in their group title'
505 },
506 {
507 query: ':Main',
508 expectedMatches: {
509 namespace: [ 'namespace__0' ]
510 },
511 reason: 'Finds item in view when a prefix is used'
512 },
513 {
514 query: ':group',
515 expectedMatches: {},
516 reason: 'Finds no results if using namespaces prefix (:) to search for filter title'
517 }
518 ],
519 model = new rcfilters.dm.FiltersViewModel(),
520 extractNames = function ( matches ) {
521 const result = {};
522 Object.keys( matches ).forEach( ( groupName ) => {
523 result[ groupName ] = matches[ groupName ].map( ( item ) => item.getName() );
524 } );
525 return result;
526 };
527
528 model.initializeFilters( filterDefinition, viewsDefinition );
529
530 testCases.forEach( ( testCase ) => {
531 const matches = model.findMatches( testCase.query );
532 assert.deepEqual(
533 extractNames( matches ),
534 testCase.expectedMatches,
535 testCase.reason
536 );
537 } );
538
539 const foundMatches = model.findMatches( 'foo' );
540 assert.true(
541 $.isEmptyObject( foundMatches ),
542 'findMatches returns an empty object when no results found'
543 );
544 } );
545
546 QUnit.test( 'getParametersFromFilters', ( assert ) => {
547 let model = new rcfilters.dm.FiltersViewModel();
548
549 model.initializeFilters( filterDefinition, viewsDefinition );
550
551 // Starting with all filters unselected
552 assert.deepEqual(
553 model.getParametersFromFilters(),
554 baseParamRepresentation,
555 'Unselected filters return all parameters falsey or \'\'.'
556 );
557
558 // Select 1 filter
559 model.toggleFiltersSelected( {
560 group1__filter1: true
561 } );
562 // Only one filter in one group
563 assert.deepEqual(
564 model.getParametersFromFilters(),
565 $.extend( true, {}, baseParamRepresentation, {
566 // Group 1 (one selected, the others are true)
567 filter2: '1',
568 filter3: '1'
569 } ),
570 'One filter in one "send_unselected_if_any" group returns the other parameters truthy.'
571 );
572
573 // Select 2 filters
574 model.toggleFiltersSelected( {
575 group1__filter1: true,
576 group1__filter2: true
577 } );
578 // Two selected filters in one group
579 assert.deepEqual(
580 model.getParametersFromFilters(),
581 $.extend( true, {}, baseParamRepresentation, {
582 // Group 1 (two selected, the other is true)
583 filter3: '1'
584 } ),
585 'Two filters in one "send_unselected_if_any" group returns the other parameters truthy.'
586 );
587
588 // Select 3 filters
589 model.toggleFiltersSelected( {
590 group1__filter1: true,
591 group1__filter2: true,
592 group1__filter3: true
593 } );
594 // All filters of the group are selected == this is the same as not selecting any
595 assert.deepEqual(
596 model.getParametersFromFilters(),
597 baseParamRepresentation,
598 'All filters selected in one "send_unselected_if_any" group returns all parameters falsy.'
599 );
600
601 // Select 1 filter from string_options
602 model.toggleFiltersSelected( {
603 group3__filter7: true,
604 group3__filter8: false,
605 group3__filter9: false
606 } );
607 // All filters of the group are selected == this is the same as not selecting any
608 assert.deepEqual(
609 model.getParametersFromFilters(),
610 $.extend( true, {}, baseParamRepresentation, {
611 group3: 'filter7'
612 } ),
613 'One filter selected in "string_option" group returns that filter in the value.'
614 );
615
616 // Select 2 filters from string_options
617 model.toggleFiltersSelected( {
618 group3__filter7: true,
619 group3__filter8: true,
620 group3__filter9: false
621 } );
622 // All filters of the group are selected == this is the same as not selecting any
623 assert.deepEqual(
624 model.getParametersFromFilters(),
625 $.extend( true, {}, baseParamRepresentation, {
626 group3: 'filter7,filter8'
627 } ),
628 'Two filters selected in "string_option" group returns those filters in the value.'
629 );
630
631 // Select 3 filters from string_options
632 model.toggleFiltersSelected( {
633 group3__filter7: true,
634 group3__filter8: true,
635 group3__filter9: true
636 } );
637 // All filters of the group are selected == this is the same as not selecting any
638 assert.deepEqual(
639 model.getParametersFromFilters(),
640 $.extend( true, {}, baseParamRepresentation, {
641 group3: 'all'
642 } ),
643 'All filters selected in "string_option" group returns \'all\'.'
644 );
645
646 // Reset
647 model = new rcfilters.dm.FiltersViewModel();
648 model.initializeFilters( filterDefinition, viewsDefinition );
649
650 // Select an option from single_option group
651 model.toggleFiltersSelected( {
652 group4__option2: true
653 } );
654 // All filters of the group are selected == this is the same as not selecting any
655 assert.deepEqual(
656 model.getParametersFromFilters(),
657 $.extend( true, {}, baseParamRepresentation, {
658 group4: 'option2'
659 } ),
660 'Selecting an option from "single_option" group returns that option as a value.'
661 );
662
663 // Select a different option from single_option group
664 model.toggleFiltersSelected( {
665 group4__option3: true
666 } );
667 // All filters of the group are selected == this is the same as not selecting any
668 assert.deepEqual(
669 model.getParametersFromFilters(),
670 $.extend( true, {}, baseParamRepresentation, {
671 group4: 'option3'
672 } ),
673 'Selecting a different option from "single_option" group changes the selection.'
674 );
675 } );
676
677 QUnit.test( 'getParametersFromFilters (custom object)', ( assert ) => {
678 // This entire test uses different base definition than the global one
679 // on purpose, to verify that the values inserted as a custom object
680 // are the ones we expect in return
681 const model = new rcfilters.dm.FiltersViewModel(),
682 definition = [ {
683 name: 'group1',
684 title: 'Group 1',
685 type: 'send_unselected_if_any',
686 filters: [
687 { name: 'hidefilter1', label: 'Hide filter 1', description: '' },
688 { name: 'hidefilter2', label: 'Hide filter 2', description: '' },
689 { name: 'hidefilter3', label: 'Hide filter 3', description: '' }
690 ]
691 }, {
692 name: 'group2',
693 title: 'Group 2',
694 type: 'send_unselected_if_any',
695 filters: [
696 { name: 'hidefilter4', label: 'Hide filter 4', description: '' },
697 { name: 'hidefilter5', label: 'Hide filter 5', description: '' },
698 { name: 'hidefilter6', label: 'Hide filter 6', description: '' }
699 ]
700 }, {
701 name: 'group3',
702 title: 'Group 3',
703 type: 'string_options',
704 separator: ',',
705 filters: [
706 { name: 'filter7', label: 'Hide filter 7', description: '' },
707 { name: 'filter8', label: 'Hide filter 8', description: '' },
708 { name: 'filter9', label: 'Hide filter 9', description: '' }
709 ]
710 }, {
711 name: 'group4',
712 title: 'Group 4',
713 type: 'single_option',
714 filters: [
715 { name: 'filter10', label: 'Hide filter 10', description: '' },
716 { name: 'filter11', label: 'Hide filter 11', description: '' },
717 { name: 'filter12', label: 'Hide filter 12', description: '' }
718 ]
719 } ],
720 baseResult = {
721 hidefilter1: '0',
722 hidefilter2: '0',
723 hidefilter3: '0',
724 hidefilter4: '0',
725 hidefilter5: '0',
726 hidefilter6: '0',
727 group3: '',
728 group4: ''
729 },
730 cases = [
731 {
732 // This is mocking the cases above, both
733 // - 'Two filters in one "send_unselected_if_any" group returns the other parameters truthy.'
734 // - 'Two filters selected in "string_option" group returns those filters in the value.'
735 input: {
736 group1__hidefilter1: true,
737 group1__hidefilter2: true,
738 group1__hidefilter3: false,
739 group2__hidefilter4: false,
740 group2__hidefilter5: false,
741 group2__hidefilter6: false,
742 group3__filter7: true,
743 group3__filter8: true,
744 group3__filter9: false
745 },
746 expected: $.extend( true, {}, baseResult, {
747 // Group 1 (two selected, the others are true)
748 hidefilter3: '1',
749 // Group 3 (two selected)
750 group3: 'filter7,filter8'
751 } ),
752 msg: 'Given an explicit (complete) filter state object, the result is the same as if the object given represented the model state.'
753 },
754 {
755 // This is mocking case above
756 // - 'One filter in one "send_unselected_if_any" group returns the other parameters truthy.'
757 input: {
758 group1__hidefilter1: 1
759 },
760 expected: $.extend( true, {}, baseResult, {
761 // Group 1 (one selected, the others are true)
762 hidefilter2: '1',
763 hidefilter3: '1'
764 } ),
765 msg: 'Given an explicit (incomplete) filter state object, the result is the same as if the object give represented the model state.'
766 },
767 {
768 input: {
769 group4__filter10: true
770 },
771 expected: $.extend( true, {}, baseResult, {
772 group4: 'filter10'
773 } ),
774 msg: 'Given a single value for "single_option" that option is represented in the result.'
775 },
776 {
777 input: {
778 group4__filter10: true,
779 group4__filter11: true
780 },
781 expected: $.extend( true, {}, baseResult, {
782 group4: 'filter10'
783 } ),
784 msg: 'Given more than one true value for "single_option" (which should not happen!) only the first value counts, and the second is ignored.'
785 },
786 {
787 input: {},
788 expected: baseResult,
789 msg: 'Given an explicit empty object, the result is all filters set to their falsey unselected value.'
790 }
791 ];
792
793 model.initializeFilters( definition );
794 // Store original state
795 const originalState = model.getSelectedState();
796
797 // Test each case
798 cases.forEach( ( test ) => {
799 assert.deepEqual(
800 model.getParametersFromFilters( test.input ),
801 test.expected,
802 test.msg
803 );
804 } );
805
806 // After doing the above tests, make sure the actual state
807 // of the filter stayed the same
808 assert.deepEqual(
809 model.getSelectedState(),
810 originalState,
811 'Running the method with external definition to parse does not actually change the state of the model'
812 );
813 } );
814
815 QUnit.test( 'getFiltersFromParameters', ( assert ) => {
816 let model = new rcfilters.dm.FiltersViewModel();
817
818 model.initializeFilters( filterDefinition, viewsDefinition );
819
820 // Empty query = only default values
821 assert.deepEqual(
822 model.getFiltersFromParameters( {} ),
823 baseFilterRepresentation,
824 'Empty parameter query results in an object representing all filters set to their base state'
825 );
826
827 assert.deepEqual(
828 model.getFiltersFromParameters( {
829 filter2: '1'
830 } ),
831 Object.assign( {}, baseFilterRepresentation, {
832 group1__filter1: true, // The text is "show filter 1"
833 group1__filter2: false, // The text is "show filter 2"
834 group1__filter3: true // The text is "show filter 3"
835 } ),
836 'One truthy parameter in a group whose other parameters are true by default makes the rest of the filters in the group false (unchecked)'
837 );
838
839 assert.deepEqual(
840 model.getFiltersFromParameters( {
841 filter1: '1',
842 filter2: '1',
843 filter3: '1'
844 } ),
845 Object.assign( {}, baseFilterRepresentation, {
846 group1__filter1: false, // The text is "show filter 1"
847 group1__filter2: false, // The text is "show filter 2"
848 group1__filter3: false // The text is "show filter 3"
849 } ),
850 'All paremeters in the same \'send_unselected_if_any\' group false is equivalent to none are truthy (checked) in the interface'
851 );
852
853 // The ones above don't update the model, so we have a clean state.
854 // getFiltersFromParameters is stateless; any change is unaffected by the current state
855 // This test is demonstrating wrong usage of the method;
856 // We should be aware that getFiltersFromParameters is stateless,
857 // so each call gives us a filter state that only reflects the query given.
858 // This means that the two calls to toggleFiltersSelected() below collide.
859 // The result of the first is overridden by the result of the second,
860 // since both get a full state object from getFiltersFromParameters that **only** relates
861 // to the input it receives.
862 model.toggleFiltersSelected(
863 model.getFiltersFromParameters( {
864 filter1: '1'
865 } )
866 );
867
868 model.toggleFiltersSelected(
869 model.getFiltersFromParameters( {
870 filter6: '1'
871 } )
872 );
873
874 // The result here is ignoring the first toggleFiltersSelected call
875 assert.deepEqual(
876 model.getSelectedState(),
877 Object.assign( {}, baseFilterRepresentation, {
878 group2__filter4: true,
879 group2__filter5: true,
880 group2__filter6: false
881 } ),
882 'getFiltersFromParameters does not care about previous or existing state.'
883 );
884
885 // Reset
886 model = new rcfilters.dm.FiltersViewModel();
887 model.initializeFilters( filterDefinition, viewsDefinition );
888
889 model.toggleFiltersSelected(
890 model.getFiltersFromParameters( {
891 group3: 'filter7'
892 } )
893 );
894 assert.deepEqual(
895 model.getSelectedState(),
896 Object.assign( {}, baseFilterRepresentation, {
897 group3__filter7: true,
898 group3__filter8: false,
899 group3__filter9: false
900 } ),
901 'A \'string_options\' parameter containing 1 value, results in the corresponding filter as checked'
902 );
903
904 model.toggleFiltersSelected(
905 model.getFiltersFromParameters( {
906 group3: 'filter7,filter8'
907 } )
908 );
909 assert.deepEqual(
910 model.getSelectedState(),
911 Object.assign( {}, baseFilterRepresentation, {
912 group3__filter7: true,
913 group3__filter8: true,
914 group3__filter9: false
915 } ),
916 'A \'string_options\' parameter containing 2 values, results in both corresponding filters as checked'
917 );
918
919 model.toggleFiltersSelected(
920 model.getFiltersFromParameters( {
921 group3: 'filter7,filter8,filter9'
922 } )
923 );
924 assert.deepEqual(
925 model.getSelectedState(),
926 Object.assign( {}, baseFilterRepresentation, {
927 group3__filter7: true,
928 group3__filter8: true,
929 group3__filter9: true
930 } ),
931 'A \'string_options\' parameter containing all values, results in all filters of the group as checked.'
932 );
933
934 model.toggleFiltersSelected(
935 model.getFiltersFromParameters( {
936 group3: 'filter7,all,filter9'
937 } )
938 );
939 assert.deepEqual(
940 model.getSelectedState(),
941 Object.assign( {}, baseFilterRepresentation, {
942 group3__filter7: true,
943 group3__filter8: true,
944 group3__filter9: true
945 } ),
946 'A \'string_options\' parameter containing the value \'all\', results in all filters of the group as checked.'
947 );
948
949 model.toggleFiltersSelected(
950 model.getFiltersFromParameters( {
951 group3: 'filter7,foo,filter9'
952 } )
953 );
954 assert.deepEqual(
955 model.getSelectedState(),
956 Object.assign( {}, baseFilterRepresentation, {
957 group3__filter7: true,
958 group3__filter8: false,
959 group3__filter9: true
960 } ),
961 'A \'string_options\' parameter containing an invalid value, results in the invalid value ignored and the valid corresponding filters checked.'
962 );
963
964 model.toggleFiltersSelected(
965 model.getFiltersFromParameters( {
966 group4: 'option1'
967 } )
968 );
969 assert.deepEqual(
970 model.getSelectedState(),
971 Object.assign( {}, baseFilterRepresentation, {
972 group4__option1: true,
973 group4__option2: false
974 } ),
975 'A \'single_option\' parameter reflects a single selected value.'
976 );
977
978 assert.deepEqual(
979 model.getFiltersFromParameters( {
980 group4: 'option1,option2'
981 } ),
982 baseFilterRepresentation,
983 'An invalid \'single_option\' parameter is ignored.'
984 );
985
986 // Change to one value
987 model.toggleFiltersSelected(
988 model.getFiltersFromParameters( {
989 group4: 'option1'
990 } )
991 );
992 // Change again to another value
993 model.toggleFiltersSelected(
994 model.getFiltersFromParameters( {
995 group4: 'option2'
996 } )
997 );
998 assert.deepEqual(
999 model.getSelectedState(),
1000 Object.assign( {}, baseFilterRepresentation, {
1001 group4__option2: true
1002 } ),
1003 'A \'single_option\' parameter always reflects the latest selected value.'
1004 );
1005 } );
1006
1007 QUnit.test( 'sanitizeStringOptionGroup', ( assert ) => {
1008 const model = new rcfilters.dm.FiltersViewModel();
1009
1010 model.initializeFilters( filterDefinition, viewsDefinition );
1011
1012 assert.deepEqual(
1013 model.sanitizeStringOptionGroup( 'group1', [ 'filter1', 'filter1', 'filter2' ] ),
1014 [ 'filter1', 'filter2' ],
1015 'Remove duplicate values'
1016 );
1017
1018 assert.deepEqual(
1019 model.sanitizeStringOptionGroup( 'group1', [ 'filter1', 'foo', 'filter2' ] ),
1020 [ 'filter1', 'filter2' ],
1021 'Remove invalid values'
1022 );
1023
1024 assert.deepEqual(
1025 model.sanitizeStringOptionGroup( 'group1', [ 'filter1', 'all', 'filter2' ] ),
1026 [ 'all' ],
1027 'If any value is "all", the only value is "all".'
1028 );
1029 } );
1030
1031 QUnit.test( 'Filter interaction: subsets', ( assert ) => {
1032 const model = new rcfilters.dm.FiltersViewModel();
1033
1034 model.initializeFilters( filterDefinition, viewsDefinition );
1035
1036 // Select a filter that has subset with another filter
1037 model.toggleFiltersSelected( {
1038 group1__filter1: true
1039 } );
1040
1041 model.reassessFilterInteractions( model.getItemByName( 'group1__filter1' ) );
1042 assert.deepEqual(
1043 model.getFullState(),
1044 $.extend( true, {}, baseFullFilterState, {
1045 group1__filter1: { selected: true },
1046 group1__filter2: { included: true },
1047 group1__filter3: { included: true },
1048 // Conflicts are affected
1049 group2__filter4: { conflicted: true },
1050 group2__filter5: { conflicted: true },
1051 group2__filter6: { conflicted: true }
1052 } ),
1053 'Filters with subsets are represented in the model.'
1054 );
1055
1056 // Select another filter that has a subset with the same previous filter
1057 model.toggleFiltersSelected( {
1058 group1__filter2: true
1059 } );
1060 model.reassessFilterInteractions( model.getItemByName( 'filter2' ) );
1061 assert.deepEqual(
1062 model.getFullState(),
1063 $.extend( true, {}, baseFullFilterState, {
1064 group1__filter1: { selected: true },
1065 group1__filter2: { selected: true, included: true },
1066 group1__filter3: { included: true },
1067 // Conflicts are affected
1068 group2__filter6: { conflicted: true }
1069 } ),
1070 'Filters that have multiple subsets are represented.'
1071 );
1072
1073 // Remove one filter (but leave the other) that affects filter3
1074 model.toggleFiltersSelected( {
1075 group1__filter1: false
1076 } );
1077 model.reassessFilterInteractions( model.getItemByName( 'group1__filter1' ) );
1078 assert.deepEqual(
1079 model.getFullState(),
1080 $.extend( true, {}, baseFullFilterState, {
1081 group1__filter2: { selected: true, included: false },
1082 group1__filter3: { included: true },
1083 // Conflicts are affected
1084 group2__filter6: { conflicted: true }
1085 } ),
1086 'Removing a filter only un-includes its subset if there is no other filter affecting.'
1087 );
1088
1089 model.toggleFiltersSelected( {
1090 group1__filter2: false
1091 } );
1092 model.reassessFilterInteractions( model.getItemByName( 'group1__filter2' ) );
1093 assert.deepEqual(
1094 model.getFullState(),
1095 baseFullFilterState,
1096 'Removing all supersets also un-includes the subsets.'
1097 );
1098 } );
1099
1100 QUnit.test( 'Filter interaction: full coverage', ( assert ) => {
1101 const model = new rcfilters.dm.FiltersViewModel(),
1102 isCapsuleItemMuted = function ( filterName ) {
1103 const itemModel = model.getItemByName( filterName ),
1104 groupModel = itemModel.getGroupModel();
1105
1106 // This is the logic inside the capsule widget
1107 return (
1108 // The capsule item widget only appears if the item is selected
1109 itemModel.isSelected() &&
1110 // Muted state is only valid if group is full coverage and all items are selected
1111 groupModel.isFullCoverage() && groupModel.areAllSelected()
1112 );
1113 },
1114 getCurrentItemsMutedState = function () {
1115 return {
1116 group1__filter1: isCapsuleItemMuted( 'group1__filter1' ),
1117 group1__filter2: isCapsuleItemMuted( 'group1__filter2' ),
1118 group1__filter3: isCapsuleItemMuted( 'group1__filter3' ),
1119 group2__filter4: isCapsuleItemMuted( 'group2__filter4' ),
1120 group2__filter5: isCapsuleItemMuted( 'group2__filter5' ),
1121 group2__filter6: isCapsuleItemMuted( 'group2__filter6' )
1122 };
1123 },
1124 baseMuteState = {
1125 group1__filter1: false,
1126 group1__filter2: false,
1127 group1__filter3: false,
1128 group2__filter4: false,
1129 group2__filter5: false,
1130 group2__filter6: false
1131 };
1132
1133 model.initializeFilters( filterDefinition, viewsDefinition );
1134
1135 // Starting state, no selection, all items are non-muted
1136 assert.deepEqual(
1137 getCurrentItemsMutedState(),
1138 baseMuteState,
1139 'No selection - all items are non-muted'
1140 );
1141
1142 // Select most (but not all) items in each group
1143 model.toggleFiltersSelected( {
1144 group1__filter1: true,
1145 group1__filter2: true,
1146 group2__filter4: true,
1147 group2__filter5: true
1148 } );
1149
1150 // Both groups have multiple (but not all) items selected, all items are non-muted
1151 assert.deepEqual(
1152 getCurrentItemsMutedState(),
1153 baseMuteState,
1154 'Not all items in the group selected - all items are non-muted'
1155 );
1156
1157 // Select all items in 'fullCoverage' group (group2)
1158 model.toggleFiltersSelected( {
1159 group2__filter6: true
1160 } );
1161
1162 // Group2 (full coverage) has all items selected, all its items are muted
1163 assert.deepEqual(
1164 getCurrentItemsMutedState(),
1165 Object.assign( {}, baseMuteState, {
1166 group2__filter4: true,
1167 group2__filter5: true,
1168 group2__filter6: true
1169 } ),
1170 'All items in \'full coverage\' group are selected - all items in the group are muted'
1171 );
1172
1173 // Select all items in non 'fullCoverage' group (group1)
1174 model.toggleFiltersSelected( {
1175 group1__filter3: true
1176 } );
1177
1178 // Group1 (full coverage) has all items selected, no items in it are muted (non full coverage)
1179 assert.deepEqual(
1180 getCurrentItemsMutedState(),
1181 Object.assign( {}, baseMuteState, {
1182 group2__filter4: true,
1183 group2__filter5: true,
1184 group2__filter6: true
1185 } ),
1186 'All items in a non \'full coverage\' group are selected - none of the items in the group are muted'
1187 );
1188
1189 // Uncheck an item from each group
1190 model.toggleFiltersSelected( {
1191 group1__filter3: false,
1192 group2__filter5: false
1193 } );
1194 assert.deepEqual(
1195 getCurrentItemsMutedState(),
1196 baseMuteState,
1197 'Not all items in the group are checked - all items are non-muted regardless of group coverage'
1198 );
1199 } );
1200
1201 QUnit.test( 'Filter interaction: conflicts', ( assert ) => {
1202 let model = new rcfilters.dm.FiltersViewModel();
1203
1204 model.initializeFilters( filterDefinition, viewsDefinition );
1205
1206 assert.deepEqual(
1207 model.getFullState(),
1208 baseFullFilterState,
1209 'Initial state: no conflicts because no selections.'
1210 );
1211
1212 // Select a filter that has a conflict with an entire group
1213 model.toggleFiltersSelected( {
1214 group1__filter1: true // conflicts: entire of group 2 ( filter4, filter5, filter6)
1215 } );
1216
1217 model.reassessFilterInteractions( model.getItemByName( 'group1__filter1' ) );
1218
1219 assert.deepEqual(
1220 model.getFullState(),
1221 $.extend( true, {}, baseFullFilterState, {
1222 group1__filter1: { selected: true },
1223 group2__filter4: { conflicted: true },
1224 group2__filter5: { conflicted: true },
1225 group2__filter6: { conflicted: true },
1226 // Subsets are affected by the selection
1227 group1__filter2: { included: true },
1228 group1__filter3: { included: true }
1229 } ),
1230 'Selecting a filter that conflicts with a group sets all the conflicted group items as "conflicted".'
1231 );
1232
1233 // Select one of the conflicts (both filters are now conflicted and selected)
1234 model.toggleFiltersSelected( {
1235 group2__filter4: true // conflicts: filter 1
1236 } );
1237 model.reassessFilterInteractions( model.getItemByName( 'group2__filter4' ) );
1238
1239 assert.deepEqual(
1240 model.getFullState(),
1241 $.extend( true, {}, baseFullFilterState, {
1242 group1__filter1: { selected: true, conflicted: true },
1243 group2__filter4: { selected: true, conflicted: true },
1244 group2__filter5: { conflicted: true },
1245 group2__filter6: { conflicted: true },
1246 // Subsets are affected by the selection
1247 group1__filter2: { included: true },
1248 group1__filter3: { included: true }
1249 } ),
1250 'Selecting a conflicting filter inside a group, sets both sides to conflicted and selected.'
1251 );
1252
1253 // Reset
1254 model = new rcfilters.dm.FiltersViewModel();
1255 model.initializeFilters( filterDefinition, viewsDefinition );
1256
1257 // Select a filter that has a conflict with a specific filter
1258 model.toggleFiltersSelected( {
1259 group1__filter2: true // conflicts: filter6
1260 } );
1261 model.reassessFilterInteractions( model.getItemByName( 'group1__filter2' ) );
1262
1263 assert.deepEqual(
1264 model.getFullState(),
1265 $.extend( true, {}, baseFullFilterState, {
1266 group1__filter2: { selected: true },
1267 group2__filter6: { conflicted: true },
1268 // Subsets are affected by the selection
1269 group1__filter3: { included: true }
1270 } ),
1271 'Selecting a filter that conflicts with another filter sets the other as "conflicted".'
1272 );
1273
1274 // Select the conflicting filter
1275 model.toggleFiltersSelected( {
1276 group2__filter6: true // conflicts: filter2
1277 } );
1278
1279 model.reassessFilterInteractions( model.getItemByName( 'group2__filter6' ) );
1280
1281 assert.deepEqual(
1282 model.getFullState(),
1283 $.extend( true, {}, baseFullFilterState, {
1284 group1__filter2: { selected: true, conflicted: true },
1285 group2__filter6: { selected: true, conflicted: true },
1286 // This is added to the conflicts because filter6 is part of group2,
1287 // who is in conflict with filter1; note that filter2 also conflicts
1288 // with filter6 which means that filter1 conflicts with filter6 (because it's in group2)
1289 // and also because its **own sibling** (filter2) is **also** in conflict with the
1290 // selected items in group2 (filter6)
1291 group1__filter1: { conflicted: true },
1292
1293 // Subsets are affected by the selection
1294 group1__filter3: { included: true }
1295 } ),
1296 'Selecting a conflicting filter with an individual filter, sets both sides to conflicted and selected.'
1297 );
1298
1299 // Now choose a non-conflicting filter from the group
1300 model.toggleFiltersSelected( {
1301 group2__filter5: true
1302 } );
1303
1304 model.reassessFilterInteractions( model.getItemByName( 'group2__filter5' ) );
1305
1306 assert.deepEqual(
1307 model.getFullState(),
1308 $.extend( true, {}, baseFullFilterState, {
1309 group1__filter2: { selected: true },
1310 group2__filter6: { selected: true },
1311 group2__filter5: { selected: true },
1312 // Filter6 and filter1 are no longer in conflict because
1313 // filter5, while it is in conflict with filter1, it is
1314 // not in conflict with filter2 - and since filter2 is
1315 // selected, it removes the conflict bidirectionally
1316
1317 // Subsets are affected by the selection
1318 group1__filter3: { included: true }
1319 } ),
1320 'Selecting a non-conflicting filter within the group of a conflicting filter removes the conflicts.'
1321 );
1322
1323 // Followup on the previous test, unselect filter2 so filter1
1324 // is now the only one selected in its own group, and since
1325 // it is in conflict with the entire of group2, it means
1326 // filter1 is once again conflicted
1327 model.toggleFiltersSelected( {
1328 group1__filter2: false
1329 } );
1330
1331 model.reassessFilterInteractions( model.getItemByName( 'group1__filter2' ) );
1332
1333 assert.deepEqual(
1334 model.getFullState(),
1335 $.extend( true, {}, baseFullFilterState, {
1336 group1__filter1: { conflicted: true },
1337 group2__filter6: { selected: true },
1338 group2__filter5: { selected: true }
1339 } ),
1340 'Unselecting an item that did not conflict returns the conflict state.'
1341 );
1342
1343 // Followup #2: Now actually select filter1, and make everything conflicted
1344 model.toggleFiltersSelected( {
1345 group1__filter1: true
1346 } );
1347
1348 model.reassessFilterInteractions( model.getItemByName( 'group1__filter1' ) );
1349
1350 assert.deepEqual(
1351 model.getFullState(),
1352 $.extend( true, {}, baseFullFilterState, {
1353 group1__filter1: { selected: true, conflicted: true },
1354 group2__filter6: { selected: true, conflicted: true },
1355 group2__filter5: { selected: true, conflicted: true },
1356 group2__filter4: { conflicted: true }, // Not selected but conflicted because it's in group2
1357 // Subsets are affected by the selection
1358 group1__filter2: { included: true },
1359 group1__filter3: { included: true }
1360 } ),
1361 'Selecting an item that conflicts with a whole group makes all selections in that group conflicted.'
1362 );
1363
1364 /* Simple case */
1365 // Reset
1366 model = new rcfilters.dm.FiltersViewModel();
1367 model.initializeFilters( filterDefinition, viewsDefinition );
1368
1369 // Select a filter that has a conflict with a specific filter
1370 model.toggleFiltersSelected( {
1371 group1__filter2: true // conflicts: filter6
1372 } );
1373
1374 model.reassessFilterInteractions( model.getItemByName( 'group1__filter2' ) );
1375
1376 assert.deepEqual(
1377 model.getFullState(),
1378 $.extend( true, {}, baseFullFilterState, {
1379 group1__filter2: { selected: true },
1380 group2__filter6: { conflicted: true },
1381 // Subsets are affected by the selection
1382 group1__filter3: { included: true }
1383 } ),
1384 'Simple case: Selecting a filter that conflicts with another filter sets the other as "conflicted".'
1385 );
1386
1387 model.toggleFiltersSelected( {
1388 group1__filter3: true // conflicts: filter6
1389 } );
1390
1391 model.reassessFilterInteractions( model.getItemByName( 'group1__filter3' ) );
1392
1393 assert.deepEqual(
1394 model.getFullState(),
1395 $.extend( true, {}, baseFullFilterState, {
1396 group1__filter2: { selected: true },
1397 // Subsets are affected by the selection
1398 group1__filter3: { selected: true, included: true }
1399 } ),
1400 'Simple case: Selecting a filter that is not in conflict removes the conflict.'
1401 );
1402 } );
1403
1404 QUnit.test( 'Filter highlights', ( assert ) => {
1405 // We are using a different (smaller) definition here than the global one
1406 const definition = [ {
1407 name: 'group1',
1408 title: 'Group 1',
1409 type: 'string_options',
1410 filters: [
1411 { name: 'filter1', cssClass: 'class1', label: '1', description: '1' },
1412 { name: 'filter2', cssClass: 'class2', label: '2', description: '2' },
1413 { name: 'filter3', cssClass: 'class3', label: '3', description: '3' },
1414 { name: 'filter4', cssClass: 'class4', label: '4', description: '4' },
1415 { name: 'filter5', cssClass: 'class5', label: '5', description: '5' },
1416 { name: 'filter6', label: '6', description: '6' }
1417 ]
1418 } ];
1419 let model = new rcfilters.dm.FiltersViewModel();
1420
1421 model.initializeFilters( definition );
1422
1423 assert.strictEqual(
1424 model.isHighlightEnabled(),
1425 false,
1426 'Initially, highlight is disabled.'
1427 );
1428
1429 model.toggleHighlight( true );
1430 assert.strictEqual(
1431 model.isHighlightEnabled(),
1432 true,
1433 'Highlight is enabled on toggle.'
1434 );
1435
1436 model.setHighlightColor( 'group1__filter1', 'color1' );
1437 model.setHighlightColor( 'group1__filter2', 'color2' );
1438
1439 assert.deepEqual(
1440 model.getHighlightedItems().map( ( item ) => item.getName() ),
1441 [
1442 'group1__filter1',
1443 'group1__filter2'
1444 ],
1445 'Highlighted items are highlighted.'
1446 );
1447
1448 assert.strictEqual(
1449 model.getItemByName( 'group1__filter1' ).getHighlightColor(),
1450 'color1',
1451 'Item highlight color is set.'
1452 );
1453
1454 model.setHighlightColor( 'group1__filter1', 'color1changed' );
1455 assert.strictEqual(
1456 model.getItemByName( 'group1__filter1' ).getHighlightColor(),
1457 'color1changed',
1458 'Item highlight color is changed on setHighlightColor.'
1459 );
1460
1461 model.clearHighlightColor( 'group1__filter1' );
1462 assert.deepEqual(
1463 model.getHighlightedItems().map( ( item ) => item.getName() ),
1464 [
1465 'group1__filter2'
1466 ],
1467 'Clear highlight from an item results in the item no longer being highlighted.'
1468 );
1469
1470 // Reset
1471 model = new rcfilters.dm.FiltersViewModel();
1472 model.initializeFilters( definition );
1473
1474 model.setHighlightColor( 'group1__filter1', 'color1' );
1475 model.setHighlightColor( 'group1__filter2', 'color2' );
1476 model.setHighlightColor( 'group1__filter3', 'color3' );
1477
1478 assert.deepEqual(
1479 model.getHighlightedItems().map( ( item ) => item.getName() ),
1480 [
1481 'group1__filter1',
1482 'group1__filter2',
1483 'group1__filter3'
1484 ],
1485 'Even if highlights are not enabled, the items remember their highlight state'
1486 // NOTE: When actually displaying the highlights, the UI checks whether
1487 // highlighting is generally active and then goes over the highlighted
1488 // items. The item models, however, and the view model in general, still
1489 // retains the knowledge about which filters have different colors, so we
1490 // can seamlessly return to the colors the user previously chose if they
1491 // reapply highlights.
1492 );
1493
1494 // Reset
1495 model = new rcfilters.dm.FiltersViewModel();
1496 model.initializeFilters( definition );
1497
1498 model.setHighlightColor( 'group1__filter1', 'color1' );
1499 model.setHighlightColor( 'group1__filter6', 'color6' );
1500
1501 assert.deepEqual(
1502 model.getHighlightedItems().map( ( item ) => item.getName() ),
1503 [
1504 'group1__filter1'
1505 ],
1506 'Items without a specified class identifier are not highlighted.'
1507 );
1508 } );
1509
1510 QUnit.test( 'emptyAllFilters', ( assert ) => {
1511 const model = new rcfilters.dm.FiltersViewModel();
1512
1513 model.initializeFilters( shortFilterDefinition, null );
1514
1515 model.toggleFiltersSelected( {
1516 group1__filter1: true,
1517 group2__filter4: true, // hidden
1518 group3__filter5: true // sticky
1519 } );
1520
1521 model.emptyAllFilters();
1522
1523 assert.deepEqual(
1524 model.getSelectedState( true ),
1525 {
1526 group3__filter5: true,
1527 group3__filter6: true
1528 },
1529 'Emptying filters does not affect sticky filters'
1530 );
1531 } );
1532
1533 QUnit.test( 'areVisibleFiltersEmpty', ( assert ) => {
1534 const model = new rcfilters.dm.FiltersViewModel();
1535 model.initializeFilters( shortFilterDefinition, null );
1536
1537 model.emptyAllFilters();
1538 assert.strictEqual( model.areVisibleFiltersEmpty(), true );
1539
1540 model.toggleFiltersSelected( {
1541 group3__filter5: true // sticky
1542 } );
1543 assert.strictEqual( model.areVisibleFiltersEmpty(), true );
1544
1545 model.toggleFiltersSelected( {
1546 group1__filter1: true
1547 } );
1548 assert.strictEqual( model.areVisibleFiltersEmpty(), false );
1549 } );
1550 }() );