diff options
| author | Di Peng | 2011-07-18 15:31:14 -0700 | 
|---|---|---|
| committer | Igor Minar | 2011-07-20 16:50:44 -0700 | 
| commit | 0fbaa2f12ab96328fe2e7b4b9f3ec0c8d7f30e37 (patch) | |
| tree | 2d8eb6eb5e2835848819e61c1b24811c50750e39 | |
| parent | ad3b8d7bcf89454e5567378f3d2771de8c9dc027 (diff) | |
| download | angular.js-0fbaa2f12ab96328fe2e7b4b9f3ec0c8d7f30e37.tar.bz2 | |
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
| -rw-r--r-- | src/angular-mocks.js | 8 | ||||
| -rw-r--r-- | test/angular-mocksSpec.js | 16 | 
2 files changed, 23 insertions, 1 deletions
| diff --git a/src/angular-mocks.js b/src/angular-mocks.js index 20423f05..b105e22b 100644 --- a/src/angular-mocks.js +++ b/src/angular-mocks.js @@ -457,7 +457,7 @@ function MockLogFactory() {   * </pre>   *   */ -function TzDate(offset, timestamp) { +function TzDate(offset, timestamp, toStringVal) {    if (angular.isString(timestamp)) {      var tsStr = timestamp; @@ -481,6 +481,10 @@ function TzDate(offset, timestamp) {      return this.date.getTime() - this.offsetDiff;    }; +  this.toString = function() { +   return toStringVal; +  } +    this.toLocaleDateString = function() {      return this.date.toLocaleDateString();    }; @@ -551,6 +555,8 @@ function TzDate(offset, timestamp) {        'toLocaleTimeString', 'toSource', 'toString', 'toTimeString', 'toUTCString', 'valueOf'];    angular.forEach(unimplementedMethods, function(methodName) { +    if (methodName == 'toString' && toStringVal) return; +      self[methodName] = function() {        throw {          name: "MethodNotImplemented", diff --git a/test/angular-mocksSpec.js b/test/angular-mocksSpec.js index 97949f63..b85503e6 100644 --- a/test/angular-mocksSpec.js +++ b/test/angular-mocksSpec.js @@ -121,6 +121,22 @@ describe('mocks', function(){        expect(date2.getUTCMinutes()).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() { | 
