2
0

RelatedObjectLookups.test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* global QUnit, RelatedObjectLookups */
  2. 'use strict';
  3. QUnit.module('admin.RelatedObjectLookups', {
  4. beforeEach: function() {
  5. const $ = django.jQuery;
  6. $('#qunit-fixture').append(`
  7. <input type="text" id="test_id" name="test" />
  8. <input type="text" id="many_test_id" name="many_test" class="vManyToManyRawIdAdminField" />
  9. `);
  10. window.relatedWindows = window.relatedWindows || [];
  11. }
  12. });
  13. QUnit.test('dismissRelatedLookupPopup closes popup window', function(assert) {
  14. const testId = 'test_id';
  15. let windowClosed = false;
  16. const mockWin = {
  17. name: testId,
  18. close: function() {
  19. windowClosed = true;
  20. }
  21. };
  22. window.dismissRelatedLookupPopup(mockWin, '123');
  23. assert.true(windowClosed, 'Popup window should be closed');
  24. });
  25. QUnit.test('dismissRelatedLookupPopup removes window from relatedWindows array', function(assert) {
  26. const testId = 'test_id';
  27. const mockWin = {
  28. name: testId,
  29. close: function() {}
  30. };
  31. window.relatedWindows.push(mockWin);
  32. assert.equal(window.relatedWindows.indexOf(mockWin), 0, 'Window should be in relatedWindows array');
  33. window.dismissRelatedLookupPopup(mockWin, '123');
  34. assert.equal(window.relatedWindows.indexOf(mockWin), -1, 'Window should be removed from relatedWindows array');
  35. });
  36. QUnit.test('dismissRelatedLookupPopup triggers change event for single value field', function(assert) {
  37. assert.timeout(1000);
  38. const done = assert.async();
  39. const $ = django.jQuery;
  40. const testId = 'test_id';
  41. const newValue = '123';
  42. const mockWin = {
  43. name: testId,
  44. close: function() {}
  45. };
  46. let changeTriggered = false;
  47. $('#test_id').on('change', function() {
  48. changeTriggered = true;
  49. assert.equal(this.value, newValue, 'Value should be updated');
  50. done();
  51. });
  52. window.dismissRelatedLookupPopup(mockWin, newValue);
  53. assert.true(changeTriggered, 'Change event should be triggered');
  54. });
  55. QUnit.test('dismissRelatedLookupPopup triggers change event for many-to-many field', function(assert) {
  56. assert.timeout(1000);
  57. const $ = django.jQuery;
  58. const testId = 'many_test_id';
  59. const existingValue = '1,2';
  60. const newValue = '3';
  61. $('#many_test_id').val(existingValue);
  62. const mockWin = {
  63. name: testId,
  64. close: function() {}
  65. };
  66. let changeTriggered = false;
  67. $('#many_test_id').on('change', function() {
  68. changeTriggered = true;
  69. assert.equal(this.value, existingValue + ',' + newValue, 'Value should be appended for many-to-many fields');
  70. });
  71. window.dismissRelatedLookupPopup(mockWin, newValue);
  72. assert.true(changeTriggered, 'Change event should be triggered');
  73. });