public
Description: JavaScript helper libraries for unit testing
Homepage: http://drnicwilliams.com
Clone URL: git://github.com/drnic/drnic_js_test_helpers.git
drnic (author)
Wed Oct 08 21:21:44 -0700 2008
commit  d2a24bebdebc085a670b4c92289e5bb83bafb264
tree    4be4783a83cfc94b82048e6066363c995244ec3c
parent  93973972cacb3cfa74d3f6747ed885f7f8389c56
100644 122 lines (77 sloc) 4.597 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
h1. Dr Nic's JavaScript Test Helpers
 
h2. What
 
This JavaScript project provides help for unit testing other JavaScript projects:
 
* <code>Event.simulate('element', 'eventName')</code>
* <code>Ajax.Request</code> mocking (for "Prototypejs":http://www.prototypejs.org/ based projects)
* Extra test case assertions (assertDifference, assertNoDifference)
 
h2. Downloading
 
Latest version: <a href="dist/drnic_js_test_helpers.js">drnic_js_test_helpers.js</a>
 
Alternately, to get the complete project source go to the "downloads":http://rubyforge.org/frs/?group_id=2351&release_id=19240
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 <code>test/assets</code>,
and link to it from your HTML test files.
 
h2. Setup/installation
 
This library is for use within unittest.js related HTML test files. If your test HTML
files are in <code>test/</code> and the library is saved at
<code>test/assets/drnic_js_test_helpers.js</code>, then within the &lt;head&gt;
of each HTML test file add:
 
<pre syntax="html">
<script src="assets/drnic_js_test_helpers.js" type="text/javascript"></script>
</pre>
 
h2. Demonstration of usage
 
This library currently offers some helpers:
 
* Event simulation - simulating mouse events (click, mouseover, etc), or HTML events (change, etc)
* Ajax request mocking - no Ajax requests available during unit testing, so mock them out
* Extra test assertions - bonus assertXXX methods for unittest.js
 
h3. Event simulation
 
<pre syntax="javascript">Event.simulate('element', 'eventName')</pre>
 
 
h3. Ajax request mocking
 
<code>Ajax.Request</code> mocking (for "Prototypejs":http://www.prototypejs.org/ based projects)
 
Within your test or setup method:
 
<pre syntax="javascript">Ajax.Request.setupMock('/some/url', function(request, response) {
  response.responseJSON = "{data: 123}";
  request.options.onComplete(response);
});</pre>
 
Now, during your test if <code>new Ajax.Request('/some/url', {onComplete: function() {...}})</code>
is called, then the <code>onComplete</code> callback will be invoked,
and <code>responseJSON</code> 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:
 
<pre>teardown: function() {
  Ajax.Request.clearMocks();
},
</pre>
 
The <code>clearMocks</code> call is to restore the Ajax mechanism; which is used
when you run tests via <code>rake test</code> to communicate the test results back
to the command-line test server. If <code>rake test</code> is hanging, it is
likely you are not calling <code>clearMocks</code> in your teardown method.
 
h3. Extra assertions
 
Your test cases can use the following additional assertions:
  
* <code>assertDifference(evalStr, block, count)</code>
* <code>assertNoDifference(evalStr, block)</code> - same as <code>assertDifference(evalStr, block, 0)</code>
* <code>assertTagDifference(tag, block, count)</code>
 
For example, if you wanted to test that some code created an additional &gt;div&lt; element
in the DOM, then your test might be:
 
<pre syntax="javascript">
testExtraDiv: function() { with(this) {
  assertDifference("$$('div').length", function() {
    // do something that creates new <div> element
  }, 1, "should not be different");
}},
</pre>
 
There is a primitive selector assertion <code>assertTagDifference</code>:
  
<pre syntax="javascript">assertTagDifference('span', function() {
  var span = document.createElement("span");
  Test.$('sample').appendChild(span);
}, 1);</pre>
 
h2. Other test helpers
 
The protolicious source has some helper files, see "source":http://code.google.com/p/protolicious/source/browse/trunk/
 
h2. Forum
 
"http://groups.google.com/group/drnicutilities":http://groups.google.com/group/drnicutilities
 
 
h2. How to submit patches
 
Read the "8 steps for fixing other people's code":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/ and for section "8b: Submit patch to Google Groups":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/#8b-google-groups, use the Google Group above.
 
The source project is a "Git":http://git.or.cz/ repository. See Dr Nic's "master branch":http://github.com/drnic/drnic_js_test_helpers/tree/master for clone/checkout details.
 
h2. License
 
This code is free to use under the terms of the MIT license.
 
h2. Contact
 
Comments are welcome. Send an email to "Dr Nic Williams":mailto:drnicwilliams@gmail.com via the "forum":http://groups.google.com/group/drnicutilities