diff options
| author | Igor Minar | 2012-03-27 08:05:07 -0700 |
|---|---|---|
| committer | Igor Minar | 2012-03-28 16:30:31 -0700 |
| commit | da9f4dfcf4f3d0c21821d8474ac0bb19a3c51415 (patch) | |
| tree | c70b8e9b3db5bd5a266ea7156ee7312c3cd80554 | |
| parent | ac4318a2fa5c6d306dbc19466246292a81767fca (diff) | |
| download | angular.js-da9f4dfcf4f3d0c21821d8474ac0bb19a3c51415.tar.bz2 | |
feat(TzDate): add support for toISOString method
| -rw-r--r-- | src/ngMock/angular-mocks.js | 28 | ||||
| -rw-r--r-- | test/ngMock/angular-mocksSpec.js | 11 |
2 files changed, 38 insertions, 1 deletions
diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index f5dd6758..8c50c90e 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -397,6 +397,19 @@ angular.mock.$LogProvider = function() { return parseInt(str, 10); } + function padNumber(num, digits, trim) { + var neg = ''; + if (num < 0) { + neg = '-'; + num = -num; + } + num = '' + num; + while(num.length < digits) num = '0' + num; + if (trim) + num = num.substr(num.length - digits); + return neg + num; + } + /** * @ngdoc object @@ -523,12 +536,25 @@ angular.mock.$LogProvider = function() { return self.date.getDay(); }; + // provide this method only on browsers that already have it + if (self.toISOString) { + self.toISOString = function() { + return padNumber(self.origDate.getUTCFullYear(), 4) + '-' + + padNumber(self.origDate.getUTCMonth() + 1, 2) + '-' + + padNumber(self.origDate.getUTCDate(), 2) + 'T' + + padNumber(self.origDate.getUTCHours(), 2) + ':' + + padNumber(self.origDate.getUTCMinutes(), 2) + ':' + + padNumber(self.origDate.getUTCSeconds(), 2) + '.' + + padNumber(self.origDate.getUTCMilliseconds(), 3) + 'Z' + } + } + //hide all methods not implemented in this mock that the Date prototype exposes var unimplementedMethods = ['getMilliseconds', 'getUTCDay', 'getYear', 'setDate', 'setFullYear', 'setHours', 'setMilliseconds', 'setMinutes', 'setMonth', 'setSeconds', 'setTime', 'setUTCDate', 'setUTCFullYear', 'setUTCHours', 'setUTCMilliseconds', 'setUTCMinutes', 'setUTCMonth', 'setUTCSeconds', - 'setYear', 'toDateString', 'toJSON', 'toGMTString', 'toLocaleFormat', 'toLocaleString', + 'setYear', 'toDateString', 'toGMTString', 'toJSON', 'toLocaleFormat', 'toLocaleString', 'toLocaleTimeString', 'toSource', 'toString', 'toTimeString', 'toUTCString', 'valueOf']; angular.forEach(unimplementedMethods, function(methodName) { diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index d80b1976..c656e940 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -63,6 +63,17 @@ describe('ngMock', function() { }); + it('should fake toISOString method', function() { + var date = new angular.mock.TzDate(-1, '2009-10-09T01:02:03.027Z'); + + if (new Date().toISOString) { + expect(date.toISOString()).toEqual('2009-10-09T01:02:03.027Z'); + } else { + expect(date.toISOString).toBeUndefined(); + } + }); + + it('should fake getHours method', function() { //0 in -3h var t0 = new angular.mock.TzDate(-3, 0); |
