Commit d1de3000 authored by Rick Waldron's avatar Rick Waldron

2.0: Rewrite data.js

Signed-off-by: 's avatarRick Waldron <waldron.rick@gmail.com>
parent e1dcf96b
This diff is collapsed.
...@@ -200,47 +200,51 @@ var Globals = (function() { ...@@ -200,47 +200,51 @@ var Globals = (function() {
*/ */
QUnit.expectJqData = function( elems, key ) { QUnit.expectJqData = function( elems, key ) {
var i, elem, expando; var i, elem, expando;
QUnit.current_testEnvironment.checkJqData = true;
if ( elems.jquery && elems.toArray ) { if ( jQuery.cache ) {
elems = elems.toArray(); QUnit.current_testEnvironment.checkJqData = true;
}
if ( !jQuery.isArray( elems ) ) {
elems = [ elems ];
}
for ( i = 0; i < elems.length; i++ ) { if ( elems.jquery && elems.toArray ) {
elem = elems[i]; elems = elems.toArray();
}
// jQuery.data only stores data for nodes in jQuery.cache, if ( !jQuery.isArray( elems ) ) {
// for other data targets the data is stored in the object itself, elems = [ elems ];
// in that case we can't test that target for memory leaks.
// But we don't have to since in that case the data will/must will
// be available as long as the object is not garbage collected by
// the js engine, and when it is, the data will be removed with it.
if ( !elem.nodeType ) {
// Fixes false positives for dataTests(window), dataTests({}).
continue;
} }
expando = elem[ jQuery.expando ]; for ( i = 0; i < elems.length; i++ ) {
elem = elems[i];
if ( expando === undefined ) {
// In this case the element exists fine, but // jQuery.data only stores data for nodes in jQuery.cache,
// jQuery.data (or internal data) was never (in)directly // for other data targets the data is stored in the object itself,
// called. // in that case we can't test that target for memory leaks.
// Since this method was called it means some data was // But we don't have to since in that case the data will/must will
// expected to be found, but since there is nothing, fail early // be available as long as the object is not garbage collected by
// (instead of in teardown). // the js engine, and when it is, the data will be removed with it.
notStrictEqual( expando, undefined, "Target for expectJqData must have an expando, for else there can be no data to expect." ); if ( !elem.nodeType ) {
} else { // Fixes false positives for dataTests(window), dataTests({}).
if ( expectedDataKeys[expando] ) { continue;
expectedDataKeys[expando].push( key ); }
expando = elem[ jQuery.expando ];
if ( expando === undefined ) {
// In this case the element exists fine, but
// jQuery.data (or internal data) was never (in)directly
// called.
// Since this method was called it means some data was
// expected to be found, but since there is nothing, fail early
// (instead of in teardown).
notStrictEqual( expando, undefined, "Target for expectJqData must have an expando, for else there can be no data to expect." );
} else { } else {
expectedDataKeys[expando] = [ key ]; if ( expectedDataKeys[expando] ) {
expectedDataKeys[expando].push( key );
} else {
expectedDataKeys[expando] = [ key ];
}
} }
} }
} }
}; };
QUnit.config.urlConfig.push( { QUnit.config.urlConfig.push( {
id: "jqdata", id: "jqdata",
...@@ -258,33 +262,38 @@ var Globals = (function() { ...@@ -258,33 +262,38 @@ var Globals = (function() {
fragmentsLength = 0, fragmentsLength = 0,
cacheLength = 0; cacheLength = 0;
// Only look for jQuery data problems if this test actually if ( jQuery.cache ) {
// provided some information to compare against. // Only look for jQuery data problems if this test actually
if ( QUnit.urlParams.jqdata || this.checkJqData ) { // provided some information to compare against.
for ( i in jQuery.cache ) { if ( QUnit.urlParams.jqdata || this.checkJqData ) {
expectedKeys = expectedDataKeys[i]; for ( i in jQuery.cache ) {
actualKeys = jQuery.cache[i] ? keys( jQuery.cache[i] ) : jQuery.cache[i]; expectedKeys = expectedDataKeys[i];
if ( !QUnit.equiv( expectedKeys, actualKeys ) ) { actualKeys = jQuery.cache[i] ? keys( jQuery.cache[i] ) : jQuery.cache[i];
deepEqual( actualKeys, expectedKeys, "Expected keys exist in jQuery.cache" ); if ( !QUnit.equiv( expectedKeys, actualKeys ) ) {
deepEqual( actualKeys, expectedKeys, "Expected keys exist in jQuery.cache" );
}
delete jQuery.cache[i];
delete expectedDataKeys[i];
}
// In case it was removed from cache before (or never there in the first place)
for ( i in expectedDataKeys ) {
deepEqual( expectedDataKeys[i], undefined, "No unexpected keys were left in jQuery.cache (#" + i + ")" );
delete expectedDataKeys[i];
} }
delete jQuery.cache[i];
delete expectedDataKeys[i];
}
// In case it was removed from cache before (or never there in the first place)
for ( i in expectedDataKeys ) {
deepEqual( expectedDataKeys[i], undefined, "No unexpected keys were left in jQuery.cache (#" + i + ")" );
delete expectedDataKeys[i];
} }
// Reset data register
expectedDataKeys = {};
} }
// Reset data register
expectedDataKeys = {};
// Allow QUnit.reset to clean up any attached elements before checking for leaks // Allow QUnit.reset to clean up any attached elements before checking for leaks
QUnit.reset(); QUnit.reset();
for ( i in jQuery.cache ) { if ( jQuery.cache ) {
++cacheLength; for ( i in jQuery.cache ) {
++cacheLength;
}
} }
jQuery.fragments = {}; jQuery.fragments = {};
...@@ -334,7 +343,7 @@ var Globals = (function() { ...@@ -334,7 +343,7 @@ var Globals = (function() {
} else { } else {
delete jQuery.ajaxSettings; delete jQuery.ajaxSettings;
} }
// Cleanup globals // Cleanup globals
Globals.cleanup(); Globals.cleanup();
......
module("data", { teardown: moduleTeardown }); module( "data", { teardown: moduleTeardown });
test("expando", function(){ test( "expando", function() {
expect(1); expect(1);
equal(jQuery.expando !== undefined, true, "jQuery is exposing the expando"); equal(jQuery.expando !== undefined, true, "jQuery is exposing the expando");
}); });
test( "jQuery.data & removeData, expected returns", function() {
expect(2);
equal(
jQuery.data( document.body, "hello", "world" ), "world",
"jjQuery.data( elem, key, value ) returns value"
);
equal(
jQuery.removeData( document.body, "hello" ), undefined,
"jjQuery.removeData( elem, key, value ) returns undefined"
);
});
test( "jQuery._data & _removeData, expected returns", function() {
expect(2);
equal(
jQuery._data( document.body, "hello", "world" ), "world",
"jjQuery.data( elem, key, value ) returns value"
);
equal(
jQuery._removeData( document.body, "hello" ), undefined,
"jjQuery.removeData( elem, key, value ) returns undefined"
);
});
function dataTests (elem) { function dataTests (elem) {
var oldCacheLength, dataObj, internalDataObj, expected, actual; var oldCacheLength, dataObj, internalDataObj, expected, actual;
...@@ -99,6 +126,7 @@ test("jQuery.data(document)", 25, function() { ...@@ -99,6 +126,7 @@ test("jQuery.data(document)", 25, function() {
QUnit.expectJqData(document, "foo"); QUnit.expectJqData(document, "foo");
}); });
/*
test("Expando cleanup", 4, function() { test("Expando cleanup", 4, function() {
var expected, actual, var expected, actual,
div = document.createElement("div"); div = document.createElement("div");
...@@ -132,7 +160,8 @@ test("Expando cleanup", 4, function() { ...@@ -132,7 +160,8 @@ test("Expando cleanup", 4, function() {
// Clean up unattached element // Clean up unattached element
jQuery(div).remove(); jQuery(div).remove();
}); });
*/
/*
test("jQuery.acceptData", function() { test("jQuery.acceptData", function() {
expect(7); expect(7);
...@@ -150,7 +179,7 @@ test("jQuery.acceptData", function() { ...@@ -150,7 +179,7 @@ test("jQuery.acceptData", function() {
applet.setAttribute("classid", "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"); applet.setAttribute("classid", "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93");
ok( !jQuery.acceptData( applet ), "applet" ); ok( !jQuery.acceptData( applet ), "applet" );
}); });
*/
test(".data()", function() { test(".data()", function() {
expect(5); expect(5);
...@@ -465,8 +494,8 @@ test("jQuery.data should follow html5 specification regarding camel casing", fun ...@@ -465,8 +494,8 @@ test("jQuery.data should follow html5 specification regarding camel casing", fun
div.data("foo-bar", "d"); div.data("foo-bar", "d");
equal( div.data("fooBar"), "d", "Verify updated data-* key" ); equal( div.data("fooBar"), "d", "Verify updated data-* key (fooBar)" );
equal( div.data("foo-bar"), "d", "Verify updated data-* key" ); equal( div.data("foo-bar"), "d", "Verify updated data-* key (foo-bar)" );
div.remove(); div.remove();
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment