diff options
| -rw-r--r-- | src/services.js | 4 | ||||
| -rw-r--r-- | test/ResourceSpec.js | 58 | ||||
| -rw-r--r-- | test/servicesSpec.js | 136 | 
3 files changed, 76 insertions, 122 deletions
diff --git a/src/services.js b/src/services.js index d6c3ad8a..b854c8d6 100644 --- a/src/services.js +++ b/src/services.js @@ -189,3 +189,7 @@ angularService('$route', function(location, params){    return $route;  }, {inject: ['$location']}); +angularService('$resource', function(browser){ +  var resource = new ResourceFactory(bind(browser, browser.xhr)); +  return bind(resource, resource.route); +}, {inject: ['$browser']}); diff --git a/test/ResourceSpec.js b/test/ResourceSpec.js index 08ca86b3..f2a0ff41 100644 --- a/test/ResourceSpec.js +++ b/test/ResourceSpec.js @@ -1,61 +1,3 @@ -function MockXHR(){ -  this.expectations = { -    'GET': {}, -    'POST': {}, -    'DELETE': {} -  }; -  this.queue = []; -} -MockXHR.prototype = { -  method: function(verb, url, data, callback) { -    if (verb == 'POST') -      url += '|' + angular.toJson(data); -    var response = this.expectations[verb][url]; -    if (!response) -      throw "No expectation for " + verb + " on '" + url + "'."; -    this.queue.push(function(){ -      callback(response); -    }); -  }, - -  expectGET: function(url) { -    var self = this; -    return { -      respond: function(response){ -        self.expectations.GET[url] = response; -      } -    }; -  }, - -  expectDELETE: function(url) { -    var self = this; -    return { -      respond: function(response){ -        self.expectations.DELETE[url] = response; -      } -    }; -  }, - -  expectPOST: function(url) { -    var self = this; -    return { -      data: function(data){ -        return { -          respond: function(response){ -            self.expectations.POST[url + '|' + angular.toJson(data)] = response; -          } -        }; -      } -    }; -  }, - -  flush: function(){ -    while(this.queue.length) { -      this.queue.shift()(); -    } -  } -}; -  describe("resource", function() {    var xhr, resource, CreditCard, callback; diff --git a/test/servicesSpec.js b/test/servicesSpec.js index f917f968..a7391f34 100644 --- a/test/servicesSpec.js +++ b/test/servicesSpec.js @@ -1,10 +1,17 @@ -describe("services", function(){ +describe("service", function(){    var scope;    beforeEach(function(){      scope = createScope(null, angularService, {});    }); +  afterEach(function(){ +    if (scope && scope.$element) +      scope.$element.remove(); +  }); + + +    it("should inject $window", function(){      expect(scope.$window).toEqual(window);    }); @@ -82,75 +89,76 @@ describe("services", function(){      });    }); -}); -describe("service $invalidWidgets", function(){ -  var scope; -  beforeEach(function(){ -    scope = null; -  }); -  afterEach(function(){ -    if (scope && scope.$element) +  describe("$invalidWidgets", function(){ +    it("should count number of invalid widgets", function(){ +      var scope = compile('<input name="price" ng-required ng-validate="number"></input>').$init(); +      expect(scope.$invalidWidgets.length).toEqual(1); +      scope.price = 123; +      scope.$eval(); +      expect(scope.$invalidWidgets.length).toEqual(0);        scope.$element.remove(); +      scope.price = 'abc'; +      scope.$eval(); +      expect(scope.$invalidWidgets.length).toEqual(1); + +      jqLite(document.body).append(scope.$element); +      scope.$invalidWidgets.clearOrphans(); +      expect(scope.$invalidWidgets.length).toEqual(1); + +      jqLite(document.body).html(''); +      scope.$invalidWidgets.clearOrphans(); +      expect(scope.$invalidWidgets.length).toEqual(0); +    });    }); -  it("should count number of invalid widgets", function(){ -    var scope = compile('<input name="price" ng-required ng-validate="number"></input>').$init(); -    expect(scope.$invalidWidgets.length).toEqual(1); -    scope.price = 123; -    scope.$eval(); -    expect(scope.$invalidWidgets.length).toEqual(0); -    scope.$element.remove(); -    scope.price = 'abc'; -    scope.$eval(); -    expect(scope.$invalidWidgets.length).toEqual(1); - -    jqLite(document.body).append(scope.$element); -    scope.$invalidWidgets.clearOrphans(); -    expect(scope.$invalidWidgets.length).toEqual(1); - -    jqLite(document.body).html(''); -    scope.$invalidWidgets.clearOrphans(); -    expect(scope.$invalidWidgets.length).toEqual(0); + +  describe("$route", function(){ +    it('should route and fire change event', function(){ +      var log = ''; +      function BookChapter() { +        this.log = '<init>'; +      } +      BookChapter.prototype.init = function(){ +        log += 'init();'; +      }; +      var scope = compile('<div></div>').$init(); +      scope.$route.when('/Book/:book/Chapter/:chapter', {controller: BookChapter, template:'Chapter.html'}); +      scope.$route.when('/Blank'); +      scope.$route.onChange(function(){ +        log += 'onChange();'; +      }); +      scope.$location.parse('http://server#/Book/Moby/Chapter/Intro?p=123'); +      scope.$eval(); +      expect(log).toEqual('onChange();init();'); +      expect(scope.$route.current.params).toEqual({book:'Moby', chapter:'Intro', p:'123'}); +      expect(scope.$route.current.scope.log).toEqual('<init>'); +      var lastId = scope.$route.current.scope.$id; + +      log = ''; +      scope.$location.parse('http://server#/Blank?ignore'); +      scope.$eval(); +      expect(log).toEqual('onChange();'); +      expect(scope.$route.current.params).toEqual({ignore:true}); +      expect(scope.$route.current.scope.$id).not.toEqual(lastId); + +      log = ''; +      scope.$location.parse('http://server#/NONE'); +      scope.$eval(); +      expect(log).toEqual('onChange();'); +      expect(scope.$route.current).toEqual(null); + +      scope.$route.when('/NONE', {template:'instant update'}); +      expect(scope.$route.current.template).toEqual('instant update'); +    });    }); -}); -describe("service $route", function(){ -  it('should route and fire change event', function(){ -    var log = ''; -    function BookChapter() { -      this.log = '<init>'; -    } -    BookChapter.prototype.init = function(){ -      log += 'init();'; -    }; -    var scope = compile('<div></div>').$init(); -    scope.$route.when('/Book/:book/Chapter/:chapter', {controller: BookChapter, template:'Chapter.html'}); -    scope.$route.when('/Blank'); -    scope.$route.onChange(function(){ -      log += 'onChange();'; +  describe('$resource', function(){ +    it('should publish to root scope', function(){ +      expect(scope.$resource).toBeTruthy();      }); -    scope.$location.parse('http://server#/Book/Moby/Chapter/Intro?p=123'); -    scope.$eval(); -    expect(log).toEqual('onChange();init();'); -    expect(scope.$route.current.params).toEqual({book:'Moby', chapter:'Intro', p:'123'}); -    expect(scope.$route.current.scope.log).toEqual('<init>'); -    var lastId = scope.$route.current.scope.$id; - -    log = ''; -    scope.$location.parse('http://server#/Blank?ignore'); -    scope.$eval(); -    expect(log).toEqual('onChange();'); -    expect(scope.$route.current.params).toEqual({ignore:true}); -    expect(scope.$route.current.scope.$id).not.toEqual(lastId); - -    log = ''; -    scope.$location.parse('http://server#/NONE'); -    scope.$eval(); -    expect(log).toEqual('onChange();'); -    expect(scope.$route.current).toEqual(null); - -    scope.$route.when('/NONE', {template:'instant update'}); -    expect(scope.$route.current.template).toEqual('instant update');    }); +  }); + +  | 
