From 7f94a5cc3a167a710576c008da2c186a98ce2dd4 Mon Sep 17 00:00:00 2001 From: Rick Waldron Date: Sun, 3 Feb 2013 15:27:55 -0500 Subject: 2.0: Rewrite data.js (Incl. event, manipulation, tests) --- test/data/onbeforeunload.html | 20 +++++++++++ test/data/testrunner.js | 74 ++++++++++++++++++++++------------------- test/unit/data.js | 38 +++++++++++++++++++-- test/unit/event.js | 77 +++++++------------------------------------ 4 files changed, 107 insertions(+), 102 deletions(-) create mode 100644 test/data/onbeforeunload.html (limited to 'test') diff --git a/test/data/onbeforeunload.html b/test/data/onbeforeunload.html new file mode 100644 index 000000000..62c424e89 --- /dev/null +++ b/test/data/onbeforeunload.html @@ -0,0 +1,20 @@ + + + + + diff --git a/test/data/testrunner.js b/test/data/testrunner.js index f7aca724a..aa7c13019 100644 --- a/test/data/testrunner.js +++ b/test/data/testrunner.js @@ -200,47 +200,53 @@ var Globals = (function() { */ QUnit.expectJqData = function( elems, key ) { var i, elem, expando; - QUnit.current_testEnvironment.checkJqData = true; - if ( elems.jquery && elems.toArray ) { - elems = elems.toArray(); - } - if ( !jQuery.isArray( elems ) ) { - elems = [ elems ]; - } + // As of jQuery 2.0, there will be no "cache"-data is + // stored and managed completely below the API surface + if ( jQuery.cache ) { + QUnit.current_testEnvironment.checkJqData = true; - for ( i = 0; i < elems.length; i++ ) { - elem = elems[i]; - - // jQuery.data only stores data for nodes in jQuery.cache, - // for other data targets the data is stored in the object itself, - // 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; + if ( elems.jquery && elems.toArray ) { + elems = elems.toArray(); + } + if ( !jQuery.isArray( elems ) ) { + elems = [ elems ]; } - 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 { - if ( expectedDataKeys[expando] ) { - expectedDataKeys[expando].push( key ); + for ( i = 0; i < elems.length; i++ ) { + elem = elems[i]; + + // jQuery.data only stores data for nodes in jQuery.cache, + // for other data targets the data is stored in the object itself, + // 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 ]; + + 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 { - expectedDataKeys[expando] = [ key ]; + if ( expectedDataKeys[expando] ) { + expectedDataKeys[expando].push( key ); + } else { + expectedDataKeys[expando] = [ key ]; + } } } } + }; QUnit.config.urlConfig.push( { id: "jqdata", @@ -334,7 +340,7 @@ var Globals = (function() { } else { delete jQuery.ajaxSettings; } - + // Cleanup globals Globals.cleanup(); diff --git a/test/unit/data.js b/test/unit/data.js index 34c98140b..840014fd8 100644 --- a/test/unit/data.js +++ b/test/unit/data.js @@ -6,6 +6,33 @@ test("expando", function(){ 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", + "jQuery.data( elem, key, value ) returns value" + ); + equal( + jQuery.removeData( document.body, "hello" ), undefined, + "jQuery.removeData( elem, key, value ) returns undefined" + ); + +}); + +test( "jQuery._data & _removeData, expected returns", function() { + expect(2); + + equal( + jQuery._data( document.body, "hello", "world" ), "world", + "jQuery.data( elem, key, value ) returns value" + ); + equal( + jQuery._removeData( document.body, "hello" ), undefined, + "jQuery.removeData( elem, key, value ) returns undefined" + ); +}); + function dataTests (elem) { var oldCacheLength, dataObj, internalDataObj, expected, actual; @@ -106,6 +133,10 @@ test("Data is not being set on comment and text nodes", function() { ok( !jQuery.hasData( jQuery("text").contents().data("foo", 0) ) ); }); +/* +// Since the new data system does not rely on exandos, limiting the type of +// nodes that can have data is no longer necessary. jQuery.acceptData is now irrelevant +// and should be removed from the library. test("jQuery.acceptData", function() { expect(9); @@ -127,7 +158,7 @@ test("jQuery.acceptData", function() { ok( !jQuery.acceptData( document.createComment("") ), "comment" ); ok( !jQuery.acceptData( document.createTextNode("") ), "text" ); }); - +*/ test(".data()", function() { expect(5); @@ -424,7 +455,7 @@ if (window.JSON && window.JSON.stringify) { } test("jQuery.data should follow html5 specification regarding camel casing", function() { - expect(10); + expect(12); var div = jQuery("
") .prependTo("body"); @@ -445,6 +476,9 @@ test("jQuery.data should follow html5 specification regarding camel casing", fun equal( div.data("fooBar"), "d", "Verify updated data-* key" ); equal( div.data("foo-bar"), "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 (foo-bar)" ); + div.remove(); }); diff --git a/test/unit/event.js b/test/unit/event.js index 295a4b8d4..c72d873f0 100644 --- a/test/unit/event.js +++ b/test/unit/event.js @@ -1371,75 +1371,20 @@ test("Submit event can be stopped (#11049)", function() { // Test beforeunload event only if it supported (i.e. not Opera) if ( window.onbeforeunload === null ) { - asyncTest("on(beforeunload)", 4, function() { - var win, - forIE6 = 0, - fired = false, - iframe = jQuery("