Adds jQuery collection to objects that will be used as global events context if provi...
[jquery.git] / test / data / testinit.js
1 var jQuery = this.jQuery || "jQuery", // For testing .noConflict()
2         $ = this.$ || "$",
3         originaljQuery = jQuery,
4         original$ = $;
5
6 /**
7  * Returns an array of elements with the given IDs, eg.
8  * @example q("main", "foo", "bar")
9  * @result [<div id="main">, <span id="foo">, <input id="bar">]
10  */
11 function q() {
12         var r = [];
13
14         for ( var i = 0; i < arguments.length; i++ ) {
15                 r.push( document.getElementById( arguments[i] ) );
16         }
17
18         return r;
19 }
20
21 /**
22  * Asserts that a select matches the given IDs * @example t("Check for something", "//[a]", ["foo", "baar"]);
23  * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baa
24 r'
25  */
26 function t(a,b,c) {
27         var f = jQuery(b).get(), s = "";
28
29         for ( var i = 0; i < f.length; i++ ) {
30                 s += (s && ",") + '"' + f[i].id + '"';
31         }
32
33         same(f, q.apply(q,c), a + " (" + b + ")");
34 }
35
36 /**
37  * Add random number to url to stop IE from caching
38  *
39  * @example url("data/test.html")
40  * @result "data/test.html?10538358428943"
41  *
42  * @example url("data/test.php?foo=bar")
43  * @result "data/test.php?foo=bar&10538358345554"
44  */
45 function url(value) {
46         return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000);
47 }
48
49 (function () {
50         // Store the old counts so that we only assert on tests that have actually leaked,
51         // instead of asserting every time a test has leaked sometime in the past
52         var oldCacheLength = 0,
53                 oldFragmentsLength = 0,
54                 oldTimersLength = 0,
55                 oldActive = 0;
56
57         /**
58          * Ensures that tests have cleaned up properly after themselves. Should be passed as the
59          * teardown function on all modules' lifecycle object.
60          */
61         this.moduleTeardown = function () {
62                 var i, fragmentsLength = 0, cacheLength = 0;
63
64                 // Allow QUnit.reset to clean up any attached elements before checking for leaks
65                 QUnit.reset();
66
67                 for ( i in jQuery.cache ) {
68                         ++cacheLength;
69                 }
70
71                 jQuery.fragments = {};
72
73                 for ( i in jQuery.fragments ) {
74                         ++fragmentsLength;
75                 }
76
77                 // Because QUnit doesn't have a mechanism for retrieving the number of expected assertions for a test,
78                 // if we unconditionally assert any of these, the test will fail with too many assertions :|
79                 if ( cacheLength !== oldCacheLength ) {
80                         equals( cacheLength, oldCacheLength, "No unit tests leak memory in jQuery.cache" );
81                         oldCacheLength = cacheLength;
82                 }
83                 if ( fragmentsLength !== oldFragmentsLength ) {
84                         equals( fragmentsLength, oldFragmentsLength, "No unit tests leak memory in jQuery.fragments" );
85                         oldFragmentsLength = fragmentsLength;
86                 }
87                 if ( jQuery.timers.length !== oldTimersLength ) {
88                         equals( jQuery.timers.length, oldTimersLength, "No timers are still running" );
89                         oldTimersLength = jQuery.timers.length;
90                 }
91                 if ( jQuery.active !== oldActive ) {
92                         equals( jQuery.active, 0, "No AJAX requests are still active" );
93                         oldActive = jQuery.active;
94                 }
95         }
96 }());