Skip to content

Commit

Permalink
More unit tests and a $.hasData that works for JS objects too.
Browse files Browse the repository at this point in the history
  • Loading branch information
csnover committed Dec 22, 2010
1 parent f5d4bf8 commit e199ead
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
18 changes: 12 additions & 6 deletions test/unit/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit e199ead

Please sign in to comment.