2016-12-19 17:43:47 +00:00
|
|
|
/* global QUnit */
|
2015-08-26 04:24:55 +00:00
|
|
|
/* eslint global-strict: 0, strict: 0 */
|
|
|
|
'use strict';
|
|
|
|
|
2016-12-19 17:43:47 +00:00
|
|
|
QUnit.module('admin.inlines: tabular formsets', {
|
2015-04-14 14:55:57 +00:00
|
|
|
beforeEach: function() {
|
|
|
|
var $ = django.jQuery;
|
|
|
|
var that = this;
|
|
|
|
this.addText = 'Add another';
|
|
|
|
|
|
|
|
$('#qunit-fixture').append($('#tabular-formset').text());
|
|
|
|
this.table = $('table.inline');
|
|
|
|
this.inlineRow = this.table.find('tr');
|
2017-12-21 19:26:54 +00:00
|
|
|
that.inlineRow.tabularFormset('table.inline tr', {
|
2015-04-14 14:55:57 +00:00
|
|
|
prefix: 'first',
|
|
|
|
addText: that.addText,
|
|
|
|
deleteText: 'Remove'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-12-19 17:43:47 +00:00
|
|
|
QUnit.test('no forms', function(assert) {
|
2015-04-14 14:55:57 +00:00
|
|
|
assert.ok(this.inlineRow.hasClass('dynamic-first'));
|
|
|
|
assert.equal(this.table.find('.add-row a').text(), this.addText);
|
|
|
|
});
|
|
|
|
|
2016-12-19 17:43:47 +00:00
|
|
|
QUnit.test('add form', function(assert) {
|
2015-04-14 14:55:57 +00:00
|
|
|
var addButton = this.table.find('.add-row a');
|
|
|
|
assert.equal(addButton.text(), this.addText);
|
|
|
|
addButton.click();
|
|
|
|
assert.ok(this.table.find('#first-1').hasClass('row2'));
|
|
|
|
});
|
2015-06-18 16:27:58 +00:00
|
|
|
|
2016-12-19 17:43:47 +00:00
|
|
|
QUnit.test('add/remove form events', function(assert) {
|
2015-06-18 16:27:58 +00:00
|
|
|
assert.expect(6);
|
|
|
|
var $ = django.jQuery;
|
|
|
|
var $document = $(document);
|
|
|
|
var addButton = this.table.find('.add-row a');
|
|
|
|
$document.on('formset:added', function(event, $row, formsetName) {
|
|
|
|
assert.ok(true, 'event `formset:added` triggered');
|
|
|
|
assert.equal(true, $row.is($('.row2')));
|
|
|
|
assert.equal(formsetName, 'first');
|
|
|
|
});
|
|
|
|
addButton.click();
|
|
|
|
var deletedRow = $('.row2');
|
|
|
|
var deleteLink = this.table.find('.inline-deletelink');
|
|
|
|
$document.on('formset:removed', function(event, $row, formsetName) {
|
|
|
|
assert.ok(true, 'event `formset:removed` triggered');
|
|
|
|
assert.equal(true, $row.is(deletedRow));
|
|
|
|
assert.equal(formsetName, 'first');
|
|
|
|
});
|
|
|
|
deleteLink.click();
|
|
|
|
});
|
2016-06-28 19:19:53 +00:00
|
|
|
|
2016-12-19 17:43:47 +00:00
|
|
|
QUnit.test('existing add button', function(assert) {
|
2016-06-28 19:19:53 +00:00
|
|
|
var $ = django.jQuery;
|
|
|
|
$('#qunit-fixture').empty(); // Clear the table added in beforeEach
|
|
|
|
$('#qunit-fixture').append($('#tabular-formset').text());
|
|
|
|
this.table = $('table.inline');
|
|
|
|
this.inlineRow = this.table.find('tr');
|
|
|
|
this.table.append('<i class="add-button"></i>');
|
|
|
|
var addButton = this.table.find('.add-button');
|
2017-12-21 19:26:54 +00:00
|
|
|
this.inlineRow.tabularFormset('table.inline tr', {
|
2016-06-28 19:19:53 +00:00
|
|
|
prefix: 'first',
|
|
|
|
deleteText: 'Remove',
|
|
|
|
addButton: addButton
|
|
|
|
});
|
|
|
|
assert.equal(this.table.find('.add-row a').length, 0);
|
|
|
|
addButton.click();
|
|
|
|
assert.ok(this.table.find('#first-1').hasClass('row2'));
|
|
|
|
});
|