Last active
December 12, 2015 08:19
-
-
Save thoolihan/4743467 to your computer and use it in GitHub Desktop.
Using base jQuery functionality to demonstrate jasmine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe("jQuery", function() { | |
it("should return correct type for null", function(){ | |
expect($.type(null)).toBe("null"); | |
}); | |
it("should see an empty string as an empty object", function(){ | |
expect($.isEmptyObject("")).toBeTruthy(); | |
}); | |
it("should see an empty array as an empty object", function(){ | |
expect($.isEmptyObject([])).toBeTruthy(); | |
}); | |
it("should see an empty object as an empty object", function(){ | |
expect($.isEmptyObject({})).toBeTruthy(); | |
}); | |
it("should not see jQuery as an plain object", function(){ | |
expect($.isPlainObject($)).toBeFalsy(); | |
}); | |
it("should see jQuery as a function", function(){ | |
expect($.isFunction($)).toBeTruthy(); | |
}); | |
it("should see an extended object as having equal properties", function(){ | |
var a = {'foo': 'bar'}; | |
var b = $.extend(true, {}, a); | |
expect(a.foo==b.foo).toBeTruthy(); | |
}); | |
it("should not see an extended object as identity equal", function(){ | |
var a = {'foo': 'bar'}; | |
var b = $.extend(true, {}, a); | |
expect(a===b).toBeFalsy(); | |
}); | |
it("should merge arrays", function() { | |
var a = [1,2,3]; | |
var b = [4,5,6]; | |
var c = $.merge(a, b); | |
expect(c.length).toBe(6); | |
}); | |
it("should be able to uniquify dom elements", function() { | |
var span = $('<span class="foo bar"/>'); | |
$('body').append(span); | |
var foos = $.makeArray($('span.foo')); | |
var bars = $.makeArray($('span.bar')); | |
var spans = $.merge(foos, bars); | |
expect(spans.length).toBe(2); | |
spans = $.unique(spans); | |
expect(spans.length).toBe(1); | |
}); | |
it("should be able to iterate with each", function(){ | |
var a = [1, 2, 3]; | |
var b = []; | |
$.each(a, function(val){ | |
b.push(val); | |
}); | |
expect(b.length).toBe(3); | |
}); | |
it("should be able to alter with map", function(){ | |
var a = [1, 2, 3]; | |
var b = $.map(a, function(val){ | |
return val * val; | |
}); | |
expect(b[0]).toBe(1); | |
expect(b[1]).toBe(4); | |
expect(b[2]).toBe(9); | |
}); | |
it("should trim string", function(){ | |
expect($.trim(" a sentence ")).toBe("a sentence"); | |
}); | |
it("should be able to check for contained elements", function(){ | |
expect($.contains($('head')[0], $('title')[0])).toBeTruthy(); | |
}); | |
it("should be able to store data on an element", function(){ | |
var el = $('<div/>'); | |
el.data({'foo': 1}); | |
expect($.isPlainObject(el.data())).toBeTruthy(); | |
expect(el.data().foo).toBe(1); | |
}); | |
it("should be able to create current data", function(){ | |
expect($.now()).toBe((new Date()).getTime()); | |
}); | |
it("should be able to check for the main window", function(){ | |
expect($.isWindow(window)).toBeTruthy(); | |
}); | |
it("should be able to parse a json string", function(){ | |
expect($.parseJSON('{"name": "jQuery"}').name).toBe('jQuery'); | |
}); | |
it("should be able to parse xml", function(){ | |
var xml = $.parseXML('<code><author>Tim</author></code>'); | |
expect($(xml).first('code author').text()).toBe('Tim'); | |
}); | |
it("should be able to find by attribute", function(){ | |
var html = '<div><a href="http://facebook.com">facebook</a> \ | |
<a href="http://twitter.com">twitter</a></div>'; | |
var doc = $.parseHTML(html); | |
expect($(doc).find('a[href*="twitter"]').length).toBe(1); | |
}); | |
it("should be able to find by attribute name presence", function(){ | |
expect($('script[src]').length).toBeGreaterThan(1); | |
}); | |
}); | |
jasmine-fixture looks interesting, I'll check it out. I was just working through this to finally get working with jasmine while buffing back up on some of the core jquery stuff. I've been using jquery for a long while now, but rarely use much outside of selectors and ajax calls. Given I've been working on a lot of single page app stuff I wanted to go back and check out some of the other core parts I was missing.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice. I love unit testing languages to learn base functionality. Never occurred to me that I could flip it and do the same with testing frameworks.
I'm a huge fan of https://github.com/searls/jasmine-fixture to cut down on the amount of dom setup steps.