mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-03 20:54:43 +00:00
feat(TzDate): add mock "toString" method to TzDate.
- If the third param of TzDate constructor is defined, toStirng will just return this third parameter. Otherwise, toString will still be treated as unimplemented method
This commit is contained in:
parent
ad3b8d7bcf
commit
0fbaa2f12a
2 changed files with 23 additions and 1 deletions
8
src/angular-mocks.js
vendored
8
src/angular-mocks.js
vendored
|
|
@ -457,7 +457,7 @@ function MockLogFactory() {
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
function TzDate(offset, timestamp) {
|
function TzDate(offset, timestamp, toStringVal) {
|
||||||
if (angular.isString(timestamp)) {
|
if (angular.isString(timestamp)) {
|
||||||
var tsStr = timestamp;
|
var tsStr = timestamp;
|
||||||
|
|
||||||
|
|
@ -481,6 +481,10 @@ function TzDate(offset, timestamp) {
|
||||||
return this.date.getTime() - this.offsetDiff;
|
return this.date.getTime() - this.offsetDiff;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.toString = function() {
|
||||||
|
return toStringVal;
|
||||||
|
}
|
||||||
|
|
||||||
this.toLocaleDateString = function() {
|
this.toLocaleDateString = function() {
|
||||||
return this.date.toLocaleDateString();
|
return this.date.toLocaleDateString();
|
||||||
};
|
};
|
||||||
|
|
@ -551,6 +555,8 @@ function TzDate(offset, timestamp) {
|
||||||
'toLocaleTimeString', 'toSource', 'toString', 'toTimeString', 'toUTCString', 'valueOf'];
|
'toLocaleTimeString', 'toSource', 'toString', 'toTimeString', 'toUTCString', 'valueOf'];
|
||||||
|
|
||||||
angular.forEach(unimplementedMethods, function(methodName) {
|
angular.forEach(unimplementedMethods, function(methodName) {
|
||||||
|
if (methodName == 'toString' && toStringVal) return;
|
||||||
|
|
||||||
self[methodName] = function() {
|
self[methodName] = function() {
|
||||||
throw {
|
throw {
|
||||||
name: "MethodNotImplemented",
|
name: "MethodNotImplemented",
|
||||||
|
|
|
||||||
16
test/angular-mocksSpec.js
vendored
16
test/angular-mocksSpec.js
vendored
|
|
@ -121,6 +121,22 @@ describe('mocks', function(){
|
||||||
expect(date2.getUTCMinutes()).toBe(0);
|
expect(date2.getUTCMinutes()).toBe(0);
|
||||||
expect(date2.getUTCSeconds()).toBe(0);
|
expect(date2.getUTCSeconds()).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('should fake toString method when a third param is passed in', function() {
|
||||||
|
var t = new TzDate(0, 0, 'Mon Sep 3 2010 17:05:08 GMT+0500 (XYZ)');
|
||||||
|
expect(t.toString()).toBe('Mon Sep 3 2010 17:05:08 GMT+0500 (XYZ)');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('should throw error when no third param but toString called', function() {
|
||||||
|
var t = new TzDate(0, 0);
|
||||||
|
try {
|
||||||
|
t.toString();
|
||||||
|
} catch(err) {
|
||||||
|
expect(err.name).toBe('MethodNotImplemented');
|
||||||
|
}
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('$log mock', function() {
|
describe('$log mock', function() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue