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 ); + } ); +} );