Expand RC filters search to include desc and group title
authorStephane Bisson <sbisson@wikimedia.org>
Tue, 31 Jan 2017 15:41:23 +0000 (10:41 -0500)
committerCatrope <roan@wikimedia.org>
Tue, 7 Feb 2017 22:24:06 +0000 (22:24 +0000)
Bug: T156215
Change-Id: Ieb1c30d8403fcdf2e4d236211affdbb6199f84f6

resources/src/mediawiki.rcfilters/dm/mw.rcfilters.dm.FiltersViewModel.js
tests/qunit/suites/resources/mediawiki.rcfilters/dm.FiltersViewModel.test.js

index d1b7925..3c4ed4b 100644 (file)
        /**
         * Find items whose labels match the given string
         *
        /**
         * Find items whose labels match the given string
         *
-        * @param {string} str Search string
+        * @param {string} query Search string
         * @return {Object} An object of items to show
         *  arranged by their group names
         */
         * @return {Object} An object of items to show
         *  arranged by their group names
         */
-       mw.rcfilters.dm.FiltersViewModel.prototype.findMatches = function ( str ) {
+       mw.rcfilters.dm.FiltersViewModel.prototype.findMatches = function ( query ) {
                var i,
                var i,
+                       groupTitle,
                        result = {},
                        items = this.getItems();
 
                // Normalize so we can search strings regardless of case
                        result = {},
                        items = this.getItems();
 
                // Normalize so we can search strings regardless of case
-               str = str.toLowerCase();
+               query = query.toLowerCase();
+
+               // item label starting with the query string
                for ( i = 0; i < items.length; i++ ) {
                for ( i = 0; i < items.length; i++ ) {
-                       if ( items[ i ].getLabel().toLowerCase().indexOf( str ) > -1 ) {
+                       if ( items[ i ].getLabel().toLowerCase().indexOf( query ) === 0 ) {
                                result[ items[ i ].getGroup() ] = result[ items[ i ].getGroup() ] || [];
                                result[ items[ i ].getGroup() ].push( items[ i ] );
                        }
                }
                                result[ items[ i ].getGroup() ] = result[ items[ i ].getGroup() ] || [];
                                result[ items[ i ].getGroup() ].push( items[ i ] );
                        }
                }
+
+               if ( $.isEmptyObject( result ) ) {
+                       // item containing the query string in their label, description, or group title
+                       for ( i = 0; i < items.length; i++ ) {
+                               groupTitle = this.getGroup( items[ i ].getGroup() ).getTitle();
+                               if (
+                                       items[ i ].getLabel().toLowerCase().indexOf( query ) > -1 ||
+                                       items[ i ].getDescription().toLowerCase().indexOf( query ) > -1 ||
+                                       groupTitle.toLowerCase().indexOf( query ) > -1
+                               ) {
+                                       result[ items[ i ].getGroup() ] = result[ items[ i ].getGroup() ] || [];
+                                       result[ items[ i ].getGroup() ].push( items[ i ] );
+                               }
+                       }
+               }
+
                return result;
        };
 
                return result;
        };
 
index 62d247a..998817d 100644 (file)
                var matches,
                        definition = {
                                group1: {
                var matches,
                        definition = {
                                group1: {
-                                       title: 'Group 1',
+                                       title: 'Group 1 title',
                                        type: 'send_unselected_if_any',
                                        filters: [
                                                {
                                        type: 'send_unselected_if_any',
                                        filters: [
                                                {
                                        ]
                                },
                                group2: {
                                        ]
                                },
                                group2: {
-                                       title: 'Group 2',
+                                       title: 'Group 2 title',
                                        type: 'send_unselected_if_any',
                                        filters: [
                                                {
                                        type: 'send_unselected_if_any',
                                        filters: [
                                                {
                                                },
                                                {
                                                        name: 'group2filter2',
                                                },
                                                {
                                                        name: 'group2filter2',
-                                                       label: 'Group 2: Filter 2',
+                                                       label: 'xGroup 2: Filter 2',
                                                        description: 'Description of Filter 2 in Group 2'
                                                }
                                        ]
                                }
                        },
                                                        description: 'Description of Filter 2 in Group 2'
                                                }
                                        ]
                                }
                        },
-                       model = new mw.rcfilters.dm.FiltersViewModel();
+                       testCases = [
+                               {
+                                       query: 'group',
+                                       expectedMatches: {
+                                               group1: [ 'group1filter1', 'group1filter2' ],
+                                               group2: [ 'group2filter1' ]
+                                       },
+                                       reason: 'Finds filters starting with the query string'
+                               },
+                               {
+                                       query: 'filter 2 in group',
+                                       expectedMatches: {
+                                               group1: [ 'group1filter2' ],
+                                               group2: [ 'group2filter2' ]
+                                       },
+                                       reason: 'Finds filters containing the query string in their description'
+                               },
+                               {
+                                       query: 'title',
+                                       expectedMatches: {
+                                               group1: [ 'group1filter1', 'group1filter2' ],
+                                               group2: [ 'group2filter1', 'group2filter2' ]
+                                       },
+                                       reason: 'Finds filters containing the query string in their group title'
+                               }
+                       ],
+                       model = new mw.rcfilters.dm.FiltersViewModel(),
+                       extractNames = function ( matches ) {
+                               var result = {};
+                               Object.keys( matches ).forEach( function ( groupName ) {
+                                       result[ groupName ] = matches[ groupName ].map( function ( item ) {
+                                               return item.getName();
+                                       } );
+                               } );
+                               return result;
+                       };
 
                model.initializeFilters( definition );
 
 
                model.initializeFilters( definition );
 
-               matches = model.findMatches( 'group 1' );
-               assert.equal(
-                       matches.group1.length,
-                       2,
-                       'findMatches finds correct group with correct number of results'
-               );
-
-               assert.deepEqual(
-                       matches.group1.map( function ( item ) { return item.getName(); } ),
-                       [ 'group1filter1', 'group1filter2' ],
-                       'findMatches finds the correct items within a single group'
-               );
-
-               matches = model.findMatches( 'filter 1' );
-               assert.ok(
-                       matches.group1.length === 1 && matches.group2.length === 1,
-                       'findMatches finds correct number of results in multiple groups'
-               );
-
-               assert.deepEqual(
-                       [
-                               matches.group1.map( function ( item ) { return item.getName(); } ),
-                               matches.group2.map( function ( item ) { return item.getName(); } )
-                       ],
-                       [
-                               [ 'group1filter1' ],
-                               [ 'group2filter1' ]
-                       ],
-                       'findMatches finds the correct items within multiple groups'
-               );
+               testCases.forEach( function ( testCase ) {
+                       matches = model.findMatches( testCase.query );
+                       assert.deepEqual(
+                               extractNames( matches ),
+                               testCase.expectedMatches,
+                               testCase.reason
+                       );
+               } );
 
                matches = model.findMatches( 'foo' );
                assert.ok(
 
                matches = model.findMatches( 'foo' );
                assert.ok(