Dr Nic’s JavaScript Test Helpers

Get Version

0.8.0

What

This JavaScript project provides help for unit testing other JavaScript projects:

Downloading

Latest version: drnic_js_test_helpers.js

Alternately, to get the complete project source go to the downloads page on rubyforge.org, or by clicking on “Get Version” badge above and clicking through to “drnic_js_test_helpers”.

Save the file within your JavaScript project, say in test/assets, and link to it from your HTML test files.

Setup/installation

This library is for use within unittest.js related HTML test files. If your test HTML files are in test/ and the library is saved at test/assets/drnic_js_test_helpers.js, then within the <head> of each HTML test file add:

<script src="assets/drnic_js_test_helpers.js" type="text/javascript"></script>  

Demonstration of usage

This library currently offers some helpers:

Event simulation

Event.simulate('element', 'eventName')

Ajax request mocking

Ajax.Request mocking (for Prototypejs based projects)

Within your test or setup method:

Ajax.Request.setupMock('/some/url', function(request, response) {
  response.responseJSON = "{data: 123}";
  request.options.onComplete(response);
});

Now, during your test if new Ajax.Request('/some/url', {onComplete: function() {...}}) is called, then the onComplete callback will be invoked, and responseJSON will have a mocked out value.

Within your teardown method it is advisable to clear the mocks, thus each test needs to setup the mock it requires:

teardown: function() {
  Ajax.Request.clearMocks();
},

The clearMocks call is to restore the Ajax mechanism; which is used when you run tests via rake test to communicate the test results back to the command-line test server. If rake test is hanging, it is likely you are not calling clearMocks in your teardown method.

Extra assertions

Your test cases can use the following additional assertions:

For example, if you wanted to test that some code created an additional >div< element in the DOM, then your test might be:

testExtraDiv: function() { with(this) {
  assertDifference("$$('div').length", function() {
    // do something that creates new <div> element
  }, 1, "should not be different");
}},

There is a primitive selector assertion assertTagDifference:

assertTagDifference('span', function() {
  var span = document.createElement("span");
  Test.$('sample').appendChild(span);
}, 1);

Other test helpers

The protolicious source has some helper files, see source

Forum

http://groups.google.com/group/drnicutilities

How to submit patches

Read the 8 steps for fixing other people’s code and for section 8b: Submit patch to Google Groups, use the Google Group above.

The source project is a Git repository. See Dr Nic’s master branch for clone/checkout details.

License

This code is free to use under the terms of the MIT license.

Contact

Comments are welcome. Send an email to Dr Nic Williams via the forum

Dr Nic Williams, 10th April 2008
Theme extended from Paul Battley