Unit testing handling of date inputs in JavaScript regardless of time zone
I have a form where a user can enter a date, i.e. <input type="date"> the
value is submitted in yyyy-MM-dd format. When I create a Date object with
the string it assumes the time zone is the one the user's browser is set
to – this is the behavior I want.
I'm then using the date value to make queries against a REST API that
expects ISO date/time strings. That's no problem as the toISOString
function on the Date object handles everything correctly.
However, when I'm unit testing this code – setting my input to a
yyyy-MM-dd string then asserting that the output is an expected ISO
timestamp string the tests can only work in a particular time zone. Is
there a way I can force the time zone in the test?
I've tried using Jasmine spies to do something like:
var fixedTime = moment().zone(60).toDate()
spyOn(window, 'Date').andCallFake(function() {
return fixedTime;
});
But given there are so many variants of the constructor and so many ways
it gets called by moment.js this is pretty impractical and is getting me
into infinite loops.
No comments:
Post a Comment