Created
December 29, 2011 17:14
-
-
Save nordyke/1535041 to your computer and use it in GitHub Desktop.
Some Jasmine Tests for idiomatic.js
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
/* | |
* Writing Unit Tests to learn code or a library has been a fantastic strategy for me. | |
* I wrote these Jasmine Tests to enforce some of the very powerful concepts from | |
* https://github.com/rwldrn/idiomatic.js?utm_source=javascriptweekly&utm_medium=email. | |
*/ | |
describe("JavaScript Idiomatics", function() { | |
describe("Coercion", function() { | |
it("coerces string into number", function() { | |
var one = "1"; | |
expect(one).toNotEqual(1); | |
expect(+one).toEqual(1); | |
}); | |
it("coerces number into string", function() { | |
var one = 1; | |
expect(one).toNotEqual("1"); | |
expect(one + "").toEqual("1"); | |
}); | |
it("coerces boolean into number", function() { | |
var bool = false; | |
expect(bool).toNotEqual(0); | |
expect(+bool).toEqual(0); | |
}); | |
it("coerces boolean into string", function() { | |
var bool = false; | |
expect(bool).toNotEqual("false"); | |
expect(bool + "").toEqual("false"); | |
}); | |
it("coerces undefined into string", function() { | |
var undef; | |
expect(undef).toNotEqual("undefined"); | |
expect(undef + "").toEqual("undefined"); | |
}); | |
}); | |
describe("Conditional Evaluations", function() { | |
it("evaluates an empty array length as false", function() { | |
var array = []; | |
expect(array.length).toBeFalsy(); | |
}); | |
it("evaluates a non-empty array length as true",function(){ | |
var array = [1]; | |
expect(array.length).toBeTruthy(); | |
}); | |
it("evaluates an empty string length as false",function(){ | |
var string = ""; | |
expect(string.length).toBeFalsy(); | |
}); | |
it("evaluates a non-empty string length as true",function(){ | |
var string = "hi"; | |
expect(string.length).toBeTruthy(); | |
}); | |
it("evaluates object existence as true",function(){ | |
var string = "hi"; | |
expect(string).toBeTruthy(); | |
}); | |
it("uses !! as true",function(){ | |
var bool = true; | |
expect(!!bool).toBeTruthy(); | |
}); | |
}); | |
describe("Type Checking",function(){ | |
it("checks types",function(){ | |
expect(typeof "hello" === "string").toBeTruthy(); | |
expect(typeof 1 === "number").toBeTruthy(); | |
expect(typeof false === "boolean").toBeTruthy(); | |
expect(typeof {} === "object").toBeTruthy(); | |
}); | |
it("can both distinguish undefined/null and group them",function(){ | |
var variable; | |
expect(variable === null).toBeFalsy(); | |
expect(variable == null).toBeTruthy(); | |
expect(variable === undefined).toBeTruthy(); //local | |
expect(variable === "undefined").toBeFalsy(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment