Skip to content

Instantly share code, notes, and snippets.

@pawelpabich
Created October 30, 2012 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawelpabich/3979127 to your computer and use it in GitHub Desktop.
Save pawelpabich/3979127 to your computer and use it in GitHub Desktop.
Java Script unit testing with YUI Test and Jack mocking framework
JSUnitTesting = {
getPredefinedReasons: function () {
var values = [];
$("input[type='checkbox']").each(function () {
values.push($(this).is(":checked"));
});
return values;
},
getCustomReason: function () {
return $("input[type='text']:first").val();
},
validate: function (predefinedReasons, customReason) {
var numberOfSelectedPredefiniedReasons = 0;
for (var i = 0; i < predefinedReasons.length; i++) {
if (predefinedReasons[i]) {
numberOfSelectedPredefiniedReasons++;
}
}
var isCustomResonSelected = customReason.length > 0;
if (numberOfSelectedPredefiniedReasons > 0 && isCustomResonSelected) return false;
if (numberOfSelectedPredefiniedReasons == 0 && !isCustomResonSelected) return false;
return true;
},
init: function () {
var self = this;
$("form").submit(function () {
self.formSubmission();
return false;
});
},
submitForm: function () {
var predefinedReasons = this.getPredefinedReasons();
var customReason = this.getCustomReason();
if (!this.validate(predefinedReasons, customReason)) {
this.notify("Wrong ! Select either one (or more checkboxes) " + "or type a custom reason.");
} else {
this.notify("Correct !");
}
},
notify: function (message) {
alert(message);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment