1 /* eslint-disable camelcase */
3 var filterDefinition
= [ {
5 type
: 'send_unselected_if_any',
7 // Note: The fact filter2 is default means that in the
8 // filter representation, filter1 and filter3 are 'true'
9 { name
: 'filter1', cssClass
: 'filter1class' },
10 { name
: 'filter2', cssClass
: 'filter2class', default: true },
11 { name
: 'filter3', cssClass
: 'filter3class' }
15 type
: 'string_options',
18 { name
: 'filter4', cssClass
: 'filter4class' },
19 { name
: 'filter5' }, // NOTE: Not supporting highlights!
20 { name
: 'filter6', cssClass
: 'filter6class' }
27 { name
: 'group3option1', cssClass
: 'filter1class' },
28 { name
: 'group3option2', cssClass
: 'filter1class' },
29 { name
: 'group3option3', cssClass
: 'filter1class' }
32 // Copy of the way the controller defines invert
33 // to check whether the conversion works
42 queriesFilterRepresentation
= {
45 label
: 'Item converted',
48 // - This value is true, but the original filter-representation
49 // of the saved queries ran against defaults. Since filter1 was
50 // set as default in the definition, the value would actually
51 // not appear in the representation itself.
52 // It is considered 'true', though, and should appear in the
53 // converted result in its parameter representation.
54 // >> group1__filter1: true,
55 // - The reverse is true for filter3. Filter3 is set as default
56 // but we don't want it in this representation of the saved query.
57 // Since the filter representation ran against default values,
58 // it will appear as 'false' value in this representation explicitly
59 // and the resulting parameter representation should have that
60 // as the result as well
61 group1__filter3
: false,
62 group2__filter4
: true,
63 group3__group3option1
: true
67 group1__filter1
: 'c5',
68 group3__group3option1
: 'c1'
75 queriesParamRepresentation
= {
79 label
: 'Item converted',
82 // filter1 is 'true' so filter2 and filter3 are both '1'
83 // in param representation
84 filter2
: '1', filter3
: '1',
85 // Group type string_options
87 // Note - Group3 is sticky, so it won't show in output
90 group1__filter1_color
: 'c5',
91 group3__group3option1_color
: 'c1'
97 removeHighlights = function ( data
) {
98 var copy
= $.extend( true, {}, data
);
99 copy
.queries
[ 1234 ].data
.highlights
= {};
103 QUnit
.module( 'mediawiki.rcfilters - SavedQueriesModel' );
105 QUnit
.test( 'Initializing queries', function ( assert
) {
106 var filtersModel
= new mw
.rcfilters
.dm
.FiltersViewModel(),
107 queriesModel
= new mw
.rcfilters
.dm
.SavedQueriesModel( filtersModel
),
108 exampleQueryStructure
= {
119 group1__filter3_color
: 'c2'
128 finalState
: { version
: '2', queries
: {} },
129 msg
: 'Empty initial query structure results in base saved queries structure.'
132 input
: $.extend( true, {}, exampleQueryStructure
),
133 finalState
: $.extend( true, {}, exampleQueryStructure
),
134 msg
: 'Initialization of given query structure does not corrupt the structure.'
137 // Converting from old structure
138 input
: $.extend( true, {}, queriesFilterRepresentation
),
139 finalState
: $.extend( true, {}, queriesParamRepresentation
),
140 msg
: 'Conversion from filter representation to parameters retains data.'
143 // Converting from old structure
144 input
: $.extend( true, {}, queriesFilterRepresentation
, { queries
: { 1234: { data
: {
146 // Entire group true: normalize params
152 filter3
: null // Get rid of empty highlight
155 finalState
: $.extend( true, {}, queriesParamRepresentation
),
156 msg
: 'Conversion from filter representation to parameters normalizes params and highlights.'
159 // Converting from old structure with default
160 input
: $.extend( true, { default: '1234' }, queriesFilterRepresentation
),
161 finalState
: $.extend( true, { default: '1234' }, queriesParamRepresentation
),
162 msg
: 'Conversion from filter representation to parameters, with default set up, retains data.'
165 // Converting from old structure and cleaning up highlights
166 input
: $.extend( true, queriesFilterRepresentation
, { queries
: { 1234: { data
: { highlights
: { highlight
: false } } } } } ),
167 finalState
: removeHighlights( queriesParamRepresentation
),
168 msg
: 'Conversion from filter representation to parameters and highlight cleanup'
172 input
: $.extend( true, {}, queriesParamRepresentation
),
173 finalState
: $.extend( true, {}, queriesParamRepresentation
),
174 msg
: 'Parameter representation retains its queries structure'
177 // Do not touch invalid color parameters from the initialization routine
178 // (Normalization, or "fixing" the query should only happen when we add new query or actively convert queries)
179 input
: $.extend( true, { queries
: { 1234: { data
: { highlights
: { group2__filter5_color
: 'c2' } } } } }, exampleQueryStructure
),
180 finalState
: $.extend( true, { queries
: { 1234: { data
: { highlights
: { group2__filter5_color
: 'c2' } } } } }, exampleQueryStructure
),
181 msg
: 'Structure that contains invalid highlights remains the same in initialization'
184 // Trim colors when highlight=false is stored
185 input
: $.extend( true, { queries
: { 1234: { data
: { params
: { highlight
: '0' } } } } }, queriesParamRepresentation
),
186 finalState
: removeHighlights( queriesParamRepresentation
),
187 msg
: 'Colors are removed when highlight=false'
190 // Remove highlight when it is true but no colors are specified
191 input
: $.extend( true, { queries
: { 1234: { data
: { params
: { highlight
: '1' } } } } }, removeHighlights( queriesParamRepresentation
) ),
192 finalState
: removeHighlights( queriesParamRepresentation
),
193 msg
: 'remove highlight when it is true but there is no colors'
197 filtersModel
.initializeFilters( filterDefinition
);
199 cases
.forEach( function ( testCase
) {
200 queriesModel
.initialize( testCase
.input
);
202 queriesModel
.getState(),
209 QUnit
.test( 'Adding new queries', function ( assert
) {
210 var filtersModel
= new mw
.rcfilters
.dm
.FiltersViewModel(),
211 queriesModel
= new mw
.rcfilters
.dm
.SavedQueriesModel( filtersModel
),
219 group1__filter1_color
: 'c2',
220 group1__filter3_color
: 'c5'
234 group1__filter1_color
: 'c2',
235 group1__filter3_color
: 'c5'
242 msg
: 'Given valid data is preserved.'
250 filter15
: '1', // Invalid filter - removed
251 filter2
: '0', // Falsey value - removed
252 group1__filter1_color
: 'c3',
253 foobar
: 'w00t' // Unrecognized parameter - removed
265 group1__filter1_color
: 'c3'
271 msg
: 'Given data with invalid filters and highlights is normalized'
275 filtersModel
.initializeFilters( filterDefinition
);
277 // Start with an empty saved queries model
278 queriesModel
.initialize( {} );
280 cases
.forEach( function ( testCase
) {
281 var itemID
= queriesModel
.addNewQuery
.apply( queriesModel
, testCase
.methodParams
),
282 item
= queriesModel
.getItemByID( itemID
);
286 testCase
.result
.itemState
,
287 testCase
.msg
+ ' (itemState)'
292 testCase
.result
.isDefault
,
293 testCase
.msg
+ ' (isDefault)'
296 if ( testCase
.result
.id
!== undefined ) {
300 testCase
.msg
+ ' (item ID)'
306 QUnit
.test( 'Manipulating queries', function ( assert
) {
307 var id1
, id2
, item1
, matchingItem
,
308 queriesStructure
= {},
309 filtersModel
= new mw
.rcfilters
.dm
.FiltersViewModel(),
310 queriesModel
= new mw
.rcfilters
.dm
.SavedQueriesModel( filtersModel
);
312 filtersModel
.initializeFilters( filterDefinition
);
314 // Start with an empty saved queries model
315 queriesModel
.initialize( {} );
318 id1
= queriesModel
.addNewQuery(
322 group1__filter1_color
: 'c5',
323 group3__group3option1_color
: 'c1'
326 id2
= queriesModel
.addNewQuery(
334 item1
= queriesModel
.getItemByID( id1
);
339 'Item created and its data retained successfully'
342 // NOTE: All other methods that the item itself returns are
343 // tested in the dm.SavedQueryItemModel.test.js file
345 // Build the query structure we expect per item
346 queriesStructure
[ id1
] = {
347 label
: 'New query 1',
353 group1__filter1_color
: 'c5',
354 group3__group3option1_color
: 'c1'
358 queriesStructure
[ id2
] = {
359 label
: 'New query 2',
371 queriesModel
.getState(),
374 queries
: queriesStructure
376 'Full query represents current state of items'
380 queriesModel
.setDefault( id2
);
383 queriesModel
.getState(),
387 queries
: queriesStructure
389 'Setting default is reflected in queries state'
393 queriesModel
.setDefault( null );
396 queriesModel
.getState(),
399 queries
: queriesStructure
401 'Removing default is reflected in queries state'
404 // Find matching query
405 matchingItem
= queriesModel
.findMatchingQuery(
408 group1__filter1_color
: 'c5',
409 group3__group3option1_color
: 'c1'
413 matchingItem
.getID(),
415 'Finding matching item by identical state'
418 // Find matching query with 0-values (base state)
419 matchingItem
= queriesModel
.findMatchingQuery(
425 group1__filter1_color
: 'c5',
426 group3__group3option1_color
: 'c1'
430 matchingItem
.getID(),
432 'Finding matching item by "dirty" state with 0-base values'