Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 0fbaa2f

Browse files
Di PengIgorMinar
Di Peng
authored andcommitted
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
1 parent ad3b8d7 commit 0fbaa2f

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/angular-mocks.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ function MockLogFactory() {
457457
* </pre>
458458
*
459459
*/
460-
function TzDate(offset, timestamp) {
460+
function TzDate(offset, timestamp, toStringVal) {
461461
if (angular.isString(timestamp)) {
462462
var tsStr = timestamp;
463463

@@ -481,6 +481,10 @@ function TzDate(offset, timestamp) {
481481
return this.date.getTime() - this.offsetDiff;
482482
};
483483

484+
this.toString = function() {
485+
return toStringVal;
486+
}
487+
484488
this.toLocaleDateString = function() {
485489
return this.date.toLocaleDateString();
486490
};
@@ -551,6 +555,8 @@ function TzDate(offset, timestamp) {
551555
'toLocaleTimeString', 'toSource', 'toString', 'toTimeString', 'toUTCString', 'valueOf'];
552556

553557
angular.forEach(unimplementedMethods, function(methodName) {
558+
if (methodName == 'toString' && toStringVal) return;
559+
554560
self[methodName] = function() {
555561
throw {
556562
name: "MethodNotImplemented",

test/angular-mocksSpec.js

+16
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,22 @@ describe('mocks', function(){
121121
expect(date2.getUTCMinutes()).toBe(0);
122122
expect(date2.getUTCSeconds()).toBe(0);
123123
});
124+
125+
126+
it('should fake toString method when a third param is passed in', function() {
127+
var t = new TzDate(0, 0, 'Mon Sep 3 2010 17:05:08 GMT+0500 (XYZ)');
128+
expect(t.toString()).toBe('Mon Sep 3 2010 17:05:08 GMT+0500 (XYZ)');
129+
});
130+
131+
132+
it('should throw error when no third param but toString called', function() {
133+
var t = new TzDate(0, 0);
134+
try {
135+
t.toString();
136+
} catch(err) {
137+
expect(err.name).toBe('MethodNotImplemented');
138+
}
139+
})
124140
});
125141

126142
describe('$log mock', function() {

0 commit comments

Comments
 (0)