diff options
| author | Кайрат Макым <kairatmakym@kajrats-macbook-pro.local> | 2025-02-18 09:48:13 +0100 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2025-02-18 10:36:33 +0100 |
| commit | b0d497ada04bb6dccdfc2c4072744de391b7d3b7 (patch) | |
| tree | 9a06c0aec57c9a678637d6a40ead34f71be6fccd | |
| parent | 7cf6a34cb2f3ff973e1321818186e45da900ff3a (diff) | |
[5.2.x] Fixed #31170 -- Added change event trigger to dismissRelatedLookupPopup.
Backport of 51398f8bd568a6324a8cafe20c068d0974913ad5 from main.
| -rw-r--r-- | django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js | 1 | ||||
| -rw-r--r-- | js_tests/admin/RelatedObjectLookups.test.js | 40 |
2 files changed, 41 insertions, 0 deletions
diff --git a/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js b/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js index be37fda973..5395386087 100644 --- a/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js +++ b/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js @@ -57,6 +57,7 @@ } else { elem.value = chosenId; } + $(elem).trigger('change'); const index = window.relatedWindows.indexOf(win); if (index > -1) { window.relatedWindows.splice(index, 1); diff --git a/js_tests/admin/RelatedObjectLookups.test.js b/js_tests/admin/RelatedObjectLookups.test.js index 64433cb623..722aa7ae7b 100644 --- a/js_tests/admin/RelatedObjectLookups.test.js +++ b/js_tests/admin/RelatedObjectLookups.test.js @@ -36,3 +36,43 @@ QUnit.test('dismissRelatedLookupPopup removes window from relatedWindows array', window.dismissRelatedLookupPopup(mockWin, '123'); assert.equal(window.relatedWindows.indexOf(mockWin), -1, 'Window should be removed from relatedWindows array'); }); + +QUnit.test('dismissRelatedLookupPopup triggers change event for single value field', function(assert) { + assert.timeout(1000); + const done = assert.async(); + const $ = django.jQuery; + const testId = 'test_id'; + const newValue = '123'; + const mockWin = { + name: testId, + close: function() {} + }; + let changeTriggered = false; + $('#test_id').on('change', function() { + changeTriggered = true; + assert.equal(this.value, newValue, 'Value should be updated'); + done(); + }); + window.dismissRelatedLookupPopup(mockWin, newValue); + assert.true(changeTriggered, 'Change event should be triggered'); +}); + +QUnit.test('dismissRelatedLookupPopup triggers change event for many-to-many field', function(assert) { + assert.timeout(1000); + const $ = django.jQuery; + const testId = 'many_test_id'; + const existingValue = '1,2'; + const newValue = '3'; + $('#many_test_id').val(existingValue); + const mockWin = { + name: testId, + close: function() {} + }; + let changeTriggered = false; + $('#many_test_id').on('change', function() { + changeTriggered = true; + assert.equal(this.value, existingValue + ',' + newValue, 'Value should be appended for many-to-many fields'); + }); + window.dismissRelatedLookupPopup(mockWin, newValue); + assert.true(changeTriggered, 'Change event should be triggered'); +}); |
