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
64 queriesParamRepresentation
= {
68 label
: 'Item converted',
71 // filter1 is 'true' so filter2 and filter3 are both '1'
72 // in param representation
73 filter2
: '1', filter3
: '1',
74 // Group type string_options
76 // Note - Group3 is sticky, so it won't show in output
82 group3option1_color
: 'c1'
89 QUnit
.module( 'mediawiki.rcfilters - SavedQueriesModel' );
91 QUnit
.test( 'Initializing queries', function ( assert
) {
92 var filtersModel
= new mw
.rcfilters
.dm
.FiltersViewModel(),
93 queriesModel
= new mw
.rcfilters
.dm
.SavedQueriesModel( filtersModel
),
94 exampleQueryStructure
= {
114 finalState
: { version
: '2', queries
: {} },
115 msg
: 'Empty initial query structure results in base saved queries structure.'
118 input
: $.extend( true, {}, exampleQueryStructure
),
119 finalState
: $.extend( true, {}, exampleQueryStructure
),
120 msg
: 'Initialization of given query structure does not corrupt the structure.'
123 // Converting from old structure
124 input
: $.extend( true, {}, queriesFilterRepresentation
),
125 finalState
: $.extend( true, {}, queriesParamRepresentation
),
126 msg
: 'Conversion from filter representation to parameters retains data.'
129 // Converting from old structure with default
130 input
: $.extend( true, { default: '1234' }, queriesFilterRepresentation
),
131 finalState
: $.extend( true, { default: '1234' }, queriesParamRepresentation
),
132 msg
: 'Conversion from filter representation to parameters, with default set up, retains data.'
136 input
: $.extend( true, {}, queriesParamRepresentation
),
137 finalState
: $.extend( true, {}, queriesParamRepresentation
),
138 msg
: 'Parameter representation retains its queries structure'
142 filtersModel
.initializeFilters( filterDefinition
);
144 cases
.forEach( function ( testCase
) {
145 queriesModel
.initialize( testCase
.input
);
147 queriesModel
.getState(),
154 QUnit
.test( 'Manipulating queries', function ( assert
) {
155 var id1
, id2
, item1
, matchingItem
,
156 queriesStructure
= {},
157 filtersModel
= new mw
.rcfilters
.dm
.FiltersViewModel(),
158 queriesModel
= new mw
.rcfilters
.dm
.SavedQueriesModel( filtersModel
);
160 filtersModel
.initializeFilters( filterDefinition
);
162 // Start with an empty saved queries model
163 queriesModel
.initialize( {} );
166 id1
= queriesModel
.addNewQuery(
175 group3option1_color
: 'c1'
179 id2
= queriesModel
.addNewQuery(
190 item1
= queriesModel
.getItemByID( id1
);
195 'Item created and its data retained successfully'
198 // NOTE: All other methods that the item itself returns are
199 // tested in the dm.SavedQueryItemModel.test.js file
201 // Build the query structure we expect per item
202 queriesStructure
[ id1
] = {
203 label
: 'New query 1',
211 group3option1_color
: 'c1'
215 queriesStructure
[ id2
] = {
216 label
: 'New query 2',
228 queriesModel
.getState(),
231 queries
: queriesStructure
233 'Full query represents current state of items'
237 queriesModel
.setDefault( id2
);
240 queriesModel
.getState(),
244 queries
: queriesStructure
246 'Setting default is reflected in queries state'
250 queriesModel
.setDefault( null );
253 queriesModel
.getState(),
256 queries
: queriesStructure
258 'Removing default is reflected in queries state'
261 // Find matching query
262 matchingItem
= queriesModel
.findMatchingQuery(
270 group3option1_color
: 'c1'
275 matchingItem
.getID(),
277 'Finding matching item by identical state'
280 // Find matching query with 0-values (base state)
281 matchingItem
= queriesModel
.findMatchingQuery(
291 group3option1_color
: 'c1'
296 matchingItem
.getID(),
298 'Finding matching item by "dirty" state with 0-base values'