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