inlines.test.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* global QUnit */
  2. 'use strict';
  3. QUnit.module('admin.inlines: tabular formsets', {
  4. beforeEach: function() {
  5. const $ = django.jQuery;
  6. const that = this;
  7. this.addText = 'Add another';
  8. $('#qunit-fixture').append($('#tabular-formset').text());
  9. this.table = $('table.inline');
  10. this.inlineRow = this.table.find('tr');
  11. this.inlineRow.tabularFormset('table.inline tr.form-row', {
  12. prefix: 'first',
  13. addText: that.addText,
  14. deleteText: 'Remove'
  15. });
  16. }
  17. });
  18. QUnit.test('no forms', function(assert) {
  19. assert.ok(this.inlineRow.hasClass('dynamic-first'));
  20. assert.equal(this.table.find('.add-row a').text(), this.addText);
  21. });
  22. QUnit.test('add form', function(assert) {
  23. const addButton = this.table.find('.add-row a');
  24. assert.equal(addButton.text(), this.addText);
  25. addButton.click();
  26. assert.ok(this.table.find('#first-1'));
  27. });
  28. QUnit.test('added form has remove button', function(assert) {
  29. const addButton = this.table.find('.add-row a');
  30. assert.equal(addButton.text(), this.addText);
  31. addButton.click();
  32. assert.equal(this.table.find('#first-1 .inline-deletelink').length, 1);
  33. });
  34. QUnit.test('add/remove form events', function(assert) {
  35. assert.expect(5);
  36. const addButton = this.table.find('.add-row a');
  37. document.addEventListener('formset:added', (event) => {
  38. assert.ok(true, 'event `formset:added` triggered');
  39. assert.equal(true, event.target.matches('#first-1'));
  40. assert.equal(event.detail.formsetName, 'first');
  41. }, {once: true});
  42. addButton.click();
  43. const deleteLink = this.table.find('.inline-deletelink');
  44. document.addEventListener('formset:removed', (event) => {
  45. assert.ok(true, 'event `formset:removed` triggered');
  46. assert.equal(event.detail.formsetName, 'first');
  47. }, {once: true});
  48. deleteLink.click();
  49. });
  50. QUnit.test('existing add button', function(assert) {
  51. const $ = django.jQuery;
  52. $('#qunit-fixture').empty(); // Clear the table added in beforeEach
  53. $('#qunit-fixture').append($('#tabular-formset').text());
  54. this.table = $('table.inline');
  55. this.inlineRow = this.table.find('tr');
  56. this.table.append('<i class="add-button"></i>');
  57. const addButton = this.table.find('.add-button');
  58. this.inlineRow.tabularFormset('table.inline tr', {
  59. prefix: 'first',
  60. deleteText: 'Remove',
  61. addButton: addButton
  62. });
  63. assert.equal(this.table.find('.add-row a').length, 0);
  64. addButton.click();
  65. assert.ok(this.table.find('#first-1'));
  66. });
  67. QUnit.module('admin.inlines: tabular formsets with validation errors', {
  68. beforeEach: function() {
  69. const $ = django.jQuery;
  70. $('#qunit-fixture').append($('#tabular-formset-with-validation-error').text());
  71. this.table = $('table.inline');
  72. this.inlineRows = this.table.find('tr.form-row');
  73. this.inlineRows.tabularFormset('table.inline tr.form-row', {
  74. prefix: 'second'
  75. });
  76. }
  77. });
  78. QUnit.test('first form has delete checkbox and no button', function(assert) {
  79. const tr = this.inlineRows.slice(0, 1);
  80. assert.ok(tr.hasClass('dynamic-second'));
  81. assert.ok(tr.hasClass('has_original'));
  82. assert.equal(tr.find('td.delete input').length, 1);
  83. assert.equal(tr.find('td.delete .inline-deletelink').length, 0);
  84. });
  85. QUnit.test('dynamic form has remove button', function(assert) {
  86. const tr = this.inlineRows.slice(1, 2);
  87. assert.ok(tr.hasClass('dynamic-second'));
  88. assert.notOk(tr.hasClass('has_original'));
  89. assert.equal(tr.find('.inline-deletelink').length, 1);
  90. });
  91. QUnit.test('dynamic template has nothing', function(assert) {
  92. const tr = this.inlineRows.slice(2, 3);
  93. assert.ok(tr.hasClass('empty-form'));
  94. assert.notOk(tr.hasClass('dynamic-second'));
  95. assert.notOk(tr.hasClass('has_original'));
  96. assert.equal(tr.find('td.delete')[0].innerHTML, '');
  97. });
  98. QUnit.test('removing a form-row also removed related row with non-field errors', function(assert) {
  99. const $ = django.jQuery;
  100. assert.ok(this.table.find('.row-form-errors').length);
  101. const tr = this.inlineRows.slice(1, 2);
  102. const trWithErrors = tr.prev();
  103. assert.ok(trWithErrors.hasClass('row-form-errors'));
  104. const deleteLink = tr.find('a.inline-deletelink');
  105. deleteLink.trigger($.Event('click', {target: deleteLink}));
  106. assert.notOk(this.table.find('.row-form-errors').length);
  107. });
  108. QUnit.module('admin.inlines: tabular formsets with max_num', {
  109. beforeEach: function() {
  110. const $ = django.jQuery;
  111. $('#qunit-fixture').append($('#tabular-formset-with-validation-error').text());
  112. this.table = $('table.inline');
  113. this.maxNum = $('input.id_second-MAX_NUM_FORMS');
  114. this.maxNum.val(2);
  115. this.inlineRows = this.table.find('tr.form-row');
  116. this.inlineRows.tabularFormset('table.inline tr.form-row', {
  117. prefix: 'second'
  118. });
  119. }
  120. });
  121. QUnit.test('does not show the add button if already at max_num', function(assert) {
  122. const addButton = this.table.find('tr.add_row > td > a');
  123. assert.notOk(addButton.is(':visible'));
  124. });
  125. QUnit.test('make addButton visible again', function(assert) {
  126. const $ = django.jQuery;
  127. const addButton = this.table.find('tr.add_row > td > a');
  128. const removeButton = this.table.find('tr.form-row:first').find('a.inline-deletelink');
  129. removeButton.trigger($.Event( "click", { target: removeButton } ));
  130. assert.notOk(addButton.is(':visible'));
  131. });
  132. QUnit.module('admin.inlines: tabular formsets with min_num', {
  133. beforeEach: function() {
  134. const $ = django.jQuery;
  135. $('#qunit-fixture').append($('#tabular-formset-with-validation-error').text());
  136. this.table = $('table.inline');
  137. this.minNum = $('input#id_second-MIN_NUM_FORMS');
  138. this.minNum.val(2);
  139. this.inlineRows = this.table.find('tr.form-row');
  140. this.inlineRows.tabularFormset('table.inline tr.form-row', {
  141. prefix: 'second'
  142. });
  143. }
  144. });
  145. QUnit.test('does not show the remove buttons if already at min_num', function(assert) {
  146. assert.notOk(this.table.find('.inline-deletelink:visible').length);
  147. });
  148. QUnit.test('make removeButtons visible again', function(assert) {
  149. const $ = django.jQuery;
  150. const addButton = this.table.find('tr.add-row > td > a');
  151. addButton.trigger($.Event( "click", { target: addButton } ));
  152. assert.equal(this.table.find('.inline-deletelink:visible').length, 2);
  153. });