Created
October 30, 2012 08:59
-
-
Save pawelpabich/3979127 to your computer and use it in GitHub Desktop.
Java Script unit testing with YUI Test and Jack mocking framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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