diff --git a/media/js/zamboni/devhub.js b/media/js/zamboni/devhub.js index 3588381fb4..26be49ab2b 100644 --- a/media/js/zamboni/devhub.js +++ b/media/js/zamboni/devhub.js @@ -909,16 +909,21 @@ function initCatFields() { $main = $(this).find(".addon-categories"), $misc = $(this).find(".addon-misc-category"), maxCats = parseInt($parent.attr("data-max-categories")); - var checkMain = function() { + var checkMainDefault = function() { var checkedLength = $("input:checked", $main).length, disabled = checkedLength >= maxCats; - $("input", $misc).attr("checked", checkedLength <= 0); $("input:not(:checked)", $main).attr("disabled", disabled); + return checkedLength; + }; + var checkMain = function() { + var checkedLength = checkMainDefault(); + $("input", $misc).attr("checked", checkedLength <= 0); }; var checkOther = function() { $("input", $main).attr("checked", false).attr("disabled", false); }; - $("input", $main).live("change", checkMain).trigger("change"); + checkMainDefault(); + $("input", $main).live("change", checkMain); $("input", $misc).live("change", checkOther); }); } diff --git a/media/js/zamboni/tests/devhub_tests.js b/media/js/zamboni/tests/devhub_tests.js index 7871477d45..5cb9c1ea92 100644 --- a/media/js/zamboni/tests/devhub_tests.js +++ b/media/js/zamboni/tests/devhub_tests.js @@ -1,5 +1,37 @@ $(document).ready(function(){ +var catFixture = { + setup: function() { + this.sandbox = tests.createSandbox('#addon-cats'); + initCatFields(); + }, + teardown: function() { + this.sandbox.remove(); + } +}; + +module('initCatFields', catFixture); + +test('Default with initial categories', function() { + var scope = $("#addon-cats-fx", self.sandbox); + var checkedChoices = $("input:checked", scope); + equals(checkedChoices.length, 2); + equals(checkedChoices[0].id, "id_form-0-categories_1"); + equals(checkedChoices[1].id, "id_form-0-categories_2"); + + // 2 categories are selected, the other category should be disabled. + var max = scope.parent("div").attr("data-max-categories"); + equals(parseInt(max), 2); + var disabledChoices = $("input:disabled", scope); + equals(disabledChoices.length, 1); + equals(disabledChoices[0].id, "id_form-0-categories_0"); +}); + +test('Default without initial categories', function() { + equals($("#addon-cats-tb input:checked", self.sandbox).length, 0); +}); + + function pushTiersAndResults($suite, tiers, results) { $.each(['1','2','3','4'], function(i, val) { tiers.push($('[class~="test-tier"][data-tier="' + val + '"]', diff --git a/templates/qunit.html b/templates/qunit.html index 22c8fc59a3..194d479b07 100644 --- a/templates/qunit.html +++ b/templates/qunit.html @@ -117,6 +117,32 @@ {{ _('Edit') }} +