Unit testing JavaScript with JSUnit

Run the test suite below

function suite() { var ajax_suite = new top.jsUnitTestSuite(); ajax_suite.addTestPage('tests/event_dispatcher.html'); ajax_suite.addTestPage('tests/ajax_request.html'); return ajax_suite; }

Test the EventDispatcher Object

/** * A simple test to verify that the CustomEvent object has not broken */ function testCustomEvent() { assertEquals("CustomEvent must have an object member variable of 'type' set to 'custom'", (new CustomEvent).type, 'custom'); } /** * Extend the EventDispatcher with types one and three */ function TestDispatcher() { } TestDispatcher.prototype = new EventDispatcher; TestDispatcher.prototype.events = { 'one' : new Array(), 'three' : new Array() }; /** * A variable to catch output generated by events */ var answers; /** * A variable to become the TestDispatcher instance */ /** * A simple function to return the answer */ function whatDoYouGetWhenYouMultiplySixByNine(e) { if (!answers[e.type]) { answers[e.type] = {}; } answers[e.type].whatDoYouGetWhenYouMultiplySixByNine = 42; } /** * A simple object to return its answer */ function TestObject() { } TestObject.prototype = { answer : 42, howManyRoadsMustAManWalkDown : function(e) { if (!answers[e.type]) { answers[e.type] = {}; } answers[e.type].howManyRoadsMustAManWalkDown = this.answer; } } /** * Create a new instance of the TestDispatcher for testing */ function setUp() { dispatcher = new TestDispatcher(); // Collects the results of each test fired answers = {}; } /** * Clean up from the setUp() function */ function tearDown() { dispatcher = null; answers = null; } /** * Test the EventListener method by attempting to add several types * of listeners and examining valid and invalid dispatching. */ function testEventDispatcher() { // Add the function to an event type "one", "two" and "three" dispatcher.addEventListener('one', whatDoYouGetWhenYouMultiplySixByNine); dispatcher.addEventListener('two', whatDoYouGetWhenYouMultiplySixByNine); dispatcher.addEventListener('three', whatDoYouGetWhenYouMultiplySixByNine); // Add the object method to an event type "one" and "three" var test = new TestObject(); var object_method = new Array( test, TestObject.prototype.howManyRoadsMustAManWalkDown ); dispatcher.addEventListener('one', object_method); dispatcher.addEventListener('two', object_method); dispatcher.addEventListener('three', object_method); // Now remove the initial listener on "three" dispatcher.removeEventListener('three', whatDoYouGetWhenYouMultiplySixByNine); // Trigger each event, testing the outcome var e1 = new CustomEvent(); e1.type = 'one'; var e2 = new CustomEvent(); e2.type = 'two'; var e3 = new CustomEvent(); e3.type = 'three'; // The "one" event should have triggered the function and method responses dispatcher.dispatchEvent('one', e1); assertEquals(answers.one.whatDoYouGetWhenYouMultiplySixByNine, 42); assertUndefined(answers.two); assertUndefined(answers.three); // The "two" event should have triggered nothing at all dispatcher.dispatchEvent('two', e2); assertEquals(answers.one.whatDoYouGetWhenYouMultiplySixByNine, 42); assertUndefined(answers.two); assertUndefined(answers.three); // The "three" event should have triggered only the method response dispatcher.dispatchEvent('three', e3); assertEquals(answers.one.whatDoYouGetWhenYouMultiplySixByNine, 42); assertUndefined(answers.two); assertUndefined(answers.three.whatDoYouGetWhenYouMultiplySixByNine); assertEquals(answers.three.howManyRoadsMustAManWalkDown, 42); }