I suggest splitting this in these parts:
- Check the conversion from input to output, i.e.:
"51" -> "51.0%" - Check if your way of changing
input value works - Check if the event listener you called is called when it is needed.
If all of these tests are successful, you can assume that combining them together will work.
To test the conversion method, I would suggest translating its logic into a separate, clean function. I pasted your format logic below and removed the side effect of setValue . I have included some tests (you should check them and see if you need more / they meet your requirements). As an example, I cited two failed tests.
String formation test:
function addPercentSign(val) { var inputNumber = parseFloat(val); if (inputNumber) { if (inputNumber < 50 || inputNumber > 100) {
<link href="https://code.jquery.com/qunit/qunit-1.12.0.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/qunit/qunit-1.12.0.js"></script> <div id="qunit"></div> <div id="qunit-fixture"></div>
Now that you have received this method, you need to find out if you can get the values โโfrom and enter the values โโin the input field. They are similar to the ones you already wrote:
Getting and setting value input
function createInput() { return $('<input id="learningCurveInput" type="text" value="hello world"/>') .appendTo('#qunit-fixture'); } module("Writing and reading to input", {}) test("writes to value", function(assert) { var $input = createInput(); var result = "55";
<link href="https://code.jquery.com/qunit/qunit-1.12.0.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/qunit/qunit-1.12.0.js"></script> <div id="qunit"></div> <div id="qunit-fixture"></div>
Now that you know that you can (a) correctly convert the values โโand (b) read and write the values โโcorrectly, you will need to check if the value of your variable value โ transform value โ set value is called correctly. For instance:
Testing event listeners
jQuery has several convenient methods for hooking up and running event listeners. You can use .change or submit without an argument to simulate user interface input. Alternatively, you can click on the submit button.
function createForm() { return $("<form></form>") .append(createInput()); } function createInput() { return $('<input id="learningCurveInput" type="text" value="hello world"/>') .appendTo('#qunit-fixture'); } module("Form event listeners", {}) test("input executes method on change", function(assert) { var called = false; var onChange = function() { called = true; }; var $input = createInput(); $input.change(onChange); $input.val("test"); strictEqual(called, false); $input.change(); strictEqual(called, true); }); test("form executes method on submit", function(assert) { var called = false; var onSubmit = function() { called = true; }; var $form = createForm(); var $input = $form.find("input"); $form.submit(onSubmit); strictEqual(called, false); $form.submit(); strictEqual(called, true); });
<link href="https://code.jquery.com/qunit/qunit-1.12.0.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/qunit/qunit-1.12.0.js"></script> <div id="qunit"></div> <div id="qunit-fixture"></div>
Conclusion
Now you can determine if your code execution extends to your tests:
$("form").submit(function() { // Tested in test 3 var $input = $(this).find("input"); var originalValue = $input.val(); // Tested in test 2 var newValue = addPercentSign(originalValue); // Tested in test 1 $input.val(newValue); // Tested in test 2 });
Note that this is basically the first test module that has user logic and requirements. If you use jQuery, which is already heavily tested, you will not need to re-test the tests for methods such as .val() : check their public repo to see the coverage for them. If you use special methods to interact with the DOM, you need to test them.
So, in a word: rewrite addPercentageSign will be a pure function that takes a string and returns a string; make sure it is thoroughly tested. Interact with the DOM through the test library or write tests for editing the DOM as well as for listening to events.