Using QUnit to Unit Test my Javascript Masks for Textboxes
Well after boasting that is COULD be unit tested in my previous article, I decided to show how I did unit test it using the very easy QUnit javascript unit testing library from jQuery.
To create a QUnit Test you will need a few things:
Javscript in its own file
Blank javascript unit test file
So i set my page that will run the tests in the following way:
Included my javascript file that contains the masking code (TempMember.ascx.js)
Included my unit test javascript file (TempMember.Test.js)
Created the QUnit placeholder <div>s and <ol>
Added my UI controls that I will be testing my Masks against
<!DOCTYPE html> <html> <head> <link rel="Stylesheet" href="../include/qunit.css" /> <script type="text/javascript" src="../include/qunit.js"></script> <script type="text/javascript" src="../include/qmock.js"></script> <script type="text/javascript" src="../../../../../Source/MyProject/include/jquery.js"></script> <script type="text/javascript" src="../../../../../Source/MyProject/TempMember.ascx.js"></script> <script type="text/javascript" src="TempMember.Test.js"></script> <title>Temp Member Tests</title> </head> <body> <div id="qunit-header"></div> <div id="qunit-banner"></div> <div id="qunit-testrunner-toolbar"></div> <div id="qunit-userAgent"></div> <ol id="qunit-tests"></ol> <div style="display:none;"> <input type="text" id="NumericMaskTest" onkeydown="maskFiltration.CaptureForMask(this, maskFiltration.masks.numericOnly)" onchange="maskFiltration.ApplyMask(this, maskFiltration.masks.numericOnly)" /> <input type="text" id="AlphaMaskTest" onkeydown="maskFiltration.CaptureForMask(this, maskFiltration.masks.alphaOnly)" onchange="maskFiltration.ApplyMask(this, maskFiltration.masks.alphaOnly)" /> <input type="text" id="FourCharNumericTest" onkeydown="maskFiltration.CaptureForMask(this, maskFiltration.masks.fourDigitMax)" onchange="maskFiltration.ApplyMask(this, maskFiltration.masks.fourDigitMax)" /> </div> </body> </html>
CREATE YOUR JAVSCRIPT UNIT TESTS
So we are ready now to write some tests! Some of my colleagues like to write their javascript test as anonymous functions. But I find declared functions significantly easier to debug.
// Three groups of tests /////////// var MaskTests = { NumericTest: function () { var test1 = jQuery('#NumericMaskTest') test1.val('1234'); test1.keydown(); test1.change(); same(test1.val(), "1234", "Result should be 1234"); test1.val('123sss4'); test1.keydown(); test1.change(); same(test1.val(), "1234", "Result should be 1234"); test1.val('123456'); test1.keydown(); test1.change(); same(test1.val(), "123456", "Result should be 123456"); }, AlphaTest: function () { var test1 = jQuery('#AlphaMaskTest') test1.val('aBcD'); test1.keydown(); test1.change(); same(test1.val(), "aBcD", "Result should be aBcD"); test1.val('123ssS4'); test1.keydown(); test1.change(); same(test1.val(), "aBcD", "Result should be aBcD"); test1.val('aBcDeFg'); test1.keydown(); test1.change(); same(test1.val(), "aBcDeFg", "Result should be aBcDeFg"); }, FourCharNumericTest: function () { var test1 = jQuery('#FourCharNumericTest') test1.val('1234'); test1.keydown(); test1.change(); same(test1.val(), "1234", "Result should be 1234"); test1.val('123a'); test1.keydown(); test1.change(); same(test1.val(), "1234", "Result should be 1234"); test1.val('12345'); test1.keydown(); test1.change(); same(test1.val(), "1234", "Result should be 1234"); test1.val('123'); test1.keydown(); test1.change(); same(test1.val(), "123", "Result should be 123"); } }; // The function to run before each test is executed function ResetMock() { jQuery('#NumericMaskTest').val(""); jQuery('#AlphaMaskTest').val(""); jQuery('#FourCharNumericTest').val(""); } // Give QUnit the function pointer QUnit.testStart = ResetMock; // Declare a section header (module) module("Mask Filtration Test"); // Execute Each group of tests test("Testing numeric", MaskTests.NumericTest); test("Testing alpha", MaskTests.AlphaTest); test("Testing 4 char numeric", MaskTests.FourCharNumericTest);
Now to run the unit tests, you can simply view the web page, and the test will execute. With more setup you can make the test results be captured by your automated build server.