plaa / TimeShift-js

Library for mocking JavaScript's Date object
MIT License
65 stars 28 forks source link

[object Object] instead of [object Date] #5

Closed ubenzer closed 9 years ago

ubenzer commented 10 years ago

Hi,

I am trying to test my code in different timezones using this library in Angular.js. Angular.js checks if an object is a date or not using the following piece:

function isDate(value) {
  return toString.call(value) === '[object Date]';
}

Before mocking if I call:

toString.call(new Date()) === '[object Date]'

it returns `[object Date] I mock javascript usingwindow.Date = TimeShift.Date`` After mocking, if I call the same, what I receive is

[object Object]

This is the way Angular.js checks if the given argument is a date or not. Is there way to make TimeShift return [object Date] ?

cvakiitho commented 9 years ago

Pretty late, but I guess someone still might get into this issue in the future.

It is not possible to overide [[Class]] internal property to Date: "The value of the [[Class]] internal property of a host object may be any String value except one of "Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "Number", "Object", "RegExp", and "String"." http://ecma262-5.com/ELS5_Section_8.htm#Section_8.6.2

So I simply overide angular.isDate() function before Date = TimeShift.Date;

angular.isDate = function(x){return x instanceof Date};

ubenzer commented 9 years ago

Thanks!