2016-12-20 01:43:47 +08:00
|
|
|
/* global QUnit */
|
2015-08-26 12:24:55 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-12-20 01:43:47 +08:00
|
|
|
QUnit.module('admin.inlines: tabular formsets', {
|
2015-04-14 22:55:57 +08:00
|
|
|
beforeEach: function() {
|
2020-04-21 08:39:15 +08:00
|
|
|
const $ = django.jQuery;
|
|
|
|
const that = this;
|
2015-04-14 22:55:57 +08:00
|
|
|
this.addText = 'Add another';
|
|
|
|
|
|
|
|
$('#qunit-fixture').append($('#tabular-formset').text());
|
|
|
|
this.table = $('table.inline');
|
|
|
|
this.inlineRow = this.table.find('tr');
|
2019-10-24 22:37:55 +08:00
|
|
|
this.inlineRow.tabularFormset('table.inline tr.form-row', {
|
2015-04-14 22:55:57 +08:00
|
|
|
prefix: 'first',
|
|
|
|
addText: that.addText,
|
|
|
|
deleteText: 'Remove'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-12-20 01:43:47 +08:00
|
|
|
QUnit.test('no forms', function(assert) {
|
2015-04-14 22:55:57 +08:00
|
|
|
assert.ok(this.inlineRow.hasClass('dynamic-first'));
|
|
|
|
assert.equal(this.table.find('.add-row a').text(), this.addText);
|
|
|
|
});
|
|
|
|
|
2016-12-20 01:43:47 +08:00
|
|
|
QUnit.test('add form', function(assert) {
|
2020-04-21 08:39:15 +08:00
|
|
|
const addButton = this.table.find('.add-row a');
|
2015-04-14 22:55:57 +08:00
|
|
|
assert.equal(addButton.text(), this.addText);
|
|
|
|
addButton.click();
|
2020-03-08 01:35:59 +08:00
|
|
|
assert.ok(this.table.find('#first-1'));
|
2015-04-14 22:55:57 +08:00
|
|
|
});
|
2015-06-19 00:27:58 +08:00
|
|
|
|
2019-10-24 22:37:55 +08:00
|
|
|
QUnit.test('added form has remove button', function(assert) {
|
2020-04-21 08:39:15 +08:00
|
|
|
const addButton = this.table.find('.add-row a');
|
2019-10-24 22:37:55 +08:00
|
|
|
assert.equal(addButton.text(), this.addText);
|
|
|
|
addButton.click();
|
2020-03-08 01:35:59 +08:00
|
|
|
assert.equal(this.table.find('#first-1 .inline-deletelink').length, 1);
|
2019-10-24 22:37:55 +08:00
|
|
|
});
|
|
|
|
|
2016-12-20 01:43:47 +08:00
|
|
|
QUnit.test('add/remove form events', function(assert) {
|
2022-02-23 17:33:07 +08:00
|
|
|
assert.expect(5);
|
2020-04-21 08:39:15 +08:00
|
|
|
const addButton = this.table.find('.add-row a');
|
2022-02-23 17:33:07 +08:00
|
|
|
document.addEventListener('formset:added', (event) => {
|
2015-06-19 00:27:58 +08:00
|
|
|
assert.ok(true, 'event `formset:added` triggered');
|
2022-02-23 17:33:07 +08:00
|
|
|
assert.equal(true, event.target.matches('#first-1'));
|
|
|
|
assert.equal(event.detail.formsetName, 'first');
|
|
|
|
}, {once: true});
|
2015-06-19 00:27:58 +08:00
|
|
|
addButton.click();
|
2020-04-21 08:39:15 +08:00
|
|
|
const deleteLink = this.table.find('.inline-deletelink');
|
2022-02-23 17:33:07 +08:00
|
|
|
document.addEventListener('formset:removed', (event) => {
|
2015-06-19 00:27:58 +08:00
|
|
|
assert.ok(true, 'event `formset:removed` triggered');
|
2022-02-23 17:33:07 +08:00
|
|
|
assert.equal(event.detail.formsetName, 'first');
|
|
|
|
}, {once: true});
|
|
|
|
deleteLink.click();
|
2015-06-19 00:27:58 +08:00
|
|
|
});
|
2016-06-29 03:19:53 +08:00
|
|
|
|
2016-12-20 01:43:47 +08:00
|
|
|
QUnit.test('existing add button', function(assert) {
|
2020-04-21 08:39:15 +08:00
|
|
|
const $ = django.jQuery;
|
2019-06-21 23:57:36 +08:00
|
|
|
$('#qunit-fixture').empty(); // Clear the table added in beforeEach
|
2016-06-29 03:19:53 +08:00
|
|
|
$('#qunit-fixture').append($('#tabular-formset').text());
|
|
|
|
this.table = $('table.inline');
|
|
|
|
this.inlineRow = this.table.find('tr');
|
|
|
|
this.table.append('<i class="add-button"></i>');
|
2020-04-21 08:39:15 +08:00
|
|
|
const addButton = this.table.find('.add-button');
|
2017-12-22 03:26:54 +08:00
|
|
|
this.inlineRow.tabularFormset('table.inline tr', {
|
2016-06-29 03:19:53 +08:00
|
|
|
prefix: 'first',
|
|
|
|
deleteText: 'Remove',
|
|
|
|
addButton: addButton
|
|
|
|
});
|
|
|
|
assert.equal(this.table.find('.add-row a').length, 0);
|
|
|
|
addButton.click();
|
2020-03-08 01:35:59 +08:00
|
|
|
assert.ok(this.table.find('#first-1'));
|
2016-06-29 03:19:53 +08:00
|
|
|
});
|
2019-10-24 22:37:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
QUnit.module('admin.inlines: tabular formsets with validation errors', {
|
|
|
|
beforeEach: function() {
|
2020-04-21 08:39:15 +08:00
|
|
|
const $ = django.jQuery;
|
2019-10-24 22:37:55 +08:00
|
|
|
|
|
|
|
$('#qunit-fixture').append($('#tabular-formset-with-validation-error').text());
|
|
|
|
this.table = $('table.inline');
|
|
|
|
this.inlineRows = this.table.find('tr.form-row');
|
|
|
|
this.inlineRows.tabularFormset('table.inline tr.form-row', {
|
|
|
|
prefix: 'second'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test('first form has delete checkbox and no button', function(assert) {
|
2020-04-21 08:39:15 +08:00
|
|
|
const tr = this.inlineRows.slice(0, 1);
|
2019-10-24 22:37:55 +08:00
|
|
|
assert.ok(tr.hasClass('dynamic-second'));
|
|
|
|
assert.ok(tr.hasClass('has_original'));
|
|
|
|
assert.equal(tr.find('td.delete input').length, 1);
|
|
|
|
assert.equal(tr.find('td.delete .inline-deletelink').length, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test('dynamic form has remove button', function(assert) {
|
2020-04-21 08:39:15 +08:00
|
|
|
const tr = this.inlineRows.slice(1, 2);
|
2019-10-24 22:37:55 +08:00
|
|
|
assert.ok(tr.hasClass('dynamic-second'));
|
|
|
|
assert.notOk(tr.hasClass('has_original'));
|
|
|
|
assert.equal(tr.find('.inline-deletelink').length, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test('dynamic template has nothing', function(assert) {
|
2020-04-21 08:39:15 +08:00
|
|
|
const tr = this.inlineRows.slice(2, 3);
|
2019-10-24 22:37:55 +08:00
|
|
|
assert.ok(tr.hasClass('empty-form'));
|
|
|
|
assert.notOk(tr.hasClass('dynamic-second'));
|
|
|
|
assert.notOk(tr.hasClass('has_original'));
|
|
|
|
assert.equal(tr.find('td.delete')[0].innerHTML, '');
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test('removing a form-row also removed related row with non-field errors', function(assert) {
|
2020-04-21 08:39:15 +08:00
|
|
|
const $ = django.jQuery;
|
2019-10-24 22:37:55 +08:00
|
|
|
assert.ok(this.table.find('.row-form-errors').length);
|
2020-04-21 08:39:15 +08:00
|
|
|
const tr = this.inlineRows.slice(1, 2);
|
|
|
|
const trWithErrors = tr.prev();
|
2019-10-24 22:37:55 +08:00
|
|
|
assert.ok(trWithErrors.hasClass('row-form-errors'));
|
2020-04-21 08:39:15 +08:00
|
|
|
const deleteLink = tr.find('a.inline-deletelink');
|
2019-10-24 22:37:55 +08:00
|
|
|
deleteLink.trigger($.Event('click', {target: deleteLink}));
|
|
|
|
assert.notOk(this.table.find('.row-form-errors').length);
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.module('admin.inlines: tabular formsets with max_num', {
|
|
|
|
beforeEach: function() {
|
2020-04-21 08:39:15 +08:00
|
|
|
const $ = django.jQuery;
|
2019-10-24 22:37:55 +08:00
|
|
|
$('#qunit-fixture').append($('#tabular-formset-with-validation-error').text());
|
|
|
|
this.table = $('table.inline');
|
|
|
|
this.maxNum = $('input.id_second-MAX_NUM_FORMS');
|
|
|
|
this.maxNum.val(2);
|
|
|
|
this.inlineRows = this.table.find('tr.form-row');
|
|
|
|
this.inlineRows.tabularFormset('table.inline tr.form-row', {
|
|
|
|
prefix: 'second'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test('does not show the add button if already at max_num', function(assert) {
|
2020-04-21 08:39:15 +08:00
|
|
|
const addButton = this.table.find('tr.add_row > td > a');
|
2019-10-24 22:37:55 +08:00
|
|
|
assert.notOk(addButton.is(':visible'));
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test('make addButton visible again', function(assert) {
|
2020-04-21 08:39:15 +08:00
|
|
|
const $ = django.jQuery;
|
|
|
|
const addButton = this.table.find('tr.add_row > td > a');
|
|
|
|
const removeButton = this.table.find('tr.form-row:first').find('a.inline-deletelink');
|
2019-10-24 22:37:55 +08:00
|
|
|
removeButton.trigger($.Event( "click", { target: removeButton } ));
|
|
|
|
assert.notOk(addButton.is(':visible'));
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
QUnit.module('admin.inlines: tabular formsets with min_num', {
|
|
|
|
beforeEach: function() {
|
2020-04-21 08:39:15 +08:00
|
|
|
const $ = django.jQuery;
|
2019-10-24 22:37:55 +08:00
|
|
|
$('#qunit-fixture').append($('#tabular-formset-with-validation-error').text());
|
|
|
|
this.table = $('table.inline');
|
|
|
|
this.minNum = $('input#id_second-MIN_NUM_FORMS');
|
|
|
|
this.minNum.val(2);
|
|
|
|
this.inlineRows = this.table.find('tr.form-row');
|
|
|
|
this.inlineRows.tabularFormset('table.inline tr.form-row', {
|
|
|
|
prefix: 'second'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test('does not show the remove buttons if already at min_num', function(assert) {
|
|
|
|
assert.notOk(this.table.find('.inline-deletelink:visible').length);
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test('make removeButtons visible again', function(assert) {
|
2020-04-21 08:39:15 +08:00
|
|
|
const $ = django.jQuery;
|
|
|
|
const addButton = this.table.find('tr.add-row > td > a');
|
2019-10-24 22:37:55 +08:00
|
|
|
addButton.trigger($.Event( "click", { target: addButton } ));
|
|
|
|
assert.equal(this.table.find('.inline-deletelink:visible').length, 2);
|
|
|
|
});
|