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'
10 { name
: 'filter2', default: true },
15 type
: 'string_options',
27 { name
: 'group3option1' },
28 { name
: 'group3option2' },
29 { name
: 'group3option3' }
32 queriesFilterRepresentation
= {
35 label
: 'Item converted',
38 // - This value is true, but the original filter-representation
39 // of the saved queries ran against defaults. Since filter1 was
40 // set as default in the definition, the value would actually
41 // not appear in the representation itself.
42 // It is considered 'true', though, and should appear in the
43 // converted result in its parameter representation.
44 // >> group1__filter1: true,
45 // - The reverse is true for filter3. Filter3 is set as default
46 // but we don't want it in this representation of the saved query.
47 // Since the filter representation ran against default values,
48 // it will appear as 'false' value in this representation explicitly
49 // and the resulting parameter representation should have that
50 // as the result as well
51 group1__filter3
: false,
52 group2__filter4
: true,
53 group3__group3option1
: true
65 queriesParamRepresentation
= {
69 label
: 'Item converted',
72 // filter1 is 'true' so filter2 and filter3 are both '1'
73 // in param representation
74 filter2
: '1', filter3
: '1',
75 // Group type string_options
77 // Note - Group3 is sticky, so it won't show in output
78 // Invert/highlight toggles
84 group3option1_color
: 'c1'
91 QUnit
.module( 'mediawiki.rcfilters - SavedQueriesModel' );
93 QUnit
.test( 'Initializing queries', function ( assert
) {
94 var filtersModel
= new mw
.rcfilters
.dm
.FiltersViewModel(),
95 queriesModel
= new mw
.rcfilters
.dm
.SavedQueriesModel( filtersModel
),
96 exampleQueryStructure
= {
116 finalState
: { version
: '2', queries
: {} },
117 msg
: 'Empty initial query structure results in base saved queries structure.'
120 input
: $.extend( true, {}, exampleQueryStructure
),
121 finalState
: $.extend( true, {}, exampleQueryStructure
),
122 msg
: 'Initialization of given query structure does not corrupt the structure.'
125 // Converting from old structure
126 input
: $.extend( true, {}, queriesFilterRepresentation
),
127 finalState
: $.extend( true, {}, queriesParamRepresentation
),
128 msg
: 'Conversion from filter representation to parameters retains data.'
131 // Converting from old structure with default
132 input
: $.extend( true, { default: '1234' }, queriesFilterRepresentation
),
133 finalState
: $.extend( true, { default: '1234' }, queriesParamRepresentation
),
134 msg
: 'Conversion from filter representation to parameters, with default set up, retains data.'
138 input
: $.extend( true, {}, queriesParamRepresentation
),
139 finalState
: $.extend( true, {}, queriesParamRepresentation
),
140 msg
: 'Parameter representation retains its queries structure'
144 filtersModel
.initializeFilters( filterDefinition
);
146 cases
.forEach( function ( testCase
) {
147 queriesModel
.initialize( testCase
.input
);
149 queriesModel
.getState(),
156 QUnit
.test( 'Manipulating queries', function ( assert
) {
157 var id1
, id2
, item1
, matchingItem
,
158 queriesStructure
= {},
159 filtersModel
= new mw
.rcfilters
.dm
.FiltersViewModel(),
160 queriesModel
= new mw
.rcfilters
.dm
.SavedQueriesModel( filtersModel
);
162 filtersModel
.initializeFilters( filterDefinition
);
164 // Start with an empty saved queries model
165 queriesModel
.initialize( {} );
168 id1
= queriesModel
.addNewQuery(
177 group3option1_color
: 'c1'
181 id2
= queriesModel
.addNewQuery(
192 item1
= queriesModel
.getItemByID( id1
);
197 'Item created and its data retained successfully'
200 // NOTE: All other methods that the item itself returns are
201 // tested in the dm.SavedQueryItemModel.test.js file
203 // Build the query structure we expect per item
204 queriesStructure
[ id1
] = {
205 label
: 'New query 1',
213 group3option1_color
: 'c1'
217 queriesStructure
[ id2
] = {
218 label
: 'New query 2',
230 queriesModel
.getState(),
233 queries
: queriesStructure
235 'Full query represents current state of items'
239 queriesModel
.setDefault( id2
);
242 queriesModel
.getState(),
246 queries
: queriesStructure
248 'Setting default is reflected in queries state'
252 queriesModel
.setDefault( null );
255 queriesModel
.getState(),
258 queries
: queriesStructure
260 'Removing default is reflected in queries state'
263 // Find matching query
264 matchingItem
= queriesModel
.findMatchingQuery(
272 group3option1_color
: 'c1'
277 matchingItem
.getID(),
279 'Finding matching item by identical state'
282 // Find matching query with 0-values (base state)
283 matchingItem
= queriesModel
.findMatchingQuery(
294 group3option1_color
: 'c1'
299 matchingItem
.getID(),
301 'Finding matching item by "dirty" state with 0-base values'