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
92 group1__filter1_color
: 'c5',
93 group3__group3option1_color
: 'c1'
100 QUnit
.module( 'mediawiki.rcfilters - SavedQueriesModel' );
102 QUnit
.test( 'Initializing queries', function ( assert
) {
103 var filtersModel
= new mw
.rcfilters
.dm
.FiltersViewModel(),
104 queriesModel
= new mw
.rcfilters
.dm
.SavedQueriesModel( filtersModel
),
105 exampleQueryStructure
= {
116 group1__filter3_color
: 'c2'
125 finalState
: { version
: '2', queries
: {} },
126 msg
: 'Empty initial query structure results in base saved queries structure.'
129 input
: $.extend( true, {}, exampleQueryStructure
),
130 finalState
: $.extend( true, {}, exampleQueryStructure
),
131 msg
: 'Initialization of given query structure does not corrupt the structure.'
134 // Converting from old structure
135 input
: $.extend( true, {}, queriesFilterRepresentation
),
136 finalState
: $.extend( true, {}, queriesParamRepresentation
),
137 msg
: 'Conversion from filter representation to parameters retains data.'
140 // Converting from old structure
141 input
: $.extend( true, {}, queriesFilterRepresentation
, { queries
: { 1234: { data
: {
143 // Entire group true: normalize params
149 filter3
: null // Get rid of empty highlight
152 finalState
: $.extend( true, {}, queriesParamRepresentation
),
153 msg
: 'Conversion from filter representation to parameters normalizes params and highlights.'
156 // Converting from old structure with default
157 input
: $.extend( true, { default: '1234' }, queriesFilterRepresentation
),
158 finalState
: $.extend( true, { default: '1234' }, queriesParamRepresentation
),
159 msg
: 'Conversion from filter representation to parameters, with default set up, retains data.'
163 input
: $.extend( true, {}, queriesParamRepresentation
),
164 finalState
: $.extend( true, {}, queriesParamRepresentation
),
165 msg
: 'Parameter representation retains its queries structure'
168 // Do not touch invalid color parameters from the initialization routine
169 // (Normalization, or "fixing" the query should only happen when we add new query or actively convert queries)
170 input
: $.extend( true, { queries
: { 1234: { data
: { highlights
: { group2__filter5_color
: 'c2' } } } } }, exampleQueryStructure
),
171 finalState
: $.extend( true, { queries
: { 1234: { data
: { highlights
: { group2__filter5_color
: 'c2' } } } } }, exampleQueryStructure
),
172 msg
: 'Structure that contains invalid highlights remains the same in initialization'
176 filtersModel
.initializeFilters( filterDefinition
);
178 cases
.forEach( function ( testCase
) {
179 queriesModel
.initialize( testCase
.input
);
181 queriesModel
.getState(),
188 QUnit
.test( 'Adding new queries', function ( assert
) {
189 var filtersModel
= new mw
.rcfilters
.dm
.FiltersViewModel(),
190 queriesModel
= new mw
.rcfilters
.dm
.SavedQueriesModel( filtersModel
),
198 group1__filter1_color
: 'c2',
199 group1__filter3_color
: 'c5'
213 group1__filter1_color
: 'c2',
214 group1__filter3_color
: 'c5'
221 msg
: 'Given valid data is preserved.'
229 filter15
: '1', // Invalid filter - removed
230 filter2
: '0', // Falsey value - removed
231 group1__filter1_color
: 'c3',
232 foobar
: 'w00t' // Unrecognized parameter - removed
244 group1__filter1_color
: 'c3'
250 msg
: 'Given data with invalid filters and highlights is normalized'
254 filtersModel
.initializeFilters( filterDefinition
);
256 // Start with an empty saved queries model
257 queriesModel
.initialize( {} );
259 cases
.forEach( function ( testCase
) {
260 var itemID
= queriesModel
.addNewQuery
.apply( queriesModel
, testCase
.methodParams
),
261 item
= queriesModel
.getItemByID( itemID
);
265 testCase
.result
.itemState
,
266 testCase
.msg
+ ' (itemState)'
271 testCase
.result
.isDefault
,
272 testCase
.msg
+ ' (isDefault)'
275 if ( testCase
.result
.id
!== undefined ) {
279 testCase
.msg
+ ' (item ID)'
285 QUnit
.test( 'Manipulating queries', function ( assert
) {
286 var id1
, id2
, item1
, matchingItem
,
287 queriesStructure
= {},
288 filtersModel
= new mw
.rcfilters
.dm
.FiltersViewModel(),
289 queriesModel
= new mw
.rcfilters
.dm
.SavedQueriesModel( filtersModel
);
291 filtersModel
.initializeFilters( filterDefinition
);
293 // Start with an empty saved queries model
294 queriesModel
.initialize( {} );
297 id1
= queriesModel
.addNewQuery(
302 group1__filter1_color
: 'c5',
303 group3__group3option1_color
: 'c1'
306 id2
= queriesModel
.addNewQuery(
314 item1
= queriesModel
.getItemByID( id1
);
319 'Item created and its data retained successfully'
322 // NOTE: All other methods that the item itself returns are
323 // tested in the dm.SavedQueryItemModel.test.js file
325 // Build the query structure we expect per item
326 queriesStructure
[ id1
] = {
327 label
: 'New query 1',
334 group1__filter1_color
: 'c5',
335 group3__group3option1_color
: 'c1'
339 queriesStructure
[ id2
] = {
340 label
: 'New query 2',
352 queriesModel
.getState(),
355 queries
: queriesStructure
357 'Full query represents current state of items'
361 queriesModel
.setDefault( id2
);
364 queriesModel
.getState(),
368 queries
: queriesStructure
370 'Setting default is reflected in queries state'
374 queriesModel
.setDefault( null );
377 queriesModel
.getState(),
380 queries
: queriesStructure
382 'Removing default is reflected in queries state'
385 // Find matching query
386 matchingItem
= queriesModel
.findMatchingQuery(
390 group1__filter1_color
: 'c5',
391 group3__group3option1_color
: 'c1'
395 matchingItem
.getID(),
397 'Finding matching item by identical state'
400 // Find matching query with 0-values (base state)
401 matchingItem
= queriesModel
.findMatchingQuery(
408 group1__filter1_color
: 'c5',
409 group3__group3option1_color
: 'c1'
413 matchingItem
.getID(),
415 'Finding matching item by "dirty" state with 0-base values'