Created
November 17, 2012 17:04
-
-
Save joelhooks/4097691 to your computer and use it in GitHub Desktop.
Custom Jasmine Matchers
This file contains hidden or 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
it("convertToSplitDate should turen a Date into a split date", function() { | |
var date = new Date(2012, 10, 17), | |
splitDate = dateUtil.convertDateToSplitDate(date); | |
expect(splitDate).toBeDefined(); | |
expect(splitDate.year).toBe(date.year); | |
expect(splitDate.month - 1).toBe(date.month); | |
expect(splitDate.day).toBe(date.day); | |
}); |
This file contains hidden or 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
it("convertToDate should turn a splitDate into a Date object", function() { | |
var splitDate = {year: 2012, month: 11, day: 17}, | |
date = dateUtil.splitDateToDate(splitDate); | |
expect(date instanceof Date).toBe(true); | |
expect(date.getFullYear()).toBe(splitDate.year); | |
expect(date.getMonth()).toBe(splitDate.month - 1); | |
expect(date.getDate()).toBe(splitDate.day); | |
}); |
This file contains hidden or 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
it("convertToDate should turn a splitDate into a Date object", function() { | |
var splitDate = {year: 2012, month: 11, day: 17}, | |
date = dateUtil.splitDateToDate(splitDate); | |
expect(date).toEqualDate(splitDate); | |
}); | |
it("convertToSplitDate should turen a Date into a split date", function() { | |
var date = new Date(2012, 10, 17), | |
splitDate = dateUtil.convertDateToSplitDate(date); | |
expect(splitDate).toEqualDate(date); | |
}); |
This file contains hidden or 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
//Jasmine will call this before EVERY spec. | |
beforeEach(function () { | |
function isValidSplitDate(splitDate) { | |
return splitDate && !isNaN(splitDate.year) && !isNaN(splitDate.month) && !isNaN(splitDate.day); | |
} | |
function convertSplitDateToDate(splitDate) { | |
if (isValidSplitDate(splitDate)) { | |
splitDate = new Date(splitDate.year, splitDate.month - 1, splitDate.day); | |
} | |
return splitDate; | |
} | |
//addMatchers() is a method that Jasmine will see and understand to | |
//make the custom matchers within easy to understand. | |
this.addMatchers({ | |
toEqualDate: function (expectedDate) { | |
var areDates; | |
expectedDate = convertSplitDateToDate(expectedDate); | |
this.actual = convertSplitDateToDate(this.actual); | |
areDates = this.actual instanceof Date && expectedDate instanceof Date; | |
//Jasmine will look for this function and utilize it for custom error messages | |
this.message = function () { | |
if (areDates) { | |
return "Expected date of " + this.actual + " to be " + expectedDate; | |
} | |
return "Expected " + this.actual + " and " + expectedDate + " to be valid Date/SplitDate objects. They were not."; | |
}; | |
if (areDates) { | |
//Ultimately Jasmine expects a matcher to return true or false | |
return (this.actual.getTime() === expectedDate.getTime()); | |
} | |
return false; | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment