1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

DEP 0003 -- Added JavaScript unit tests.

Setup QUnit, added tests, and measured test coverage.

Thanks to Nick Sanford for the initial tests.
This commit is contained in:
Trey Hunner
2015-04-14 10:55:57 -04:00
committed by Tim Graham
parent 3bbaf84d65
commit 2d0dead224
19 changed files with 4716 additions and 2 deletions

View File

@@ -0,0 +1,15 @@
module('admin.DateTimeShortcuts');
test('init', function(assert) {
var $ = django.jQuery;
var dateField = $('<input type="text" class="vDateField" value="2015-03-16"><br>');
$('#qunit-fixture').append(dateField);
DateTimeShortcuts.init();
var shortcuts = $('.datetimeshortcuts');
assert.equal(shortcuts.length, 1);
assert.equal(shortcuts.find('a:first').text(), 'Today');
assert.equal(shortcuts.find('a:last .date-icon').length, 1);
});

View File

@@ -0,0 +1,22 @@
module('admin.RelatedObjectLookups');
test('html_unescape', function(assert) {
function assert_unescape(then, expected, message) {
assert.equal(html_unescape(then), expected, message);
}
assert_unescape('&lt;', '<', 'less thans are unescaped');
assert_unescape('&gt;', '>', 'greater thans are unescaped');
assert_unescape('&quot;', '"', 'double quotes are unescaped');
assert_unescape('&#39;', "'", 'single quotes are unescaped');
assert_unescape('&amp;', '&', 'ampersands are unescaped');
});
test('id_to_windowname', function(assert) {
assert.equal(id_to_windowname('.test'), '__dot__test');
assert.equal(id_to_windowname('misc-test'), 'misc__dash__test');
});
test('windowname_to_id', function(assert) {
assert.equal(windowname_to_id('__dot__test'), '.test');
assert.equal(windowname_to_id('misc__dash__test'), 'misc-test');
});

View File

@@ -0,0 +1,20 @@
module('admin.SelectBox');
test('init: no options', function(assert) {
var $ = django.jQuery;
$('<select id="id"></select>').appendTo('#qunit-fixture');
SelectBox.init('id');
assert.equal(SelectBox.cache['id'].length, 0);
});
test('filter', function(assert) {
var $ = django.jQuery;
$('<select id="id"></select>').appendTo('#qunit-fixture');
$('<option value="0">A</option>').appendTo('#id');
$('<option value="1">B</option>').appendTo('#id');
SelectBox.init('id');
assert.equal($('#id option').length, 2);
SelectBox.filter('id', "A");
assert.equal($('#id option').length, 1);
assert.equal($('#id option').text(), "A");
});

View File

@@ -0,0 +1,14 @@
module('admin.SelectFilter2');
test('init', function(assert) {
var $ = django.jQuery;
$('<form><select id="id"></select></form>').appendTo('#qunit-fixture');
$('<option value="0">A</option>').appendTo('#id');
SelectFilter.init('id', 'things', 0);
assert.equal($('.selector-available h2').text().trim(), "Available things");
assert.equal($('.selector-chosen h2').text().trim(), "Chosen things");
assert.equal($('.selector-chooseall').text(), "Choose all");
assert.equal($('.selector-add').text(), "Choose");
assert.equal($('.selector-remove').text(), "Remove");
assert.equal($('.selector-clearall').text(), "Remove all");
});

View File

@@ -0,0 +1,18 @@
module('admin.actions', {
beforeEach: function() {
// Number of results shown on page
window._actions_icnt = '100';
var $ = django.jQuery;
$('#qunit-fixture').append($('#result-table').text());
$('tr input.action-select').actions();
}
});
test('check', function(assert) {
var $ = django.jQuery;
assert.notOk($('.action-select').is(':checked'));
$('#action-toggle').click();
assert.ok($('.action-select').is(':checked'));
});

View File

@@ -0,0 +1,59 @@
module('admin.core');
test('Date.getTwelveHours', function(assert) {
assert.equal(new Date(2011, 0, 1, 0, 0).getTwelveHours(), 12, '0:00');
assert.equal(new Date(2011, 0, 1, 11, 0).getTwelveHours(), 11, '11:00');
assert.equal(new Date(2011, 0, 1, 16, 0).getTwelveHours(), 4, '16:00');
});
test('Date.getTwoDigitMonth', function(assert) {
assert.equal(new Date(2011, 0, 1).getTwoDigitMonth(), '01', 'jan 1');
assert.equal(new Date(2011, 9, 1).getTwoDigitMonth(), '10', 'oct 1');
});
test('Date.getTwoDigitDate', function(assert) {
assert.equal(new Date(2011, 0, 1).getTwoDigitDate(), '01', 'jan 1');
assert.equal(new Date(2011, 0, 15).getTwoDigitDate(), '15', 'jan 15');
});
test('Date.getTwoDigitTwelveHour', function(assert) {
assert.equal(new Date(2011, 0, 1, 0, 0).getTwoDigitTwelveHour(), '12', '0:00');
assert.equal(new Date(2011, 0, 1, 4, 0).getTwoDigitTwelveHour(), '04', '4:00');
assert.equal(new Date(2011, 0, 1, 22, 0).getTwoDigitTwelveHour(), '10', '22:00');
});
test('Date.getTwoDigitHour', function(assert) {
assert.equal(new Date(2014, 6, 1, 9, 0).getTwoDigitHour(), '09', '9:00 am is 09');
assert.equal(new Date(2014, 6, 1, 11, 0).getTwoDigitHour(), '11', '11:00 am is 11');
});
test('Date.getTwoDigitMinute', function(assert) {
assert.equal(new Date(2014, 6, 1, 0, 5).getTwoDigitMinute(), '05', '12:05 am is 05');
assert.equal(new Date(2014, 6, 1, 0, 15).getTwoDigitMinute(), '15', '12:15 am is 15');
});
test('Date.getTwoDigitSecond', function(assert) {
assert.equal(new Date(2014, 6, 1, 0, 0, 2).getTwoDigitSecond(), '02', '12:00:02 am is 02');
assert.equal(new Date(2014, 6, 1, 0, 0, 20).getTwoDigitSecond(), '20', '12:00:20 am is 20');
});
test('Date.getHourMinute', function(assert) {
assert.equal(new Date(2014, 6, 1, 11, 0).getHourMinute(), '11:00', '11:00 am is 11:00');
assert.equal(new Date(2014, 6, 1, 13, 25).getHourMinute(), '13:25', '1:25 pm is 13:25');
});
test('Date.getHourMinuteSecond', function(assert) {
assert.equal(new Date(2014, 6, 1, 11, 0, 0).getHourMinuteSecond(), '11:00:00', '11:00 am is 11:00:00');
assert.equal(new Date(2014, 6, 1, 17, 45, 30).getHourMinuteSecond(), '17:45:30', '5:45:30 pm is 17:45:30');
});
test('Date.strftime', function(assert) {
var date = new Date(2014, 6, 1, 11, 0, 5);
assert.equal(date.strftime('%Y-%m-%d %H:%M:%S'), '2014-07-01 11:00:05');
});
test('String.strptime', function(assert) {
var date = new Date(1988, 1, 26);
assert.equal('1988-02-26'.strptime('%Y-%m-%d').toString(), date.toString());
assert.equal('26/02/88'.strptime('%d/%m/%y').toString(), date.toString());
});

View File

@@ -0,0 +1,28 @@
module('admin.inlines: tabular formsets', {
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');
that.inlineRow.tabularFormset({
prefix: 'first',
addText: that.addText,
deleteText: 'Remove'
});
}
});
test('no forms', function(assert) {
assert.ok(this.inlineRow.hasClass('dynamic-first'));
assert.equal(this.table.find('.add-row a').text(), this.addText);
});
test('add form', function(assert) {
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'));
});

View File

@@ -0,0 +1,82 @@
(function (globals) {
var django = globals.django || (globals.django = {});
django.pluralidx = function (count) { return (count == 1) ? 0 : 1; };
/* gettext identity library */
django.gettext = function (msgid) { return msgid; };
django.ngettext = function (singular, plural, count) { return (count == 1) ? singular : plural; };
django.gettext_noop = function (msgid) { return msgid; };
django.pgettext = function (context, msgid) { return msgid; };
django.npgettext = function (context, singular, plural, count) { return (count == 1) ? singular : plural; };
django.interpolate = function (fmt, obj, named) {
if (named) {
return fmt.replace(/%\(\w+\)s/g, function(match){return String(obj[match.slice(2,-2)])});
} else {
return fmt.replace(/%s/g, function(match){return String(obj.shift())});
}
};
/* formatting library */
django.formats = {
"DATETIME_FORMAT": "N j, Y, P",
"DATETIME_INPUT_FORMATS": [
"%Y-%m-%d %H:%M:%S",
"%Y-%m-%d %H:%M:%S.%f",
"%Y-%m-%d %H:%M",
"%Y-%m-%d",
"%m/%d/%Y %H:%M:%S",
"%m/%d/%Y %H:%M:%S.%f",
"%m/%d/%Y %H:%M",
"%m/%d/%Y",
"%m/%d/%y %H:%M:%S",
"%m/%d/%y %H:%M:%S.%f",
"%m/%d/%y %H:%M",
"%m/%d/%y"
],
"DATE_FORMAT": "N j, Y",
"DATE_INPUT_FORMATS": [
"%Y-%m-%d",
"%m/%d/%Y",
"%m/%d/%y"
],
"DECIMAL_SEPARATOR": ".",
"FIRST_DAY_OF_WEEK": "0",
"MONTH_DAY_FORMAT": "F j",
"NUMBER_GROUPING": "3",
"SHORT_DATETIME_FORMAT": "m/d/Y P",
"SHORT_DATE_FORMAT": "m/d/Y",
"THOUSAND_SEPARATOR": ",",
"TIME_FORMAT": "P",
"TIME_INPUT_FORMATS": [
"%H:%M:%S",
"%H:%M:%S.%f",
"%H:%M"
],
"YEAR_MONTH_FORMAT": "F Y"
};
django.get_format = function (format_type) {
var value = django.formats[format_type];
if (typeof(value) == 'undefined') {
return format_type;
} else {
return value;
}
};
/* add to global namespace */
globals.pluralidx = django.pluralidx;
globals.gettext = django.gettext;
globals.ngettext = django.ngettext;
globals.gettext_noop = django.gettext_noop;
globals.pgettext = django.pgettext;
globals.npgettext = django.npgettext;
globals.interpolate = django.interpolate;
globals.get_format = django.get_format;
}(this));

View File

@@ -0,0 +1,28 @@
module('admin.timeparse');
test('parseTimeString', function(assert) {
function time(then, expected) {
assert.equal(parseTimeString(then), expected);
}
time('9', '09:00');
time('09', '09:00');
time('13:00', '13:00');
time('13.00', '13:00');
time('9:00', '09:00');
time('9.00', '09:00');
time('3 am', '03:00');
time('3 a.m.', '03:00');
time('12 am', '00:00');
time('11 am', '11:00');
time('12 pm', '12:00');
time('3am', '03:00');
time('3.30 am', '03:30');
time('3:15 a.m.', '03:15');
time('3.00am', '03:00');
time('12.00am', '00:00');
time('11.00am', '11:00');
time('12.00pm', '12:00');
time('noon', '12:00');
time('midnight', '00:00');
time('something else', 'something else');
});