From 0f8bf1627cc067423cb6375546ff0e98ac663d7b Mon Sep 17 00:00:00 2001 From: James Addison Date: Wed, 18 Oct 2023 10:29:42 +0100 Subject: [PATCH 1/6] tests: add vendored jquery.mockjax dependency --- tests/integration-jq1.html | 1 + tests/integration-jq2.html | 1 + tests/integration-jq3.html | 1 + tests/vendor/jquery.mockjax-2.6.0.js | 1061 ++++++++++++++++++++++++++ 4 files changed, 1064 insertions(+) create mode 100644 tests/vendor/jquery.mockjax-2.6.0.js diff --git a/tests/integration-jq1.html b/tests/integration-jq1.html index ae00a811ed..cd79b6e112 100644 --- a/tests/integration-jq1.html +++ b/tests/integration-jq1.html @@ -10,6 +10,7 @@ + diff --git a/tests/integration-jq2.html b/tests/integration-jq2.html index 97f73828e0..8a966a950a 100644 --- a/tests/integration-jq2.html +++ b/tests/integration-jq2.html @@ -10,6 +10,7 @@ + diff --git a/tests/integration-jq3.html b/tests/integration-jq3.html index 2f99fb5bc3..5948c5b405 100644 --- a/tests/integration-jq3.html +++ b/tests/integration-jq3.html @@ -10,6 +10,7 @@ + diff --git a/tests/vendor/jquery.mockjax-2.6.0.js b/tests/vendor/jquery.mockjax-2.6.0.js new file mode 100644 index 0000000000..cfdb6ae083 --- /dev/null +++ b/tests/vendor/jquery.mockjax-2.6.0.js @@ -0,0 +1,1061 @@ +/*! jQuery Mockjax + * A Plugin providing simple and flexible mocking of ajax requests and responses + * + * Version: 2.6.0 + * Home: https://github.com/jakerella/jquery-mockjax + * Copyright (c) 2020 Jordan Kasper, formerly appendTo; + * NOTE: This repository was taken over by Jordan Kasper (@jakerella) October, 2014 + * + * Dual licensed under the MIT or GPL licenses. + * http://opensource.org/licenses/MIT OR http://www.gnu.org/licenses/gpl-2.0.html + */ +(function(root, factory) { + 'use strict'; + + // AMDJS module definition + if ( typeof define === 'function' && define.amd && define.amd.jQuery ) { + define(['jquery'], function($) { + return factory($, root); + }); + + // CommonJS module definition + } else if ( typeof exports === 'object') { + + // NOTE: To use Mockjax as a Node module you MUST provide the factory with + // a valid version of jQuery and a window object (the global scope): + // var mockjax = require('jquery.mockjax')(jQuery, window); + + module.exports = factory; + + // Global jQuery in web browsers + } else { + return factory(root.jQuery || root.$, root); + } +}(this, function($, window) { + 'use strict'; + + var _ajax = $.ajax, + mockHandlers = [], + mockedAjaxCalls = [], + unmockedAjaxCalls = [], + CALLBACK_REGEX = /=\?(&|$)/, + jsc = (new Date()).getTime(), + DEFAULT_RESPONSE_TIME = 500; + + // Parse the given XML string. + function parseXML(xml) { + if ( window.DOMParser === undefined && window.ActiveXObject ) { + window.DOMParser = function() { }; + DOMParser.prototype.parseFromString = function( xmlString ) { + var doc = new ActiveXObject('Microsoft.XMLDOM'); + doc.async = 'false'; + doc.loadXML( xmlString ); + return doc; + }; + } + + try { + var xmlDoc = ( new DOMParser() ).parseFromString( xml, 'text/xml' ); + if ( $.isXMLDoc( xmlDoc ) ) { + var err = $('parsererror', xmlDoc); + if ( err.length === 1 ) { + throw new Error('Error: ' + $(xmlDoc).text() ); + } + } else { + throw new Error('Unable to parse XML'); + } + return xmlDoc; + } catch( e ) { + var msg = ( e.name === undefined ? e : e.name + ': ' + e.message ); + $(document).trigger('xmlParseError', [ msg ]); + return undefined; + } + } + + // Check if the data field on the mock handler and the request match. This + // can be used to restrict a mock handler to being used only when a certain + // set of data is passed to it. + function isMockDataEqual( mock, live ) { + logger.debug( mock, ['Checking mock data against request data', mock, live] ); + var identical = true; + + if ( $.isFunction(mock) ) { + return !!mock(live); + } + + // Test for situations where the data is a querystring (not an object) + if (typeof live === 'string') { + // Querystring may be a regex + if ($.isFunction( mock.test )) { + return mock.test(live); + } else if (typeof mock === 'object') { + live = getQueryParams(live); + } else { + return mock === live; + } + } + + $.each(mock, function(k) { + if ( live[k] === undefined ) { + identical = false; + return identical; + } else { + if ( typeof live[k] === 'object' && live[k] !== null ) { + if ( identical && $.isArray( live[k] ) ) { + identical = $.isArray( mock[k] ) && live[k].length === mock[k].length; + } + identical = identical && isMockDataEqual(mock[k], live[k]); + } else { + if ( mock[k] && $.isFunction( mock[k].test ) ) { + identical = identical && mock[k].test(live[k]); + } else { + identical = identical && ( mock[k] === live[k] ); + } + } + } + }); + + return identical; + } + + function getQueryParams(queryString) { + var i, l, param, tmp, + paramsObj = {}, + params = String(queryString).split(/&/); + + for (i=0, l=params.length; i= 0; + } + + function parseResponseTimeOpt(responseTime) { + if ($.isArray(responseTime) && responseTime.length === 2) { + var min = responseTime[0]; + var max = responseTime[1]; + if(isPosNum(min) && isPosNum(max)) { + return Math.floor(Math.random() * (max - min)) + min; + } + } else if(isPosNum(responseTime)) { + return responseTime; + } + return DEFAULT_RESPONSE_TIME; + } + + // Process the xhr objects send operation + function _xhrSend(mockHandler, requestSettings, origSettings) { + logger.debug( mockHandler, ['Sending fake XHR request', mockHandler, requestSettings, origSettings] ); + + // This is a substitute for < 1.4 which lacks $.proxy + var process = (function(that) { + return function() { + return (function() { + // The request has returned + this.status = mockHandler.status; + this.statusText = mockHandler.statusText; + this.readyState = 1; + + var finishRequest = function () { + this.readyState = 4; + + var onReady; + // Copy over our mock to our xhr object before passing control back to + // jQuery's onreadystatechange callback + if ( requestSettings.dataType === 'json' && ( typeof mockHandler.responseText === 'object' ) ) { + this.responseText = JSON.stringify(mockHandler.responseText); + } else if ( requestSettings.dataType === 'xml' ) { + if ( typeof mockHandler.responseXML === 'string' ) { + this.responseXML = parseXML(mockHandler.responseXML); + //in jQuery 1.9.1+, responseXML is processed differently and relies on responseText + this.responseText = mockHandler.responseXML; + } else { + this.responseXML = mockHandler.responseXML; + } + } else if (typeof mockHandler.responseText === 'object' && mockHandler.responseText !== null) { + // since jQuery 1.9 responseText type has to match contentType + mockHandler.contentType = 'application/json'; + this.responseText = JSON.stringify(mockHandler.responseText); + } else { + this.responseText = mockHandler.responseText; + } + + if ($.isArray(mockHandler.status)) { + var idxStatus = Math.floor(Math.random() * mockHandler.status.length); + this.status = mockHandler.status[idxStatus]; + } else if (typeof mockHandler.status === 'number' || typeof mockHandler.status === 'string') { + this.status = mockHandler.status; + } + + if( typeof mockHandler.statusText === 'string') { + this.statusText = mockHandler.statusText; + } + // jQuery 2.0 renamed onreadystatechange to onload + onReady = this.onload || this.onreadystatechange; + + // jQuery < 1.4 doesn't have onreadystate change for xhr + if ( $.isFunction( onReady ) ) { + if( mockHandler.isTimeout) { + this.status = -1; + } + onReady.call( this, mockHandler.isTimeout ? 'timeout' : undefined ); + } else if ( mockHandler.isTimeout ) { + // Fix for 1.3.2 timeout to keep success from firing. + this.status = -1; + } + }; + + // We have an executable function, call it to give + // the mock handler a chance to update it's data + if ( $.isFunction(mockHandler.response) ) { + // Wait for it to finish + if ( mockHandler.response.length === 2 ) { + mockHandler.response(origSettings, function () { + finishRequest.call(that); + }); + return; + } else { + mockHandler.response(origSettings); + } + } + + finishRequest.call(that); + }).apply(that); + }; + })(this); + + if ( mockHandler.proxy ) { + logger.info( mockHandler, ['Retrieving proxy file: ' + mockHandler.proxy, mockHandler] ); + // We're proxying this request and loading in an external file instead + _ajax({ + global: false, + url: mockHandler.proxy, + type: mockHandler.proxyType, + data: mockHandler.data, + async: requestSettings.async, + dataType: requestSettings.dataType === 'script' ? 'text/plain' : requestSettings.dataType, + complete: function(xhr) { + // Fix for bug #105 + // jQuery will convert the text to XML for us, and if we use the actual responseXML here + // then some other things don't happen, resulting in no data given to the 'success' cb + mockHandler.responseXML = mockHandler.responseText = xhr.responseText; + + // Don't override the handler status/statusText if it's specified by the config + if (isDefaultSetting(mockHandler, 'status')) { + mockHandler.status = xhr.status; + } + if (isDefaultSetting(mockHandler, 'statusText')) { + mockHandler.statusText = xhr.statusText; + } + + if ( requestSettings.async === false ) { + // TODO: Blocking delay + process(); + } else { + this.responseTimer = setTimeout(process, parseResponseTimeOpt(mockHandler.responseTime)); + } + } + }); + } else { + // type === 'POST' || 'GET' || 'DELETE' + if ( requestSettings.async === false ) { + // TODO: Blocking delay + process(); + } else { + this.responseTimer = setTimeout(process, parseResponseTimeOpt(mockHandler.responseTime)); + } + } + + } + + // Construct a mocked XHR Object + function xhr(mockHandler, requestSettings, origSettings, origHandler) { + logger.debug( mockHandler, ['Creating new mock XHR object', mockHandler, requestSettings, origSettings, origHandler] ); + + // Extend with our default mockjax settings + mockHandler = $.extend(true, {}, $.mockjaxSettings, mockHandler); + + if (typeof mockHandler.headers === 'undefined') { + mockHandler.headers = {}; + } + if (typeof requestSettings.headers === 'undefined') { + requestSettings.headers = {}; + } + if ( mockHandler.contentType ) { + mockHandler.headers['content-type'] = mockHandler.contentType; + } + + return { + status: mockHandler.status, + statusText: mockHandler.statusText, + readyState: 1, + open: function() { }, + send: function() { + origHandler.fired = true; + _xhrSend.call(this, mockHandler, requestSettings, origSettings); + }, + abort: function() { + clearTimeout(this.responseTimer); + }, + setRequestHeader: function(header, value) { + requestSettings.headers[header] = value; + }, + getResponseHeader: function(header) { + // 'Last-modified', 'Etag', 'content-type' are all checked by jQuery + if ( mockHandler.headers && mockHandler.headers[header] ) { + // Return arbitrary headers + return mockHandler.headers[header]; + } else if ( header.toLowerCase() === 'last-modified' ) { + return mockHandler.lastModified || (new Date()).toString(); + } else if ( header.toLowerCase() === 'etag' ) { + return mockHandler.etag || ''; + } else if ( header.toLowerCase() === 'content-type' ) { + return mockHandler.contentType || 'text/plain'; + } + }, + getAllResponseHeaders: function() { + var headers = ''; + // since jQuery 1.9 responseText type has to match contentType + if (mockHandler.contentType) { + mockHandler.headers['content-type'] = mockHandler.contentType; + } + $.each(mockHandler.headers, function(k, v) { + headers += k + ': ' + v + '\n'; + }); + return headers; + } + }; + } + + // Process a JSONP mock request. + function processJsonpMock( requestSettings, mockHandler, origSettings ) { + // Handle JSONP Parameter Callbacks, we need to replicate some of the jQuery core here + // because there isn't an easy hook for the cross domain script tag of jsonp + + processJsonpUrl( requestSettings ); + + requestSettings.dataType = 'json'; + if(requestSettings.data && CALLBACK_REGEX.test(requestSettings.data) || CALLBACK_REGEX.test(requestSettings.url)) { + createJsonpCallback(requestSettings, mockHandler, origSettings); + + // We need to make sure + // that a JSONP style response is executed properly + + var rurl = /^(\w+:)?\/\/([^\/?#]+)/, + parts = rurl.exec( requestSettings.url ), + remote = parts && (parts[1] && parts[1] !== location.protocol || parts[2] !== location.host); + + requestSettings.dataType = 'script'; + if(requestSettings.type.toUpperCase() === 'GET' && remote ) { + var newMockReturn = processJsonpRequest( requestSettings, mockHandler, origSettings ); + + // Check if we are supposed to return a Deferred back to the mock call, or just + // signal success + if(newMockReturn) { + return newMockReturn; + } else { + return true; + } + } + } + return null; + } + + // Append the required callback parameter to the end of the request URL, for a JSONP request + function processJsonpUrl( requestSettings ) { + if ( requestSettings.type.toUpperCase() === 'GET' ) { + if ( !CALLBACK_REGEX.test( requestSettings.url ) ) { + requestSettings.url += (/\?/.test( requestSettings.url ) ? '&' : '?') + + (requestSettings.jsonp || 'callback') + '=?'; + } + } else if ( !requestSettings.data || !CALLBACK_REGEX.test(requestSettings.data) ) { + requestSettings.data = (requestSettings.data ? requestSettings.data + '&' : '') + (requestSettings.jsonp || 'callback') + '=?'; + } + } + + // Process a JSONP request by evaluating the mocked response text + function processJsonpRequest( requestSettings, mockHandler, origSettings ) { + logger.debug( mockHandler, ['Performing JSONP request', mockHandler, requestSettings, origSettings] ); + + // Synthesize the mock request for adding a script tag + var callbackContext = origSettings && origSettings.context || requestSettings, + // If we are running under jQuery 1.5+, return a deferred object + newMock = ($.Deferred) ? (new $.Deferred()) : null; + + // If the response handler on the moock is a function, call it + if ( mockHandler.response && $.isFunction(mockHandler.response) ) { + + mockHandler.response(origSettings); + + + } else if ( typeof mockHandler.responseText === 'object' ) { + // Evaluate the responseText javascript in a global context + $.globalEval( '(' + JSON.stringify( mockHandler.responseText ) + ')'); + + } else if (mockHandler.proxy) { + logger.info( mockHandler, ['Performing JSONP proxy request: ' + mockHandler.proxy, mockHandler] ); + + // This handles the unique case where we have a remote URL, but want to proxy the JSONP + // response to another file (not the same URL as the mock matching) + _ajax({ + global: false, + url: mockHandler.proxy, + type: mockHandler.proxyType, + data: mockHandler.data, + dataType: requestSettings.dataType === 'script' ? 'text/plain' : requestSettings.dataType, + complete: function(xhr) { + $.globalEval( '(' + xhr.responseText + ')'); + completeJsonpCall( requestSettings, mockHandler, callbackContext, newMock ); + } + }); + + return newMock; + + } else { + $.globalEval( '(' + + ((typeof mockHandler.responseText === 'string') ? + ('"' + mockHandler.responseText + '"') : mockHandler.responseText) + + ')'); + } + + completeJsonpCall( requestSettings, mockHandler, callbackContext, newMock ); + + return newMock; + } + + function completeJsonpCall( requestSettings, mockHandler, callbackContext, newMock ) { + var json; + + // Successful response + setTimeout(function() { + jsonpSuccess( requestSettings, callbackContext, mockHandler ); + jsonpComplete( requestSettings, callbackContext ); + + if ( newMock ) { + try { + json = $.parseJSON( mockHandler.responseText ); + } catch (err) { /* just checking... */ } + + newMock.resolveWith( callbackContext, [json || mockHandler.responseText] ); + logger.log( mockHandler, ['JSONP mock call complete', mockHandler, newMock] ); + } + }, parseResponseTimeOpt( mockHandler.responseTime )); + } + + + // Create the required JSONP callback function for the request + function createJsonpCallback( requestSettings, mockHandler, origSettings ) { + var callbackContext = origSettings && origSettings.context || requestSettings; + var jsonp = (typeof requestSettings.jsonpCallback === 'string' && requestSettings.jsonpCallback) || ('jsonp' + jsc++); + + // Replace the =? sequence both in the query string and the data + if ( requestSettings.data ) { + requestSettings.data = (requestSettings.data + '').replace(CALLBACK_REGEX, '=' + jsonp + '$1'); + } + + requestSettings.url = requestSettings.url.replace(CALLBACK_REGEX, '=' + jsonp + '$1'); + + + // Handle JSONP-style loading + window[ jsonp ] = window[ jsonp ] || function() { + jsonpSuccess( requestSettings, callbackContext, mockHandler ); + jsonpComplete( requestSettings, callbackContext ); + // Garbage collect + window[ jsonp ] = undefined; + + try { + delete window[ jsonp ]; + } catch(e) {} + }; + requestSettings.jsonpCallback = jsonp; + } + + // The JSONP request was successful + function jsonpSuccess(requestSettings, callbackContext, mockHandler) { + // If a local callback was specified, fire it and pass it the data + if ( requestSettings.success ) { + requestSettings.success.call( callbackContext, mockHandler.responseText || '', 'success', {} ); + } + + // Fire the global callback + if ( requestSettings.global ) { + (requestSettings.context ? $(requestSettings.context) : $.event).trigger('ajaxSuccess', [{}, requestSettings]); + } + } + + // The JSONP request was completed + function jsonpComplete(requestSettings, callbackContext) { + if ( requestSettings.complete ) { + requestSettings.complete.call( callbackContext, { + statusText: 'success', + status: 200 + } , 'success' ); + } + + // The request was completed + if ( requestSettings.global ) { + (requestSettings.context ? $(requestSettings.context) : $.event).trigger('ajaxComplete', [{}, requestSettings]); + } + + // Handle the global AJAX counter + if ( requestSettings.global && ! --$.active ) { + $.event.trigger( 'ajaxStop' ); + } + } + + + // The core $.ajax replacement. + function handleAjax( url, origSettings ) { + var mockRequest, requestSettings, mockHandler, overrideCallback; + + logger.debug( null, ['Ajax call intercepted', url, origSettings] ); + + // If url is an object, simulate pre-1.5 signature + if ( typeof url === 'object' ) { + origSettings = url; + url = undefined; + } else { + // work around to support 1.5 signature + origSettings = origSettings || {}; + origSettings.url = url || origSettings.url; + } + + // Extend the original settings for the request + requestSettings = $.ajaxSetup({}, origSettings); + requestSettings.type = requestSettings.method = requestSettings.method || requestSettings.type; + + // Generic function to override callback methods for use with + // callback options (onAfterSuccess, onAfterError, onAfterComplete) + overrideCallback = function(action, mockHandler) { + var origHandler = origSettings[action.toLowerCase()]; + return function() { + if ( $.isFunction(origHandler) ) { + origHandler.apply(this, [].slice.call(arguments)); + } + mockHandler['onAfter' + action](); + }; + }; + + // Iterate over our mock handlers (in order depending on the matchInRegistrationOrder setting) + // until we find one that is willing to intercept the request + for(var k = 0; k < mockHandlers.length; k++) { + var handlerIndex = $.mockjaxSettings.matchInRegistrationOrder ? k : mockHandlers.length - 1 - k; + var origHandler = mockHandlers[handlerIndex]; + if ( !origHandler ) { + continue; + } + + mockHandler = getMockForRequest( origHandler, requestSettings ); + if(!mockHandler) { + logger.debug( origHandler, ['Mock does not match request', url, requestSettings] ); + // No valid mock found for this request + continue; + } + + if ($.mockjaxSettings.retainAjaxCalls) { + mockedAjaxCalls.push(requestSettings); + } + + // If logging is enabled, log the mock to the console + logger.info( mockHandler, [ + 'MOCK ' + requestSettings.type.toUpperCase() + ': ' + requestSettings.url, + $.ajaxSetup({}, requestSettings) + ] ); + + if ((mockHandler.status === 301 || mockHandler.status === 302) && + (requestSettings.type.toUpperCase() === 'GET' || requestSettings.type.toUpperCase() === 'HEAD') && + mockHandler.headers.Location) { + logger.debug('Doing mock redirect to', mockHandler.headers.Location, requestSettings.type); + + var redirectSettings = {}; + var origKeys = Object.keys(origSettings); + // We can't alter origSettings, so we need a shallow copy of it... + for (var oi=0; oi 1 ) { + origSettings.timeout = mockHandler.responseTime - 1; + } else { + mockHandler.responseTime = 2; + origSettings.timeout = 1; + } + } + + // Set up onAfter[X] callback functions + if ( $.isFunction( mockHandler.onAfterSuccess ) ) { + origSettings.success = overrideCallback('Success', mockHandler); + } + if ( $.isFunction( mockHandler.onAfterError ) ) { + origSettings.error = overrideCallback('Error', mockHandler); + } + if ( $.isFunction( mockHandler.onAfterComplete ) ) { + origSettings.complete = overrideCallback('Complete', mockHandler); + } + + copyUrlParameters(mockHandler, origSettings); + + /* jshint loopfunc:true */ + (function(mockHandler, requestSettings, origSettings, origHandler) { + + mockRequest = _ajax.call($, $.extend(true, {}, origSettings, { + // Mock the XHR object + xhr: function() { return xhr( mockHandler, requestSettings, origSettings, origHandler ); } + })); + })(mockHandler, requestSettings, origSettings, origHandler); + /* jshint loopfunc:false */ + + return mockRequest; + } + + // We don't have a mock request + logger.log( null, ['No mock matched to request', url, origSettings] ); + if ($.mockjaxSettings.retainAjaxCalls) { + unmockedAjaxCalls.push(origSettings); + } + if($.mockjaxSettings.throwUnmocked === true) { + throw new Error('AJAX not mocked: ' + origSettings.url); + } + else { // trigger a normal request + logger.log('Real ajax call to', origSettings.url); + return _ajax.apply($, [origSettings]); + } + } + + /** + * Copies URL parameter values if they were captured by a regular expression + * @param {Object} mockHandler + * @param {Object} origSettings + */ + function copyUrlParameters(mockHandler, origSettings) { + //parameters aren't captured if the URL isn't a RegExp + if (!(mockHandler.url instanceof RegExp)) { + return; + } + //if no URL params were defined on the handler, don't attempt a capture + if (!mockHandler.hasOwnProperty('urlParams')) { + return; + } + var captures = mockHandler.url.exec(origSettings.url); + //the whole RegExp match is always the first value in the capture results + if (captures.length === 1) { + return; + } + captures.shift(); + //use handler params as keys and capture resuts as values + var i = 0, + capturesLength = captures.length, + paramsLength = mockHandler.urlParams.length, + //in case the number of params specified is less than actual captures + maxIterations = Math.min(capturesLength, paramsLength), + paramValues = {}; + for (i; i < maxIterations; i++) { + var key = mockHandler.urlParams[i]; + paramValues[key] = captures[i]; + } + origSettings.urlParams = paramValues; + } + + /** + * Clears handlers that mock given url + * @param url + * @returns {Array} + */ + function clearByUrl(url) { + var i, len, + handler, + results = [], + match=url instanceof RegExp ? + function(testUrl) { return url.test(testUrl); } : + function(testUrl) { return url === testUrl; }; + for (i=0, len=mockHandlers.length; i Date: Wed, 18 Oct 2023 11:05:15 +0100 Subject: [PATCH 2/6] tests: add illustrative test case: HTML option element should be removed after unselecting an AJAX-sourced item --- tests/integration/select2-methods.js | 57 ++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests/integration/select2-methods.js b/tests/integration/select2-methods.js index b1e344a567..4d4323bcc8 100644 --- a/tests/integration/select2-methods.js +++ b/tests/integration/select2-methods.js @@ -137,3 +137,60 @@ test('multiple value matches the jquery value', function (assert) { 'The values should match the jquery values' ); }); + +test('selection and clearing of data from ajax source', function (assert) { + var asyncDone = assert.async(); + + var dataURL = 'http://127.0.0.1/test'; + $.mockjax({ + url: dataURL, + responseText: {results: [{id: 6128, text: '6128'}]}, + logging: 1 + }); + + var $container = $('#qunit-fixture'); + var $select = $(''); + $container.append($select); + + var select = new Select2($select, {ajax: {url: dataURL}, multiple: true}); + + assert.equal( + $select.find(':selected').length, + 0, + 'No items should be selected' + ); + + // Open the dropdown menu, to perform an AJAX request + select.selection.trigger('query', {term: '6128'}); + + var selectionStatus = null; + select.on('results:all', function() { + + // First call: select a result from the dropdown menu + if (selectionStatus === null) { + + $('.select2-results__option').trigger('mouseup'); + assert.equal( + $select.find('option').length, + 1, + 'An HTML option element should have been created for the item' + ); + + // Trigger a second call + selectionStatus = true; + select.selection.trigger('query', {term: '6128'}); + + // Second call: unselect the previously-selected item + } else if (selectionStatus == true) { + + $('.select2-results__option[aria-selected=true]').trigger('mouseup'); + assert.equal( + $select.find('option').length, + 0, + 'The previously-created HTML option element should have been removed' + ); + + asyncDone(); + } + }); +}); From b9b96f44dd334f0e30aa137433c33866f0cdeab4 Mon Sep 17 00:00:00 2001 From: James Addison Date: Wed, 18 Oct 2023 11:35:57 +0100 Subject: [PATCH 3/6] fix: remove local DOM HTML 'option' element after AJAX-sourced item is unselected --- src/js/select2/data/select.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/js/select2/data/select.js b/src/js/select2/data/select.js index f4eec511cd..3ca5d35de8 100644 --- a/src/js/select2/data/select.js +++ b/src/js/select2/data/select.js @@ -70,6 +70,14 @@ define([ SelectAdapter.prototype.unselect = function (data) { var self = this; + // Remove local HTML option elements when unselected items from AJAX sources + if (this.options.get('ajax')) { + var $option = this.$element.find('option').filter(function (i, elm) { + return elm.value == data.id.toString(); + }); + $option.remove(); + } + if (!this.$element.prop('multiple')) { return; } From b2017d0db177cd8a18c121b9eb326969d2149af8 Mon Sep 17 00:00:00 2001 From: James Addison Date: Wed, 18 Oct 2023 18:27:13 +0100 Subject: [PATCH 4/6] Nitpick: update comment --- src/js/select2/data/select.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/select2/data/select.js b/src/js/select2/data/select.js index 3ca5d35de8..7916d3529e 100644 --- a/src/js/select2/data/select.js +++ b/src/js/select2/data/select.js @@ -70,7 +70,7 @@ define([ SelectAdapter.prototype.unselect = function (data) { var self = this; - // Remove local HTML option elements when unselected items from AJAX sources + // Remote-origin (AJAX) items: remove matching option elements from the DOM if (this.options.get('ajax')) { var $option = this.$element.find('option').filter(function (i, elm) { return elm.value == data.id.toString(); From ad502d3fdb57becddd2ab2467457b3f2c2e986a4 Mon Sep 17 00:00:00 2001 From: James Addison Date: Wed, 18 Oct 2023 20:32:08 +0100 Subject: [PATCH 5/6] tests: assert that after unselecting and re-selecting an AJAX-based item that the updated text from the remote datasource is applied to the