Last active
April 10, 2017 23:54
-
-
Save fotinakis/d6e32825ffd28e604def5b7f2d46ec73 to your computer and use it in GitHub Desktop.
Ember freezeMoment test helper
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
import Ember from 'ember'; | |
import { | |
beforeEach, | |
afterEach | |
} from 'mocha'; | |
// Support for overriding the baseline time used by Moment.js. Accepts ISO formatted timestamps. | |
// | |
// Requires: | |
// moment >= 2.11.0 in order to override moment.now. | |
// Usage: | |
// describe('test', function() { | |
// freezeMoment('2015-05-22'); | |
// freezeMoment('2014-11-04T19:00:00.000Z'); | |
// freezeMoment(moment('8:00 -08:00', 'H:mm a ZZ')); | |
// }); | |
export default function freezeMoment(momentObj) { | |
// If given an ISO date or timestamp, create a moment object from it. Validity is checked below. | |
if (typeof(momentObj) === 'string') { | |
momentObj = window.moment(momentObj); | |
} | |
if (!momentObj || typeof(momentObj) !== 'object' || !momentObj.isValid()) { | |
throw 'Invalid moment object given: ' + momentObj.toString(); | |
} | |
let originalMomentNow = window.moment.now; | |
beforeEach(function(){ | |
// Override the baseline time used by Moment. | |
window.moment.now = function() { | |
return momentObj.toDate(); | |
}; | |
}); | |
afterEach(function() { | |
// Restore original moment.now method. | |
window.moment.now = originalMomentNow; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment