Debian Codegraph @

js_tests/admin/RelatedObjectLookups.test.js

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