From 6fc25f87b9e53607591b166d2a0901b8d0ccce91 Mon Sep 17 00:00:00 2001 From: Lucas Werkmeister Date: Wed, 30 Nov 2022 17:25:59 +0100 Subject: [PATCH] search: Remove unused generateUrl() function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As far as I can tell, this has been dead code ever since it was introduced in change Ica040cd18d (commit aa10668e6d), and was never called; the effective search URL was initially constructed via inputs to the search form (e.g. a hidden input for wprov), and later the separate urlGenerator.js was added. (Also, “suggestion” is not a valid Special:Search paramater as far as I can tell.) Also add some tests for the separate urlGenerator.js, to make up for the other tests being removed. This reduces the module size of skins.vector.search enough that we can decrease the bundle size test config to 3.2 kB [KiB]. Change-Id: I6be5ba362402c2c2ec582d9a0192c80f46e7a7ce --- bundlesize.config.json | 2 +- .../skins.vector.search/instrumentation.js | 32 ----------- tests/jest/instrumentation.test.js | 11 ---- tests/jest/urlGenerator.test.js | 53 +++++++++++++++++++ 4 files changed, 54 insertions(+), 44 deletions(-) create mode 100644 tests/jest/urlGenerator.test.js diff --git a/bundlesize.config.json b/bundlesize.config.json index 5f5fc5def..3d27c4957 100644 --- a/bundlesize.config.json +++ b/bundlesize.config.json @@ -13,7 +13,7 @@ }, { "resourceModule": "skins.vector.search", - "maxSize": "3.3 kB" + "maxSize": "3.2 kB" }, { "resourceModule": "skins.vector.icons", diff --git a/resources/skins.vector.search/instrumentation.js b/resources/skins.vector.search/instrumentation.js index 5a3ce0d58..8f1128fcf 100644 --- a/resources/skins.vector.search/instrumentation.js +++ b/resources/skins.vector.search/instrumentation.js @@ -6,7 +6,6 @@ * @see https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/skins/Vector/+/refs/heads/master/includes/Constants.php */ const INPUT_LOCATION_MOVED = 'header-moved', - wgScript = mw.config.get( 'wgScript' ), // T251544: Collect search performance metrics to compare Vue search with // mediawiki.searchSuggest performance. Marks and Measures will only be // recorded on the Vector skin and only if browser supported. @@ -122,35 +121,6 @@ function getWprovFromResultIndex( index ) { * @property {string} [url] */ -/** - * @typedef {Object} GenerateUrlMeta - * @property {number} index - */ - -/** - * Used by the Vue-enhanced search component to generate URLs for the search results. Adds a - * `wprov` paramater to the URL to satisfy the SearchSatisfaction instrumentation. - * - * @see getWprovFromResultIndex - * - * @param {SearchResultPartial|string} suggestion - * @param {GenerateUrlMeta} meta - * @return {string} - */ -function generateUrl( suggestion, meta ) { - const result = new mw.Uri( wgScript ); - - if ( typeof suggestion !== 'string' ) { - suggestion = suggestion.title; - } - - result.query.title = 'Special:Search'; - result.query.suggestion = suggestion; - result.query.wprov = getWprovFromResultIndex( meta.index ); - - return result.toString(); -} - /** * Return a new list of search results, * with the `wprov` parameter added to each result's url (if any). @@ -174,7 +144,6 @@ function addWprovToSearchResultUrls( results, offset ) { * @typedef {Object} Instrumentation * @property {Object} listeners * @property {Function} getWprovFromResultIndex - * @property {Function} generateUrl * @property {Function} addWprovToSearchResultUrls */ @@ -203,6 +172,5 @@ module.exports = { onSubmit: onSuggestionClick }, getWprovFromResultIndex, - generateUrl, addWprovToSearchResultUrls }; diff --git a/tests/jest/instrumentation.test.js b/tests/jest/instrumentation.test.js index 64148f78e..0d4403f86 100644 --- a/tests/jest/instrumentation.test.js +++ b/tests/jest/instrumentation.test.js @@ -10,17 +10,6 @@ describe( 'instrumentation', () => { .toBe( expected ); } ); - describe( 'generateUrl', () => { - test.each( [ - [ 'string', 'title' ], - [ 'object', { title: 'title' } ] - ] )( 'should generate URL from %s', ( _name, suggestion ) => { - const meta = { index: 1 }; - expect( instrumentation.generateUrl( suggestion, meta ) ) - .toBe( 'https://host/?title=Special%3ASearch&suggestion=title&wprov=acrw1_1' ); - } ); - } ); - test( 'addWprovToSearchResultUrls without offset', () => { const url1 = 'https://host/?title=Special%3ASearch&search=Aa', url2Base = 'https://host/?title=Special%3ASearch&search=Ab', diff --git a/tests/jest/urlGenerator.test.js b/tests/jest/urlGenerator.test.js new file mode 100644 index 000000000..013969ad6 --- /dev/null +++ b/tests/jest/urlGenerator.test.js @@ -0,0 +1,53 @@ +const urlGenerator = require( '../../resources/skins.vector.search/urlGenerator.js' ); + +describe( 'urlGenerator', () => { + describe( 'default', () => { + test.each( [ + [ 'string', 'title', '&fulltext=1' ], + [ 'object', { title: 'title', id: 0, key: '' } ] + ] )( 'suggestion as %s', ( _name, suggestion, extraParams = '' ) => { + const config = { + get: jest.fn().mockImplementation( ( key, fallback ) => { + if ( key === 'wgScript' ) { + return '/w/index.php'; + } + return fallback; + } ), + set: jest.fn() + }; + + expect( urlGenerator( config ).generateUrl( suggestion ) ) + .toBe( `/w/index.php?title=Special%3ASearch${extraParams}&search=title` ); + } ); + + test( 'custom params, articlePath', () => { + const config = { + get: jest.fn().mockImplementation( ( _key, fallback ) => { + return fallback; + } ), + set: jest.fn() + }; + + expect( urlGenerator( config ).generateUrl( + { title: 'title', id: 0, key: '' }, + { TITLE: 'SPECIAL:SEARCH' }, + '/W/INDEX.PHP' + ) ).toBe( '/W/INDEX.PHP?TITLE=SPECIAL%3ASEARCH&search=title' ); + } ); + } ); + + test( 'custom', () => { + const customGenerator = {}; + const config = { + get: jest.fn().mockImplementation( ( key, fallback ) => { + if ( key === 'wgVectorSearchUrlGenerator' ) { + return customGenerator; + } + return fallback; + } ), + set: jest.fn() + }; + + expect( urlGenerator( config ) ).toBe( customGenerator ); + } ); +} );