From e199ead4cba3687beca2444eea4bb8abc20f55b6 Mon Sep 17 00:00:00 2001 From: Colin Snover Date: Wed, 22 Dec 2010 15:03:01 -0600 Subject: [PATCH] More unit tests and a $.hasData that works for JS objects too. --- src/data.js | 6 +++++- test/unit/data.js | 18 ++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/data.js b/src/data.js index c3530c83..549e73ed 100644 --- a/src/data.js +++ b/src/data.js @@ -22,7 +22,11 @@ jQuery.extend({ }, hasData: function( elem ) { - return !elem.nodeType || (!!elem[ jQuery.expando ] && !jQuery.isEmptyObject(jQuery.cache[ elem[jQuery.expando] ])); + if (elem.nodeType) { + elem = jQuery.cache[ elem[jQuery.expando] ]; + } + + return !!elem && !jQuery.isEmptyObject(elem); }, data: function( elem, name, data ) { diff --git a/test/unit/data.js b/test/unit/data.js index 204d979c..310cd6bc 100644 --- a/test/unit/data.js +++ b/test/unit/data.js @@ -79,12 +79,18 @@ test("jQuery.data", function() { }); test("jQuery.hasData", function() { - var div = document.createElement( "div" ); - equals( jQuery.hasData(div), false, "No data exists" ); - jQuery.data( div, "foo", "bar" ); - equals( jQuery.hasData(div), true, "Data exists" ); - jQuery.removeData( div, "foo" ); - equals( jQuery.hasData(div), false, "Data was removed" ); + expect(6); + + function testData(obj) { + equals( jQuery.hasData(obj), false, "No data exists" ); + jQuery.data( obj, "foo", "bar" ); + equals( jQuery.hasData(obj), true, "Data exists" ); + jQuery.removeData( obj, "foo" ); + equals( jQuery.hasData(obj), false, "Data was removed" ); + } + + testData(document.createElement('div')); + testData({}); }); test(".data()", function() {