diff options
| author | Misko Hevery | 2011-10-17 16:56:56 -0700 | 
|---|---|---|
| committer | Misko Hevery | 2011-11-14 16:39:31 -0800 | 
| commit | 48697a2b86dbb12ea8de64cc5fece7caf68b321e (patch) | |
| tree | 1fa50659f0bb5de2640dea2a2e5bb5628f2bb14a | |
| parent | 93b777c916ccff243c5a6080bf5f39860ac7bf39 (diff) | |
| download | angular.js-48697a2b86dbb12ea8de64cc5fece7caf68b321e.tar.bz2 | |
refactor(injector): turn scope into a service
- turn scope into a $rootScope service.
- injector is now a starting point for creating angular application.
- added inject() method which wraps jasmine its/beforeEach/afterEach,
  and which allows configuration and injection of services.
- refactor tests to use inject() where possible
BREAK:
- removed angular.scope() method
51 files changed, 3114 insertions, 3199 deletions
| diff --git a/example/personalLog/test/personalLogSpec.js b/example/personalLog/test/personalLogSpec.js index 3e6935a3..cf80a420 100644 --- a/example/personalLog/test/personalLogSpec.js +++ b/example/personalLog/test/personalLogSpec.js @@ -2,8 +2,9 @@ describe('example.personalLog.LogCtrl', function() {    var logCtrl;    function createNotesCtrl() { -    var scope = angular.scope(); -    scope.$cookies = scope.$service('$cookies'); +    var injector = angular.injector(); +    var scope = injector('$rootScope'); +    scope.$cookies = injector('$cookies');      return scope.$new(example.personalLog.LogCtrl);    } diff --git a/src/Angular.js b/src/Angular.js index f1eb74ca..0ef1af8e 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -952,8 +952,12 @@ function angularInit(config, document){    var autobind = config.autobind;    if (autobind) { -    var element = isString(autobind) ? document.getElementById(autobind) : document; -    compile(element)().$apply(); +    var element = isString(autobind) ? document.getElementById(autobind) : document, +        injector = createInjector(), +        scope = injector('$rootScope'); + +    compile(element)(scope); +    scope.$apply();    }  } diff --git a/src/AngularPublic.js b/src/AngularPublic.js index fc8a90fd..308ea9c0 100644 --- a/src/AngularPublic.js +++ b/src/AngularPublic.js @@ -15,7 +15,6 @@ extend(angular, {    // disabled for now until we agree on public name    //'annotate': annotate,    'compile': compile, -  'scope': createScope,    'copy': copy,    'extend': extend,    'equals': equals, diff --git a/src/Compiler.js b/src/Compiler.js index 0411c70d..ee768a9d 100644 --- a/src/Compiler.js +++ b/src/Compiler.js @@ -102,11 +102,10 @@ Template.prototype = {   *   *   * @param {string|DOMElement} element Element or HTML to compile into a template function. - * @returns {function([scope][, cloneAttachFn])} a template function which is used to bind template + * @returns {function(scope[, cloneAttachFn])} a template function which is used to bind template   * (a DOM element/tree) to a scope. Where:   * - *  * `scope` - A {@link angular.scope Scope} to bind to. If none specified, then a new - *               root scope is created. + *  * `scope` - A {@link angular.scope Scope} to bind to.   *  * `cloneAttachFn` - If `cloneAttachFn` is provided, then the link function will clone the   *               `template` and call the `cloneAttachFn` function allowing the caller to attach the   *               cloned elements to the DOM document at the appropriate place. The `cloneAttachFn` is @@ -115,9 +114,8 @@ Template.prototype = {   *      * `clonedElement` - is a clone of the original `element` passed into the compiler.   *      * `scope` - is the current scope with which the linking function is working with.   * - * Calling the template function returns the scope to which the element is bound to. It is either - * the same scope as the one passed into the template function, or if none were provided it's the - * newly create scope. + * Calling the template function returns the element of the template. It is either the original element + * passed in, or the clone of the element if the `cloneAttachFn` is provided.   *   * It is important to understand that the returned scope is "linked" to the view DOM, but no linking   * (instance) functions registered by {@link angular.directive directives} or @@ -133,8 +131,8 @@ Template.prototype = {   * - If you are not asking the linking function to clone the template, create the DOM element(s)   *   before you send them to the compiler and keep this reference around.   *   <pre> - *     var view = angular.element('<p>{{total}}</p>'), - *         scope = angular.compile(view)(); + *     var scope = angular.injector()('$rootScope'); + *     var element = angular.compile('<p>{{total}}</p>')(scope);   *   </pre>   *   * - if on the other hand, you need the element to be cloned, the view reference from the original @@ -208,17 +206,17 @@ Compiler.prototype = {      }      template = this.templatize(templateElement, index) || new Template();      return function(scope, cloneConnectFn){ +      assertArg(scope, 'scope');        // important!!: we must call our jqLite.clone() since the jQuery one is trying to be smart        // and sometimes changes the structure of the DOM.        var element = cloneConnectFn          ? JQLitePrototype.clone.call(templateElement) // IMPORTANT!!!          : templateElement; -        scope = scope || createScope();        element.data($$scope, scope);        scope.$element = element;        (cloneConnectFn||noop)(element, scope);        template.link(element, scope); -      return scope; +      return element;      };    }, diff --git a/src/Injector.js b/src/Injector.js index d6cf15c2..ae584364 100644 --- a/src/Injector.js +++ b/src/Injector.js @@ -9,17 +9,11 @@   * Creates an injector function that can be used for retrieving services as well as for   * dependency injection (see {@link guide/dev_guide.di dependency injection}).   * - * Angular creates an injector automatically for the root scope and it is available as the - * {@link angular.scope.$service $service} property. Creating an injector doesn't automatically - * create all of the `$eager` {@link angular.service services}. You have to call `injector.eager()` - * to initialize them. + * Creating an injector doesn't automatically create all of the `$eager` + * {@link angular.service services}. You have to call `injector.eager()` to initialize them.   * - * @param {Object=} [factoryScope={}] The `this` for the service factory function.   * @param {Object.<string, function()>=} [factories=angular.service] Map of the service factory   *     functions. - * @param {Object.<string, function()>=} [instanceCache={}] Place where instances of services are - *     saved for reuse. Can also be used to override services specified by `serviceFactory` - *     (useful in tests).   * @returns {function()} Injector function:   *   *   * `injector(serviceName)`: @@ -38,30 +32,24 @@   *   * An `eager` property which is used to initialize the eager services.   *     `injector.eager()`   */ -function createInjector(factoryScope, factories, instanceCache) { +function createInjector(factories) { +  var instanceCache = {$injector: injector};    factories = factories || angularService; -  instanceCache = instanceCache || {}; -  factoryScope = factoryScope || {}; -  injector.invoke = invoke; -  injector.eager = function() { -    forEach(factories, function(factory, name){ -      if (factory.$eager) -        injector(name); +  injector.invoke = invoke; -      if (factory.$creation) -        throw new Error("Failed to register service '" + name + -        "': $creation property is unsupported. Use $eager:true or see release notes."); -    }); -  }; +  forEach(factories, function(factory, name){ +    if (factory.$eager) +      injector(name); +  });    return injector;    function injector(value){      if (!(value in instanceCache)) {        var factory = factories[value]; -      if (!factory) throw Error("Unknown provider for '"+value+"'."); +      if (!factory) throw Error("Unknown provider for '" + value + "'.");        inferInjectionArgs(factory); -      instanceCache[value] = invoke(factoryScope, factory); +      instanceCache[value] = invoke(null, factory);      }      return instanceCache[value];    } diff --git a/src/scenario/Runner.js b/src/scenario/Runner.js index f3211fd2..d7e1a82b 100644 --- a/src/scenario/Runner.js +++ b/src/scenario/Runner.js @@ -163,7 +163,7 @@ angular.scenario.Runner.prototype.createSpecRunner_ = function(scope) {   */  angular.scenario.Runner.prototype.run = function(application) {    var self = this; -  var $root = angular.scope(); +  var $root = angular.injector()('$rootScope');    angular.extend($root, this);    angular.forEach(angular.scenario.Runner.prototype, function(fn, name) {      $root[name] = angular.bind(self, fn); diff --git a/src/scenario/dsl.js b/src/scenario/dsl.js index db81dd35..13ae8b8f 100644 --- a/src/scenario/dsl.js +++ b/src/scenario/dsl.js @@ -103,25 +103,25 @@ angular.scenario.dsl('browser', function() {      api.url = function() {        return this.addFutureAction('$location.url()', function($window, $document, done) { -        done(null, $window.angular.scope().$service('$location').url()); +        done(null, $window.angular.injector()('$location').url());        });      };      api.path = function() {        return this.addFutureAction('$location.path()', function($window, $document, done) { -        done(null, $window.angular.scope().$service('$location').path()); +        done(null, $window.angular.injector()('$location').path());        });      };      api.search = function() {        return this.addFutureAction('$location.search()', function($window, $document, done) { -        done(null, $window.angular.scope().$service('$location').search()); +        done(null, $window.angular.injector()('$location').search());        });      };      api.hash = function() {        return this.addFutureAction('$location.hash()', function($window, $document, done) { -        done(null, $window.angular.scope().$service('$location').hash()); +        done(null, $window.angular.injector()('$location').hash());        });      }; diff --git a/src/service/cookies.js b/src/service/cookies.js index a2ccee09..2cd2b9d6 100644 --- a/src/service/cookies.js +++ b/src/service/cookies.js @@ -13,9 +13,8 @@   *   * @example   */ -angularServiceInject('$cookies', function($browser) { -  var rootScope = this, -      cookies = {}, +angularServiceInject('$cookies', function($rootScope, $browser) { +  var cookies = {},        lastCookies = {},        lastBrowserCookies,        runEval = false; @@ -27,7 +26,7 @@ angularServiceInject('$cookies', function($browser) {        lastBrowserCookies = currentCookies;        copy(currentCookies, lastCookies);        copy(currentCookies, cookies); -      if (runEval) rootScope.$apply(); +      if (runEval) $rootScope.$apply();      }    })(); @@ -36,7 +35,7 @@ angularServiceInject('$cookies', function($browser) {    //at the end of each eval, push cookies    //TODO: this should happen before the "delayed" watches fire, because if some cookies are not    //      strings or browser refuses to store some cookies, we update the model in the push fn. -  this.$watch(push); +  $rootScope.$watch(push);    return cookies; @@ -90,4 +89,4 @@ angularServiceInject('$cookies', function($browser) {        }      }    } -}, ['$browser']); +}, ['$rootScope', '$browser']); diff --git a/src/service/defer.js b/src/service/defer.js index 42f80d25..07c98065 100644 --- a/src/service/defer.js +++ b/src/service/defer.js @@ -28,12 +28,10 @@   * @param {*} deferId Token returned by the `$defer` function.   * @returns {boolean} Returns `true` if the task hasn't executed yet and was successfuly canceled.   */ -angularServiceInject('$defer', function($browser) { -  var scope = this; - +angularServiceInject('$defer', function($rootScope, $browser) {    function defer(fn, delay) {      return $browser.defer(function() { -      scope.$apply(fn); +      $rootScope.$apply(fn);      }, delay);    } @@ -42,4 +40,4 @@ angularServiceInject('$defer', function($browser) {    };    return defer; -}, ['$browser']); +}, ['$rootScope', '$browser']); diff --git a/src/service/formFactory.js b/src/service/formFactory.js index e7ff42ff..fa6ad201 100644 --- a/src/service/formFactory.js +++ b/src/service/formFactory.js @@ -96,7 +96,7 @@        </doc:scenario>      </doc:example>   */ -angularServiceInject('$formFactory', function() { +angularServiceInject('$formFactory', function($rootScope) {    /** @@ -109,7 +109,7 @@ angularServiceInject('$formFactory', function() {     * Each application ({@link guide/dev_guide.scopes.internals root scope}) gets a root form which     * is the top-level parent of all forms.     */ -  formFactory.rootForm = formFactory(this); +  formFactory.rootForm = formFactory($rootScope);    /** @@ -132,7 +132,7 @@ angularServiceInject('$formFactory', function() {      return (parent || formFactory.rootForm).$new(FormController);    } -}); +}, ['$rootScope']);  function propertiesUpdate(widget) {    widget.$valid = !(widget.$invalid = diff --git a/src/service/location.js b/src/service/location.js index d1d34e67..c9b76122 100644 --- a/src/service/location.js +++ b/src/service/location.js @@ -419,8 +419,8 @@ function locationGetterSetter(property, preprocess) {   *   * For more information see {@link guide/dev_guide.services.$location Developer Guide: Angular Services: Using $location}   */ -angularServiceInject('$location', function($browser, $sniffer, $locationConfig, $document) { -  var scope = this, currentUrl, +angularServiceInject('$location', function($rootScope, $browser, $sniffer, $locationConfig, $document) { +  var currentUrl,        basePath = $browser.baseHref() || '/',        pathPrefix = pathPrefixFromBase(basePath),        hashPrefix = $locationConfig.hashPrefix || '', @@ -464,7 +464,7 @@ angularServiceInject('$location', function($browser, $sniffer, $locationConfig,        href = href.indexOf(pathPrefix) === 0 ? href.substr(pathPrefix.length) : href;        currentUrl.url(href); -      scope.$apply(); +      $rootScope.$apply();        event.preventDefault();        // hack to work around FF6 bug 684208 when scenario runner clicks on links        window.angular['ff-684208-preventDefault'] = true; @@ -482,16 +482,16 @@ angularServiceInject('$location', function($browser, $sniffer, $locationConfig,    $browser.onUrlChange(function(newUrl) {      if (currentUrl.absUrl() != newUrl) {        currentUrl.$$parse(newUrl); -      scope.$apply(); +      $rootScope.$apply();      }    });    // update browser    var changeCounter = 0; -  scope.$watch(function() { +  $rootScope.$watch(function() {      if ($browser.url() != currentUrl.absUrl()) {        changeCounter++; -      scope.$evalAsync(function() { +      $rootScope.$evalAsync(function() {          $browser.url(currentUrl.absUrl(), currentUrl.$$replace);          currentUrl.$$replace = false;        }); @@ -501,7 +501,7 @@ angularServiceInject('$location', function($browser, $sniffer, $locationConfig,    });    return currentUrl; -}, ['$browser', '$sniffer', '$locationConfig', '$document']); +}, ['$rootScope', '$browser', '$sniffer', '$locationConfig', '$document']);  angular.service('$locationConfig', function() { diff --git a/src/service/route.js b/src/service/route.js index ddc3df49..3918c251 100644 --- a/src/service/route.js +++ b/src/service/route.js @@ -62,7 +62,7 @@        </doc:scenario>      </doc:example>   */ -angularServiceInject('$route', function($location, $routeParams) { +angularServiceInject('$route', function($rootScope, $location, $routeParams) {    /**     * @ngdoc event     * @name angular.service.$route#$beforeRouteChange @@ -112,8 +112,7 @@ angularServiceInject('$route', function($location, $routeParams) {    var routes = {},        matcher = switchRouteMatcher, -      parentScope = this, -      rootScope = this, +      parentScope = $rootScope,        dirty = 0,        forceReload = false,        $route = { @@ -220,7 +219,7 @@ angularServiceInject('$route', function($location, $routeParams) {          }        }; -  this.$watch(function() { return dirty + $location.url(); }, updateRoute); +  $rootScope.$watch(function() { return dirty + $location.url(); }, updateRoute);    return $route; @@ -262,7 +261,7 @@ angularServiceInject('$route', function($location, $routeParams) {        last.scope && last.scope.$emit('$routeUpdate');      } else {        forceReload = false; -      rootScope.$broadcast('$beforeRouteChange', next, last); +      $rootScope.$broadcast('$beforeRouteChange', next, last);        last && last.scope && last.scope.$destroy();        $route.current = next;        if (next) { @@ -280,7 +279,7 @@ angularServiceInject('$route', function($location, $routeParams) {            next.scope = parentScope.$new(Controller);          }        } -      rootScope.$broadcast('$afterRouteChange', next, last); +      $rootScope.$broadcast('$afterRouteChange', next, last);      }    } @@ -323,4 +322,4 @@ angularServiceInject('$route', function($location, $routeParams) {    } -}, ['$location', '$routeParams']); +}, ['$rootScope', '$location', '$routeParams']); diff --git a/src/service/scope.js b/src/service/scope.js index c4b9513b..94c41041 100644 --- a/src/service/scope.js +++ b/src/service/scope.js @@ -25,655 +25,630 @@   * are expensive to construct.   */ - -function createScope(providers, instanceCache) { -  var scope = new Scope(); -  (scope.$service = createInjector(scope, providers, instanceCache)).eager(); -  return scope; -} - - -/** - * @ngdoc function - * @name angular.scope - * - * @description - * A root scope can be created by calling {@link angular.scope angular.scope()}. Child scopes - * are created using the {@link angular.scope.$new $new()} method. - * (Most scopes are created automatically when compiled HTML template is executed.) - * - * Here is a simple scope snippet to show how you can interact with the scope. - * <pre> -       var scope = angular.scope(); -       scope.salutation = 'Hello'; -       scope.name = 'World'; - -       expect(scope.greeting).toEqual(undefined); - -       scope.$watch('name', function() { -         this.greeting = this.salutation + ' ' + this.name + '!'; -       }); // initialize the watch - -       expect(scope.greeting).toEqual(undefined); -       scope.name = 'Misko'; -       // still old value, since watches have not been called yet -       expect(scope.greeting).toEqual(undefined); - -       scope.$digest(); // fire all  the watches -       expect(scope.greeting).toEqual('Hello Misko!'); - * </pre> - * - * # Inheritance - * A scope can inherit from a parent scope, as in this example: - * <pre> -     var parent = angular.scope(); -     var child = parent.$new(); - -     parent.salutation = "Hello"; -     child.name = "World"; -     expect(child.salutation).toEqual('Hello'); - -     child.salutation = "Welcome"; -     expect(child.salutation).toEqual('Welcome'); -     expect(parent.salutation).toEqual('Hello'); - * </pre> - * - * # Dependency Injection - * See {@link guide/dev_guide.di dependency injection}. - * - * - * @param {Object.<string, function()>=} providers Map of service factory which need to be provided - *     for the current scope. Defaults to {@link angular.service}. - * @param {Object.<string, *>=} instanceCache Provides pre-instantiated services which should - *     append/override services provided by `providers`. This is handy when unit-testing and having - *     the need to override a default service. - * @returns {Object} Newly created scope. - * - */ -function Scope() { -  this.$id = nextUid(); -  this.$$phase = this.$parent = this.$$watchers = -                 this.$$nextSibling = this.$$prevSibling = -                 this.$$childHead = this.$$childTail = null; -  this.$destructor = noop; -  this['this'] = this.$root =  this; -  this.$$asyncQueue = []; -  this.$$listeners = {}; -} - -/** - * @ngdoc property - * @name angular.scope.$id - * @returns {number} Unique scope ID (monotonically increasing alphanumeric sequence) useful for - *   debugging. - */ - -/** - * @ngdoc property - * @name angular.scope.$service - * @function - * - * @description - * Provides reference to an instance of {@link angular.injector injector} which can be used to - * retrieve {@link angular.service services}. In general the use of this api is discouraged, - * in favor of proper {@link guide/dev_guide.di dependency injection}. - * - * @returns {function} {@link angular.injector injector} - */ - -/** - * @ngdoc property - * @name angular.scope.$root - * @returns {Scope} The root scope of the current scope hierarchy. - */ - -/** - * @ngdoc property - * @name angular.scope.$parent - * @returns {Scope} The parent scope of the current scope. - */ - - -Scope.prototype = { +angularServiceInject('$rootScope', function($injector, $exceptionHandler){    /**     * @ngdoc function -   * @name angular.scope.$new -   * @function +   * @name angular.scope     *     * @description -   * Creates a new child {@link angular.scope scope}. The new scope can optionally behave as a -   * controller. The parent scope will propagate the {@link angular.scope.$digest $digest()} and -   * {@link angular.scope.$digest $digest()} events. The scope can be removed from the scope -   * hierarchy using {@link angular.scope.$destroy $destroy()}. -   * -   * {@link angular.scope.$destroy $destroy()} must be called on a scope when it is desired for -   * the scope and its child scopes to be permanently detached from the parent and thus stop -   * participating in model change detection and listener notification by invoking. -   * -   * @param {function()=} Class Constructor function which the scope should be applied to the scope. -   * @param {...*} curryArguments Any additional arguments which are curried into the constructor. -   *        See {@link guide/dev_guide.di dependency injection}. -   * @returns {Object} The newly created child scope. +   * A root scope can be created by calling {@link angular.scope angular.scope()}. Child scopes +   * are created using the {@link angular.scope.$new $new()} method. +   * (Most scopes are created automatically when compiled HTML template is executed.)     * -   */ -  $new: function(Class, curryArguments) { -    var Child = function() {}; // should be anonymous; This is so that when the minifier munges -      // the name it does not become random set of chars. These will then show up as class -      // name in the debugger. -    var child; -    Child.prototype = this; -    child = new Child(); -    child['this'] = child; -    child.$$listeners = {}; -    child.$parent = this; -    child.$id = nextUid(); -    child.$$asyncQueue = []; -    child.$$phase = child.$$watchers = -      child.$$nextSibling = child.$$childHead = child.$$childTail = null; -    child.$$prevSibling = this.$$childTail; -    if (this.$$childHead) { -      this.$$childTail.$$nextSibling = child; -      this.$$childTail = child; -    } else { -      this.$$childHead = this.$$childTail = child; -    } -    // short circuit if we have no class -    if (Class) { -      // can't use forEach, we need speed! -      var ClassPrototype = Class.prototype; -      for(var key in ClassPrototype) { -        child[key] = bind(child, ClassPrototype[key]); -      } -      this.$service.invoke(child, Class, curryArguments); -    } -    return child; -  }, +   * Here is a simple scope snippet to show how you can interact with the scope. +   * <pre> +         var scope = angular.scope(); +         scope.salutation = 'Hello'; +         scope.name = 'World'; -  /** -   * @ngdoc function -   * @name angular.scope.$watch -   * @function -   * -   * @description -   * Registers a `listener` callback to be executed whenever the `watchExpression` changes. -   * -   * - The `watchExpression` is called on every call to {@link angular.scope.$digest $digest()} and -   *   should return the value which will be watched. (Since {@link angular.scope.$digest $digest()} -   *   reruns when it detects changes the `watchExpression` can execute multiple times per -   *   {@link angular.scope.$digest $digest()} and should be idempotent.) -   * - The `listener` is called only when the value from the current `watchExpression` and the -   *   previous call to `watchExpression' are not equal. The inequality is determined according to -   *   {@link angular.equals} function. To save the value of the object for later comparison -   *   {@link angular.copy} function is used. It also means that watching complex options will -   *   have adverse memory and performance implications. -   * - The watch `listener` may change the model, which may trigger other `listener`s to fire. This -   *   is achieved by rerunning the watchers until no changes are detected. The rerun iteration -   *   limit is 100 to prevent infinity loop deadlock. -   * -   * -   * If you want to be notified whenever {@link angular.scope.$digest $digest} is called, -   * you can register an `watchExpression` function with no `listener`. (Since `watchExpression`, -   * can execute multiple times per {@link angular.scope.$digest $digest} cycle when a change is -   * detected, be prepared for multiple calls to your listener.) -   * -   * -   * # Example -     <pre> -       var scope = angular.scope(); -       scope.name = 'misko'; -       scope.counter = 0; - -       expect(scope.counter).toEqual(0); -       scope.$watch('name', function(scope, newValue, oldValue) { counter = counter + 1; }); -       expect(scope.counter).toEqual(0); - -       scope.$digest(); -       // no variable change -       expect(scope.counter).toEqual(0); - -       scope.name = 'adam'; -       scope.$digest(); -       expect(scope.counter).toEqual(1); -     </pre> +         expect(scope.greeting).toEqual(undefined); + +         scope.$watch('name', function() { +           this.greeting = this.salutation + ' ' + this.name + '!'; +         }); // initialize the watch + +         expect(scope.greeting).toEqual(undefined); +         scope.name = 'Misko'; +         // still old value, since watches have not been called yet +         expect(scope.greeting).toEqual(undefined); + +         scope.$digest(); // fire all  the watches +         expect(scope.greeting).toEqual('Hello Misko!'); +   * </pre>     * +   * # Inheritance +   * A scope can inherit from a parent scope, as in this example: +   * <pre> +       var parent = angular.scope(); +       var child = parent.$new(); + +       parent.salutation = "Hello"; +       child.name = "World"; +       expect(child.salutation).toEqual('Hello'); + +       child.salutation = "Welcome"; +       expect(child.salutation).toEqual('Welcome'); +       expect(parent.salutation).toEqual('Hello'); +   * </pre>     * +   * # Dependency Injection +   * See {@link guide/dev_guide.di dependency injection}.     * -   * @param {(function()|string)} watchExpression Expression that is evaluated on each -   *    {@link angular.scope.$digest $digest} cycle. A change in the return value triggers a -   *    call to the `listener`.     * -   *    - `string`: Evaluated as {@link guide/dev_guide.expressions expression} -   *    - `function(scope)`: called with current `scope` as a parameter. -   * @param {(function()|string)=} listener Callback called whenever the return value of -   *   the `watchExpression` changes. +   * @param {Object.<string, function()>=} providers Map of service factory which need to be provided +   *     for the current scope. Defaults to {@link angular.service}. +   * @param {Object.<string, *>=} instanceCache Provides pre-instantiated services which should +   *     append/override services provided by `providers`. This is handy when unit-testing and having +   *     the need to override a default service. +   * @returns {Object} Newly created scope.     * -   *    - `string`: Evaluated as {@link guide/dev_guide.expressions expression} -   *    - `function(scope, newValue, oldValue)`: called with current `scope` an previous and -   *       current values as parameters. -   * @returns {function()} Returns a deregistration function for this listener.     */ -  $watch: function(watchExp, listener) { -    var scope = this, -        get = compileToFn(watchExp, 'watch'), -        listenFn = compileToFn(listener || noop, 'listener'), -        array = scope.$$watchers, -        watcher = { -          fn: listenFn, -          last: Number.NaN, // NaN !== NaN. We used this to force $watch to fire on first run. -          get: get, -          exp: watchExp -        }; - -    if (!array) { -      array = scope.$$watchers = []; -    } -    // we use unshift since we use a while loop in $digest for speed. -    // the while loop reads in reverse order. -    array.unshift(watcher); - -    return function() { -      angularArray.remove(array, watcher); -    }; -  }, +  function Scope() { +    this.$id = nextUid(); +    this.$$phase = this.$parent = this.$$watchers = +                   this.$$nextSibling = this.$$prevSibling = +                   this.$$childHead = this.$$childTail = null; +    this.$destructor = noop; +    this['this'] = this.$root =  this; +    this.$$asyncQueue = []; +    this.$$listeners = {}; +  }    /** -   * @ngdoc function -   * @name angular.scope.$digest -   * @function -   * -   * @description -   * Process all of the {@link angular.scope.$watch watchers} of the current scope and its children. -   * Because a {@link angular.scope.$watch watcher}'s listener can change the model, the -   * `$digest()` keeps calling the {@link angular.scope.$watch watchers} until no more listeners are -   * firing. This means that it is possible to get into an infinite loop. This function will throw -   * `'Maximum iteration limit exceeded.'` if the number of iterations exceeds 100. -   * -   * Usually you don't call `$digest()` directly in -   * {@link angular.directive.ng:controller controllers} or in {@link angular.directive directives}. -   * Instead a call to {@link angular.scope.$apply $apply()} (typically from within a -   * {@link angular.directive directive}) will force a `$digest()`. -   * -   * If you want to be notified whenever `$digest()` is called, -   * you can register a `watchExpression` function  with {@link angular.scope.$watch $watch()} -   * with no `listener`. -   * -   * You may have a need to call `$digest()` from within unit-tests, to simulate the scope -   * life-cycle. -   * -   * # Example -     <pre> -       var scope = angular.scope(); -       scope.name = 'misko'; -       scope.counter = 0; - -       expect(scope.counter).toEqual(0); -       scope.$digest('name', function(scope, newValue, oldValue) { counter = counter + 1; }); -       expect(scope.counter).toEqual(0); - -       scope.$digest(); -       // no variable change -       expect(scope.counter).toEqual(0); - -       scope.name = 'adam'; -       scope.$digest(); -       expect(scope.counter).toEqual(1); -     </pre> -   * +   * @ngdoc property +   * @name angular.scope.$id +   * @returns {number} Unique scope ID (monotonically increasing alphanumeric sequence) useful for +   *   debugging.     */ -  $digest: function() { -    var watch, value, last, -        watchers, -        asyncQueue, -        length, -        dirty, ttl = 100, -        next, current, target = this, -        watchLog = []; - -    if (target.$$phase) { -      throw Error(target.$$phase + ' already in progress'); -    } -    do { -      dirty = false; -      current = target; -      do { -        current.$$phase = '$digest'; -        asyncQueue = current.$$asyncQueue; -        while(asyncQueue.length) { -          try { -            current.$eval(asyncQueue.shift()); -          } catch (e) { -            current.$service('$exceptionHandler')(e); -          } + +  Scope.prototype = { +    /** +     * @ngdoc function +     * @name angular.scope.$new +     * @function +     * +     * @description +     * Creates a new child {@link angular.scope scope}. The new scope can optionally behave as a +     * controller. The parent scope will propagate the {@link angular.scope.$digest $digest()} and +     * {@link angular.scope.$digest $digest()} events. The scope can be removed from the scope +     * hierarchy using {@link angular.scope.$destroy $destroy()}. +     * +     * {@link angular.scope.$destroy $destroy()} must be called on a scope when it is desired for +     * the scope and its child scopes to be permanently detached from the parent and thus stop +     * participating in model change detection and listener notification by invoking. +     * +     * @param {function()=} Class Constructor function which the scope should be applied to the scope. +     * @param {...*} curryArguments Any additional arguments which are curried into the constructor. +     *        See {@link guide/dev_guide.di dependency injection}. +     * @returns {Object} The newly created child scope. +     * +     */ +    $new: function(Class, curryArguments) { +      var Child = function() {}; // should be anonymous; This is so that when the minifier munges +        // the name it does not become random set of chars. These will then show up as class +        // name in the debugger. +      var child; +      Child.prototype = this; +      child = new Child(); +      child['this'] = child; +      child.$$listeners = {}; +      child.$parent = this; +      child.$id = nextUid(); +      child.$$asyncQueue = []; +      child.$$phase = child.$$watchers = +        child.$$nextSibling = child.$$childHead = child.$$childTail = null; +      child.$$prevSibling = this.$$childTail; +      if (this.$$childHead) { +        this.$$childTail.$$nextSibling = child; +        this.$$childTail = child; +      } else { +        this.$$childHead = this.$$childTail = child; +      } +      // short circuit if we have no class +      if (Class) { +        // can't use forEach, we need speed! +        var ClassPrototype = Class.prototype; +        for(var key in ClassPrototype) { +          child[key] = bind(child, ClassPrototype[key]);          } -        if ((watchers = current.$$watchers)) { -          // process our watches -          length = watchers.length; -          while (length--) { +        $injector.invoke(child, Class, curryArguments); +      } +      return child; +    }, + +    /** +     * @ngdoc function +     * @name angular.scope.$watch +     * @function +     * +     * @description +     * Registers a `listener` callback to be executed whenever the `watchExpression` changes. +     * +     * - The `watchExpression` is called on every call to {@link angular.scope.$digest $digest()} and +     *   should return the value which will be watched. (Since {@link angular.scope.$digest $digest()} +     *   reruns when it detects changes the `watchExpression` can execute multiple times per +     *   {@link angular.scope.$digest $digest()} and should be idempotent.) +     * - The `listener` is called only when the value from the current `watchExpression` and the +     *   previous call to `watchExpression' are not equal. The inequality is determined according to +     *   {@link angular.equals} function. To save the value of the object for later comparison +     *   {@link angular.copy} function is used. It also means that watching complex options will +     *   have adverse memory and performance implications. +     * - The watch `listener` may change the model, which may trigger other `listener`s to fire. This +     *   is achieved by rerunning the watchers until no changes are detected. The rerun iteration +     *   limit is 100 to prevent infinity loop deadlock. +     * +     * +     * If you want to be notified whenever {@link angular.scope.$digest $digest} is called, +     * you can register an `watchExpression` function with no `listener`. (Since `watchExpression`, +     * can execute multiple times per {@link angular.scope.$digest $digest} cycle when a change is +     * detected, be prepared for multiple calls to your listener.) +     * +     * +     * # Example +       <pre> +         var scope = angular.scope(); +         scope.name = 'misko'; +         scope.counter = 0; + +         expect(scope.counter).toEqual(0); +         scope.$watch('name', function(scope, newValue, oldValue) { counter = counter + 1; }); +         expect(scope.counter).toEqual(0); + +         scope.$digest(); +         // no variable change +         expect(scope.counter).toEqual(0); + +         scope.name = 'adam'; +         scope.$digest(); +         expect(scope.counter).toEqual(1); +       </pre> +     * +     * +     * +     * @param {(function()|string)} watchExpression Expression that is evaluated on each +     *    {@link angular.scope.$digest $digest} cycle. A change in the return value triggers a +     *    call to the `listener`. +     * +     *    - `string`: Evaluated as {@link guide/dev_guide.expressions expression} +     *    - `function(scope)`: called with current `scope` as a parameter. +     * @param {(function()|string)=} listener Callback called whenever the return value of +     *   the `watchExpression` changes. +     * +     *    - `string`: Evaluated as {@link guide/dev_guide.expressions expression} +     *    - `function(scope, newValue, oldValue)`: called with current `scope` an previous and +     *       current values as parameters. +     * @returns {function()} Returns a deregistration function for this listener. +     */ +    $watch: function(watchExp, listener) { +      var scope = this, +          get = compileToFn(watchExp, 'watch'), +          listenFn = compileToFn(listener || noop, 'listener'), +          array = scope.$$watchers, +          watcher = { +            fn: listenFn, +            last: Number.NaN, // NaN !== NaN. We used this to force $watch to fire on first run. +            get: get, +            exp: watchExp +          }; + +      if (!array) { +        array = scope.$$watchers = []; +      } +      // we use unshift since we use a while loop in $digest for speed. +      // the while loop reads in reverse order. +      array.unshift(watcher); + +      return function() { +        angularArray.remove(array, watcher); +      }; +    }, + +    /** +     * @ngdoc function +     * @name angular.scope.$digest +     * @function +     * +     * @description +     * Process all of the {@link angular.scope.$watch watchers} of the current scope and its children. +     * Because a {@link angular.scope.$watch watcher}'s listener can change the model, the +     * `$digest()` keeps calling the {@link angular.scope.$watch watchers} until no more listeners are +     * firing. This means that it is possible to get into an infinite loop. This function will throw +     * `'Maximum iteration limit exceeded.'` if the number of iterations exceeds 100. +     * +     * Usually you don't call `$digest()` directly in +     * {@link angular.directive.ng:controller controllers} or in {@link angular.directive directives}. +     * Instead a call to {@link angular.scope.$apply $apply()} (typically from within a +     * {@link angular.directive directive}) will force a `$digest()`. +     * +     * If you want to be notified whenever `$digest()` is called, +     * you can register a `watchExpression` function  with {@link angular.scope.$watch $watch()} +     * with no `listener`. +     * +     * You may have a need to call `$digest()` from within unit-tests, to simulate the scope +     * life-cycle. +     * +     * # Example +       <pre> +         var scope = angular.scope(); +         scope.name = 'misko'; +         scope.counter = 0; + +         expect(scope.counter).toEqual(0); +         scope.$digest('name', function(scope, newValue, oldValue) { counter = counter + 1; }); +         expect(scope.counter).toEqual(0); + +         scope.$digest(); +         // no variable change +         expect(scope.counter).toEqual(0); + +         scope.name = 'adam'; +         scope.$digest(); +         expect(scope.counter).toEqual(1); +       </pre> +     * +     */ +    $digest: function() { +      var watch, value, last, +          watchers, +          asyncQueue, +          length, +          dirty, ttl = 100, +          next, current, target = this, +          watchLog = []; + +      if (target.$$phase) { +        throw Error(target.$$phase + ' already in progress'); +      } +      do { + +        dirty = false; +        current = target; +        do { +          current.$$phase = '$digest'; +          asyncQueue = current.$$asyncQueue; +          while(asyncQueue.length) {              try { -              watch = watchers[length]; -              // Most common watches are on primitives, in which case we can short -              // circuit it with === operator, only when === fails do we use .equals -              if ((value = watch.get(current)) !== (last = watch.last) && !equals(value, last)) { -                dirty = true; -                watch.last = copy(value); -                watch.fn(current, value, last); -                if (ttl < 5) { -                  if (!watchLog[4-ttl]) watchLog[4-ttl] = []; -                  if (isFunction(watch.exp)) { -                    watchLog[4-ttl].push('fn: ' + (watch.exp.name || watch.exp.toString())); -                  } else { -                    watchLog[4-ttl].push(watch.exp); +              current.$eval(asyncQueue.shift()); +            } catch (e) { +              current.$service('$exceptionHandler')(e); +            } +          } +          if ((watchers = current.$$watchers)) { +            // process our watches +            length = watchers.length; +            while (length--) { +              try { +                watch = watchers[length]; +                // Most common watches are on primitives, in which case we can short +                // circuit it with === operator, only when === fails do we use .equals +                if ((value = watch.get(current)) !== (last = watch.last) && !equals(value, last)) { +                  dirty = true; +                  watch.last = copy(value); +                  watch.fn(current, value, last); +                  if (ttl < 5) { +                    if (!watchLog[4-ttl]) watchLog[4-ttl] = []; +                    if (isFunction(watch.exp)) { +                      watchLog[4-ttl].push('fn: ' + (watch.exp.name || watch.exp.toString())); +                    } else { +                      watchLog[4-ttl].push(watch.exp); +                    }                    }                  } +              } catch (e) { +                $exceptionHandler(e);                } -            } catch (e) { -              current.$service('$exceptionHandler')(e);              }            } + +          current.$$phase = null; + +          // Insanity Warning: scope depth-first traversal +          // yes, this code is a bit crazy, but it works and we have tests to prove it! +          // this piece should be kept in sync with the traversal in $broadcast +          if (!(next = (current.$$childHead || (current !== target && current.$$nextSibling)))) { +            while(current !== target && !(next = current.$$nextSibling)) { +              current = current.$parent; +            } +          } +        } while ((current = next)); + +        if(!(ttl--)) { +          throw Error('100 $digest() iterations reached. Aborting!\n' + +              'Watchers fired in the last 5 iterations: ' + toJson(watchLog));          } +      } while (dirty); +    }, + +    /** +     * @ngdoc function +     * @name angular.scope.$destroy +     * @function +     * +     * @description +     * Remove the current scope (and all of its children) from the parent scope. Removal implies +     * that calls to {@link angular.scope.$digest $digest()} will no longer propagate to the current +     * scope and its children. Removal also implies that the current scope is eligible for garbage +     * collection. +     * +     * The destructing scope emits an `$destroy` {@link angular.scope.$emit event}. +     * +     * The `$destroy()` is usually used by directives such as +     * {@link angular.widget.@ng:repeat ng:repeat} for managing the unrolling of the loop. +     * +     */ +    $destroy: function() { +      if (this.$root == this) return; // we can't remove the root node; +      this.$emit('$destroy'); +      var parent = this.$parent; + +      if (parent.$$childHead == this) parent.$$childHead = this.$$nextSibling; +      if (parent.$$childTail == this) parent.$$childTail = this.$$prevSibling; +      if (this.$$prevSibling) this.$$prevSibling.$$nextSibling = this.$$nextSibling; +      if (this.$$nextSibling) this.$$nextSibling.$$prevSibling = this.$$prevSibling; +    }, + +    /** +     * @ngdoc function +     * @name angular.scope.$eval +     * @function +     * +     * @description +     * Executes the `expression` on the current scope returning the result. Any exceptions in the +     * expression are propagated (uncaught). This is useful when evaluating engular expressions. +     * +     * # Example +       <pre> +         var scope = angular.scope(); +         scope.a = 1; +         scope.b = 2; + +         expect(scope.$eval('a+b')).toEqual(3); +         expect(scope.$eval(function(scope){ return scope.a + scope.b; })).toEqual(3); +       </pre> +     * +     * @param {(string|function())=} expression An angular expression to be executed. +     * +     *    - `string`: execute using the rules as defined in  {@link guide/dev_guide.expressions expression}. +     *    - `function(scope)`: execute the function with the current `scope` parameter. +     * +     * @returns {*} The result of evaluating the expression. +     */ +    $eval: function(expr) { +      var fn = isString(expr) +        ? expressionCompile(expr) +        : expr || noop; +      return fn(this); +    }, + +    /** +     * @ngdoc function +     * @name angular.scope.$evalAsync +     * @function +     * +     * @description +     * Executes the expression on the current scope at a later point in time. +     * +     * The `$evalAsync` makes no guarantees as to when the `expression` will be executed, only that: +     * +     *   - it will execute in the current script execution context (before any DOM rendering). +     *   - at least one {@link angular.scope.$digest $digest cycle} will be performed after +     *     `expression` execution. +     * +     * Any exceptions from the execution of the expression are forwarded to the +     * {@link angular.service.$exceptionHandler $exceptionHandler} service. +     * +     * @param {(string|function())=} expression An angular expression to be executed. +     * +     *    - `string`: execute using the rules as defined in  {@link guide/dev_guide.expressions expression}. +     *    - `function(scope)`: execute the function with the current `scope` parameter. +     * +     */ +    $evalAsync: function(expr) { +      this.$$asyncQueue.push(expr); +    }, + +    /** +     * @ngdoc function +     * @name angular.scope.$apply +     * @function +     * +     * @description +     * `$apply()` is used to execute an expression in angular from outside of the angular framework. +     * (For example from browser DOM events, setTimeout, XHR or third party libraries). +     * Because we are calling into the angular framework we need to perform proper scope life-cycle +     * of {@link angular.service.$exceptionHandler exception handling}, +     * {@link angular.scope.$digest executing watches}. +     * +     * ## Life cycle +     * +     * # Pseudo-Code of `$apply()` +        function $apply(expr) { +          try { +            return $eval(expr); +          } catch (e) { +            $exceptionHandler(e); +          } finally { +            $root.$digest(); +          } +        } +     * +     * +     * Scope's `$apply()` method transitions through the following stages: +     * +     * 1. The {@link guide/dev_guide.expressions expression} is executed using the +     *    {@link angular.scope.$eval $eval()} method. +     * 2. Any exceptions from the execution of the expression are forwarded to the +     *    {@link angular.service.$exceptionHandler $exceptionHandler} service. +     * 3. The {@link angular.scope.$watch watch} listeners are fired immediately after the expression +     *    was executed using the {@link angular.scope.$digest $digest()} method. +     * +     * +     * @param {(string|function())=} exp An angular expression to be executed. +     * +     *    - `string`: execute using the rules as defined in {@link guide/dev_guide.expressions expression}. +     *    - `function(scope)`: execute the function with current `scope` parameter. +     * +     * @returns {*} The result of evaluating the expression. +     */ +    $apply: function(expr) { +      try { +        return this.$eval(expr); +      } catch (e) { +        $exceptionHandler(e); +      } finally { +        this.$root.$digest(); +      } +    }, + +    /** +     * @ngdoc function +     * @name angular.scope.$on +     * @function +     * +     * @description +     * Listen on events of a given type. See {@link angular.scope.$emit $emit} for discussion of +     * event life cycle. +     * +     * @param {string} name Event name to listen on. +     * @param {function(event)} listener Function to call when the event is emitted. +     * @returns {function()} Returns a deregistration function for this listener. +     * +     * The event listener function format is: `function(event)`. The `event` object passed into the +     * listener has the following attributes +     *   - `targetScope` - {Scope}: the scope on which the event was `$emit`-ed or `$broadcast`-ed. +     *   - `currentScope` - {Scope}: the current scope which is handling the event. +     *   - `name` - {string}: Name of the event. +     *   - `cancel` - {function=}: calling `cancel` function will cancel further event propagation +     *     (available only for events that were `$emit`-ed). +     */ +    $on: function(name, listener) { +      var namedListeners = this.$$listeners[name]; +      if (!namedListeners) { +        this.$$listeners[name] = namedListeners = []; +      } +      namedListeners.push(listener); + +      return function() { +        angularArray.remove(namedListeners, listener); +      }; +    }, + + +    /** +     * @ngdoc function +     * @name angular.scope.$emit +     * @function +     * +     * @description +     * Dispatches an event `name` upwards through the scope hierarchy notifying the +     * registered {@link angular.scope.$on} listeners. +     * +     * The event life cycle starts at the scope on which `$emit` was called. All +     * {@link angular.scope.$on listeners} listening for `name` event on this scope get notified. +     * Afterwards, the event traverses upwards toward the root scope and calls all registered +     * listeners along the way. The event will stop propagating if one of the listeners cancels it. +     * +     * Any exception emmited from the {@link angular.scope.$on listeners} will be passed +     * onto the {@link angular.service.$exceptionHandler $exceptionHandler} service. +     * +     * @param {string} name Event name to emit. +     * @param {...*} args Optional set of arguments which will be passed onto the event listeners. +     */ +    $emit: function(name, args) { +      var empty = [], +          namedListeners, +          canceled = false, +          scope = this, +          event = { +            name: name, +            targetScope: scope, +            cancel: function() {canceled = true;} +          }, +          listenerArgs = concat([event], arguments, 1), +          i, length; -        current.$$phase = null; +      do { +        namedListeners = scope.$$listeners[name] || empty; +        event.currentScope = scope; +        for (i=0, length=namedListeners.length; i<length; i++) { +          try { +            namedListeners[i].apply(null, listenerArgs); +            if (canceled) return; +          } catch (e) { +            $exceptionHandler(e); +          } +        } +        //traverse upwards +        scope = scope.$parent; +      } while (scope); +    }, + + +    /** +     * @ngdoc function +     * @name angular.scope.$broadcast +     * @function +     * +     * @description +     * Dispatches an event `name` downwards to all child scopes (and their children) notifying the +     * registered {@link angular.scope.$on} listeners. +     * +     * The event life cycle starts at the scope on which `$broadcast` was called. All +     * {@link angular.scope.$on listeners} listening for `name` event on this scope get notified. +     * Afterwards, the event propagates to all direct and indirect scopes of the current scope and +     * calls all registered listeners along the way. The event cannot be canceled. +     * +     * Any exception emmited from the {@link angular.scope.$on listeners} will be passed +     * onto the {@link angular.service.$exceptionHandler $exceptionHandler} service. +     * +     * @param {string} name Event name to emit. +     * @param {...*} args Optional set of arguments which will be passed onto the event listeners. +     */ +    $broadcast: function(name, args) { +      var target = this, +          current = target, +          next = target, +          event = { name: name, +                    targetScope: target }, +          listenerArgs = concat([event], arguments, 1); + +      //down while you can, then up and next sibling or up and next sibling until back at root +      do { +        current = next; +        event.currentScope = current; +        forEach(current.$$listeners[name], function(listener) { +          try { +            listener.apply(null, listenerArgs); +          } catch(e) { +            $exceptionHandler(e); +          } +        });          // Insanity Warning: scope depth-first traversal          // yes, this code is a bit crazy, but it works and we have tests to prove it! -        // this piece should be kept in sync with the traversal in $broadcast +        // this piece should be kept in sync with the traversal in $digest          if (!(next = (current.$$childHead || (current !== target && current.$$nextSibling)))) {            while(current !== target && !(next = current.$$nextSibling)) {              current = current.$parent;            }          }        } while ((current = next)); - -      if(!(ttl--)) { -        throw Error('100 $digest() iterations reached. Aborting!\n' + -            'Watchers fired in the last 5 iterations: ' + toJson(watchLog)); -      } -    } while (dirty); -  }, - -  /** -   * @ngdoc function -   * @name angular.scope.$destroy -   * @function -   * -   * @description -   * Remove the current scope (and all of its children) from the parent scope. Removal implies -   * that calls to {@link angular.scope.$digest $digest()} will no longer propagate to the current -   * scope and its children. Removal also implies that the current scope is eligible for garbage -   * collection. -   * -   * The destructing scope emits an `$destroy` {@link angular.scope.$emit event}. -   * -   * The `$destroy()` is usually used by directives such as -   * {@link angular.widget.@ng:repeat ng:repeat} for managing the unrolling of the loop. -   * -   */ -  $destroy: function() { -    if (this.$root == this) return; // we can't remove the root node; -    this.$emit('$destroy'); -    var parent = this.$parent; - -    if (parent.$$childHead == this) parent.$$childHead = this.$$nextSibling; -    if (parent.$$childTail == this) parent.$$childTail = this.$$prevSibling; -    if (this.$$prevSibling) this.$$prevSibling.$$nextSibling = this.$$nextSibling; -    if (this.$$nextSibling) this.$$nextSibling.$$prevSibling = this.$$prevSibling; -  }, - -  /** -   * @ngdoc function -   * @name angular.scope.$eval -   * @function -   * -   * @description -   * Executes the `expression` on the current scope returning the result. Any exceptions in the -   * expression are propagated (uncaught). This is useful when evaluating engular expressions. -   * -   * # Example -     <pre> -       var scope = angular.scope(); -       scope.a = 1; -       scope.b = 2; - -       expect(scope.$eval('a+b')).toEqual(3); -       expect(scope.$eval(function(scope){ return scope.a + scope.b; })).toEqual(3); -     </pre> -   * -   * @param {(string|function())=} expression An angular expression to be executed. -   * -   *    - `string`: execute using the rules as defined in  {@link guide/dev_guide.expressions expression}. -   *    - `function(scope)`: execute the function with the current `scope` parameter. -   * -   * @returns {*} The result of evaluating the expression. -   */ -  $eval: function(expr) { -    var fn = isString(expr) -      ? expressionCompile(expr) -      : expr || noop; -    return fn(this); -  }, - -  /** -   * @ngdoc function -   * @name angular.scope.$evalAsync -   * @function -   * -   * @description -   * Executes the expression on the current scope at a later point in time. -   * -   * The `$evalAsync` makes no guarantees as to when the `expression` will be executed, only that: -   * -   *   - it will execute in the current script execution context (before any DOM rendering). -   *   - at least one {@link angular.scope.$digest $digest cycle} will be performed after -   *     `expression` execution. -   * -   * Any exceptions from the execution of the expression are forwarded to the -   * {@link angular.service.$exceptionHandler $exceptionHandler} service. -   * -   * @param {(string|function())=} expression An angular expression to be executed. -   * -   *    - `string`: execute using the rules as defined in  {@link guide/dev_guide.expressions expression}. -   *    - `function(scope)`: execute the function with the current `scope` parameter. -   * -   */ -  $evalAsync: function(expr) { -    this.$$asyncQueue.push(expr); -  }, - -  /** -   * @ngdoc function -   * @name angular.scope.$apply -   * @function -   * -   * @description -   * `$apply()` is used to execute an expression in angular from outside of the angular framework. -   * (For example from browser DOM events, setTimeout, XHR or third party libraries). -   * Because we are calling into the angular framework we need to perform proper scope life-cycle -   * of {@link angular.service.$exceptionHandler exception handling}, -   * {@link angular.scope.$digest executing watches}. -   * -   * ## Life cycle -   * -   * # Pseudo-Code of `$apply()` -      function $apply(expr) { -        try { -          return $eval(expr); -        } catch (e) { -          $exceptionHandler(e); -        } finally { -          $root.$digest(); -        } -      } -   * -   * -   * Scope's `$apply()` method transitions through the following stages: -   * -   * 1. The {@link guide/dev_guide.expressions expression} is executed using the -   *    {@link angular.scope.$eval $eval()} method. -   * 2. Any exceptions from the execution of the expression are forwarded to the -   *    {@link angular.service.$exceptionHandler $exceptionHandler} service. -   * 3. The {@link angular.scope.$watch watch} listeners are fired immediately after the expression -   *    was executed using the {@link angular.scope.$digest $digest()} method. -   * -   * -   * @param {(string|function())=} exp An angular expression to be executed. -   * -   *    - `string`: execute using the rules as defined in {@link guide/dev_guide.expressions expression}. -   *    - `function(scope)`: execute the function with current `scope` parameter. -   * -   * @returns {*} The result of evaluating the expression. -   */ -  $apply: function(expr) { -    try { -      return this.$eval(expr); -    } catch (e) { -      this.$service('$exceptionHandler')(e); -    } finally { -      this.$root.$digest();      } -  }, - -  /** -   * @ngdoc function -   * @name angular.scope.$on -   * @function -   * -   * @description -   * Listen on events of a given type. See {@link angular.scope.$emit $emit} for discussion of -   * event life cycle. -   * -   * @param {string} name Event name to listen on. -   * @param {function(event)} listener Function to call when the event is emitted. -   * @returns {function()} Returns a deregistration function for this listener. -   * -   * The event listener function format is: `function(event)`. The `event` object passed into the -   * listener has the following attributes -   *   - `targetScope` - {Scope}: the scope on which the event was `$emit`-ed or `$broadcast`-ed. -   *   - `currentScope` - {Scope}: the current scope which is handling the event. -   *   - `name` - {string}: Name of the event. -   *   - `cancel` - {function=}: calling `cancel` function will cancel further event propagation -   *     (available only for events that were `$emit`-ed). -   */ -  $on: function(name, listener) { -    var namedListeners = this.$$listeners[name]; -    if (!namedListeners) { -      this.$$listeners[name] = namedListeners = []; -    } -    namedListeners.push(listener); - -    return function() { -      angularArray.remove(namedListeners, listener); -    }; -  }, - - -  /** -   * @ngdoc function -   * @name angular.scope.$emit -   * @function -   * -   * @description -   * Dispatches an event `name` upwards through the scope hierarchy notifying the -   * registered {@link angular.scope.$on} listeners. -   * -   * The event life cycle starts at the scope on which `$emit` was called. All -   * {@link angular.scope.$on listeners} listening for `name` event on this scope get notified. -   * Afterwards, the event traverses upwards toward the root scope and calls all registered -   * listeners along the way. The event will stop propagating if one of the listeners cancels it. -   * -   * Any exception emmited from the {@link angular.scope.$on listeners} will be passed -   * onto the {@link angular.service.$exceptionHandler $exceptionHandler} service. -   * -   * @param {string} name Event name to emit. -   * @param {...*} args Optional set of arguments which will be passed onto the event listeners. -   */ -  $emit: function(name, args) { -    var empty = [], -        namedListeners, -        canceled = false, -        scope = this, -        event = { -          name: name, -          targetScope: scope, -          cancel: function() {canceled = true;} -        }, -        listenerArgs = concat([event], arguments, 1), -        i, length; - -    do { -      namedListeners = scope.$$listeners[name] || empty; -      event.currentScope = scope; -      for (i=0, length=namedListeners.length; i<length; i++) { -        try { -          namedListeners[i].apply(null, listenerArgs); -          if (canceled) return; -        } catch (e) { -          scope.$service('$exceptionHandler')(e); -        } -      } -      //traverse upwards -      scope = scope.$parent; -    } while (scope); -  }, +  }; +  // TODO(misko): remove this; +  var scope = new Scope(); +  scope.$service = $injector; +  return scope; -  /** -   * @ngdoc function -   * @name angular.scope.$broadcast -   * @function -   * -   * @description -   * Dispatches an event `name` downwards to all child scopes (and their children) notifying the -   * registered {@link angular.scope.$on} listeners. -   * -   * The event life cycle starts at the scope on which `$broadcast` was called. All -   * {@link angular.scope.$on listeners} listening for `name` event on this scope get notified. -   * Afterwards, the event propagates to all direct and indirect scopes of the current scope and -   * calls all registered listeners along the way. The event cannot be canceled. -   * -   * Any exception emmited from the {@link angular.scope.$on listeners} will be passed -   * onto the {@link angular.service.$exceptionHandler $exceptionHandler} service. -   * -   * @param {string} name Event name to emit. -   * @param {...*} args Optional set of arguments which will be passed onto the event listeners. -   */ -  $broadcast: function(name, args) { -    var target = this, -        current = target, -        next = target, -        event = { name: name, -                  targetScope: target }, -        listenerArgs = concat([event], arguments, 1); - -    //down while you can, then up and next sibling or up and next sibling until back at root -    do { -      current = next; -      event.currentScope = current; -      forEach(current.$$listeners[name], function(listener) { -        try { -          listener.apply(null, listenerArgs); -        } catch(e) { -          current.$service('$exceptionHandler')(e); -        } -      }); - -      // Insanity Warning: scope depth-first traversal -      // yes, this code is a bit crazy, but it works and we have tests to prove it! -      // this piece should be kept in sync with the traversal in $digest -      if (!(next = (current.$$childHead || (current !== target && current.$$nextSibling)))) { -        while(current !== target && !(next = current.$$nextSibling)) { -          current = current.$parent; -        } -      } -    } while ((current = next)); +  function compileToFn(exp, name) { +    var fn = isString(exp) +      ? expressionCompile(exp) +      : exp; +    assertArgFn(fn, name); +    return fn;    } -}; -function compileToFn(exp, name) { -  var fn = isString(exp) -    ? expressionCompile(exp) -    : exp; -  assertArgFn(fn, name); -  return fn; -} +}, ['$injector', '$exceptionHandler']); diff --git a/src/service/xhr.bulk.js b/src/service/xhr.bulk.js index 33c9384b..bf5a1d95 100644 --- a/src/service/xhr.bulk.js +++ b/src/service/xhr.bulk.js @@ -11,9 +11,8 @@   *   * @example   */ -angularServiceInject('$xhr.bulk', function($xhr, $error, $log){ -  var requests = [], -      scope = this; +angularServiceInject('$xhr.bulk', function($rootScope, $xhr, $error, $log){ +  var requests = [];    function bulkXHR(method, url, post, success, error) {      if (isFunction(post)) {        error = success; @@ -82,6 +81,6 @@ angularServiceInject('$xhr.bulk', function($xhr, $error, $log){        }      });    }; -  this.$watch(function() { bulkXHR.flush(); }); +  $rootScope.$watch(function() { bulkXHR.flush(); });    return bulkXHR; -}, ['$xhr', '$xhr.error', '$log']); +}, ['$rootScope', '$xhr', '$xhr.error', '$log']); diff --git a/src/service/xhr.cache.js b/src/service/xhr.cache.js index 630caa5b..335c481d 100644 --- a/src/service/xhr.cache.js +++ b/src/service/xhr.cache.js @@ -29,7 +29,7 @@   * @param {boolean=} [sync=false] in case of cache hit execute `success` synchronously.   */  angularServiceInject('$xhr.cache', function($xhr, $defer, $error, $log) { -  var inflight = {}, self = this; +  var inflight = {};    function cache(method, url, post, success, error, verifyCache, sync) {      if (isFunction(post)) {        if (!isFunction(success)) { diff --git a/src/service/xhr.js b/src/service/xhr.js index fe7d42d9..b2a5bdf2 100644 --- a/src/service/xhr.js +++ b/src/service/xhr.js @@ -171,8 +171,7 @@       </doc:scenario>     </doc:example>   */ -angularServiceInject('$xhr', function($browser, $error, $log){ -  var rootScope = this; +angularServiceInject('$xhr', function($rootScope, $browser, $error, $log){    var xhrHeaderDefaults = {      common: {        "Accept": "application/json, text/plain, */*", @@ -204,7 +203,7 @@ angularServiceInject('$xhr', function($browser, $error, $log){              response = fromJson(response, true);            }          } -        rootScope.$apply(function() { +        $rootScope.$apply(function() {            if (200 <= code && code < 300) {                success(code, response);            } else if (isFunction(error)) { @@ -226,4 +225,4 @@ angularServiceInject('$xhr', function($browser, $error, $log){    xhr.defaults = {headers: xhrHeaderDefaults};    return xhr; -}, ['$browser', '$xhr.error', '$log']); +}, ['$rootScope', '$browser', '$xhr.error', '$log']); diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 299bfb8a..eb64a825 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -371,22 +371,16 @@ describe('angular', function() {                                          '</div>' +                                        '</div>');      }); - - -    xit('should add custom css when specified via css', function() { -      //TODO -    });    });    describe('angular service', function() { -    it('should override services', function() { -      var scope = createScope(); -      angular.service('fake', function() { return 'old'; }); -      angular.service('fake', function() { return 'new'; }); - -      expect(scope.$service('fake')).toEqual('new'); -    }); +    it('should override services', inject(function(service){ +      service('fake', function() { return 'old'; }); +      service('fake', function() { return 'new'; }); +    }, function(fake) { +      expect(fake).toEqual('new'); +    }));      it('should not preserve properties on override', function() {        angular.service('fake', {$one: true}, {$two: true}, {three: true}); @@ -410,19 +404,19 @@ describe('angular', function() {      it('should inject dependencies specified by $inject', function() {        angular.service('svc1', function() { return 'svc1'; });        angular.service('svc2', function(svc1) { return 'svc2-' + svc1; }, {$inject: ['svc1']}); -      expect(angular.scope().$service('svc2')).toEqual('svc2-svc1'); +      expect(createInjector()('svc2')).toEqual('svc2-svc1');      });      it('should inject dependencies specified by $inject and ignore function argument name', function() {        angular.service('svc1', function() { return 'svc1'; });        angular.service('svc2', function(foo) { return 'svc2-' + foo; }, {$inject: ['svc1']}); -      expect(angular.scope().$service('svc2')).toEqual('svc2-svc1'); +      expect(createInjector()('svc2')).toEqual('svc2-svc1');      });      it('should eagerly instantiate a service if $eager is true', function() {        var log = [];        angular.service('svc1', function() { log.push('svc1'); }, {$eager: true}); -      angular.scope(); +      createInjector();        expect(log).toEqual(['svc1']);      });    }); @@ -464,55 +458,46 @@ describe('angular', function() {    });    describe('compile', function() { -    var scope, template; - -    afterEach(function() { -      dealoc(scope); -    }); - -    it('should link to existing node and create scope', function() { -      template = angular.element('<div>{{greeting = "hello world"}}</div>'); -      scope = angular.compile(template)(); -      scope.$digest(); +    it('should link to existing node and create scope', inject(function($rootScope) { +      var template = angular.element('<div>{{greeting = "hello world"}}</div>'); +      angular.compile(template)($rootScope); +      $rootScope.$digest();        expect(template.text()).toEqual('hello world'); -      expect(scope.greeting).toEqual('hello world'); -    }); +      expect($rootScope.greeting).toEqual('hello world'); +    })); -    it('should link to existing node and given scope', function() { -      scope = angular.scope(); -      template = angular.element('<div>{{greeting = "hello world"}}</div>'); -      angular.compile(template)(scope); -      scope.$digest(); +    it('should link to existing node and given scope', inject(function($rootScope) { +      var template = angular.element('<div>{{greeting = "hello world"}}</div>'); +      angular.compile(template)($rootScope); +      $rootScope.$digest();        expect(template.text()).toEqual('hello world'); -      expect(scope).toEqual(scope); -    }); +    })); -    it('should link to new node and given scope', function() { -      scope = angular.scope(); -      template = jqLite('<div>{{greeting = "hello world"}}</div>'); +    it('should link to new node and given scope', inject(function($rootScope) { +      var template = jqLite('<div>{{greeting = "hello world"}}</div>');        var templateFn = angular.compile(template);        var templateClone = template.clone(); -      templateFn(scope, function(clone){ +      var element = templateFn($rootScope, function(clone){          templateClone = clone;        }); -      scope.$digest(); +      $rootScope.$digest();        expect(template.text()).toEqual(''); -      expect(scope.$element.text()).toEqual('hello world'); -      expect(scope.$element).toEqual(templateClone); -      expect(scope.greeting).toEqual('hello world'); -    }); - -    it('should link to cloned node and create scope', function() { -      scope = angular.scope(); -      template = jqLite('<div>{{greeting = "hello world"}}</div>'); -      angular.compile(template)(scope, noop).$digest(); +      expect(element.text()).toEqual('hello world'); +      expect(element).toEqual(templateClone); +      expect($rootScope.greeting).toEqual('hello world'); +    })); + +    it('should link to cloned node and create scope', inject(function($rootScope) { +      var template = jqLite('<div>{{greeting = "hello world"}}</div>'); +      var element = angular.compile(template)($rootScope, noop); +      $rootScope.$digest();        expect(template.text()).toEqual(''); -      expect(scope.$element.text()).toEqual('hello world'); -      expect(scope.greeting).toEqual('hello world'); -    }); +      expect(element.text()).toEqual('hello world'); +      expect($rootScope.greeting).toEqual('hello world'); +    }));    }); diff --git a/test/BinderSpec.js b/test/BinderSpec.js index bdeed675..8938bd13 100644 --- a/test/BinderSpec.js +++ b/test/BinderSpec.js @@ -2,23 +2,14 @@  describe('Binder', function() {    beforeEach(function() { -    var self = this; - -    this.compile = function(html, parent, logErrors) { -      if (self.element) dealoc(self.element); -      var element; -      if (parent) { -        parent.html(html); -        element = parent.children(); -      } else { -        element = jqLite(html); -      } -      self.element = element; -      return angular.compile(element)(angular.scope(null, -              logErrors ? {'$exceptionHandler': $exceptionHandlerMockFactory()} : null)); -    };      this.compileToHtml = function (content) { -      return sortedHtml(this.compile(content).$element); +      var html; +      inject(function($rootScope){ +        content = jqLite(content); +        angular.compile(content)($rootScope); +        html = sortedHtml(content); +      }).call(this); +      return html;      };    }); @@ -28,36 +19,39 @@ describe('Binder', function() {      }    }); -  it('BindUpdate', function() { -    var scope = this.compile('<div ng:init="a=123"/>'); -    scope.$digest(); -    assertEquals(123, scope.a); -  }); +  it('BindUpdate', inject(function($rootScope) { +    angular.compile('<div ng:init="a=123"/>')($rootScope); +    $rootScope.$digest(); +    assertEquals(123, $rootScope.a); +  })); -  it('ExecuteInitialization', function() { -    var scope = this.compile('<div ng:init="a=123">'); -    assertEquals(scope.a, 123); -  }); +  it('ExecuteInitialization', inject(function($rootScope) { +    angular.compile('<div ng:init="a=123">')($rootScope); +    assertEquals($rootScope.a, 123); +  })); -  it('ExecuteInitializationStatements', function() { -    var scope = this.compile('<div ng:init="a=123;b=345">'); -    assertEquals(scope.a, 123); -    assertEquals(scope.b, 345); -  }); +  it('ExecuteInitializationStatements', inject(function($rootScope) { +    angular.compile('<div ng:init="a=123;b=345">')($rootScope); +    assertEquals($rootScope.a, 123); +    assertEquals($rootScope.b, 345); +  })); -  it('ApplyTextBindings', function() { -    var scope = this.compile('<div ng:bind="model.a">x</div>'); -    scope.model = {a:123}; -    scope.$apply(); -    assertEquals('123', scope.$element.text()); +  it('ApplyTextBindings', inject(function($rootScope) { +    var element = angular.compile('<div ng:bind="model.a">x</div>')($rootScope); +    $rootScope.model = {a:123}; +    $rootScope.$apply(); +    assertEquals('123', element.text()); +  })); + +  it('ReplaceBindingInTextWithSpan preserve surounding text', function() { +    assertEquals(this.compileToHtml("<b>a{{b}}c</b>"), '<b>a<span ng:bind="b"></span>c</b>');    });    it('ReplaceBindingInTextWithSpan', function() { -    assertEquals(this.compileToHtml("<b>a{{b}}c</b>"), '<b>a<span ng:bind="b"></span>c</b>');      assertEquals(this.compileToHtml("<b>{{b}}</b>"), '<b><span ng:bind="b"></span></b>');    }); -  it('BindingSpaceConfusesIE', function() { +  it('BindingSpaceConfusesIE', inject(function($rootScope) {      if (!msie) return;      var span = document.createElement("span");      span.innerHTML = ' '; @@ -65,89 +59,90 @@ describe('Binder', function() {      assertEquals(          '<b><span ng:bind="a"></span><span>'+nbsp+'</span><span ng:bind="b"></span></b>',          this.compileToHtml("<b>{{a}} {{b}}</b>")); +    dealoc(($rootScope));      assertEquals(          '<b><span ng:bind="A"></span><span>'+nbsp+'x </span><span ng:bind="B"></span><span>'+nbsp+'(</span><span ng:bind="C"></span>)</b>',          this.compileToHtml("<b>{{A}} x {{B}} ({{C}})</b>")); -  }); +  })); -  it('BindingOfAttributes', function() { -    var scope = this.compile("<a href='http://s/a{{b}}c' foo='x'></a>"); -    var attrbinding = scope.$element.attr("ng:bind-attr"); +  it('BindingOfAttributes', inject(function($rootScope) { +    var element = angular.compile("<a href='http://s/a{{b}}c' foo='x'></a>")($rootScope); +    var attrbinding = element.attr("ng:bind-attr");      var bindings = fromJson(attrbinding);      assertEquals("http://s/a{{b}}c", decodeURI(bindings.href));      assertTrue(!bindings.foo); -  }); +  })); -  it('MarkMultipleAttributes', function() { -    var scope = this.compile('<a href="http://s/a{{b}}c" foo="{{d}}"></a>'); -    var attrbinding = scope.$element.attr("ng:bind-attr"); +  it('MarkMultipleAttributes', inject(function($rootScope) { +    var element = angular.compile('<a href="http://s/a{{b}}c" foo="{{d}}"></a>')($rootScope); +    var attrbinding = element.attr("ng:bind-attr");      var bindings = fromJson(attrbinding);      assertEquals(bindings.foo, "{{d}}");      assertEquals(decodeURI(bindings.href), "http://s/a{{b}}c"); -  }); +  })); -  it('AttributesNoneBound', function() { -    var scope = this.compile("<a href='abc' foo='def'></a>"); -    var a = scope.$element; +  it('AttributesNoneBound', inject(function($rootScope) { +    var a = angular.compile("<a href='abc' foo='def'></a>")($rootScope);      assertEquals(a[0].nodeName, "A");      assertTrue(!a.attr("ng:bind-attr")); -  }); +  })); -  it('ExistingAttrbindingIsAppended', function() { -    var scope = this.compile("<a href='http://s/{{abc}}' ng:bind-attr='{\"b\":\"{{def}}\"}'></a>"); -    var a = scope.$element; +  it('ExistingAttrbindingIsAppended', inject(function($rootScope) { +    var a = angular.compile("<a href='http://s/{{abc}}' ng:bind-attr='{\"b\":\"{{def}}\"}'></a>")($rootScope);      assertEquals('{"b":"{{def}}","href":"http://s/{{abc}}"}', a.attr('ng:bind-attr')); -  }); +  })); -  it('AttributesAreEvaluated', function() { -    var scope = this.compile('<a ng:bind-attr=\'{"a":"a", "b":"a+b={{a+b}}"}\'></a>'); -    scope.$eval('a=1;b=2'); -    scope.$apply(); -    var a = scope.$element; +  it('AttributesAreEvaluated', inject(function($rootScope) { +    var a = angular.compile('<a ng:bind-attr=\'{"a":"a", "b":"a+b={{a+b}}"}\'></a>')($rootScope); +    $rootScope.$eval('a=1;b=2'); +    $rootScope.$apply();      assertEquals(a.attr('a'), 'a');      assertEquals(a.attr('b'), 'a+b=3'); -  }); +  })); -  it('InputTypeButtonActionExecutesInScope', function() { +  it('InputTypeButtonActionExecutesInScope', inject(function($rootScope) {      var savedCalled = false; -    var scope = this.compile('<input type="button" ng:click="person.save()" value="Apply">'); -    scope.person = {}; -    scope.person.save = function() { +    var element = angular.compile( +      '<input type="button" ng:click="person.save()" value="Apply">')($rootScope); +    $rootScope.person = {}; +    $rootScope.person.save = function() {        savedCalled = true;      }; -    browserTrigger(scope.$element, 'click'); +    browserTrigger(element, 'click');      assertTrue(savedCalled); -  }); +  })); -  it('InputTypeButtonActionExecutesInScope2', function() { +  it('InputTypeButtonActionExecutesInScope2', inject(function($rootScope) {      var log = ""; -    var scope = this.compile('<input type="image" ng:click="action()">'); -    scope.action = function() { +    var element = angular.compile('<input type="image" ng:click="action()">')($rootScope); +    $rootScope.action = function() {        log += 'click;';      };      expect(log).toEqual(''); -    browserTrigger(scope.$element, 'click'); +    browserTrigger(element, 'click');      expect(log).toEqual('click;'); -  }); +  })); -  it('ButtonElementActionExecutesInScope', function() { +  it('ButtonElementActionExecutesInScope', inject(function($rootScope) {      var savedCalled = false; -    var scope = this.compile('<button ng:click="person.save()">Apply</button>'); -    scope.person = {}; -    scope.person.save = function() { +    var element = angular.compile('<button ng:click="person.save()">Apply</button>')($rootScope); +    $rootScope.person = {}; +    $rootScope.person.save = function() {        savedCalled = true;      }; -    browserTrigger(scope.$element, 'click'); +    browserTrigger(element, 'click');      assertTrue(savedCalled); -  }); +  })); -  it('RepeaterUpdateBindings', function() { -    var scope = this.compile('<ul><LI ng:repeat="item in model.items" ng:bind="item.a"/></ul>'); -    var form = scope.$element; +  it('RepeaterUpdateBindings', inject(function($rootScope) { +    var form = angular.compile( +      '<ul>' + +        '<LI ng:repeat="item in model.items" ng:bind="item.a"></LI>' + +      '</ul>')($rootScope);      var items = [{a:"A"}, {a:"B"}]; -    scope.model = {items:items}; +    $rootScope.model = {items:items}; -    scope.$apply(); +    $rootScope.$apply();      assertEquals('<ul>' +            '<#comment></#comment>' +            '<li ng:bind="item.a">A</li>' + @@ -155,7 +150,7 @@ describe('Binder', function() {            '</ul>', sortedHtml(form));      items.unshift({a:'C'}); -    scope.$apply(); +    $rootScope.$apply();      assertEquals('<ul>' +            '<#comment></#comment>' +            '<li ng:bind="item.a">C</li>' + @@ -164,7 +159,7 @@ describe('Binder', function() {            '</ul>', sortedHtml(form));      items.shift(); -    scope.$apply(); +    $rootScope.$apply();      assertEquals('<ul>' +            '<#comment></#comment>' +            '<li ng:bind="item.a">A</li>' + @@ -173,100 +168,113 @@ describe('Binder', function() {      items.shift();      items.shift(); -    scope.$apply(); -  }); - -  it('RepeaterContentDoesNotBind', function() { -    var scope = this.compile('<ul><LI ng:repeat="item in model.items"><span ng:bind="item.a"></span></li></ul>'); -    scope.model = {items:[{a:"A"}]}; -    scope.$apply(); +    $rootScope.$apply(); +  })); + +  it('RepeaterContentDoesNotBind', inject(function($rootScope) { +    var element = angular.compile( +      '<ul>' + +        '<LI ng:repeat="item in model.items"><span ng:bind="item.a"></span></li>' + +      '</ul>')($rootScope); +    $rootScope.model = {items:[{a:"A"}]}; +    $rootScope.$apply();      assertEquals('<ul>' +            '<#comment></#comment>' +            '<li><span ng:bind="item.a">A</span></li>' + -          '</ul>', sortedHtml(scope.$element)); -  }); +          '</ul>', sortedHtml(element)); +  }));    it('DoNotOverwriteCustomAction', function() {      var html = this.compileToHtml('<input type="submit" value="Save" action="foo();">');      assertTrue(html.indexOf('action="foo();"') > 0 );    }); -  it('RepeaterAdd', function() { -    var scope = this.compile('<div><input type="text" ng:model="item.x" ng:repeat="item in items"></div>'); -    scope.items = [{x:'a'}, {x:'b'}]; -    scope.$apply(); -    var first = childNode(scope.$element, 1); -    var second = childNode(scope.$element, 2); +  it('RepeaterAdd', inject(function($rootScope) { +    var element = angular.compile('<div><input type="text" ng:model="item.x" ng:repeat="item in items"></div>')($rootScope); +    $rootScope.items = [{x:'a'}, {x:'b'}]; +    $rootScope.$apply(); +    var first = childNode(element, 1); +    var second = childNode(element, 2);      expect(first.val()).toEqual('a');      expect(second.val()).toEqual('b');      first.val('ABC');      browserTrigger(first, 'keydown'); -    scope.$service('$browser').defer.flush(); -    expect(scope.items[0].x).toEqual('ABC'); -  }); +    $rootScope.$service('$browser').defer.flush(); +    expect($rootScope.items[0].x).toEqual('ABC'); +  })); -  it('ItShouldRemoveExtraChildrenWhenIteratingOverHash', function() { -    var scope = this.compile('<div><div ng:repeat="i in items">{{i}}</div></div>'); +  it('ItShouldRemoveExtraChildrenWhenIteratingOverHash', inject(function($rootScope) { +    var element = angular.compile('<div><div ng:repeat="i in items">{{i}}</div></div>')($rootScope);      var items = {}; -    scope.items = items; +    $rootScope.items = items; -    scope.$apply(); -    expect(scope.$element[0].childNodes.length - 1).toEqual(0); +    $rootScope.$apply(); +    expect(element[0].childNodes.length - 1).toEqual(0);      items.name = "misko"; -    scope.$apply(); -    expect(scope.$element[0].childNodes.length - 1).toEqual(1); +    $rootScope.$apply(); +    expect(element[0].childNodes.length - 1).toEqual(1);      delete items.name; -    scope.$apply(); -    expect(scope.$element[0].childNodes.length - 1).toEqual(0); -  }); - -  it('IfTextBindingThrowsErrorDecorateTheSpan', function() { -    var scope = this.compile('<div>{{error.throw()}}</div>', null, true); -    var errorLogs = scope.$service('$exceptionHandler').errors; - -    scope.error = { -        'throw': function() {throw "ErrorMsg1";} -    }; -    scope.$apply(); - -    scope.error['throw'] = function() {throw "MyError";}; -    errorLogs.length = 0; -    scope.$apply(); -    assertEquals(['MyError'], errorLogs.shift()); - -    scope.error['throw'] = function() {return "ok";}; -    scope.$apply(); -    assertEquals(0, errorLogs.length); -  }); - -  it('IfAttrBindingThrowsErrorDecorateTheAttribute', function() { -    var scope = this.compile('<div attr="before {{error.throw()}} after"></div>', null, true); -    var errorLogs = scope.$service('$exceptionHandler').errors; +    $rootScope.$apply(); +    expect(element[0].childNodes.length - 1).toEqual(0); +  })); + +  it('IfTextBindingThrowsErrorDecorateTheSpan', inject( +    function(service){ +      service('$exceptionHandler', $exceptionHandlerMockFactory); +    }, +    function($rootScope, $exceptionHandler) { +      angular.compile('<div>{{error.throw()}}</div>', null, true)($rootScope); +      var errorLogs = $exceptionHandler.errors; + +      $rootScope.error = { +          'throw': function() {throw "ErrorMsg1";} +      }; +      $rootScope.$apply(); + +      $rootScope.error['throw'] = function() {throw "MyError";}; +      errorLogs.length = 0; +      $rootScope.$apply(); +      assertEquals(['MyError'], errorLogs.shift()); + +      $rootScope.error['throw'] = function() {return "ok";}; +      $rootScope.$apply(); +      assertEquals(0, errorLogs.length); +    }) +  ); + +  it('IfAttrBindingThrowsErrorDecorateTheAttribute', inject(function(service){ +    service('$exceptionHandler', $exceptionHandlerMockFactory); +  }, function($rootScope, $exceptionHandler) { +    angular.compile('<div attr="before {{error.throw()}} after"></div>', null, true)($rootScope); +    var errorLogs = $exceptionHandler.errors;      var count = 0; -    scope.error = { +    $rootScope.error = {          'throw': function() {throw new Error("ErrorMsg" + (++count));}      }; -    scope.$apply(); +    $rootScope.$apply();      expect(errorLogs.length).not.toEqual(0);      expect(errorLogs.shift()).toMatch(/ErrorMsg1/);      errorLogs.length = 0; -    scope.error['throw'] =  function() { return 'X';}; -    scope.$apply(); +    $rootScope.error['throw'] =  function() { return 'X';}; +    $rootScope.$apply();      expect(errorLogs.length).toMatch(0); -  }); +  })); -  it('NestedRepeater', function() { -    var scope = this.compile('<div><div ng:repeat="m in model" name="{{m.name}}">' + -                     '<ul name="{{i}}" ng:repeat="i in m.item"></ul>' + -                   '</div></div>'); +  it('NestedRepeater', inject(function($rootScope) { +    var element = angular.compile( +      '<div>' + +        '<div ng:repeat="m in model" name="{{m.name}}">' + +           '<ul name="{{i}}" ng:repeat="i in m.item"></ul>' + +        '</div>' + +      '</div>')($rootScope); -    scope.model = [{name:'a', item:['a1', 'a2']}, {name:'b', item:['b1', 'b2']}]; -    scope.$apply(); +    $rootScope.model = [{name:'a', item:['a1', 'a2']}, {name:'b', item:['b1', 'b2']}]; +    $rootScope.$apply();      assertEquals('<div>'+          '<#comment></#comment>'+ @@ -279,143 +287,154 @@ describe('Binder', function() {            '<#comment></#comment>'+            '<ul name="b1" ng:bind-attr="{"name":"{{i}}"}"></ul>'+            '<ul name="b2" ng:bind-attr="{"name":"{{i}}"}"></ul>'+ -        '</div></div>', sortedHtml(scope.$element)); -  }); +        '</div></div>', sortedHtml(element)); +  })); -  it('HideBindingExpression', function() { -    var scope = this.compile('<div ng:hide="hidden == 3"/>'); +  it('HideBindingExpression', inject(function($rootScope) { +    var element = angular.compile('<div ng:hide="hidden == 3"/>')($rootScope); -    scope.hidden = 3; -    scope.$apply(); +    $rootScope.hidden = 3; +    $rootScope.$apply(); -    assertHidden(scope.$element); +    assertHidden(element); -    scope.hidden = 2; -    scope.$apply(); +    $rootScope.hidden = 2; +    $rootScope.$apply(); -    assertVisible(scope.$element); -  }); +    assertVisible(element); +  })); -  it('HideBinding', function() { -    var scope = this.compile('<div ng:hide="hidden"/>'); +  it('HideBinding', inject(function($rootScope) { +    var element = angular.compile('<div ng:hide="hidden"/>')($rootScope); -    scope.hidden = 'true'; -    scope.$apply(); +    $rootScope.hidden = 'true'; +    $rootScope.$apply(); -    assertHidden(scope.$element); +    assertHidden(element); -    scope.hidden = 'false'; -    scope.$apply(); +    $rootScope.hidden = 'false'; +    $rootScope.$apply(); -    assertVisible(scope.$element); +    assertVisible(element); -    scope.hidden = ''; -    scope.$apply(); +    $rootScope.hidden = ''; +    $rootScope.$apply(); -    assertVisible(scope.$element); -  }); +    assertVisible(element); +  })); -  it('ShowBinding', function() { -    var scope = this.compile('<div ng:show="show"/>'); +  it('ShowBinding', inject(function($rootScope) { +    var element = angular.compile('<div ng:show="show"/>')($rootScope); -    scope.show = 'true'; -    scope.$apply(); +    $rootScope.show = 'true'; +    $rootScope.$apply(); -    assertVisible(scope.$element); +    assertVisible(element); -    scope.show = 'false'; -    scope.$apply(); +    $rootScope.show = 'false'; +    $rootScope.$apply(); -    assertHidden(scope.$element); +    assertHidden(element); -    scope.show = ''; -    scope.$apply(); +    $rootScope.show = ''; +    $rootScope.$apply(); -    assertHidden(scope.$element); -  }); +    assertHidden(element); +  })); -  it('BindClass', function() { -    var scope = this.compile('<div ng:class="clazz"/>'); +  it('BindClass', inject(function($rootScope) { +    var element = angular.compile('<div ng:class="clazz"/>')($rootScope); -    scope.clazz = 'testClass'; -    scope.$apply(); +    $rootScope.clazz = 'testClass'; +    $rootScope.$apply(); -    assertEquals('<div class="testClass" ng:class="clazz"></div>', sortedHtml(scope.$element)); +    assertEquals('<div class="testClass" ng:class="clazz"></div>', sortedHtml(element)); -    scope.clazz = ['a', 'b']; -    scope.$apply(); +    $rootScope.clazz = ['a', 'b']; +    $rootScope.$apply(); -    assertEquals('<div class="a b" ng:class="clazz"></div>', sortedHtml(scope.$element)); -  }); +    assertEquals('<div class="a b" ng:class="clazz"></div>', sortedHtml(element)); +  })); -  it('BindClassEvenOdd', function() { -    var scope = this.compile('<div><div ng:repeat="i in [0,1]" ng:class-even="\'e\'" ng:class-odd="\'o\'"></div></div>'); -    scope.$apply(); -    var d1 = jqLite(scope.$element[0].childNodes[1]); -    var d2 = jqLite(scope.$element[0].childNodes[2]); +  it('BindClassEvenOdd', inject(function($rootScope) { +    var element = angular.compile( +      '<div>' + +        '<div ng:repeat="i in [0,1]" ng:class-even="\'e\'" ng:class-odd="\'o\'"></div>' + +      '</div>')($rootScope); +    $rootScope.$apply(); +    var d1 = jqLite(element[0].childNodes[1]); +    var d2 = jqLite(element[0].childNodes[2]);      expect(d1.hasClass('o')).toBeTruthy();      expect(d2.hasClass('e')).toBeTruthy();      assertEquals(          '<div><#comment></#comment>' +          '<div class="o" ng:class-even="\'e\'" ng:class-odd="\'o\'"></div>' +          '<div class="e" ng:class-even="\'e\'" ng:class-odd="\'o\'"></div></div>', -        sortedHtml(scope.$element)); -  }); - -  it('BindStyle', function() { -    var scope = this.compile('<div ng:style="style"/>'); - -    scope.$eval('style={height: "10px"}'); -    scope.$apply(); - -    assertEquals("10px", scope.$element.css('height')); - -    scope.$eval('style={}'); -    scope.$apply(); -  }); - -  it('ActionOnAHrefThrowsError', function() { -    var scope = this.compile('<a ng:click="action()">Add Phone</a>', null, true); -    scope.action = function() { -      throw new Error('MyError'); -    }; -    var input = scope.$element; -    browserTrigger(input, 'click'); -    expect(scope.$service('$exceptionHandler').errors[0]).toMatch(/MyError/); -  }); - -  it('ShoulIgnoreVbNonBindable', function() { -    var scope = this.compile("<div>{{a}}" + +        sortedHtml(element)); +  })); + +  it('BindStyle', inject(function($rootScope) { +    var element = angular.compile('<div ng:style="style"/>')($rootScope); + +    $rootScope.$eval('style={height: "10px"}'); +    $rootScope.$apply(); + +    assertEquals("10px", element.css('height')); + +    $rootScope.$eval('style={}'); +    $rootScope.$apply(); +  })); + +  it('ActionOnAHrefThrowsError', inject( +    function(service){ +      service('$exceptionHandler', $exceptionHandlerMockFactory); +    }, +    function($rootScope, $exceptionHandler) { +      var input = angular.compile('<a ng:click="action()">Add Phone</a>')($rootScope); +      $rootScope.action = function() { +        throw new Error('MyError'); +      }; +      browserTrigger(input, 'click'); +      expect($exceptionHandler.errors[0]).toMatch(/MyError/); +    }) +  ); + +  it('ShoulIgnoreVbNonBindable', inject(function($rootScope) { +    var element = angular.compile( +      "<div>{{a}}" +          "<div ng:non-bindable>{{a}}</div>" +          "<div ng:non-bindable=''>{{b}}</div>" + -        "<div ng:non-bindable='true'>{{c}}</div></div>"); -    scope.a = 123; -    scope.$apply(); -    assertEquals('123{{a}}{{b}}{{c}}', scope.$element.text()); -  }); +        "<div ng:non-bindable='true'>{{c}}</div>" + +      "</div>")($rootScope); +    $rootScope.a = 123; +    $rootScope.$apply(); +    assertEquals('123{{a}}{{b}}{{c}}', element.text()); +  })); + +  it('ShouldTemplateBindPreElements', inject(function ($rootScope) { +    var element = angular.compile('<pre>Hello {{name}}!</pre>')($rootScope); +    $rootScope.name = "World"; +    $rootScope.$apply(); -  it('ShouldTemplateBindPreElements', function () { -    var scope = this.compile('<pre>Hello {{name}}!</pre>'); -    scope.name = "World"; -    scope.$apply(); - -    assertEquals('<pre ng:bind-template="Hello {{name}}!">Hello World!</pre>', sortedHtml(scope.$element)); -  }); +    assertEquals( +      '<pre ng:bind-template="Hello {{name}}!">Hello World!</pre>', +      sortedHtml(element)); +  })); -  it('FillInOptionValueWhenMissing', function() { -    var scope = this.compile( +  it('FillInOptionValueWhenMissing', inject(function($rootScope) { +    var element = angular.compile(          '<select ng:model="foo">' +            '<option selected="true">{{a}}</option>' +            '<option value="">{{b}}</option>' +            '<option>C</option>' + -        '</select>'); -    scope.a = 'A'; -    scope.b = 'B'; -    scope.$apply(); -    var optionA = childNode(scope.$element, 0); -    var optionB = childNode(scope.$element, 1); -    var optionC = childNode(scope.$element, 2); +        '</select>')($rootScope); +    $rootScope.a = 'A'; +    $rootScope.b = 'B'; +    $rootScope.$apply(); +    var optionA = childNode(element, 0); +    var optionB = childNode(element, 1); +    var optionC = childNode(element, 2);      expect(optionA.attr('value')).toEqual('A');      expect(optionA.text()).toEqual('A'); @@ -425,19 +444,21 @@ describe('Binder', function() {      expect(optionC.attr('value')).toEqual('C');      expect(optionC.text()).toEqual('C'); -  }); +  })); -  it('DeleteAttributeIfEvaluatesFalse', function() { -    var scope = this.compile('<div>' + +  it('DeleteAttributeIfEvaluatesFalse', inject(function($rootScope) { +    var element = angular.compile( +      '<div>' +          '<input ng:model="a0" ng:bind-attr="{disabled:\'{{true}}\'}">' +          '<input ng:model="a1" ng:bind-attr="{disabled:\'{{false}}\'}">' +          '<input ng:model="b0" ng:bind-attr="{disabled:\'{{1}}\'}">' +          '<input ng:model="b1" ng:bind-attr="{disabled:\'{{0}}\'}">' +          '<input ng:model="c0" ng:bind-attr="{disabled:\'{{[0]}}\'}">' + -        '<input ng:model="c1" ng:bind-attr="{disabled:\'{{[]}}\'}"></div>'); -    scope.$apply(); +        '<input ng:model="c1" ng:bind-attr="{disabled:\'{{[]}}\'}">' + +      '</div>')($rootScope); +    $rootScope.$apply();      function assertChild(index, disabled) { -      var child = childNode(scope.$element, index); +      var child = childNode(element, index);        assertEquals(sortedHtml(child), disabled, !!child.attr('disabled'));      } @@ -447,72 +468,84 @@ describe('Binder', function() {      assertChild(3, false);      assertChild(4, true);      assertChild(5, false); -  }); - -  it('ItShouldDisplayErrorWhenActionIsSyntacticlyIncorrect', function() { -    var scope = this.compile('<div>' + -        '<input type="button" ng:click="greeting=\'ABC\'"/>' + -        '<input type="button" ng:click=":garbage:"/></div>', null, true); -    var first = jqLite(scope.$element[0].childNodes[0]); -    var second = jqLite(scope.$element[0].childNodes[1]); -    var errorLogs = scope.$service('$log').error.logs; - -    browserTrigger(first, 'click'); -    assertEquals("ABC", scope.greeting); -    expect(errorLogs).toEqual([]); - -    browserTrigger(second, 'click'); -    expect(scope.$service('$exceptionHandler').errors[0]). -      toMatchError(/Syntax Error: Token ':' not a primary expression/); -  }); - -  it('ItShouldSelectTheCorrectRadioBox', function() { -    var scope = this.compile('<div>' + +  })); + +  it('ItShouldDisplayErrorWhenActionIsSyntacticlyIncorrect', inject( +    function(service){ +      service('$exceptionHandler', $exceptionHandlerMockFactory); +    }, +    function($rootScope, $exceptionHandler, $log) { +      var element = angular.compile( +        '<div>' + +          '<input type="button" ng:click="greeting=\'ABC\'"/>' + +          '<input type="button" ng:click=":garbage:"/>' + +        '</div>')($rootScope); +      var first = jqLite(element.find('input')[0]); +      var second = jqLite(element.find('input')[1]); +      var errorLogs = $log.error.logs; + +      browserTrigger(first, 'click'); +      assertEquals("ABC", $rootScope.greeting); +      expect(errorLogs).toEqual([]); + +      browserTrigger(second, 'click'); +      expect($exceptionHandler.errors[0]). +        toMatchError(/Syntax Error: Token ':' not a primary expression/); +    }) +  ); + +  it('ItShouldSelectTheCorrectRadioBox', inject(function($rootScope) { +    var element = angular.compile( +      '<div>' +          '<input type="radio" ng:model="sex" value="female">' + -        '<input type="radio" ng:model="sex" value="male"></div>'); -    var female = jqLite(scope.$element[0].childNodes[0]); -    var male = jqLite(scope.$element[0].childNodes[1]); +        '<input type="radio" ng:model="sex" value="male">' + +      '</div>')($rootScope); +    var female = jqLite(element[0].childNodes[0]); +    var male = jqLite(element[0].childNodes[1]);      browserTrigger(female); -    assertEquals("female", scope.sex); +    assertEquals("female", $rootScope.sex);      assertEquals(true, female[0].checked);      assertEquals(false, male[0].checked);      assertEquals("female", female.val());      browserTrigger(male); -    assertEquals("male", scope.sex); +    assertEquals("male", $rootScope.sex);      assertEquals(false, female[0].checked);      assertEquals(true, male[0].checked);      assertEquals("male", male.val()); -  }); - -  it('ItShouldRepeatOnHashes', function() { -    var scope = this.compile('<ul><li ng:repeat="(k,v) in {a:0,b:1}" ng:bind=\"k + v\"></li></ul>'); -    scope.$apply(); +  })); + +  it('ItShouldRepeatOnHashes', inject(function($rootScope) { +    var element = angular.compile( +      '<ul>' + +        '<li ng:repeat="(k,v) in {a:0,b:1}" ng:bind=\"k + v\"></li>' + +      '</ul>')($rootScope); +    $rootScope.$apply();      assertEquals('<ul>' +          '<#comment></#comment>' +          '<li ng:bind=\"k + v\">a0</li>' +          '<li ng:bind=\"k + v\">b1</li>' +          '</ul>', -        sortedHtml(scope.$element)); -  }); - -  it('ItShouldFireChangeListenersBeforeUpdate', function() { -    var scope = this.compile('<div ng:bind="name"></div>'); -    scope.name = ""; -    scope.$watch("watched", "name=123"); -    scope.watched = "change"; -    scope.$apply(); -    assertEquals(123, scope.name); +        sortedHtml(element)); +  })); + +  it('ItShouldFireChangeListenersBeforeUpdate', inject(function($rootScope) { +    var element = angular.compile('<div ng:bind="name"></div>')($rootScope); +    $rootScope.name = ""; +    $rootScope.$watch("watched", "name=123"); +    $rootScope.watched = "change"; +    $rootScope.$apply(); +    assertEquals(123, $rootScope.name);      assertEquals(          '<div ng:bind="name">123</div>', -        sortedHtml(scope.$element)); -  }); - -  it('ItShouldHandleMultilineBindings', function() { -    var scope = this.compile('<div>{{\n 1 \n + \n 2 \n}}</div>'); -    scope.$apply(); -    assertEquals("3", scope.$element.text()); -  }); +        sortedHtml(element)); +  })); + +  it('ItShouldHandleMultilineBindings', inject(function($rootScope) { +    var element = angular.compile('<div>{{\n 1 \n + \n 2 \n}}</div>')($rootScope); +    $rootScope.$apply(); +    assertEquals("3", element.text()); +  }));  }); diff --git a/test/CompilerSpec.js b/test/CompilerSpec.js index 860cea4a..2dec1396 100644 --- a/test/CompilerSpec.js +++ b/test/CompilerSpec.js @@ -3,7 +3,7 @@  describe('compiler', function() {    var compiler, markup, attrMarkup, directives, widgets, compile, log, scope; -  beforeEach(function() { +  beforeEach(inject(function($rootScope) {      log = "";      directives = {        hello: function(expression, element){ @@ -29,14 +29,10 @@ describe('compiler', function() {      compiler = new Compiler(markup, attrMarkup, directives, widgets);      compile = function(html){        var e = jqLite("<div>" + html + "</div>"); -      return scope = compiler.compile(e)(); +      compiler.compile(e)($rootScope); +      return scope = $rootScope;      }; -  }); - - -  afterEach(function() { -    dealoc(scope); -  }); +  }));    it('should not allow compilation of multiple roots', function() { @@ -49,7 +45,7 @@ describe('compiler', function() {    }); -  it('should recognize a directive', function() { +  it('should recognize a directive', inject(function($rootScope) {      var e = jqLite('<div directive="expr" ignore="me"></div>');      directives.directive = function(expression, element){        log += "found"; @@ -61,10 +57,10 @@ describe('compiler', function() {      };      var template = compiler.compile(e);      expect(log).toEqual("found"); -    scope = template(angular.scope()); +    scope = template($rootScope);      expect(e.hasClass('ng-directive')).toEqual(true);      expect(log).toEqual("found:init"); -  }); +  }));    it('should recurse to children', function() { @@ -94,14 +90,15 @@ describe('compiler', function() {    }); -  it('should allow creation of templates', function() { +  it('should allow creation of templates', inject(function($rootScope) {      directives.duplicate = function(expr, element){        element.replaceWith(document.createComment("marker"));        element.removeAttr("duplicate");        var linker = this.compile(element);        return function(marker) {          this.$watch('value', function() { -          var scope = linker(angular.scope(), noop); +          var scope = $rootScope.$new; +          linker(scope, noop);            marker.after(scope.$element);          });        }; @@ -139,7 +136,7 @@ describe('compiler', function() {            '<span>x</span>' +            'after' +          '</div>'); -  }); +  }));    it('should process markup before directives', function() { diff --git a/test/FiltersSpec.js b/test/FiltersSpec.js index df47ccc3..4b69bf03 100644 --- a/test/FiltersSpec.js +++ b/test/FiltersSpec.js @@ -4,28 +4,26 @@ describe('filter', function() {    var filter = angular.filter; -  it('should called the filter when evaluating expression', function() { -    var scope = createScope(); +  it('should called the filter when evaluating expression', inject(function($rootScope) {      filter.fakeFilter = function() {};      spyOn(filter, 'fakeFilter'); -    scope.$eval('10|fakeFilter'); +    $rootScope.$eval('10|fakeFilter');      expect(filter.fakeFilter).toHaveBeenCalledWith(10);      delete filter['fakeFilter']; -  }); +  })); -  it('should call filter on scope context', function() { -    var scope = createScope(); -    scope.name = 'misko'; +  it('should call filter on scope context', inject(function($rootScope) { +    $rootScope.name = 'misko';      filter.fakeFilter = function() {        expect(this.name).toEqual('misko');      };      spyOn(filter, 'fakeFilter').andCallThrough(); -    scope.$eval('10|fakeFilter'); +    $rootScope.$eval('10|fakeFilter');      expect(filter.fakeFilter).toHaveBeenCalled();      delete filter['fakeFilter']; -  }); +  }));    describe('formatNumber', function() {      var pattern; @@ -85,17 +83,12 @@ describe('filter', function() {    describe('currency', function() {      var currency, html, context; -    beforeEach(function() { +    beforeEach(inject(function($rootScope) {        html = jqLite('<span></span>'); -      context = createScope(); +      context = $rootScope;        context.$element = html;        currency = bind(context, filter.currency); -    }); - -    afterEach(function() { -      dealoc(context); -    }); - +    }));      it('should do basic currency filtering', function() {        expect(currency(0)).toEqual('$0.00'); @@ -119,10 +112,10 @@ describe('filter', function() {    describe('number', function() {      var context, number; -    beforeEach(function() { -      context = createScope(); +    beforeEach(inject(function($rootScope) { +      context = $rootScope;        number = bind(context, filter.number); -    }); +    }));      it('should do basic filter', function() { @@ -214,10 +207,10 @@ describe('filter', function() {      var context, date; -    beforeEach(function() { -      context = createScope(); +    beforeEach(inject(function($rootScope) { +      context = $rootScope;        date = bind(context, filter.date); -    }); +    }));      it('should ignore falsy inputs', function() {        expect(date(null)).toBeNull(); diff --git a/test/InjectorSpec.js b/test/InjectorSpec.js index 2c6c102a..39b20392 100644 --- a/test/InjectorSpec.js +++ b/test/InjectorSpec.js @@ -2,24 +2,20 @@  describe('injector', function() {    var providers; -  var cache;    var injector; -  var scope;    beforeEach(function() {      providers = extensionMap({}, 'providers'); -    cache = {}; -    scope = {}; -    injector = createInjector(scope, providers, cache); +    injector = createInjector(providers);    });    it("should return same instance from calling provider", function() { -    providers('text', function() { return scope.name; }); -    scope.name = 'abc'; -    expect(injector('text')).toEqual('abc'); -    expect(cache.text).toEqual('abc'); -    scope.name = 'deleted'; -    expect(injector('text')).toEqual('abc'); +    var instance = {}, +        original = instance; +    providers('instance', function() { return instance; }); +    expect(injector('instance')).toEqual(instance); +    instance = 'deleted'; +    expect(injector('instance')).toEqual(original);    });    it("should call function", function() { @@ -35,10 +31,9 @@ describe('injector', function() {    });    it('should inject providers', function() { -    providers('a', function() {return this.mi = 'Mi';}); -    providers('b', function(mi){return this.name = mi+'sko';}, {$inject:['a']}); +    providers('a', function() {return 'Mi';}); +    providers('b', function(mi){return mi+'sko';}, {$inject:['a']});      expect(injector('b')).toEqual('Misko'); -    expect(scope).toEqual({mi:'Mi', name:'Misko'});    }); @@ -76,7 +71,7 @@ describe('injector', function() {    it('should autostart eager services', function() {      var log = '';      providers('eager', function() {log += 'eager;'; return 'foo';}, {$eager: true}); -    injector.eager(); +    injector = createInjector(providers);      expect(log).toEqual('eager;');      expect(injector('eager')).toBe('foo');    }); @@ -126,11 +121,11 @@ describe('injector', function() {      });      it('should infer injection on services', function() { -      var scope = angular.scope({ +      var $injector = createInjector({          a: function() { return 'a';},          b: function(a){ return a + 'b';}        }); -      expect(scope.$service('b')).toEqual('ab'); +      expect($injector('b')).toEqual('ab');      });    }); diff --git a/test/ParserSpec.js b/test/ParserSpec.js index 975cacc4..ce3b22ca 100644 --- a/test/ParserSpec.js +++ b/test/ParserSpec.js @@ -115,22 +115,22 @@ describe('parser', function() {        expect(tokens[0].text).toEqual(0.5);      }); -    it('should tokenize negative number', function() { -      var value = createScope().$eval("-0.5"); +    it('should tokenize negative number', inject(function($rootScope) { +      var value = $rootScope.$eval("-0.5");        expect(value).toEqual(-0.5); -      value = createScope().$eval("{a:-0.5}"); +      value = $rootScope.$eval("{a:-0.5}");        expect(value).toEqual({a:-0.5}); -    }); +    })); -    it('should tokenize number with exponent', function() { +    it('should tokenize number with exponent', inject(function($rootScope) {        var tokens = lex("0.5E-10");        expect(tokens[0].text).toEqual(0.5E-10); -      expect(createScope().$eval("0.5E-10")).toEqual(0.5E-10); +      expect($rootScope.$eval("0.5E-10")).toEqual(0.5E-10);        tokens = lex("0.5E+10");        expect(tokens[0].text).toEqual(0.5E+10); -    }); +    }));      it('should throws exception for invalid exponent', function() {        expect(function() { @@ -155,9 +155,9 @@ describe('parser', function() {    });    var scope; -  beforeEach(function () { -    scope = createScope(); -  }); +  beforeEach(inject(function ($rootScope) { +    scope = $rootScope; +  }));    it('should parse expressions', function() {      expect(scope.$eval("-1")).toEqual(-1); @@ -226,7 +226,6 @@ describe('parser', function() {      expect(scope.$eval("a=12")).toEqual(12);      expect(scope.a).toEqual(12); -    scope = createScope();      expect(scope.$eval("x.y.z=123;")).toEqual(123);      expect(scope.x.y.z).toEqual(123); @@ -392,7 +391,6 @@ describe('parser', function() {    });    it('should allow assignment after array dereference', function() { -    scope = angular.scope();      scope.obj = [{}];      scope.$eval('obj[0].name=1');      expect(scope.obj.name).toBeUndefined(); @@ -400,7 +398,6 @@ describe('parser', function() {    });    it('should short-circuit AND operator', function() { -    var scope = angular.scope();      scope.run = function() {        throw "IT SHOULD NOT HAVE RUN";      }; @@ -408,7 +405,6 @@ describe('parser', function() {    });    it('should short-circuit OR operator', function() { -    var scope = angular.scope();      scope.run = function() {        throw "IT SHOULD NOT HAVE RUN";      }; diff --git a/test/ResourceSpec.js b/test/ResourceSpec.js index 15bbbdae..75d81a89 100644 --- a/test/ResourceSpec.js +++ b/test/ResourceSpec.js @@ -1,20 +1,24 @@  'use strict';  describe("resource", function() { -  var xhr, resource, CreditCard, callback, $xhrErr; - -  beforeEach(function() { -    var scope = angular.scope(angularService, {'$xhr.error': $xhrErr = jasmine.createSpy('xhr.error')}); -    xhr = scope.$service('$browser').xhr; -    resource = new ResourceFactory(scope.$service('$xhr')); -    CreditCard = resource.route('/CreditCard/:id:verb', {id:'@id.key'}, { -      charge:{ -        method:'POST', -        params:{verb:'!charge'} -      } -    }); -    callback = jasmine.createSpy(); -  }); +  var resource, CreditCard, callback; + +  beforeEach(inject( +    function(service) { +      service('$xhr.error', function(){return jasmine.createSpy('xhr.error')}); +      service.alias('$xhr.error', '$xhrError'); +    }, +    function($xhr) { +      resource = new ResourceFactory($xhr); +      CreditCard = resource.route('/CreditCard/:id:verb', {id:'@id.key'}, { +        charge:{ +          method:'POST', +          params:{verb:'!charge'} +        } +      }); +      callback = jasmine.createSpy(); +    }) +  );    it("should build resource", function() {      expect(typeof CreditCard).toBe('function'); @@ -25,162 +29,162 @@ describe("resource", function() {      expect(typeof CreditCard.query).toBe('function');    }); -  it('should default to empty parameters', function() { -    xhr.expectGET('URL').respond({}); +  it('should default to empty parameters', inject(function($browser) { +    $browser.xhr.expectGET('URL').respond({});      resource.route('URL').query(); -  }); +  })); -  it('should ignore slashes of undefinend parameters', function() { +  it('should ignore slashes of undefinend parameters', inject(function($browser) {      var R = resource.route('/Path/:a/:b/:c'); -    xhr.expectGET('/Path').respond({}); -    xhr.expectGET('/Path/1').respond({}); -    xhr.expectGET('/Path/2/3').respond({}); -    xhr.expectGET('/Path/4/5/6').respond({}); +    $browser.xhr.expectGET('/Path').respond({}); +    $browser.xhr.expectGET('/Path/1').respond({}); +    $browser.xhr.expectGET('/Path/2/3').respond({}); +    $browser.xhr.expectGET('/Path/4/5/6').respond({});      R.get({});      R.get({a:1});      R.get({a:2, b:3});      R.get({a:4, b:5, c:6}); -  }); +  })); -  it('should correctly encode url params', function() { +  it('should correctly encode url params', inject(function($browser) {      var R = resource.route('/Path/:a'); -    xhr.expectGET('/Path/foo%231').respond({}); -    xhr.expectGET('/Path/doh!@foo?bar=baz%231').respond({}); +    $browser.xhr.expectGET('/Path/foo%231').respond({}); +    $browser.xhr.expectGET('/Path/doh!@foo?bar=baz%231').respond({});      R.get({a: 'foo#1'});      R.get({a: 'doh!@foo', bar: 'baz#1'}); -  }); +  })); -  it('should not encode @ in url params', function() { +  it('should not encode @ in url params', inject(function($browser) {      //encodeURIComponent is too agressive and doesn't follow http://www.ietf.org/rfc/rfc3986.txt      //with regards to the character set (pchar) allowed in path segments      //so we need this test to make sure that we don't over-encode the params and break stuff like      //buzz api which uses @self      var R = resource.route('/Path/:a'); -    xhr.expectGET('/Path/doh@fo%20o?!do%26h=g%3Da+h&:bar=$baz@1').respond({}); +    $browser.xhr.expectGET('/Path/doh@fo%20o?!do%26h=g%3Da+h&:bar=$baz@1').respond({});      R.get({a: 'doh@fo o', ':bar': '$baz@1', '!do&h': 'g=a h'}); -  }); +  })); -  it('should encode & in url params', function() { +  it('should encode & in url params', inject(function($browser) {      var R = resource.route('/Path/:a'); -    xhr.expectGET('/Path/doh&foo?bar=baz%261').respond({}); +    $browser.xhr.expectGET('/Path/doh&foo?bar=baz%261').respond({});      R.get({a: 'doh&foo', bar: 'baz&1'}); -  }); +  })); -  it("should build resource with default param", function() { -    xhr.expectGET('/Order/123/Line/456.visa?minimum=0.05').respond({id:'abc'}); +  it("should build resource with default param", inject(function($browser) { +    $browser.xhr.expectGET('/Order/123/Line/456.visa?minimum=0.05').respond({id:'abc'});      var LineItem = resource.route('/Order/:orderId/Line/:id:verb', {orderId: '123', id: '@id.key', verb:'.visa', minimum:0.05});      var item = LineItem.get({id:456}); -    xhr.flush(); +    $browser.xhr.flush();      nakedExpect(item).toEqual({id:'abc'}); -  }); +  })); -  it("should build resource with action default param overriding default param", function() { -    xhr.expectGET('/Customer/123').respond({id:'abc'}); +  it("should build resource with action default param overriding default param", inject(function($browser) { +    $browser.xhr.expectGET('/Customer/123').respond({id:'abc'});      var TypeItem = resource.route('/:type/:typeId', {type: 'Order'},                                    {get: {method: 'GET', params: {type: 'Customer'}}});      var item = TypeItem.get({typeId:123}); -    xhr.flush(); +    $browser.xhr.flush();      nakedExpect(item).toEqual({id:'abc'}); -  }); +  })); -  it("should create resource", function() { -    xhr.expectPOST('/CreditCard', {name:'misko'}).respond({id:123, name:'misko'}); +  it("should create resource", inject(function($browser) { +    $browser.xhr.expectPOST('/CreditCard', {name:'misko'}).respond({id:123, name:'misko'});      var cc = CreditCard.save({name:'misko'}, callback);      nakedExpect(cc).toEqual({name:'misko'});      expect(callback).not.toHaveBeenCalled(); -    xhr.flush(); +    $browser.xhr.flush();      nakedExpect(cc).toEqual({id:123, name:'misko'});      expect(callback).toHaveBeenCalledWith(cc); -  }); +  })); -  it("should read resource", function() { -    xhr.expectGET("/CreditCard/123").respond({id:123, number:'9876'}); +  it("should read resource", inject(function($browser) { +    $browser.xhr.expectGET("/CreditCard/123").respond({id:123, number:'9876'});      var cc = CreditCard.get({id:123}, callback);      expect(cc instanceof CreditCard).toBeTruthy();      nakedExpect(cc).toEqual({});      expect(callback).not.toHaveBeenCalled(); -    xhr.flush(); +    $browser.xhr.flush();      nakedExpect(cc).toEqual({id:123, number:'9876'});      expect(callback).toHaveBeenCalledWith(cc); -  }); +  })); -  it("should read partial resource", function() { -    xhr.expectGET("/CreditCard").respond([{id:{key:123}}]); -    xhr.expectGET("/CreditCard/123").respond({id:{key:123}, number:'9876'}); +  it("should read partial resource", inject(function($browser) { +    $browser.xhr.expectGET("/CreditCard").respond([{id:{key:123}}]); +    $browser.xhr.expectGET("/CreditCard/123").respond({id:{key:123}, number:'9876'});      var ccs = CreditCard.query(); -    xhr.flush(); +    $browser.xhr.flush();      expect(ccs.length).toEqual(1);      var cc = ccs[0];      expect(cc instanceof CreditCard).toBeTruthy();      expect(cc.number).not.toBeDefined();      cc.$get(callback); -    xhr.flush(); +    $browser.xhr.flush();      expect(callback).toHaveBeenCalledWith(cc);      expect(cc.number).toEqual('9876'); -  }); +  })); -  it("should update resource", function() { -    xhr.expectPOST('/CreditCard/123', {id:{key:123}, name:'misko'}).respond({id:{key:123}, name:'rama'}); +  it("should update resource", inject(function($browser) { +    $browser.xhr.expectPOST('/CreditCard/123', {id:{key:123}, name:'misko'}).respond({id:{key:123}, name:'rama'});      var cc = CreditCard.save({id:{key:123}, name:'misko'}, callback);      nakedExpect(cc).toEqual({id:{key:123}, name:'misko'});      expect(callback).not.toHaveBeenCalled(); -    xhr.flush(); -  }); +    $browser.xhr.flush(); +  })); -  it("should query resource", function() { -    xhr.expectGET("/CreditCard?key=value").respond([{id:1}, {id:2}]); +  it("should query resource", inject(function($browser) { +    $browser.xhr.expectGET("/CreditCard?key=value").respond([{id:1}, {id:2}]);      var ccs = CreditCard.query({key:'value'}, callback);      expect(ccs).toEqual([]);      expect(callback).not.toHaveBeenCalled(); -    xhr.flush(); +    $browser.xhr.flush();      nakedExpect(ccs).toEqual([{id:1}, {id:2}]);      expect(callback).toHaveBeenCalledWith(ccs); -  }); +  })); -  it("should have all arguments optional", function() { -    xhr.expectGET('/CreditCard').respond([{id:1}]); +  it("should have all arguments optional", inject(function($browser) { +    $browser.xhr.expectGET('/CreditCard').respond([{id:1}]);      var log = '';      var ccs = CreditCard.query(function() { log += 'cb;'; }); -    xhr.flush(); +    $browser.xhr.flush();      nakedExpect(ccs).toEqual([{id:1}]);      expect(log).toEqual('cb;'); -  }); +  })); -  it('should delete resource and call callback', function() { -    xhr.expectDELETE("/CreditCard/123").respond(200, {}); +  it('should delete resource and call callback', inject(function($browser) { +    $browser.xhr.expectDELETE("/CreditCard/123").respond(200, {});      CreditCard.remove({id:123}, callback);      expect(callback).not.toHaveBeenCalled(); -    xhr.flush(); +    $browser.xhr.flush();      nakedExpect(callback.mostRecentCall.args).toEqual([{}]);      callback.reset(); -    xhr.expectDELETE("/CreditCard/333").respond(204, null); +    $browser.xhr.expectDELETE("/CreditCard/333").respond(204, null);      CreditCard.remove({id:333}, callback);      expect(callback).not.toHaveBeenCalled(); -    xhr.flush(); +    $browser.xhr.flush();      nakedExpect(callback.mostRecentCall.args).toEqual([{}]); -  }); +  })); -  it('should post charge verb', function() { -    xhr.expectPOST('/CreditCard/123!charge?amount=10', {auth:'abc'}).respond({success:'ok'}); +  it('should post charge verb', inject(function($browser) { +    $browser.xhr.expectPOST('/CreditCard/123!charge?amount=10', {auth:'abc'}).respond({success:'ok'});      CreditCard.charge({id:123, amount:10},{auth:'abc'}, callback); -  }); +  })); -  it('should post charge verb on instance', function() { -    xhr.expectPOST('/CreditCard/123!charge?amount=10', {id:{key:123}, name:'misko'}).respond({success:'ok'}); +  it('should post charge verb on instance', inject(function($browser) { +    $browser.xhr.expectPOST('/CreditCard/123!charge?amount=10', {id:{key:123}, name:'misko'}).respond({success:'ok'});      var card = new CreditCard({id:{key:123}, name:'misko'});      card.$charge({amount:10}, callback); -  }); +  })); -  it('should create on save', function() { -    xhr.expectPOST('/CreditCard', {name:'misko'}).respond({id:123}); +  it('should create on save', inject(function($browser) { +    $browser.xhr.expectPOST('/CreditCard', {name:'misko'}).respond({id:123});      var cc = new CreditCard();      expect(cc.$get).toBeDefined();      expect(cc.$query).toBeDefined(); @@ -190,49 +194,46 @@ describe("resource", function() {      cc.name = 'misko';      cc.$save(callback);      nakedExpect(cc).toEqual({name:'misko'}); -    xhr.flush(); +    $browser.xhr.flush();      nakedExpect(cc).toEqual({id:123});      expect(callback).toHaveBeenCalledWith(cc); -  }); +  })); -  it('should not mutate the resource object if response contains no body', function() { +  it('should not mutate the resource object if response contains no body', inject(function($browser) {      var data = {id:{key:123}, number:'9876'}; -    xhr.expectGET("/CreditCard/123").respond(data); +    $browser.xhr.expectGET("/CreditCard/123").respond(data);      var cc = CreditCard.get({id:123}); -    xhr.flush(); +    $browser.xhr.flush();      expect(cc instanceof CreditCard).toBeTruthy();      var idBefore = cc.id; -    xhr.expectPOST("/CreditCard/123", data).respond(''); +    $browser.xhr.expectPOST("/CreditCard/123", data).respond('');      cc.$save(); -    xhr.flush(); +    $browser.xhr.flush();      expect(idBefore).toEqual(cc.id); -  }); +  })); -  it('should bind default parameters', function() { -    xhr.expectGET('/CreditCard/123.visa?minimum=0.05').respond({id:123}); +  it('should bind default parameters', inject(function($browser) { +    $browser.xhr.expectGET('/CreditCard/123.visa?minimum=0.05').respond({id:123});      var Visa = CreditCard.bind({verb:'.visa', minimum:0.05});      var visa = Visa.get({id:123}); -    xhr.flush(); +    $browser.xhr.flush();      nakedExpect(visa).toEqual({id:123}); -  }); +  })); -  it('should excersize full stack', function() { -    var scope = angular.compile('<div></div>')(); -    var $browser = scope.$service('$browser'); -    var $resource = scope.$service('$resource'); +  it('should excersize full stack', inject(function($rootScope, $browser, $resource) { +    angular.compile('<div></div>')($rootScope);      var Person = $resource('/Person/:id');      $browser.xhr.expectGET('/Person/123').respond('\n{\n"name":\n"misko"\n}\n');      var person = Person.get({id:123});      $browser.xhr.flush();      expect(person.name).toEqual('misko'); -    dealoc(scope); -  }); +  })); -  it('should return the same object when verifying the cache', function() { -    var scope = angular.compile('<div></div>')(); -    var $browser = scope.$service('$browser'); -    var $resource = scope.$service('$resource'); +  it('should return the same object when verifying the cache', inject(function($rootScope) { +    angular.compile('<div></div>')($rootScope); +    var $browser = $rootScope.$service('$browser'); +    var $resource = $rootScope.$service('$resource');      var Person = $resource('/Person/:id', null, {query: {method:'GET', isArray: true, verifyCache: true}});      $browser.xhr.expectGET('/Person/123').respond('[\n{\n"name":\n"misko"\n}\n]');      var person = Person.query({id:123}); @@ -248,8 +249,7 @@ describe("resource", function() {      $browser.xhr.flush();      expect(person2Cache).toEqual(person2);      expect(person2[0].name).toEqual('rob'); -    dealoc(scope); -  }); +  }));    describe('failure mode', function() {      var ERROR_CODE = 500, @@ -260,29 +260,32 @@ describe("resource", function() {        errorCB = jasmine.createSpy();      }); -    it('should report error when non 2xx if error callback is not provided', function() { -      xhr.expectGET('/CreditCard/123').respond(ERROR_CODE, ERROR_RESPONSE); +    it('should report error when non 2xx if error callback is not provided', +        inject(function($browser, $xhrError) { +      $browser.xhr.expectGET('/CreditCard/123').respond(ERROR_CODE, ERROR_RESPONSE);        CreditCard.get({id:123}); -      xhr.flush(); -      expect($xhrErr).toHaveBeenCalled(); -    }); +      $browser.xhr.flush(); +      expect($xhrError).toHaveBeenCalled(); +    })); -    it('should call the error callback if provided on non 2xx response', function() { -      xhr.expectGET('/CreditCard/123').respond(ERROR_CODE, ERROR_RESPONSE); +    it('should call the error callback if provided on non 2xx response', +        inject(function($browser, $xhrError) { +      $browser.xhr.expectGET('/CreditCard/123').respond(ERROR_CODE, ERROR_RESPONSE);        CreditCard.get({id:123}, callback, errorCB); -      xhr.flush(); +      $browser.xhr.flush();        expect(errorCB).toHaveBeenCalledWith(500, ERROR_RESPONSE);        expect(callback).not.toHaveBeenCalled(); -      expect($xhrErr).not.toHaveBeenCalled(); -    }); +      expect($xhrError).not.toHaveBeenCalled(); +    })); -    it('should call the error callback if provided on non 2xx response', function() { -      xhr.expectGET('/CreditCard').respond(ERROR_CODE, ERROR_RESPONSE); +    it('should call the error callback if provided on non 2xx response', +        inject(function($browser, $xhrError) { +      $browser.xhr.expectGET('/CreditCard').respond(ERROR_CODE, ERROR_RESPONSE);        CreditCard.get(callback, errorCB); -      xhr.flush(); +      $browser.xhr.flush();        expect(errorCB).toHaveBeenCalledWith(500, ERROR_RESPONSE);        expect(callback).not.toHaveBeenCalled(); -      expect($xhrErr).not.toHaveBeenCalled(); -    }); +      expect($xhrError).not.toHaveBeenCalled(); +    }));    });  }); diff --git a/test/ScenarioSpec.js b/test/ScenarioSpec.js index 5f83ab93..d33e880d 100644 --- a/test/ScenarioSpec.js +++ b/test/ScenarioSpec.js @@ -1,35 +1,25 @@  'use strict';  describe("ScenarioSpec: Compilation", function() { -  var scope; - -  beforeEach(function() { -    scope = null; -  }); - -  afterEach(function() { -    dealoc(scope); -  }); -    describe('compilation', function() { -    it("should compile dom node and return scope", function() { +    it("should compile dom node and return scope", inject(function($rootScope) {        var node = jqLite('<div ng:init="a=1">{{b=a+1}}</div>')[0]; -      scope = angular.compile(node)(); -      scope.$digest(); -      expect(scope.a).toEqual(1); -      expect(scope.b).toEqual(2); -    }); +      angular.compile(node)($rootScope); +      $rootScope.$digest(); +      expect($rootScope.a).toEqual(1); +      expect($rootScope.b).toEqual(2); +    })); -    it("should compile jQuery node and return scope", function() { -      scope = compile(jqLite('<div>{{a=123}}</div>'))(); -      scope.$digest(); -      expect(jqLite(scope.$element).text()).toEqual('123'); -    }); +    it("should compile jQuery node and return scope", inject(function($rootScope) { +      var element = compile(jqLite('<div>{{a=123}}</div>'))($rootScope); +      $rootScope.$digest(); +      expect(jqLite(element).text()).toEqual('123'); +    })); -    it("should compile text node and return scope", function() { -      scope = angular.compile('<div>{{a=123}}</div>')(); -      scope.$digest(); -      expect(jqLite(scope.$element).text()).toEqual('123'); -    }); +    it("should compile text node and return scope", inject(function($rootScope) { +      var element = angular.compile('<div>{{a=123}}</div>')($rootScope); +      $rootScope.$digest(); +      expect(jqLite(element).text()).toEqual('123'); +    }));    });  }); diff --git a/test/angular-mocksSpec.js b/test/angular-mocksSpec.js index c2cffca6..205461af 100644 --- a/test/angular-mocksSpec.js +++ b/test/angular-mocksSpec.js @@ -226,11 +226,8 @@ describe('mocks', function() {    describe('$exceptionHandler', function() { -    it('should rethrow exceptions', function() { -      var rootScope = angular.scope(), -          exHandler = rootScope.$service('$exceptionHandler'); - -      expect(function() { exHandler('myException'); }).toThrow('myException'); -    }); +    it('should rethrow exceptions', inject(function($exceptionHandler) { +      expect(function() { $exceptionHandler('myException'); }).toThrow('myException'); +    }));    });  }); diff --git a/test/directivesSpec.js b/test/directivesSpec.js index e92cb719..eb74d227 100644 --- a/test/directivesSpec.js +++ b/test/directivesSpec.js @@ -2,329 +2,316 @@  describe("directive", function() { -  var compile, model, element; - -  beforeEach(function() { -    compile = function(html) { -      element = jqLite(html); -      return model = angular.compile(element)(); -    }; -  }); - -  afterEach(function() { -    dealoc(model); -  }); - -  it("should ng:init", function() { -    var scope = compile('<div ng:init="a=123"></div>'); -    expect(scope.a).toEqual(123); -  }); +  it("should ng:init", inject(function($rootScope) { +    var element = angular.compile('<div ng:init="a=123"></div>')($rootScope); +    expect($rootScope.a).toEqual(123); +  }));    describe('ng:bind', function() { -    it('should set text', function() { -      var scope = compile('<div ng:bind="a"></div>'); +    it('should set text', inject(function($rootScope) { +      var element = angular.compile('<div ng:bind="a"></div>')($rootScope);        expect(element.text()).toEqual(''); -      scope.a = 'misko'; -      scope.$digest(); +      $rootScope.a = 'misko'; +      $rootScope.$digest();        expect(element.hasClass('ng-binding')).toEqual(true);        expect(element.text()).toEqual('misko'); -    }); +    })); -    it('should set text to blank if undefined', function() { -      var scope = compile('<div ng:bind="a"></div>'); -      scope.a = 'misko'; -      scope.$digest(); +    it('should set text to blank if undefined', inject(function($rootScope) { +      var element = angular.compile('<div ng:bind="a"></div>')($rootScope); +      $rootScope.a = 'misko'; +      $rootScope.$digest();        expect(element.text()).toEqual('misko'); -      scope.a = undefined; -      scope.$digest(); +      $rootScope.a = undefined; +      $rootScope.$digest();        expect(element.text()).toEqual(''); -    }); +    })); -    it('should set html', function() { -      var scope = compile('<div ng:bind="html|html"></div>'); -      scope.html = '<div unknown>hello</div>'; -      scope.$digest(); +    it('should set html', inject(function($rootScope) { +      var element = angular.compile('<div ng:bind="html|html"></div>')($rootScope); +      $rootScope.html = '<div unknown>hello</div>'; +      $rootScope.$digest();        expect(lowercase(element.html())).toEqual('<div>hello</div>'); -    }); +    })); -    it('should set unsafe html', function() { -      var scope = compile('<div ng:bind="html|html:\'unsafe\'"></div>'); -      scope.html = '<div onclick="">hello</div>'; -      scope.$digest(); +    it('should set unsafe html', inject(function($rootScope) { +      var element = angular.compile('<div ng:bind="html|html:\'unsafe\'"></div>')($rootScope); +      $rootScope.html = '<div onclick="">hello</div>'; +      $rootScope.$digest();        expect(lowercase(element.html())).toEqual('<div onclick="">hello</div>'); -    }); +    })); -    it('should set element element', function() { +    it('should set element element', inject(function($rootScope) {        angularFilter.myElement = function() {          return jqLite('<a>hello</a>');        }; -      var scope = compile('<div ng:bind="0|myElement"></div>'); -      scope.$digest(); +      var element = angular.compile('<div ng:bind="0|myElement"></div>')($rootScope); +      $rootScope.$digest();        expect(lowercase(element.html())).toEqual('<a>hello</a>'); -    }); +    })); -    it('should have $element set to current bind element', function() { +    it('should have $element set to current bind element', inject(function($rootScope) {        angularFilter.myFilter = function() {          this.$element.addClass("filter");          return 'HELLO';        }; -      var scope = compile('<div>before<div ng:bind="0|myFilter"></div>after</div>'); -      scope.$digest(); -      expect(sortedHtml(scope.$element)).toEqual('<div>before<div class="filter" ng:bind="0|myFilter">HELLO</div>after</div>'); -    }); - - -    it('should suppress rendering of falsy values', function() { -      var scope = compile('<div>{{ null }}{{ undefined }}{{ "" }}-{{ 0 }}{{ false }}</div>'); -      scope.$digest(); -      expect(scope.$element.text()).toEqual('-0false'); -    }); - -    it('should render object as JSON ignore $$', function() { -      var scope = compile('<div>{{ {key:"value", $$key:"hide"}  }}</div>'); -      scope.$digest(); -      expect(fromJson(scope.$element.text())).toEqual({key:'value'}); -    }); +      var element = angular.compile('<div>before<div ng:bind="0|myFilter"></div>after</div>')($rootScope); +      $rootScope.$digest(); +      expect(sortedHtml(element)).toEqual('<div>before<div class="filter" ng:bind="0|myFilter">HELLO</div>after</div>'); +    })); + + +    it('should suppress rendering of falsy values', inject(function($rootScope) { +      var element = angular.compile('<div>{{ null }}{{ undefined }}{{ "" }}-{{ 0 }}{{ false }}</div>')($rootScope); +      $rootScope.$digest(); +      expect(element.text()).toEqual('-0false'); +    })); + +    it('should render object as JSON ignore $$', inject(function($rootScope) { +      var element = angular.compile('<div>{{ {key:"value", $$key:"hide"}  }}</div>')($rootScope); +      $rootScope.$digest(); +      expect(fromJson(element.text())).toEqual({key:'value'}); +    }));    });    describe('ng:bind-template', function() { -    it('should ng:bind-template', function() { -      var scope = compile('<div ng:bind-template="Hello {{name}}!"></div>'); -      scope.name = 'Misko'; -      scope.$digest(); +    it('should ng:bind-template', inject(function($rootScope) { +      var element = angular.compile('<div ng:bind-template="Hello {{name}}!"></div>')($rootScope); +      $rootScope.name = 'Misko'; +      $rootScope.$digest();        expect(element.hasClass('ng-binding')).toEqual(true);        expect(element.text()).toEqual('Hello Misko!'); -    }); +    })); -    it('should have $element set to current bind element', function() { +    it('should have $element set to current bind element', inject(function($rootScope) {        var innerText;        angularFilter.myFilter = function(text) {          innerText = innerText || this.$element.text();          return text;        }; -      var scope = compile('<div>before<span ng:bind-template="{{\'HELLO\'|myFilter}}">INNER</span>after</div>'); -      scope.$digest(); -      expect(scope.$element.text()).toEqual("beforeHELLOafter"); +      var element = angular.compile('<div>before<span ng:bind-template="{{\'HELLO\'|myFilter}}">INNER</span>after</div>')($rootScope); +      $rootScope.$digest(); +      expect(element.text()).toEqual("beforeHELLOafter");        expect(innerText).toEqual('INNER'); -    }); +    })); -    it('should render object as JSON ignore $$', function() { -      var scope = compile('<pre>{{ {key:"value", $$key:"hide"}  }}</pre>'); -      scope.$digest(); -      expect(fromJson(scope.$element.text())).toEqual({key:'value'}); -    }); +    it('should render object as JSON ignore $$', inject(function($rootScope) { +      var element = angular.compile('<pre>{{ {key:"value", $$key:"hide"}  }}</pre>')($rootScope); +      $rootScope.$digest(); +      expect(fromJson(element.text())).toEqual({key:'value'}); +    }));    });    describe('ng:bind-attr', function() { -    it('should bind attributes', function() { -      var scope = compile('<div ng:bind-attr="{src:\'http://localhost/mysrc\', alt:\'myalt\'}"/>'); -      scope.$digest(); +    it('should bind attributes', inject(function($rootScope) { +      var element = angular.compile('<div ng:bind-attr="{src:\'http://localhost/mysrc\', alt:\'myalt\'}"/>')($rootScope); +      $rootScope.$digest();        expect(element.attr('src')).toEqual('http://localhost/mysrc');        expect(element.attr('alt')).toEqual('myalt'); -    }); +    })); -    it('should not pretty print JSON in attributes', function() { -      var scope = compile('<img alt="{{ {a:1} }}"/>'); -      scope.$digest(); +    it('should not pretty print JSON in attributes', inject(function($rootScope) { +      var element = angular.compile('<img alt="{{ {a:1} }}"/>')($rootScope); +      $rootScope.$digest();        expect(element.attr('alt')).toEqual('{"a":1}'); -    }); +    }));    }); -  it('should remove special attributes on false', function() { -    var scope = compile('<input ng:bind-attr="{disabled:\'{{disabled}}\', readonly:\'{{readonly}}\', checked:\'{{checked}}\'}"/>'); -    var input = scope.$element[0]; +  it('should remove special attributes on false', inject(function($rootScope) { +    var element = angular.compile('<input ng:bind-attr="{disabled:\'{{disabled}}\', readonly:\'{{readonly}}\', checked:\'{{checked}}\'}"/>')($rootScope); +    var input = element[0];      expect(input.disabled).toEqual(false);      expect(input.readOnly).toEqual(false);      expect(input.checked).toEqual(false); -    scope.disabled = true; -    scope.readonly = true; -    scope.checked = true; -    scope.$digest(); +    $rootScope.disabled = true; +    $rootScope.readonly = true; +    $rootScope.checked = true; +    $rootScope.$digest();      expect(input.disabled).toEqual(true);      expect(input.readOnly).toEqual(true);      expect(input.checked).toEqual(true); -  }); +  }));    describe('ng:click', function() { -    it('should get called on a click', function() { -      var scope = compile('<div ng:click="clicked = true"></div>'); -      scope.$digest(); -      expect(scope.clicked).toBeFalsy(); +    it('should get called on a click', inject(function($rootScope) { +      var element = angular.compile('<div ng:click="clicked = true"></div>')($rootScope); +      $rootScope.$digest(); +      expect($rootScope.clicked).toBeFalsy();        browserTrigger(element, 'click'); -      expect(scope.clicked).toEqual(true); -    }); +      expect($rootScope.clicked).toEqual(true); +    })); -    it('should stop event propagation', function() { -      var scope = compile('<div ng:click="outer = true"><div ng:click="inner = true"></div></div>'); -      scope.$digest(); -      expect(scope.outer).not.toBeDefined(); -      expect(scope.inner).not.toBeDefined(); +    it('should stop event propagation', inject(function($rootScope) { +      var element = angular.compile('<div ng:click="outer = true"><div ng:click="inner = true"></div></div>')($rootScope); +      $rootScope.$digest(); +      expect($rootScope.outer).not.toBeDefined(); +      expect($rootScope.inner).not.toBeDefined();        var innerDiv = element.children()[0];        browserTrigger(innerDiv, 'click'); -      expect(scope.outer).not.toBeDefined(); -      expect(scope.inner).toEqual(true); -    }); +      expect($rootScope.outer).not.toBeDefined(); +      expect($rootScope.inner).toEqual(true); +    }));    });    describe('ng:submit', function() { -    it('should get called on form submit', function() { -      var scope = compile('<form action="" ng:submit="submitted = true">' + -                            '<input type="submit"/>' + -                          '</form>'); -      scope.$digest(); -      expect(scope.submitted).not.toBeDefined(); +    it('should get called on form submit', inject(function($rootScope) { +      var element = angular.compile('<form action="" ng:submit="submitted = true">' + +        '<input type="submit"/>' + +        '</form>')($rootScope); +      $rootScope.$digest(); +      expect($rootScope.submitted).not.toBeDefined();        browserTrigger(element.children()[0]); -      expect(scope.submitted).toEqual(true); -    }); +      expect($rootScope.submitted).toEqual(true); +    }));    });    describe('ng:class', function() { -    it('should add new and remove old classes dynamically', function() { -      var scope = compile('<div class="existing" ng:class="dynClass"></div>'); -      scope.dynClass = 'A'; -      scope.$digest(); +    it('should add new and remove old classes dynamically', inject(function($rootScope) { +      var element = angular.compile('<div class="existing" ng:class="dynClass"></div>')($rootScope); +      $rootScope.dynClass = 'A'; +      $rootScope.$digest();        expect(element.hasClass('existing')).toBe(true);        expect(element.hasClass('A')).toBe(true); -      scope.dynClass = 'B'; -      scope.$digest(); +      $rootScope.dynClass = 'B'; +      $rootScope.$digest();        expect(element.hasClass('existing')).toBe(true);        expect(element.hasClass('A')).toBe(false);        expect(element.hasClass('B')).toBe(true); -      delete scope.dynClass; -      scope.$digest(); +      delete $rootScope.dynClass; +      $rootScope.$digest();        expect(element.hasClass('existing')).toBe(true);        expect(element.hasClass('A')).toBe(false);        expect(element.hasClass('B')).toBe(false); -    }); +    })); -    it('should support adding multiple classes via an array', function() { -      var scope = compile('<div class="existing" ng:class="[\'A\', \'B\']"></div>'); -      scope.$digest(); +    it('should support adding multiple classes via an array', inject(function($rootScope) { +      var element = angular.compile('<div class="existing" ng:class="[\'A\', \'B\']"></div>')($rootScope); +      $rootScope.$digest();        expect(element.hasClass('existing')).toBeTruthy();        expect(element.hasClass('A')).toBeTruthy();        expect(element.hasClass('B')).toBeTruthy(); -    }); +    })); -    it('should support adding multiple classes via a space delimited string', function() { -      var scope = compile('<div class="existing" ng:class="\'A B\'"></div>'); -      scope.$digest(); +    it('should support adding multiple classes via a space delimited string', inject(function($rootScope) { +      var element = angular.compile('<div class="existing" ng:class="\'A B\'"></div>')($rootScope); +      $rootScope.$digest();        expect(element.hasClass('existing')).toBeTruthy();        expect(element.hasClass('A')).toBeTruthy();        expect(element.hasClass('B')).toBeTruthy(); -    }); +    })); -    it('should preserve class added post compilation with pre-existing classes', function() { -      var scope = compile('<div class="existing" ng:class="dynClass"></div>'); -      scope.dynClass = 'A'; -      scope.$digest(); +    it('should preserve class added post compilation with pre-existing classes', inject(function($rootScope) { +      var element = angular.compile('<div class="existing" ng:class="dynClass"></div>')($rootScope); +      $rootScope.dynClass = 'A'; +      $rootScope.$digest();        expect(element.hasClass('existing')).toBe(true);        // add extra class, change model and eval        element.addClass('newClass'); -      scope.dynClass = 'B'; -      scope.$digest(); +      $rootScope.dynClass = 'B'; +      $rootScope.$digest();        expect(element.hasClass('existing')).toBe(true);        expect(element.hasClass('B')).toBe(true);        expect(element.hasClass('newClass')).toBe(true); -    }); +    })); -    it('should preserve class added post compilation without pre-existing classes"', function() { -      var scope = compile('<div ng:class="dynClass"></div>'); -      scope.dynClass = 'A'; -      scope.$digest(); +    it('should preserve class added post compilation without pre-existing classes"', inject(function($rootScope) { +      var element = angular.compile('<div ng:class="dynClass"></div>')($rootScope); +      $rootScope.dynClass = 'A'; +      $rootScope.$digest();        expect(element.hasClass('A')).toBe(true);        // add extra class, change model and eval        element.addClass('newClass'); -      scope.dynClass = 'B'; -      scope.$digest(); +      $rootScope.dynClass = 'B'; +      $rootScope.$digest();        expect(element.hasClass('B')).toBe(true);        expect(element.hasClass('newClass')).toBe(true); -    }); +    })); -    it('should preserve other classes with similar name"', function() { -      var scope = compile('<div class="ui-panel ui-selected" ng:class="dynCls"></div>'); -      scope.dynCls = 'panel'; -      scope.$digest(); -      scope.dynCls = 'foo'; -      scope.$digest(); +    it('should preserve other classes with similar name"', inject(function($rootScope) { +      var element = angular.compile('<div class="ui-panel ui-selected" ng:class="dynCls"></div>')($rootScope); +      $rootScope.dynCls = 'panel'; +      $rootScope.$digest(); +      $rootScope.dynCls = 'foo'; +      $rootScope.$digest();        expect(element[0].className).toBe('ui-panel ui-selected ng-directive foo'); -    }); +    })); -    it('should not add duplicate classes', function() { -      var scope = compile('<div class="panel bar" ng:class="dynCls"></div>'); -      scope.dynCls = 'panel'; -      scope.$digest(); +    it('should not add duplicate classes', inject(function($rootScope) { +      var element = angular.compile('<div class="panel bar" ng:class="dynCls"></div>')($rootScope); +      $rootScope.dynCls = 'panel'; +      $rootScope.$digest();        expect(element[0].className).toBe('panel bar ng-directive'); -    }); +    })); -    it('should remove classes even if it was specified via class attribute', function() { -      var scope = compile('<div class="panel bar" ng:class="dynCls"></div>'); -      scope.dynCls = 'panel'; -      scope.$digest(); -      scope.dynCls = 'window'; -      scope.$digest(); +    it('should remove classes even if it was specified via class attribute', inject(function($rootScope) { +      var element = angular.compile('<div class="panel bar" ng:class="dynCls"></div>')($rootScope); +      $rootScope.dynCls = 'panel'; +      $rootScope.$digest(); +      $rootScope.dynCls = 'window'; +      $rootScope.$digest();        expect(element[0].className).toBe('bar ng-directive window'); -    }); +    })); -    it('should remove classes even if they were added by another code', function() { -      var scope = compile('<div ng:class="dynCls"></div>'); -      scope.dynCls = 'foo'; -      scope.$digest(); +    it('should remove classes even if they were added by another code', inject(function($rootScope) { +      var element = angular.compile('<div ng:class="dynCls"></div>')($rootScope); +      $rootScope.dynCls = 'foo'; +      $rootScope.$digest();        element.addClass('foo'); -      scope.dynCls = ''; -      scope.$digest(); +      $rootScope.dynCls = ''; +      $rootScope.$digest();        expect(element[0].className).toBe('ng-directive'); -    }); +    })); -    it('should convert undefined and null values to an empty string', function() { -      var scope = compile('<div ng:class="dynCls"></div>'); -      scope.dynCls = [undefined, null]; -      scope.$digest(); +    it('should convert undefined and null values to an empty string', inject(function($rootScope) { +      var element = angular.compile('<div ng:class="dynCls"></div>')($rootScope); +      $rootScope.dynCls = [undefined, null]; +      $rootScope.$digest();        expect(element[0].className).toBe('ng-directive'); -    }); +    }));    }); -  it('should ng:class odd/even', function() { -    var scope = compile('<ul><li ng:repeat="i in [0,1]" class="existing" ng:class-odd="\'odd\'" ng:class-even="\'even\'"></li><ul>'); -    scope.$digest(); +  it('should ng:class odd/even', inject(function($rootScope) { +    var element = angular.compile('<ul><li ng:repeat="i in [0,1]" class="existing" ng:class-odd="\'odd\'" ng:class-even="\'even\'"></li><ul>')($rootScope); +    $rootScope.$digest();      var e1 = jqLite(element[0].childNodes[1]);      var e2 = jqLite(element[0].childNodes[2]);      expect(e1.hasClass('existing')).toBeTruthy();      expect(e1.hasClass('odd')).toBeTruthy();      expect(e2.hasClass('existing')).toBeTruthy();      expect(e2.hasClass('even')).toBeTruthy(); -  }); +  })); -  it('should allow both ng:class and ng:class-odd/even on the same element', function() { -    var scope = compile('<ul>' + -                          '<li ng:repeat="i in [0,1]" ng:class="\'plainClass\'" ' + -                              'ng:class-odd="\'odd\'" ng:class-even="\'even\'"></li>' + -                        '<ul>'); -    scope.$apply(); +  it('should allow both ng:class and ng:class-odd/even on the same element', inject(function($rootScope) { +    var element = angular.compile('<ul>' + +      '<li ng:repeat="i in [0,1]" ng:class="\'plainClass\'" ' + +      'ng:class-odd="\'odd\'" ng:class-even="\'even\'"></li>' + +      '<ul>')($rootScope); +    $rootScope.$apply();      var e1 = jqLite(element[0].childNodes[1]);      var e2 = jqLite(element[0].childNodes[2]); @@ -334,15 +321,15 @@ describe("directive", function() {      expect(e2.hasClass('plainClass')).toBeTruthy();      expect(e2.hasClass('even')).toBeTruthy();      expect(e2.hasClass('odd')).toBeFalsy(); -  }); +  })); -  it('should allow both ng:class and ng:class-odd/even with multiple classes', function() { -    var scope = compile('<ul>' + -                          '<li ng:repeat="i in [0,1]" ng:class="[\'A\', \'B\']" ' + -                              'ng:class-odd="[\'C\', \'D\']" ng:class-even="[\'E\', \'F\']"></li>' + -                        '<ul>'); -    scope.$apply(); +  it('should allow both ng:class and ng:class-odd/even with multiple classes', inject(function($rootScope) { +    var element = angular.compile('<ul>' + +      '<li ng:repeat="i in [0,1]" ng:class="[\'A\', \'B\']" ' + +      'ng:class-odd="[\'C\', \'D\']" ng:class-even="[\'E\', \'F\']"></li>' + +      '<ul>')($rootScope); +    $rootScope.$apply();      var e1 = jqLite(element[0].childNodes[1]);      var e2 = jqLite(element[0].childNodes[2]); @@ -359,29 +346,29 @@ describe("directive", function() {      expect(e2.hasClass('F')).toBeTruthy();      expect(e2.hasClass('C')).toBeFalsy();      expect(e2.hasClass('D')).toBeFalsy(); -  }); +  }));    describe('ng:style', function() { -    it('should set', function() { -      var scope = compile('<div ng:style="{height: \'40px\'}"></div>'); -      scope.$digest(); +    it('should set', inject(function($rootScope) { +      var element = angular.compile('<div ng:style="{height: \'40px\'}"></div>')($rootScope); +      $rootScope.$digest();        expect(element.css('height')).toEqual('40px'); -    }); +    })); -    it('should silently ignore undefined style', function() { -      var scope = compile('<div ng:style="myStyle"></div>'); -      scope.$digest(); +    it('should silently ignore undefined style', inject(function($rootScope) { +      var element = angular.compile('<div ng:style="myStyle"></div>')($rootScope); +      $rootScope.$digest();        expect(element.hasClass('ng-exception')).toBeFalsy(); -    }); +    }));      describe('preserving styles set before and after compilation', function() { -      var scope, preCompStyle, preCompVal, postCompStyle, postCompVal; +      var scope, preCompStyle, preCompVal, postCompStyle, postCompVal, element; -      beforeEach(function() { +      beforeEach(inject(function($rootScope) {          preCompStyle = 'width';          preCompVal = '300px';          postCompStyle = 'height'; @@ -389,11 +376,12 @@ describe("directive", function() {          element = jqLite('<div ng:style="styleObj"></div>');          element.css(preCompStyle, preCompVal);          jqLite(document.body).append(element); -        scope = compile(element); +        angular.compile(element)($rootScope); +        scope = $rootScope;          scope.styleObj = {'margin-top': '44px'};          scope.$apply();          element.css(postCompStyle, postCompVal); -      }); +      }));        afterEach(function() {          element.remove(); @@ -443,39 +431,36 @@ describe("directive", function() {    describe('ng:show', function() { -    it('should show and hide an element', function() { -      var element = jqLite('<div ng:show="exp"></div>'), -          scope = compile(element); - -      scope.$digest(); +    it('should show and hide an element', inject(function($rootScope) { +      var element = jqLite('<div ng:show="exp"></div>'); +      var element = angular.compile(element)($rootScope); +      $rootScope.$digest();        expect(isCssVisible(element)).toEqual(false); -      scope.exp = true; -      scope.$digest(); +      $rootScope.exp = true; +      $rootScope.$digest();        expect(isCssVisible(element)).toEqual(true); -    }); - +    })); -    it('should make hidden element visible', function() { -      var element = jqLite('<div style="display: none" ng:show="exp"></div>'), -          scope = compile(element); +    it('should make hidden element visible', inject(function($rootScope) { +      var element = jqLite('<div style="display: none" ng:show="exp"></div>'); +      var element = angular.compile(element)($rootScope);        expect(isCssVisible(element)).toBe(false); -      scope.exp = true; -      scope.$digest(); +      $rootScope.exp = true; +      $rootScope.$digest();        expect(isCssVisible(element)).toBe(true); -    }); +    }));    });    describe('ng:hide', function() { -    it('should hide an element', function() { -      var element = jqLite('<div ng:hide="exp"></div>'), -          scope = compile(element); - +    it('should hide an element', inject(function($rootScope) { +      var element = jqLite('<div ng:hide="exp"></div>'); +      var element = angular.compile(element)($rootScope);        expect(isCssVisible(element)).toBe(true); -      scope.exp = true; -      scope.$digest(); +      $rootScope.exp = true; +      $rootScope.$digest();        expect(isCssVisible(element)).toBe(false); -    }); +    }));    });    describe('ng:controller', function() { @@ -500,13 +485,13 @@ describe("directive", function() {        window.temp = undefined;      }); -    it('should bind', function() { -      var scope = compile('<div ng:controller="temp.Greeter"></div>'); -      expect(scope.greeter.greeting).toEqual('hello'); -      expect(scope.greeter.greet('misko')).toEqual('hello misko!'); -    }); +    it('should bind', inject(function($rootScope) { +      var element = angular.compile('<div ng:controller="temp.Greeter"></div>')($rootScope); +      expect($rootScope.greeter.greeting).toEqual('hello'); +      expect($rootScope.greeter.greet('misko')).toEqual('hello misko!'); +    })); -    it('should support nested controllers', function() { +    it('should support nested controllers', inject(function($rootScope) {        temp.ChildGreeter = function() {          this.greeting = 'hey';          this.$root.childGreeter = this; @@ -516,37 +501,37 @@ describe("directive", function() {            return this.greeting + ' dude' + this.suffix;          }        }; -      var scope = compile('<div ng:controller="temp.Greeter"><div ng:controller="temp.ChildGreeter">{{greet("misko")}}</div></div>'); -      expect(scope.greeting).not.toBeDefined(); -      expect(scope.greeter.greeting).toEqual('hello'); -      expect(scope.greeter.greet('misko')).toEqual('hello misko!'); -      expect(scope.greeter.greeting).toEqual('hello'); -      expect(scope.childGreeter.greeting).toEqual('hey'); -      expect(scope.childGreeter.$parent.greeting).toEqual('hello'); -      scope.$digest(); -      expect(scope.$element.text()).toEqual('hey dude!'); -    }); - -    it('should infer injection arguments', function() { +      var element = angular.compile('<div ng:controller="temp.Greeter"><div ng:controller="temp.ChildGreeter">{{greet("misko")}}</div></div>')($rootScope); +      expect($rootScope.greeting).not.toBeDefined(); +      expect($rootScope.greeter.greeting).toEqual('hello'); +      expect($rootScope.greeter.greet('misko')).toEqual('hello misko!'); +      expect($rootScope.greeter.greeting).toEqual('hello'); +      expect($rootScope.childGreeter.greeting).toEqual('hey'); +      expect($rootScope.childGreeter.$parent.greeting).toEqual('hello'); +      $rootScope.$digest(); +      expect(element.text()).toEqual('hey dude!'); +    })); + +    it('should infer injection arguments', inject(function($rootScope) {        temp.MyController = function($xhr){          this.$root.someService = $xhr;        }; -      var scope = compile('<div ng:controller="temp.MyController"></div>'); -      expect(scope.someService).toBe(scope.$service('$xhr')); -    }); +      var element = angular.compile('<div ng:controller="temp.MyController"></div>')($rootScope); +      expect($rootScope.someService).toBe($rootScope.$service('$xhr')); +    }));    });    describe('ng:cloak', function() { -    it('should get removed when an element is compiled', function() { +    it('should get removed when an element is compiled', inject(function($rootScope) {        var element = jqLite('<div ng:cloak></div>');        expect(element.attr('ng:cloak')).toBe('');        angular.compile(element);        expect(element.attr('ng:cloak')).toBeUndefined(); -    }); +    })); -    it('should remove ng-cloak class from a compiled element', function() { +    it('should remove ng-cloak class from a compiled element', inject(function($rootScope) {        var element = jqLite('<div ng:cloak class="foo ng-cloak bar"></div>');        expect(element.hasClass('foo')).toBe(true); @@ -558,6 +543,6 @@ describe("directive", function() {        expect(element.hasClass('foo')).toBe(true);        expect(element.hasClass('ng-cloak')).toBe(false);        expect(element.hasClass('bar')).toBe(true); -    }); +    }));    });  }); diff --git a/test/jqLiteSpec.js b/test/jqLiteSpec.js index 2f9a5fb9..5c54e786 100644 --- a/test/jqLiteSpec.js +++ b/test/jqLiteSpec.js @@ -10,8 +10,8 @@ describe('jqLite', function() {    }); -  beforeEach(function() { -    scope = angular.scope(); +  beforeEach(inject(function($rootScope) { +    scope = $rootScope;      this.addMatchers({        toJqEqual: function(expected) {          var msg = "Unequal length"; @@ -29,7 +29,7 @@ describe('jqLite', function() {          return value;        }      }); -  }); +  }));    afterEach(function() { diff --git a/test/markupSpec.js b/test/markupSpec.js index 41255b4a..d97dbcb5 100644 --- a/test/markupSpec.js +++ b/test/markupSpec.js @@ -2,51 +2,36 @@  describe("markups", function() { -  var compile, element, scope; - -  beforeEach(function() { -    scope = null; -    element = null; -    compile = function(html) { -      element = jqLite(html); -      scope = angular.compile(element)(); -    }; -  }); - -  afterEach(function() { -    dealoc(element); -  }); - -  it('should translate {{}} in text', function() { -    compile('<div>hello {{name}}!</div>'); +  it('should translate {{}} in text', inject(function($rootScope) { +    var element = angular.compile('<div>hello {{name}}!</div>')($rootScope)      expect(sortedHtml(element)).toEqual('<div>hello <span ng:bind="name"></span>!</div>'); -    scope.name = 'Misko'; -    scope.$digest(); +    $rootScope.name = 'Misko'; +    $rootScope.$digest();      expect(sortedHtml(element)).toEqual('<div>hello <span ng:bind="name">Misko</span>!</div>'); -  }); +  })); -  it('should translate {{}} in terminal nodes', function() { -    compile('<select ng:model="x"><option value="">Greet {{name}}!</option></select>'); -    scope.$digest(); +  it('should translate {{}} in terminal nodes', inject(function($rootScope) { +    var element = angular.compile('<select ng:model="x"><option value="">Greet {{name}}!</option></select>')($rootScope) +    $rootScope.$digest();      expect(sortedHtml(element).replace(' selected="true"', '')).        toEqual('<select ng:model="x">' +                  '<option ng:bind-template="Greet {{name}}!">Greet !</option>' +                '</select>'); -    scope.name = 'Misko'; -    scope.$digest(); +    $rootScope.name = 'Misko'; +    $rootScope.$digest();      expect(sortedHtml(element).replace(' selected="true"', '')).        toEqual('<select ng:model="x">' +                  '<option ng:bind-template="Greet {{name}}!">Greet Misko!</option>' +                '</select>'); -  }); +  })); -  it('should translate {{}} in attributes', function() { -    compile('<div src="http://server/{{path}}.png"/>'); +  it('should translate {{}} in attributes', inject(function($rootScope) { +    var element = angular.compile('<div src="http://server/{{path}}.png"/>')($rootScope)      expect(element.attr('ng:bind-attr')).toEqual('{"src":"http://server/{{path}}.png"}'); -    scope.path = 'a/b'; -    scope.$digest(); +    $rootScope.path = 'a/b'; +    $rootScope.$digest();      expect(element.attr('src')).toEqual("http://server/a/b.png"); -  }); +  }));    describe('OPTION value', function() {      beforeEach(function() { @@ -69,127 +54,138 @@ describe("markups", function() {        });      }); -    afterEach(function() { -      if (element) element.remove(); -    }); - -    it('should populate value attribute on OPTION', function() { -      compile('<select ng:model="x"><option>abc</option></select>'); +    it('should populate value attribute on OPTION', inject(function($rootScope) { +      var element = angular.compile('<select ng:model="x"><option>abc</option></select>')($rootScope)        expect(element).toHaveValue('abc'); -    }); +    })); -    it('should ignore value if already exists', function() { -      compile('<select ng:model="x"><option value="abc">xyz</option></select>'); +    it('should ignore value if already exists', inject(function($rootScope) { +      var element = angular.compile('<select ng:model="x"><option value="abc">xyz</option></select>')($rootScope)        expect(element).toHaveValue('abc'); -    }); +    })); -    it('should set value even if newlines present', function() { -      compile('<select ng:model="x"><option attr="\ntext\n" \n>\nabc\n</option></select>'); +    it('should set value even if newlines present', inject(function($rootScope) { +      var element = angular.compile('<select ng:model="x"><option attr="\ntext\n" \n>\nabc\n</option></select>')($rootScope)        expect(element).toHaveValue('\nabc\n'); -    }); +    })); -    it('should set value even if self closing HTML', function() { +    it('should set value even if self closing HTML', inject(function($rootScope) {        // IE removes the \n from option, which makes this test pointless        if (msie) return; -      compile('<select ng:model="x"><option>\n</option></select>'); +      var element = angular.compile('<select ng:model="x"><option>\n</option></select>')($rootScope)        expect(element).toHaveValue('\n'); -    }); +    }));    }); -  it('should bind href', function() { -    compile('<a ng:href="{{url}}"></a>'); +  it('should bind href', inject(function($rootScope) { +    var element = angular.compile('<a ng:href="{{url}}"></a>')($rootScope)      expect(sortedHtml(element)).toEqual('<a ng:bind-attr="{"href":"{{url}}"}"></a>'); -  }); +  })); -  it('should bind disabled', function() { -    compile('<button ng:disabled="{{isDisabled}}">Button</button>'); -    scope.isDisabled = false; -    scope.$digest(); +  it('should bind disabled', inject(function($rootScope) { +    var element = angular.compile('<button ng:disabled="{{isDisabled}}">Button</button>')($rootScope) +    $rootScope.isDisabled = false; +    $rootScope.$digest();      expect(element.attr('disabled')).toBeFalsy(); -    scope.isDisabled = true; -    scope.$digest(); +    $rootScope.isDisabled = true; +    $rootScope.$digest();      expect(element.attr('disabled')).toBeTruthy(); -  }); +  })); -  it('should bind checked', function() { -    compile('<input type="checkbox" ng:checked="{{isChecked}}" />'); -    scope.isChecked = false; -    scope.$digest(); +  it('should bind checked', inject(function($rootScope) { +    var element = angular.compile('<input type="checkbox" ng:checked="{{isChecked}}" />')($rootScope) +    $rootScope.isChecked = false; +    $rootScope.$digest();      expect(element.attr('checked')).toBeFalsy(); -    scope.isChecked=true; -    scope.$digest(); +    $rootScope.isChecked=true; +    $rootScope.$digest();      expect(element.attr('checked')).toBeTruthy(); -  }); +  })); -  it('should bind selected', function() { -    compile('<select><option value=""></option><option ng:selected="{{isSelected}}">Greetings!</option></select>'); +  it('should bind selected', inject(function($rootScope) { +    var element = angular.compile('<select><option value=""></option><option ng:selected="{{isSelected}}">Greetings!</option></select>')($rootScope)      jqLite(document.body).append(element) -    scope.isSelected=false; -    scope.$digest(); +    $rootScope.isSelected=false; +    $rootScope.$digest();      expect(element.children()[1].selected).toBeFalsy(); -    scope.isSelected=true; -    scope.$digest(); +    $rootScope.isSelected=true; +    $rootScope.$digest();      expect(element.children()[1].selected).toBeTruthy(); -  }); +  })); -  it('should bind readonly', function() { -    compile('<input type="text" ng:readonly="{{isReadonly}}" />'); -    scope.isReadonly=false; -    scope.$digest(); +  it('should bind readonly', inject(function($rootScope) { +    var element = angular.compile('<input type="text" ng:readonly="{{isReadonly}}" />')($rootScope) +    $rootScope.isReadonly=false; +    $rootScope.$digest();      expect(element.attr('readOnly')).toBeFalsy(); -    scope.isReadonly=true; -    scope.$digest(); +    $rootScope.isReadonly=true; +    $rootScope.$digest();      expect(element.attr('readOnly')).toBeTruthy(); -  }); +  })); -  it('should bind multiple', function() { -    compile('<select ng:multiple="{{isMultiple}}"></select>'); -    scope.isMultiple=false; -    scope.$digest(); +  it('should bind multiple', inject(function($rootScope) { +    var element = angular.compile('<select ng:multiple="{{isMultiple}}"></select>')($rootScope) +    $rootScope.isMultiple=false; +    $rootScope.$digest();      expect(element.attr('multiple')).toBeFalsy(); -    scope.isMultiple='multiple'; -    scope.$digest(); +    $rootScope.isMultiple='multiple'; +    $rootScope.$digest();      expect(element.attr('multiple')).toBeTruthy(); -  }); +  })); -  it('should bind src', function() { -    compile('<div ng:src="{{url}}" />'); -    scope.url = 'http://localhost/'; -    scope.$digest(); +  it('should bind src', inject(function($rootScope) { +    var element = angular.compile('<div ng:src="{{url}}" />')($rootScope) +    $rootScope.url = 'http://localhost/'; +    $rootScope.$digest();      expect(element.attr('src')).toEqual('http://localhost/'); -  }); +  })); -  it('should bind href and merge with other attrs', function() { -    compile('<a ng:href="{{url}}" rel="{{rel}}"></a>'); +  it('should bind href and merge with other attrs', inject(function($rootScope) { +    var element = angular.compile('<a ng:href="{{url}}" rel="{{rel}}"></a>')($rootScope)      expect(sortedHtml(element)).toEqual('<a ng:bind-attr="{"href":"{{url}}","rel":"{{rel}}"}"></a>'); -  }); - -  it('should bind Text with no Bindings', function() { -    forEach(['checked', 'disabled', 'multiple', 'readonly', 'selected', 'src', 'href'], -        function(name) { -      compile('<div ng:' + name +'="some"></div>'); -      expect(sortedHtml(element)).toEqual('<div ng:bind-attr="{"' + name +'":"some"}"></div>'); +  })); + +  it('should bind Text with no Bindings', inject(function() { +    var $rootScope; +    function newScope (){ +      return $rootScope = angular.injector()('$rootScope'); +    } +    forEach(['checked', 'disabled', 'multiple', 'readonly', 'selected'], function(name) { +      var element = angular.compile('<div ng:' + name + '="some"></div>')(newScope()) +      expect(element.attr('ng:bind-attr')).toBe('{"' + name +'":"some"}'); +      $rootScope.$digest(); +      expect(element.attr(name)).toBe(name);        dealoc(element);      }); -  }); -  it('should Parse Text With No Bindings', function() { +    var element = angular.compile('<div ng:src="some"></div>')(newScope()) +    $rootScope.$digest(); +    expect(sortedHtml(element)).toEqual('<div ng:bind-attr="{"src":"some"}" src="some"></div>'); +    dealoc(element); + +    var element = angular.compile('<div ng:href="some"></div>')(newScope()) +    $rootScope.$digest(); +    expect(sortedHtml(element)).toEqual('<div href="some" ng:bind-attr="{"href":"some"}"></div>'); +    dealoc(element); +  })); + +  it('should Parse Text With No Bindings', inject(function($rootScope) {      var parts = parseBindings("a");      assertEquals(parts.length, 1);      assertEquals(parts[0], "a");      assertTrue(!binding(parts[0])); -  }); +  })); -  it('should Parse Empty Text', function() { +  it('should Parse Empty Text', inject(function($rootScope) {      var parts = parseBindings("");      assertEquals(parts.length, 1);      assertEquals(parts[0], "");      assertTrue(!binding(parts[0])); -  }); +  })); -  it('should Parse Inner Binding', function() { +  it('should Parse Inner Binding', inject(function($rootScope) {      var parts = parseBindings("a{{b}}C");      assertEquals(parts.length, 3);      assertEquals(parts[0], "a"); @@ -198,43 +194,43 @@ describe("markups", function() {      assertEquals(binding(parts[1]), "b");      assertEquals(parts[2], "C");      assertTrue(!binding(parts[2])); -  }); +  })); -  it('should Parse Ending Binding', function() { +  it('should Parse Ending Binding', inject(function($rootScope) {      var parts = parseBindings("a{{b}}");      assertEquals(parts.length, 2);      assertEquals(parts[0], "a");      assertTrue(!binding(parts[0]));      assertEquals(parts[1], "{{b}}");      assertEquals(binding(parts[1]), "b"); -  }); +  })); -  it('should Parse Begging Binding', function() { +  it('should Parse Begging Binding', inject(function($rootScope) {      var parts = parseBindings("{{b}}c");      assertEquals(parts.length, 2);      assertEquals(parts[0], "{{b}}");      assertEquals(binding(parts[0]), "b");      assertEquals(parts[1], "c");      assertTrue(!binding(parts[1])); -  }); +  })); -  it('should Parse Loan Binding', function() { +  it('should Parse Loan Binding', inject(function($rootScope) {      var parts = parseBindings("{{b}}");      assertEquals(parts.length, 1);      assertEquals(parts[0], "{{b}}");      assertEquals(binding(parts[0]), "b"); -  }); +  })); -  it('should Parse Two Bindings', function() { +  it('should Parse Two Bindings', inject(function($rootScope) {      var parts = parseBindings("{{b}}{{c}}");      assertEquals(parts.length, 2);      assertEquals(parts[0], "{{b}}");      assertEquals(binding(parts[0]), "b");      assertEquals(parts[1], "{{c}}");      assertEquals(binding(parts[1]), "c"); -  }); +  })); -  it('should Parse Two Bindings With Text In Middle', function() { +  it('should Parse Two Bindings With Text In Middle', inject(function($rootScope) {      var parts = parseBindings("{{b}}x{{c}}");      assertEquals(parts.length, 3);      assertEquals(parts[0], "{{b}}"); @@ -243,22 +239,22 @@ describe("markups", function() {      assertTrue(!binding(parts[1]));      assertEquals(parts[2], "{{c}}");      assertEquals(binding(parts[2]), "c"); -  }); +  })); -  it('should Parse Multiline', function() { +  it('should Parse Multiline', inject(function($rootScope) {      var parts = parseBindings('"X\nY{{A\nB}}C\nD"');      assertTrue(!!binding('{{A\nB}}'));      assertEquals(parts.length, 3);      assertEquals(parts[0], '"X\nY');      assertEquals(parts[1], '{{A\nB}}');      assertEquals(parts[2], 'C\nD"'); -  }); +  })); -  it('should Has Binding', function() { +  it('should Has Binding', inject(function($rootScope) {      assertTrue(hasBindings(parseBindings("{{a}}")));      assertTrue(!hasBindings(parseBindings("a")));      assertTrue(hasBindings(parseBindings("{{b}}x{{c}}"))); -  }); +  }));  }); diff --git a/test/scenario/SpecRunnerSpec.js b/test/scenario/SpecRunnerSpec.js index 92f000ba..4cffc63a 100644 --- a/test/scenario/SpecRunnerSpec.js +++ b/test/scenario/SpecRunnerSpec.js @@ -25,13 +25,13 @@ describe('angular.scenario.SpecRunner', function() {      };    } -  beforeEach(function() { +  beforeEach(inject(function($rootScope) {      log = [];      $window = {};      $window.setTimeout = function(fn, timeout) {        fn();      }; -    $root = angular.scope(); +    $root = $rootScope;      $root.emit = function(eventName) {        log.push(eventName);      }; @@ -41,7 +41,7 @@ describe('angular.scenario.SpecRunner', function() {      $root.application = new ApplicationMock($window);      $root.$window = $window;      runner = $root.$new(angular.scenario.SpecRunner); -  }); +  }));    it('should bind futures to the spec', function() {      runner.addFuture('test future', function(done) { diff --git a/test/scenario/dslSpec.js b/test/scenario/dslSpec.js index 32d7ebb6..6ecc386d 100644 --- a/test/scenario/dslSpec.js +++ b/test/scenario/dslSpec.js @@ -10,7 +10,7 @@ describe("angular.scenario.dsl", function() {        document: _jQuery("<div></div>"),        angular: new angular.scenario.testing.MockAngular()      }; -    $root = angular.scope(); +    $root = angular.injector()('$rootScope');      $root.emit = function(eventName) {        eventLog.push(eventName);      }; @@ -156,19 +156,17 @@ describe("angular.scenario.dsl", function() {      describe('location', function() {        beforeEach(function() { -        $window.angular.scope = function() { -          return { -            $service: function(serviceId) { -              if (serviceId == '$location') { -                return { -                  url: function() {return '/path?search=a#hhh';}, -                  path: function() {return '/path';}, -                  search: function() {return {search: 'a'};}, -                  hash: function() {return 'hhh';} -                }; -              } -              throw new Error('unknown service id ' + serviceId); +        $window.angular.injector = function() { +          return function(serviceId) { +            if (serviceId == '$location') { +              return { +                url: function() {return '/path?search=a#hhh';}, +                path: function() {return '/path';}, +                search: function() {return {search: 'a'};}, +                hash: function() {return 'hhh';} +              };              } +            throw new Error('unknown service id ' + serviceId);            };          };        }); diff --git a/test/service/cookieStoreSpec.js b/test/service/cookieStoreSpec.js index 0bf7e99d..50ac7797 100644 --- a/test/service/cookieStoreSpec.js +++ b/test/service/cookieStoreSpec.js @@ -1,41 +1,30 @@  'use strict';  describe('$cookieStore', function() { -  var scope, $browser, $cookieStore; -  beforeEach(function() { -    scope = angular.scope(); -    $cookieStore = scope.$service('$cookieStore'); -    $browser = scope.$service('$browser'); -  }); -  afterEach(function() { -    dealoc(scope); -  }); - - -  it('should serialize objects to json', function() { +  it('should serialize objects to json', inject(function($cookieStore, $browser, $rootScope) {      $cookieStore.put('objectCookie', {id: 123, name: 'blah'}); -    scope.$digest(); +    $rootScope.$digest();      expect($browser.cookies()).toEqual({'objectCookie': '{"id":123,"name":"blah"}'}); -  }); +  })); -  it('should deserialize json to object', function() { +  it('should deserialize json to object', inject(function($cookieStore, $browser) {      $browser.cookies('objectCookie', '{"id":123,"name":"blah"}');      $browser.poll();      expect($cookieStore.get('objectCookie')).toEqual({id: 123, name: 'blah'}); -  }); +  })); -  it('should delete objects from the store when remove is called', function() { +  it('should delete objects from the store when remove is called', inject(function($cookieStore, $browser, $rootScope) {      $cookieStore.put('gonner', { "I'll":"Be Back"}); -    scope.$digest(); //force eval in test +    $rootScope.$digest(); //force eval in test      $browser.poll();      expect($browser.cookies()).toEqual({'gonner': '{"I\'ll":"Be Back"}'});      $cookieStore.remove('gonner'); -    scope.$digest(); +    $rootScope.$digest();      expect($browser.cookies()).toEqual({}); -  }); +  }));  }); diff --git a/test/service/cookiesSpec.js b/test/service/cookiesSpec.js index f078c20c..2569645b 100644 --- a/test/service/cookiesSpec.js +++ b/test/service/cookiesSpec.js @@ -1,82 +1,79 @@  'use strict';  describe('$cookies', function() { -  var scope, $browser; - -  beforeEach(function() { -    $browser = new MockBrowser(); -    $browser.cookieHash['preexisting'] = 'oldCookie'; -    scope = angular.scope(angular.service, {$browser: $browser}); -    scope.$cookies = scope.$service('$cookies'); -  }); - -  afterEach(function() { -    dealoc(scope); -  }); - +  beforeEach(inject(function(service) { +    service('$browser', function(){  +      return angular.extend(new MockBrowser(), {cookieHash: {preexisting:'oldCookie'}});  +    }); +  })); +      it('should provide access to existing cookies via object properties and keep them in sync', -      function() { -    expect(scope.$cookies).toEqual({'preexisting': 'oldCookie'}); +      inject(function($cookies, $browser, $rootScope) { +    expect($cookies).toEqual({'preexisting': 'oldCookie'});      // access internal cookie storage of the browser mock directly to simulate behavior of      // document.cookie      $browser.cookieHash['brandNew'] = 'cookie';      $browser.poll(); -    expect(scope.$cookies).toEqual({'preexisting': 'oldCookie', 'brandNew':'cookie'}); +    expect($cookies).toEqual({'preexisting': 'oldCookie', 'brandNew':'cookie'});      $browser.cookieHash['brandNew'] = 'cookie2';      $browser.poll(); -    expect(scope.$cookies).toEqual({'preexisting': 'oldCookie', 'brandNew':'cookie2'}); +    expect($cookies).toEqual({'preexisting': 'oldCookie', 'brandNew':'cookie2'});      delete $browser.cookieHash['brandNew'];      $browser.poll(); -    expect(scope.$cookies).toEqual({'preexisting': 'oldCookie'}); -  }); +    expect($cookies).toEqual({'preexisting': 'oldCookie'}); +  })); -  it('should create or update a cookie when a value is assigned to a property', function() { -    scope.$cookies.oatmealCookie = 'nom nom'; -    scope.$digest(); +  it('should create or update a cookie when a value is assigned to a property', +      inject(function($cookies, $browser, $rootScope) { +    $cookies.oatmealCookie = 'nom nom'; +    $rootScope.$digest();      expect($browser.cookies()).        toEqual({'preexisting': 'oldCookie', 'oatmealCookie':'nom nom'}); -    scope.$cookies.oatmealCookie = 'gone'; -    scope.$digest(); +    $cookies.oatmealCookie = 'gone'; +    $rootScope.$digest();      expect($browser.cookies()).        toEqual({'preexisting': 'oldCookie', 'oatmealCookie': 'gone'}); -  }); +  })); -  it('should drop or reset any cookie that was set to a non-string value', function() { -    scope.$cookies.nonString = [1, 2, 3]; -    scope.$cookies.nullVal = null; -    scope.$cookies.undefVal = undefined; -    scope.$cookies.preexisting = function() {}; -    scope.$digest(); +  it('should drop or reset any cookie that was set to a non-string value', +      inject(function($cookies, $browser, $rootScope) { +    $cookies.nonString = [1, 2, 3]; +    $cookies.nullVal = null; +    $cookies.undefVal = undefined; +    $cookies.preexisting = function() {}; +    $rootScope.$digest();      expect($browser.cookies()).toEqual({'preexisting': 'oldCookie'}); -    expect(scope.$cookies).toEqual({'preexisting': 'oldCookie'}); -  }); +    expect($cookies).toEqual({'preexisting': 'oldCookie'}); +  })); -  it('should remove a cookie when a $cookies property is deleted', function() { -    scope.$cookies.oatmealCookie = 'nom nom'; -    scope.$digest(); +  it('should remove a cookie when a $cookies property is deleted', +      inject(function($cookies, $browser, $rootScope) { +    $cookies.oatmealCookie = 'nom nom'; +    $rootScope.$digest();      $browser.poll();      expect($browser.cookies()).        toEqual({'preexisting': 'oldCookie', 'oatmealCookie':'nom nom'}); -    delete scope.$cookies.oatmealCookie; -    scope.$digest(); +    delete $cookies.oatmealCookie; +    $rootScope.$digest();      expect($browser.cookies()).toEqual({'preexisting': 'oldCookie'}); -  }); +  })); -  it('should drop or reset cookies that browser refused to store', function() { +  it('should drop or reset cookies that browser refused to store', +      inject(function($cookies, $browser, $rootScope) {      var i, longVal;      for (i=0; i<5000; i++) { @@ -84,17 +81,17 @@ describe('$cookies', function() {      }      //drop if no previous value -    scope.$cookies.longCookie = longVal; -    scope.$digest(); -    expect(scope.$cookies).toEqual({'preexisting': 'oldCookie'}); +    $cookies.longCookie = longVal; +    $rootScope.$digest(); +    expect($cookies).toEqual({'preexisting': 'oldCookie'});      //reset if previous value existed -    scope.$cookies.longCookie = 'shortVal'; -    scope.$digest(); -    expect(scope.$cookies).toEqual({'preexisting': 'oldCookie', 'longCookie': 'shortVal'}); -    scope.$cookies.longCookie = longVal; -    scope.$digest(); -    expect(scope.$cookies).toEqual({'preexisting': 'oldCookie', 'longCookie': 'shortVal'}); -  }); +    $cookies.longCookie = 'shortVal'; +    $rootScope.$digest(); +    expect($cookies).toEqual({'preexisting': 'oldCookie', 'longCookie': 'shortVal'}); +    $cookies.longCookie = longVal; +    $rootScope.$digest(); +    expect($cookies).toEqual({'preexisting': 'oldCookie', 'longCookie': 'shortVal'}); +  }));  }); diff --git a/test/service/deferSpec.js b/test/service/deferSpec.js index ff48c93e..98ddeac5 100644 --- a/test/service/deferSpec.js +++ b/test/service/deferSpec.js @@ -1,22 +1,14 @@  'use strict';  describe('$defer', function() { -  var scope, $browser, $defer, $exceptionHandler; - -  beforeEach(function() { -    scope = angular.scope(angular.service, -                          {'$exceptionHandler': jasmine.createSpy('$exceptionHandler')}); -    $browser = scope.$service('$browser'); -    $defer = scope.$service('$defer'); -    $exceptionHandler = scope.$service('$exceptionHandler'); -  }); - -  afterEach(function() { -    dealoc(scope); -  }); +  beforeEach(inject(function(service) { +    service('$exceptionHandler', function(){ +      return jasmine.createSpy('$exceptionHandler'); +    }); +  })); -  it('should delegate functions to $browser.defer', function() { +  it('should delegate functions to $browser.defer', inject(function($defer, $browser, $exceptionHandler) {      var counter = 0;      $defer(function() { counter++; }); @@ -29,20 +21,20 @@ describe('$defer', function() {      expect(counter).toBe(1);      expect($exceptionHandler).not.toHaveBeenCalled(); -  }); +  })); -  it('should delegate exception to the $exceptionHandler service', function() { +  it('should delegate exception to the $exceptionHandler service', inject(function($defer, $browser, $exceptionHandler) {      $defer(function() {throw "Test Error";});      expect($exceptionHandler).not.toHaveBeenCalled();      $browser.defer.flush();      expect($exceptionHandler).toHaveBeenCalledWith("Test Error"); -  }); +  })); -  it('should call $apply after each callback is executed', function() { -    var applySpy = this.spyOn(scope, '$apply').andCallThrough(); +  it('should call $apply after each callback is executed', inject(function($defer, $browser, $rootScope) { +    var applySpy = this.spyOn($rootScope, '$apply').andCallThrough();      $defer(function() {});      expect(applySpy).not.toHaveBeenCalled(); @@ -56,36 +48,36 @@ describe('$defer', function() {      $defer(function() {});      $browser.defer.flush();      expect(applySpy.callCount).toBe(2); -  }); +  })); -  it('should call $apply even if an exception is thrown in callback', function() { -    var applySpy = this.spyOn(scope, '$apply').andCallThrough(); +  it('should call $apply even if an exception is thrown in callback', inject(function($defer, $browser, $rootScope) { +    var applySpy = this.spyOn($rootScope, '$apply').andCallThrough();      $defer(function() {throw "Test Error";});      expect(applySpy).not.toHaveBeenCalled();      $browser.defer.flush();      expect(applySpy).toHaveBeenCalled(); -  }); +  })); -  it('should allow you to specify the delay time', function() { +  it('should allow you to specify the delay time', inject(function($defer, $browser) {      var defer = this.spyOn($browser, 'defer');      $defer(noop, 123);      expect(defer.callCount).toEqual(1);      expect(defer.mostRecentCall.args[1]).toEqual(123); -  }); +  })); -  it('should return a cancelation token', function() { +  it('should return a cancelation token', inject(function($defer, $browser) {      var defer = this.spyOn($browser, 'defer').andReturn('xxx');      expect($defer(noop)).toEqual('xxx'); -  }); +  }));    describe('cancel', function() { -    it('should cancel tasks', function() { +    it('should cancel tasks', inject(function($defer, $browser) {        var task1 = jasmine.createSpy('task1'),            task2 = jasmine.createSpy('task2'),            task3 = jasmine.createSpy('task3'), @@ -102,10 +94,10 @@ describe('$defer', function() {        expect(task1).not.toHaveBeenCalled();        expect(task2).toHaveBeenCalledOnce();        expect(task3).not.toHaveBeenCalled(); -    }); +    })); -    it('should return true if a task was succesffuly canceled', function() { +    it('should return true if a task was succesffuly canceled', inject(function($defer, $browser) {        var task1 = jasmine.createSpy('task1'),            task2 = jasmine.createSpy('task2'),            token1, token2; @@ -116,6 +108,6 @@ describe('$defer', function() {        expect($defer.cancel(token1)).toBe(false);        expect($defer.cancel(token2)).toBe(true); -    }); +    }));    });  }); diff --git a/test/service/documentSpec.js b/test/service/documentSpec.js index 885331e4..064904a2 100644 --- a/test/service/documentSpec.js +++ b/test/service/documentSpec.js @@ -1,19 +1,9 @@  'use strict';  describe('$document', function() { -  var scope; -  beforeEach(function() { -    scope = angular.scope(); -  }); - -  afterEach(function() { -    dealoc(scope); -  }); - - -  it("should inject $document", function() { -    expect(scope.$service('$document')).toEqual(jqLite(document)); -  }); +  it("should inject $document", inject(function($document) { +    expect($document).toEqual(jqLite(document)); +  }));  }); diff --git a/test/service/exceptionHandlerSpec.js b/test/service/exceptionHandlerSpec.js index 61e652b5..3bfb70c0 100644 --- a/test/service/exceptionHandlerSpec.js +++ b/test/service/exceptionHandlerSpec.js @@ -1,26 +1,17 @@  'use strict';  describe('$exceptionHandler', function() { -  var scope; -  beforeEach(function() { -    scope = angular.scope(); -  }); - -  afterEach(function() { -    dealoc(scope); -  }); - - -  it('should log errors', function() { -    var scope = createScope({$exceptionHandler: $exceptionHandlerFactory}, -                            {$log: $logMock}), -        $log = scope.$service('$log'), -        $exceptionHandler = scope.$service('$exceptionHandler'); - -    $log.error.rethrow = false; -    $exceptionHandler('myError'); -    expect($log.error.logs.shift()).toEqual(['myError']); -  }); +  it('should log errors', inject( +    function(service){ +      service('$exceptionHandler', $exceptionHandlerFactory); +      service('$log', valueFn($logMock)); +    }, +    function($log, $exceptionHandler) { +      $log.error.rethrow = false; +      $exceptionHandler('myError'); +      expect($log.error.logs.shift()).toEqual(['myError']); +    } +  ));  }); diff --git a/test/service/formFactorySpec.js b/test/service/formFactorySpec.js index 23b8ae0a..fbe601c6 100644 --- a/test/service/formFactorySpec.js +++ b/test/service/formFactorySpec.js @@ -2,19 +2,10 @@  describe('$formFactory', function() { -  var rootScope; -  var formFactory; - -  beforeEach(function() { -    rootScope = angular.scope(); -    formFactory = rootScope.$service('$formFactory'); -  }); - - -  it('should have global form', function() { -    expect(formFactory.rootForm).toBeTruthy(); -    expect(formFactory.rootForm.$createWidget).toBeTruthy(); -  }); +  it('should have global form', inject(function($rootScope, $formFactory) { +    expect($formFactory.rootForm).toBeTruthy(); +    expect($formFactory.rootForm.$createWidget).toBeTruthy(); +  }));    describe('new form', function() { @@ -41,11 +32,11 @@ describe('$formFactory', function() {          }      }; -    beforeEach(function() { +    beforeEach(inject(function($rootScope, $formFactory) {        log = ''; -      scope = rootScope.$new(); -      form = formFactory(scope); -    }); +      scope = $rootScope.$new(); +      form = $formFactory(scope); +    }));      describe('$createWidget', function() {        var widget; @@ -61,14 +52,14 @@ describe('$formFactory', function() {        describe('data flow', function() { -        it('should have status properties', function() { +        it('should have status properties', inject(function($rootScope, $formFactory) {            expect(widget.$error).toEqual({});            expect(widget.$valid).toBe(true);            expect(widget.$invalid).toBe(false); -        }); +        })); -        it('should update view when model changes', function() { +        it('should update view when model changes', inject(function($rootScope, $formFactory) {            scope.text = 'abc';            scope.$digest();            expect(log).toEqual('<init>$validate();$render();'); @@ -78,17 +69,17 @@ describe('$formFactory', function() {            scope.$digest();            expect(widget.$modelValue).toEqual('xyz'); -        }); +        })); -        it('should have controller prototype methods', function() { -          expect(widget.getFormFactory()).toEqual(formFactory); -        }); +        it('should have controller prototype methods', inject(function($rootScope, $formFactory) { +          expect(widget.getFormFactory()).toEqual($formFactory); +        }));        });        describe('validation', function() { -        it('should update state on error', function() { +        it('should update state on error', inject(function($rootScope, $formFactory) {            widget.$emit('$invalid', 'E');            expect(widget.$valid).toEqual(false);            expect(widget.$invalid).toEqual(true); @@ -96,21 +87,21 @@ describe('$formFactory', function() {            widget.$emit('$valid', 'E');            expect(widget.$valid).toEqual(true);            expect(widget.$invalid).toEqual(false); -        }); +        })); -        it('should have called the model setter before the validation', function() { +        it('should have called the model setter before the validation', inject(function($rootScope, $formFactory) {            var modelValue;            widget.$on('$validate', function() {              modelValue = scope.text;            });            widget.$emit('$viewChange', 'abc');            expect(modelValue).toEqual('abc'); -        }); +        }));          describe('form', function() { -          it('should invalidate form when widget is invalid', function() { +          it('should invalidate form when widget is invalid', inject(function($rootScope, $formFactory) {              expect(form.$error).toEqual({});              expect(form.$valid).toEqual(true);              expect(form.$invalid).toEqual(false); @@ -143,18 +134,18 @@ describe('$formFactory', function() {              expect(form.$error).toEqual({});              expect(form.$valid).toEqual(true);              expect(form.$invalid).toEqual(false); -          }); +          }));          });        });        describe('id assignment', function() { -        it('should default to name expression', function() { +        it('should default to name expression', inject(function($rootScope, $formFactory) {            expect(form.text).toEqual(widget); -        }); +        })); -        it('should use ng:id', function() { +        it('should use ng:id', inject(function($rootScope, $formFactory) {            widget = form.$createWidget({              scope:scope,              model:'text', @@ -162,10 +153,10 @@ describe('$formFactory', function() {              controller:WidgetCtrl            });            expect(form['my.id']).toEqual(widget); -        }); +        })); -        it('should not override existing names', function() { +        it('should not override existing names', inject(function($rootScope, $formFactory) {            var widget2 = form.$createWidget({              scope:scope,              model:'text', @@ -174,11 +165,11 @@ describe('$formFactory', function() {            });            expect(form.text).toEqual(widget);            expect(widget2).not.toEqual(widget); -        }); +        }));        });        describe('dealocation', function() { -        it('should dealocate', function() { +        it('should dealocate', inject(function($rootScope, $formFactory) {            var widget2 = form.$createWidget({              scope:scope,              model:'text', @@ -199,10 +190,10 @@ describe('$formFactory', function() {            widget2.$destroy();            expect(form.myId).toBeUndefined(); -        }); +        })); -        it('should remove invalid fields from errors, when child widget removed', function() { +        it('should remove invalid fields from errors, when child widget removed', inject(function($rootScope, $formFactory) {            widget.$emit('$invalid', 'MyError');            expect(form.$error.MyError).toEqual([widget]); @@ -212,7 +203,7 @@ describe('$formFactory', function() {            expect(form.$error.MyError).toBeUndefined();            expect(form.$invalid).toEqual(false); -        }); +        }));        });      });    }); diff --git a/test/service/locationSpec.js b/test/service/locationSpec.js index 9a7aa943..38df2619 100644 --- a/test/service/locationSpec.js +++ b/test/service/locationSpec.js @@ -307,164 +307,187 @@ describe('$location', function() {    }); -  var $browser, $location, scope; - -  function init(url, html5Mode, basePath, hashPrefix, supportHistory) { -    scope = angular.scope(null, { -      $locationConfig: {html5Mode: html5Mode, hashPrefix: hashPrefix}, -      $sniffer: {history: supportHistory}}); - -    $browser = scope.$service('$browser'); -    $browser.url(url); -    $browser.$$baseHref = basePath; -    $location = scope.$service('$location'); +  function initService(html5Mode, hashPrefix, supportHistory) { +    return function(service){ +      service('$locationConfig', function(){ +        return {html5Mode: html5Mode, hashPrefix: hashPrefix}; +      }); +      service('$sniffer', function(){ +        return {history: supportHistory}; +      }); +    };    } - -  function dealocRootElement() { -    dealoc(scope.$service('$document')); +  function initBrowser(url, basePath) { +    return function($browser){ +      $browser.url(url); +      $browser.$$baseHref = basePath; +    };    } -    describe('wiring', function() { -    beforeEach(function() { -      init('http://new.com/a/b#!', false, '/a/b', '!', true); -    }); +    beforeEach(inject(initService(false, '!', true), initBrowser('http://new.com/a/b#!', '/a/b'))); -    it('should update $location when browser url changes', function() { +    it('should update $location when browser url changes', inject(function($browser, $location) {        spyOn($location, '$$parse').andCallThrough();        $browser.url('http://new.com/a/b#!/aaa');        $browser.poll();        expect($location.absUrl()).toBe('http://new.com/a/b#!/aaa');        expect($location.path()).toBe('/aaa');        expect($location.$$parse).toHaveBeenCalledOnce(); -    }); +    })); -    it('should update browser when $location changes', function() { +    it('should update browser when $location changes', inject(function($rootScope, $browser, $location) {        var $browserUrl = spyOnlyCallsWithArgs($browser, 'url').andCallThrough();        $location.path('/new/path');        expect($browserUrl).not.toHaveBeenCalled(); -      scope.$apply(); +      $rootScope.$apply();        expect($browserUrl).toHaveBeenCalledOnce();        expect($browser.url()).toBe('http://new.com/a/b#!/new/path'); -    }); +    })); -    it('should update browser only once per $apply cycle', function() { +    it('should update browser only once per $apply cycle', inject(function($rootScope, $browser, $location) {        var $browserUrl = spyOnlyCallsWithArgs($browser, 'url').andCallThrough();        $location.path('/new/path'); -      scope.$watch(function() { +      $rootScope.$watch(function() {          $location.search('a=b');        }); -      scope.$apply(); +      $rootScope.$apply();        expect($browserUrl).toHaveBeenCalledOnce();        expect($browser.url()).toBe('http://new.com/a/b#!/new/path?a=b'); -    }); +    })); -    it('should replace browser url when url was replaced at least once', function() { +    it('should replace browser url when url was replaced at least once', +        inject(function($rootScope, $location, $browser) {        var $browserUrl = spyOnlyCallsWithArgs($browser, 'url').andCallThrough();        $location.path('/n/url').replace(); -      scope.$apply(); +      $rootScope.$apply();        expect($browserUrl).toHaveBeenCalledOnce();        expect($browserUrl.mostRecentCall.args).toEqual(['http://new.com/a/b#!/n/url', true]); -    }); +    })); -    it('should update the browser if changed from within a watcher', function() { -      scope.$watch(function() { return true; }, function() { +    it('should update the browser if changed from within a watcher', inject(function($rootScope, $location, $browser) { +      $rootScope.$watch(function() { return true; }, function() {          $location.path('/changed');        }); -      scope.$digest(); +      $rootScope.$digest();        expect($browser.url()).toBe('http://new.com/a/b#!/changed'); -    }); +    }));    });    // html5 history is disabled    describe('disabled history', function() { -    it('should use hashbang url with hash prefix', function() { -      init('http://domain.com/base/index.html#!/a/b', false, '/base/index.html', '!'); -      expect($browser.url()).toBe('http://domain.com/base/index.html#!/a/b'); -      $location.path('/new'); -      $location.search({a: true}); -      scope.$apply(); -      expect($browser.url()).toBe('http://domain.com/base/index.html#!/new?a'); -    }); - - -    it('should use hashbang url without hash prefix', function() { -      init('http://domain.com/base/index.html#/a/b', false, '/base/index.html', ''); -      expect($browser.url()).toBe('http://domain.com/base/index.html#/a/b'); -      $location.path('/new'); -      $location.search({a: true}); -      scope.$apply(); -      expect($browser.url()).toBe('http://domain.com/base/index.html#/new?a'); -    }); +    it('should use hashbang url with hash prefix', inject( +      initService(false, '!'), +      initBrowser('http://domain.com/base/index.html#!/a/b', '/base/index.html'), +      function($rootScope, $location, $browser) { +        expect($browser.url()).toBe('http://domain.com/base/index.html#!/a/b'); +        $location.path('/new'); +        $location.search({a: true}); +        $rootScope.$apply(); +        expect($browser.url()).toBe('http://domain.com/base/index.html#!/new?a'); +      }) +    ); + + +    it('should use hashbang url without hash prefix', inject( +      initService(false, ''), +      initBrowser('http://domain.com/base/index.html#/a/b', '/base/index.html'), +      function($rootScope, $location, $browser) { +        expect($browser.url()).toBe('http://domain.com/base/index.html#/a/b'); +        $location.path('/new'); +        $location.search({a: true}); +        $rootScope.$apply(); +        expect($browser.url()).toBe('http://domain.com/base/index.html#/new?a'); +      }) +    );    });    // html5 history enabled, but not supported by browser    describe('history on old browser', function() { -    afterEach(dealocRootElement); - -    it('should use hashbang url with hash prefix', function() { -      init('http://domain.com/base/index.html#!!/a/b', true, '/base/index.html', '!!', false); -      expect($browser.url()).toBe('http://domain.com/base/index.html#!!/a/b'); -      $location.path('/new'); -      $location.search({a: true}); -      scope.$apply(); -      expect($browser.url()).toBe('http://domain.com/base/index.html#!!/new?a'); -    }); - - -    it('should redirect to hashbang url when new url given', function() { -      init('http://domain.com/base/new-path/index.html', true, '/base/index.html', '!'); -      expect($browser.url()).toBe('http://domain.com/base/index.html#!/new-path/index.html'); -    }); +    afterEach(inject(function($document){ +      dealoc($document); +    })); + +    it('should use hashbang url with hash prefix', inject( +      initService(true, '!!', false), +      initBrowser('http://domain.com/base/index.html#!!/a/b', '/base/index.html'), +      function($rootScope, $location,  $browser) { +        expect($browser.url()).toBe('http://domain.com/base/index.html#!!/a/b'); +        $location.path('/new'); +        $location.search({a: true}); +        $rootScope.$apply(); +        expect($browser.url()).toBe('http://domain.com/base/index.html#!!/new?a'); +      }) +    ); + + +    it('should redirect to hashbang url when new url given', inject( +      initService(true, '!'), +      initBrowser('http://domain.com/base/new-path/index.html', '/base/index.html'), +      function($browser, $location) { +        expect($browser.url()).toBe('http://domain.com/base/index.html#!/new-path/index.html'); +      }) +    );    });    // html5 history enabled and supported by browser    describe('history on new browser', function() { -    afterEach(dealocRootElement); - -    it('should use new url', function() { -      init('http://domain.com/base/old/index.html#a', true, '/base/index.html', '', true); -      expect($browser.url()).toBe('http://domain.com/base/old/index.html#a'); -      $location.path('/new'); -      $location.search({a: true}); -      scope.$apply(); -      expect($browser.url()).toBe('http://domain.com/base/new?a#a'); -    }); - - -    it('should rewrite when hashbang url given', function() { -      init('http://domain.com/base/index.html#!/a/b', true, '/base/index.html', '!', true); -      expect($browser.url()).toBe('http://domain.com/base/a/b'); -      $location.path('/new'); -      $location.hash('abc'); -      scope.$apply(); -      expect($browser.url()).toBe('http://domain.com/base/new#abc'); -      expect($location.path()).toBe('/new'); -    }); - - -    it('should rewrite when hashbang url given (without hash prefix)', function() { -      init('http://domain.com/base/index.html#/a/b', true, '/base/index.html', '', true); -      expect($browser.url()).toBe('http://domain.com/base/a/b'); -      expect($location.path()).toBe('/a/b'); -    }); +    afterEach(inject(function($document){ +      dealoc($document); +    })); + +    it('should use new url', inject( +      initService(true, '', true), +      initBrowser('http://domain.com/base/old/index.html#a', '/base/index.html'), +      function($rootScope, $location, $browser) { +        expect($browser.url()).toBe('http://domain.com/base/old/index.html#a'); +        $location.path('/new'); +        $location.search({a: true}); +        $rootScope.$apply(); +        expect($browser.url()).toBe('http://domain.com/base/new?a#a'); +      }) +    ); + + +    it('should rewrite when hashbang url given', inject( +      initService(true, '!', true), +      initBrowser('http://domain.com/base/index.html#!/a/b', '/base/index.html'), +      function($rootScope, $location, $browser) { +        expect($browser.url()).toBe('http://domain.com/base/a/b'); +        $location.path('/new'); +        $location.hash('abc'); +        $rootScope.$apply(); +        expect($browser.url()).toBe('http://domain.com/base/new#abc'); +        expect($location.path()).toBe('/new'); +      }) +    ); + + +    it('should rewrite when hashbang url given (without hash prefix)', inject( +      initService(true, '', true), +      initBrowser('http://domain.com/base/index.html#/a/b', '/base/index.html'), +      function($rootScope, $location, $browser) { +        expect($browser.url()).toBe('http://domain.com/base/a/b'); +        expect($location.path()).toBe('/a/b'); +      }) +    );    }); @@ -554,43 +577,48 @@ describe('$location', function() {    describe('link rewriting', function() { -    var root, link, extLink, $browser, originalBrowser, lastEventPreventDefault; +    var root, link, originalBrowser, lastEventPreventDefault; -    function init(linkHref, html5Mode, supportHist, attrs, content) { -      var jqRoot = jqLite('<div></div>'); -      attrs = attrs ? ' ' + attrs + ' ' : ''; -      content = content || 'link'; -      link = jqLite('<a href="' + linkHref + '"' + attrs + '>' + content + '</a>')[0]; -      root = jqRoot.append(link)[0]; +    function configureService(linkHref, html5Mode, supportHist, attrs, content) { +      return function(service){ +        var jqRoot = jqLite('<div></div>'); +        attrs = attrs ? ' ' + attrs + ' ' : ''; +        link = jqLite('<a href="' + linkHref + '"' + attrs + '>' + content + '</a>')[0]; +        root = jqRoot.append(link)[0]; -      jqLite(document.body).append(jqRoot); +        jqLite(document.body).append(jqRoot); -      var scope = angular.scope(null, { -        $document: jqRoot, -        $sniffer: {history: supportHist}, -        $locationConfig: {html5Mode: html5Mode, hashPrefix: '!'} -      }); +        service('$document', function(){ return jqRoot; }); +        service('$sniffer', function(){ return {history: supportHist}; }); +        service('$locationConfig', function(){ return {html5Mode: html5Mode, hashPrefix: '!'}; }); +      }; +    } -      $browser = scope.$service('$browser'); -      $browser.url('http://host.com/base'); -      $browser.$$baseHref = '/base/index.html'; -      var $location = scope.$service('$location'); -      originalBrowser = $browser.url(); - -      // we have to prevent the default operation, as we need to test absolute links (http://...) -      // and navigating to these links would kill jstd -      jqRoot.bind('click', function(e) { -        lastEventPreventDefault = e.isDefaultPrevented(); -        e.preventDefault(); -      }); +    function initBrowser() { +      return function($browser){ +        $browser.url('http://host.com/base'); +        $browser.$$baseHref = '/base/index.html'; +      }; +    } + +    function initLocation() { +      return function($browser, $location, $document) { +        originalBrowser = $browser.url(); +        // we have to prevent the default operation, as we need to test absolute links (http://...) +        // and navigating to these links would kill jstd +        $document.bind('click', function(e) { +          lastEventPreventDefault = e.isDefaultPrevented(); +          e.preventDefault(); +        }); +      };      } -    function expectRewriteTo(url) { +    function expectRewriteTo($browser, url) {        expect(lastEventPreventDefault).toBe(true);        expect($browser.url()).toBe(url);      } -    function expectNoRewrite() { +    function expectNoRewrite($browser) {        expect(lastEventPreventDefault).toBe(false);        expect($browser.url()).toBe(originalBrowser);      } @@ -601,100 +629,152 @@ describe('$location', function() {      }); -    it('should rewrite rel link to new url when history enabled on new browser', function() { -      init('link?a#b', true, true); -      browserTrigger(link, 'click'); -      expectRewriteTo('http://host.com/base/link?a#b'); -    }); - - -    it('should rewrite abs link to new url when history enabled on new browser', function() { -      init('/base/link?a#b', true, true); -      browserTrigger(link, 'click'); -      expectRewriteTo('http://host.com/base/link?a#b'); -    }); - - -    it('should rewrite rel link to hashbang url when history enabled on old browser', function() { -      init('link?a#b', true, false); -      browserTrigger(link, 'click'); -      expectRewriteTo('http://host.com/base/index.html#!/link?a#b'); -    }); - - -    it('should rewrite abs link to hashbang url when history enabled on old browser', function() { -      init('/base/link?a#b', true, false); -      browserTrigger(link, 'click'); -      expectRewriteTo('http://host.com/base/index.html#!/link?a#b'); -    }); - - -    it('should not rewrite when history disabled', function() { -      init('#new', false); -      browserTrigger(link, 'click'); -      expectNoRewrite(); -    }); - - -    it('should not rewrite ng:ext-link', function() { -      init('#new', true, true, 'ng:ext-link'); -      browserTrigger(link, 'click'); -      expectNoRewrite(); -    }); - - -    it('should not rewrite full url links do different domain', function() { -      init('http://www.dot.abc/a?b=c', true); -      browserTrigger(link, 'click'); -      expectNoRewrite(); -    }); - - -    it('should not rewrite links with target="_blank"', function() { -      init('/a?b=c', true, true, 'target="_blank"'); -      browserTrigger(link, 'click'); -      expectNoRewrite(); -    }); - - -    it('should not rewrite links with target specified', function() { -      init('/a?b=c', true, true, 'target="some-frame"'); -      browserTrigger(link, 'click'); -      expectNoRewrite(); -    }); - - -    it('should rewrite full url links to same domain and base path', function() { -      init('http://host.com/base/new', true); -      browserTrigger(link, 'click'); -      expectRewriteTo('http://host.com/base/index.html#!/new'); -    }); - - -    it('should rewrite when clicked span inside link', function() { -      init('some/link', true, true, '', '<span>link</span>'); -      var span = jqLite(link).find('span'); - -      browserTrigger(span, 'click'); -      expectRewriteTo('http://host.com/base/some/link'); -    }); +    it('should rewrite rel link to new url when history enabled on new browser', inject( +      configureService('link?a#b', true, true), +      initBrowser(), +      initLocation(), +      function($browser) { +        browserTrigger(link, 'click'); +        expectRewriteTo($browser, 'http://host.com/base/link?a#b'); +      }) +    ); + + +    it('should rewrite abs link to new url when history enabled on new browser', inject( +      configureService('/base/link?a#b', true, true), +      initBrowser(), +      initLocation(), +      function($browser) { +        browserTrigger(link, 'click'); +        expectRewriteTo($browser, 'http://host.com/base/link?a#b'); +      }) +    ); + + +    it('should rewrite rel link to hashbang url when history enabled on old browser', inject( +      configureService('link?a#b', true, false), +      initBrowser(), +      initLocation(), +      function($browser) { +        browserTrigger(link, 'click'); +        expectRewriteTo($browser, 'http://host.com/base/index.html#!/link?a#b'); +      }) +    ); + + +    it('should rewrite abs link to hashbang url when history enabled on old browser', inject( +      configureService('/base/link?a#b', true, false), +      initBrowser(), +      initLocation(), +      function($browser) { +        browserTrigger(link, 'click'); +        expectRewriteTo($browser, 'http://host.com/base/index.html#!/link?a#b'); +      }) +    ); + + +    it('should not rewrite when history disabled', inject( +      configureService('#new', false), +      initBrowser(), +      initLocation(), +      function($browser) { +        browserTrigger(link, 'click'); +        expectNoRewrite($browser); +      }) +    ); + + +    it('should not rewrite ng:ext-link', inject( +      configureService('#new', true, true, 'ng:ext-link'), +      initBrowser(), +      initLocation(), +      function($browser) { +        browserTrigger(link, 'click'); +        expectNoRewrite($browser); +      }) +    ); + + +    it('should not rewrite full url links do different domain', inject( +      configureService('http://www.dot.abc/a?b=c', true), +      initBrowser(), +      initLocation(), +      function($browser) { +        browserTrigger(link, 'click'); +        expectNoRewrite($browser); +      }) +    ); + + +    it('should not rewrite links with target="_blank"', inject( +      configureService('/a?b=c', true, true, 'target="_blank"'), +      initBrowser(), +      initLocation(), +      function($browser) { +        browserTrigger(link, 'click'); +        expectNoRewrite($browser); +      }) +    ); + + +    it('should not rewrite links with target specified', inject( +      configureService('/a?b=c', true, true, 'target="some-frame"'), +      initBrowser(), +      initLocation(), +      function($browser) { +        browserTrigger(link, 'click'); +        expectNoRewrite($browser); +      }) +    ); + + +    it('should rewrite full url links to same domain and base path', inject( +      configureService('http://host.com/base/new', true), +      initBrowser(), +      initLocation(), +      function($browser) { +        browserTrigger(link, 'click'); +        expectRewriteTo($browser, 'http://host.com/base/index.html#!/new'); +      }) +    ); + + +    it('should rewrite when clicked span inside link', inject( +      configureService('some/link', true, true, '', '<span>link</span>'), +      initBrowser(), +      initLocation(), +      function($browser) { +        var span = jqLite(link).find('span'); + +        browserTrigger(span, 'click'); +        expectRewriteTo($browser, 'http://host.com/base/some/link'); +      }) +    );      // don't run next tests on IE<9, as browserTrigger does not simulate pressed keys      if (!(msie < 9)) { -      it('should not rewrite when clicked with ctrl pressed', function() { -        init('/a?b=c', true, true); -        browserTrigger(link, 'click', ['ctrl']); -        expectNoRewrite(); -      }); - - -      it('should not rewrite when clicked with meta pressed', function() { -        init('/a?b=c', true, true); -        browserTrigger(link, 'click', ['meta']); -        expectNoRewrite(); -      }); +      it('should not rewrite when clicked with ctrl pressed', inject( +        configureService('/a?b=c', true, true), +        initBrowser(), +        initLocation(), +        function($browser) { +          browserTrigger(link, 'click', ['ctrl']); +          expectNoRewrite($browser); +        }) +      ); + + +      it('should not rewrite when clicked with meta pressed', inject( +        configureService('/a?b=c', true, true), +        initBrowser(), +        initLocation(), +        function($browser) { +          browserTrigger(link, 'click', ['meta']); +          expectNoRewrite($browser); +        }) +      );      }    });  }); diff --git a/test/service/logSpec.js b/test/service/logSpec.js index c4efb8c5..8c56d99e 100644 --- a/test/service/logSpec.js +++ b/test/service/logSpec.js @@ -1,65 +1,61 @@  'use strict';  describe('$log', function() { -  var scope; - -  beforeEach(function() { -    scope = angular.scope(); -  }); - - -  afterEach(function() { -    dealoc(scope); -  }); - - -  it('should use console if present', function() { -    var logger = ""; -    function log() { logger+= 'log;'; } -    function warn() { logger+= 'warn;'; } -    function info() { logger+= 'info;'; } -    function error() { logger+= 'error;'; } -    var scope = createScope({$log: $logFactory}, -                            {$exceptionHandler: rethrow, -                             $window: {console: {log: log, -                                                 warn: warn, -                                                 info: info, -                                                 error: error}}}), -        $log = scope.$service('$log'); - -    $log.log(); -    $log.warn(); -    $log.info(); -    $log.error(); -    expect(logger).toEqual('log;warn;info;error;'); -  }); - - -  it('should use console.log() if other not present', function() { -    var logger = ""; -    function log() { logger+= 'log;'; } -    var scope = createScope({$log: $logFactory}, -                            {$window: {console:{log:log}}, -                             $exceptionHandler: rethrow}); -    var $log = scope.$service('$log'); -    $log.log(); -    $log.warn(); -    $log.info(); -    $log.error(); -    expect(logger).toEqual('log;log;log;log;'); -  }); - - -  it('should use noop if no console', function() { -    var scope = createScope({$log: $logFactory}, -                            {$window: {}, -                             $exceptionHandler: rethrow}), -        $log = scope.$service('$log'); -    $log.log(); -    $log.warn(); -    $log.info(); -    $log.error(); -  }); +  var $window; +  var logger; + +  function log() { logger+= 'log;'; } +  function warn() { logger+= 'warn;'; } +  function info() { logger+= 'info;'; } +  function error() { logger+= 'error;'; } + +  beforeEach(inject(function(service){ +    $window = {}; +    logger = ''; +    service('$log', $logFactory); +    service('$exceptionHandler', valueFn(rethrow)); +    service('$window', valueFn($window)); +  })); + +  it('should use console if present', inject( +    function(){ +      $window.console = {log: log, +                         warn: warn, +                         info: info, +                         error: error}; +    }, +    function($log) { +      $log.log(); +      $log.warn(); +      $log.info(); +      $log.error(); +      expect(logger).toEqual('log;warn;info;error;'); +    } +  )); + + +  it('should use console.log() if other not present', inject( +    function(){ +      $window.console = {log: log}; +    }, +    function($log) { +      $log.log(); +      $log.warn(); +      $log.info(); +      $log.error(); +      expect(logger).toEqual('log;log;log;log;'); +    } +  )); + + +  it('should use noop if no console', inject( +    function($log) { +      $log.log(); +      $log.warn(); +      $log.info(); +      $log.error(); +    } +  ));    describe('$log.error', function() { diff --git a/test/service/routeParamsSpec.js b/test/service/routeParamsSpec.js index 972e4314..d4088767 100644 --- a/test/service/routeParamsSpec.js +++ b/test/service/routeParamsSpec.js @@ -1,41 +1,16 @@  'use strict';  describe('$routeParams', function() { -  it('should publish the params into a service', function() { -    var scope = angular.scope(), -        $location = scope.$service('$location'), -        $route = scope.$service('$route'), -        $routeParams = scope.$service('$routeParams'); - +  it('should publish the params into a service', inject(function($rootScope, $route, $location, $routeParams) {      $route.when('/foo');      $route.when('/bar/:barId');      $location.path('/foo').search('a=b'); -    scope.$digest(); +    $rootScope.$digest();      expect($routeParams).toEqual({a:'b'});      $location.path('/bar/123').search('x=abc'); -    scope.$digest(); +    $rootScope.$digest();      expect($routeParams).toEqual({barId:'123', x:'abc'}); -  }); - - -  it('should preserve object identity during route reloads', function() { -    var scope = angular.scope(), -        $location = scope.$service('$location'), -        $route = scope.$service('$route'), -        $routeParams = scope.$service('$routeParams'), -        firstRouteParams = $routeParams; - -    $route.when('/foo'); -    $route.when('/bar/:barId'); - -    $location.path('/foo').search('a=b'); -    scope.$digest(); -    expect(scope.$service('$routeParams')).toBe(firstRouteParams); - -    $location.path('/bar/123').search('x=abc'); -    scope.$digest(); -    expect(scope.$service('$routeParams')).toBe(firstRouteParams); -  }); +  }));  }); diff --git a/test/service/routeSpec.js b/test/service/routeSpec.js index 5aba2a1f..26ae17e5 100644 --- a/test/service/routeSpec.js +++ b/test/service/routeSpec.js @@ -1,16 +1,7 @@  'use strict';  describe('$route', function() { -  var scope, $route, $location; - -  beforeEach(function() { -    scope = angular.scope(); -    $location = scope.$service('$location'); -    $route = scope.$service('$route'); -  }); - - -  it('should route and fire change event', function() { +  it('should route and fire change event', inject(function($route, $location, $rootScope) {      var log = '',          lastRoute,          nextRoute; @@ -21,13 +12,13 @@ describe('$route', function() {      $route.when('/Book/:book/Chapter/:chapter', {controller: BookChapter, template: 'Chapter.html'});      $route.when('/Blank'); -    scope.$on('$beforeRouteChange', function(event, next, current) { +    $rootScope.$on('$beforeRouteChange', function(event, next, current) {        log += 'before();';        expect(current).toBe($route.current);        lastRoute = current;        nextRoute = next;      }); -    scope.$on('$afterRouteChange', function(event, current, last) { +    $rootScope.$on('$afterRouteChange', function(event, current, last) {        log += 'after();';        expect(current).toBe($route.current);        expect(lastRoute).toBe(last); @@ -35,97 +26,98 @@ describe('$route', function() {      });      $location.path('/Book/Moby/Chapter/Intro').search('p=123'); -    scope.$digest(); +    $rootScope.$digest();      expect(log).toEqual('before();<init>;after();');      expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', p:'123'});      var lastId = $route.current.scope.$id;      log = '';      $location.path('/Blank').search('ignore'); -    scope.$digest(); +    $rootScope.$digest();      expect(log).toEqual('before();after();');      expect($route.current.params).toEqual({ignore:true});      expect($route.current.scope.$id).not.toEqual(lastId);      log = '';      $location.path('/NONE'); -    scope.$digest(); +    $rootScope.$digest();      expect(log).toEqual('before();after();');      expect($route.current).toEqual(null);      $route.when('/NONE', {template:'instant update'}); -    scope.$digest(); +    $rootScope.$digest();      expect($route.current.template).toEqual('instant update'); -  }); +  })); -  it('should match a route that contains special chars in the path', function() { +  it('should match a route that contains special chars in the path', inject(function($route, $location, $rootScope) {      $route.when('/$test.23/foo(bar)/:baz', {template: 'test.html'});      $location.path('/test'); -    scope.$digest(); +    $rootScope.$digest();      expect($route.current).toBeUndefined();      $location.path('/$testX23/foo(bar)/222'); -    scope.$digest(); +    $rootScope.$digest();      expect($route.current).toBeUndefined();      $location.path('/$test.23/foo(bar)/222'); -    scope.$digest(); +    $rootScope.$digest();      expect($route.current).toBeDefined();      $location.path('/$test.23/foo\\(bar)/222'); -    scope.$digest(); +    $rootScope.$digest();      expect($route.current).toBeUndefined(); -  }); +  })); -  it('should change route even when only search param changes', function() { +  it('should change route even when only search param changes', inject(function($route, $location, $rootScope) {      var callback = jasmine.createSpy('onRouteChange');      $route.when('/test', {template: 'test.html'}); -    scope.$on('$beforeRouteChange', callback); +    $rootScope.$on('$beforeRouteChange', callback);      $location.path('/test'); -    scope.$digest(); +    $rootScope.$digest();      callback.reset();      $location.search({any: true}); -    scope.$digest(); +    $rootScope.$digest();      expect(callback).toHaveBeenCalled(); -  }); +  })); -  it('should allow routes to be defined with just templates without controllers', function() { +  it('should allow routes to be defined with just templates without controllers', +      inject(function($route, $location, $rootScope) {      var onChangeSpy = jasmine.createSpy('onChange');      $route.when('/foo', {template: 'foo.html'}); -    scope.$on('$beforeRouteChange', onChangeSpy); +    $rootScope.$on('$beforeRouteChange', onChangeSpy);      expect($route.current).toBeUndefined();      expect(onChangeSpy).not.toHaveBeenCalled();      $location.path('/foo'); -    scope.$digest(); +    $rootScope.$digest();      expect($route.current.template).toEqual('foo.html');      expect($route.current.controller).toBeUndefined();      expect(onChangeSpy).toHaveBeenCalled(); -  }); +  })); -  it('should handle unknown routes with "otherwise" route definition', function() { +  it('should handle unknown routes with "otherwise" route definition', inject(function($route, $location, $rootScope) {      var onChangeSpy = jasmine.createSpy('onChange');      function NotFoundCtrl() {this.notFoundProp = 'not found!';}      $route.when('/foo', {template: 'foo.html'});      $route.otherwise({template: '404.html', controller: NotFoundCtrl}); -    scope.$on('$beforeRouteChange', onChangeSpy); +    $rootScope.$on('$beforeRouteChange', onChangeSpy);      expect($route.current).toBeUndefined();      expect(onChangeSpy).not.toHaveBeenCalled();      $location.path('/unknownRoute'); -    scope.$digest(); +    $rootScope.$digest();      expect($route.current.template).toBe('404.html');      expect($route.current.controller).toBe(NotFoundCtrl); @@ -134,54 +126,55 @@ describe('$route', function() {      onChangeSpy.reset();      $location.path('/foo'); -    scope.$digest(); +    $rootScope.$digest();      expect($route.current.template).toEqual('foo.html');      expect($route.current.controller).toBeUndefined();      expect($route.current.scope.notFoundProp).toBeUndefined();      expect(onChangeSpy).toHaveBeenCalled(); -  }); +  })); -  it('should $destroy old routes', function() { +  it('should $destroy old routes', inject(function($route, $location, $rootScope) {      $route.when('/foo', {template: 'foo.html', controller: function() {this.name = 'FOO';}});      $route.when('/bar', {template: 'bar.html', controller: function() {this.name = 'BAR';}});      $route.when('/baz', {template: 'baz.html'}); -    expect(scope.$childHead).toEqual(null); +    expect($rootScope.$childHead).toEqual(null);      $location.path('/foo'); -    scope.$digest(); -    expect(scope.$$childHead.$id).toBeTruthy(); -    expect(scope.$$childHead.$id).toEqual(scope.$$childTail.$id); +    $rootScope.$digest(); +    expect($rootScope.$$childHead.$id).toBeTruthy(); +    expect($rootScope.$$childHead.$id).toEqual($rootScope.$$childTail.$id);      $location.path('/bar'); -    scope.$digest(); -    expect(scope.$$childHead.$id).toBeTruthy(); -    expect(scope.$$childHead.$id).toEqual(scope.$$childTail.$id); +    $rootScope.$digest(); +    expect($rootScope.$$childHead.$id).toBeTruthy(); +    expect($rootScope.$$childHead.$id).toEqual($rootScope.$$childTail.$id);      $location.path('/baz'); -    scope.$digest(); -    expect(scope.$$childHead.$id).toBeTruthy(); -    expect(scope.$$childHead.$id).toEqual(scope.$$childTail.$id); +    $rootScope.$digest(); +    expect($rootScope.$$childHead.$id).toBeTruthy(); +    expect($rootScope.$$childHead.$id).toEqual($rootScope.$$childTail.$id);      $location.path('/'); -    scope.$digest(); -    expect(scope.$$childHead).toEqual(null); -    expect(scope.$$childTail).toEqual(null); -  }); +    $rootScope.$digest(); +    expect($rootScope.$$childHead).toEqual(null); +    expect($rootScope.$$childTail).toEqual(null); +  })); -  it('should infer arguments in injection', function() { +  it('should infer arguments in injection', inject(function($route, $location, $rootScope) {      $route.when('/test', {controller: function($route){ this.$route = $route; }});      $location.path('/test'); -    scope.$digest(); +    $rootScope.$digest();      expect($route.current.scope.$route).toBe($route); -  }); +  }));    describe('redirection', function() { -    it('should support redirection via redirectTo property by updating $location', function() { +    it('should support redirection via redirectTo property by updating $location', +        inject(function($route, $location, $rootScope) {        var onChangeSpy = jasmine.createSpy('onChange');        $route.when('/', {redirectTo: '/foo'}); @@ -189,57 +182,59 @@ describe('$route', function() {        $route.when('/bar', {template: 'bar.html'});        $route.when('/baz', {redirectTo: '/bar'});        $route.otherwise({template: '404.html'}); -      scope.$on('$beforeRouteChange', onChangeSpy); +      $rootScope.$on('$beforeRouteChange', onChangeSpy);        expect($route.current).toBeUndefined();        expect(onChangeSpy).not.toHaveBeenCalled();        $location.path('/'); -      scope.$digest(); +      $rootScope.$digest();        expect($location.path()).toBe('/foo');        expect($route.current.template).toBe('foo.html');        expect(onChangeSpy.callCount).toBe(2);        onChangeSpy.reset();        $location.path('/baz'); -      scope.$digest(); +      $rootScope.$digest();        expect($location.path()).toBe('/bar');        expect($route.current.template).toBe('bar.html');        expect(onChangeSpy.callCount).toBe(2); -    }); +    })); -    it('should interpolate route vars in the redirected path from original path', function() { +    it('should interpolate route vars in the redirected path from original path', +        inject(function($route, $location, $rootScope) {        $route.when('/foo/:id/foo/:subid/:extraId', {redirectTo: '/bar/:id/:subid/23'});        $route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'});        $location.path('/foo/id1/foo/subid3/gah'); -      scope.$digest(); +      $rootScope.$digest();        expect($location.path()).toEqual('/bar/id1/subid3/23');        expect($location.search()).toEqual({extraId: 'gah'});        expect($route.current.template).toEqual('bar.html'); -    }); +    })); -    it('should interpolate route vars in the redirected path from original search', function() { +    it('should interpolate route vars in the redirected path from original search', +        inject(function($route, $location, $rootScope) {        $route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'});        $route.when('/foo/:id/:extra', {redirectTo: '/bar/:id/:subid/99'});        $location.path('/foo/id3/eId').search('subid=sid1&appended=true'); -      scope.$digest(); +      $rootScope.$digest();        expect($location.path()).toEqual('/bar/id3/sid1/99');        expect($location.search()).toEqual({appended: 'true', extra: 'eId'});        expect($route.current.template).toEqual('bar.html'); -    }); +    })); -    it('should allow custom redirectTo function to be used', function() { +    it('should allow custom redirectTo function to be used', inject(function($route, $location, $rootScope) {        $route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'});        $route.when('/foo/:id', {redirectTo: customRedirectFn});        $location.path('/foo/id3').search('subid=sid1&appended=true'); -      scope.$digest(); +      $rootScope.$digest();        expect($location.path()).toEqual('/custom'); @@ -249,60 +244,61 @@ describe('$route', function() {          expect(search).toEqual($location.search());          return '/custom';        } -    }); +    })); -    it('should replace the url when redirecting', function() { +    it('should replace the url when redirecting', inject(function($route, $location, $rootScope) {        $route.when('/bar/:id', {template: 'bar.html'});        $route.when('/foo/:id/:extra', {redirectTo: '/bar/:id'});        var replace; -      scope.$watch(function() { +      $rootScope.$watch(function() {          if (isUndefined(replace)) replace = $location.$$replace;        });        $location.path('/foo/id3/eId'); -      scope.$digest(); +      $rootScope.$digest();        expect($location.path()).toEqual('/bar/id3');        expect(replace).toBe(true); -    }); +    }));    });    describe('reloadOnSearch', function() { -    it('should reload a route when reloadOnSearch is enabled and .search() changes', function() { -      var $routeParams = scope.$service('$routeParams'), +    it('should reload a route when reloadOnSearch is enabled and .search() changes', +        inject(function($route, $location, $rootScope) { +      var $routeParams = $rootScope.$service('$routeParams'),            reloaded = jasmine.createSpy('route reload');        $route.when('/foo', {controller: FooCtrl}); -      scope.$on('$beforeRouteChange', reloaded); +      $rootScope.$on('$beforeRouteChange', reloaded);        function FooCtrl() {          reloaded();        }        $location.path('/foo'); -      scope.$digest(); +      $rootScope.$digest();        expect(reloaded).toHaveBeenCalled();        expect($routeParams).toEqual({});        reloaded.reset();        // trigger reload        $location.search({foo: 'bar'}); -      scope.$digest(); +      $rootScope.$digest();        expect(reloaded).toHaveBeenCalled();        expect($routeParams).toEqual({foo:'bar'}); -    }); +    }));      it('should not reload a route when reloadOnSearch is disabled and only .search() changes', -        function() { +        inject(function($route, $location, $rootScope) {        var reloaded = jasmine.createSpy('route reload'),            routeUpdateEvent = jasmine.createSpy('route reload');        $route.when('/foo', {controller: FooCtrl, reloadOnSearch: false}); -      scope.$on('$beforeRouteChange', reloaded); +      $rootScope.$on('$beforeRouteChange', reloaded);        function FooCtrl() {          reloaded(); @@ -312,25 +308,26 @@ describe('$route', function() {        expect(reloaded).not.toHaveBeenCalled();        $location.path('/foo'); -      scope.$digest(); +      $rootScope.$digest();        expect(reloaded).toHaveBeenCalled();        expect(routeUpdateEvent).not.toHaveBeenCalled();        reloaded.reset();        // don't trigger reload        $location.search({foo: 'bar'}); -      scope.$digest(); +      $rootScope.$digest();        expect(reloaded).not.toHaveBeenCalled();        expect(routeUpdateEvent).toHaveBeenCalled(); -    }); +    })); -    it('should reload reloadOnSearch route when url differs only in route path param', function() { +    it('should reload reloadOnSearch route when url differs only in route path param', +        inject(function($route, $location, $rootScope) {        var reloaded = jasmine.createSpy('routeReload'),            onRouteChange = jasmine.createSpy('onRouteChange');        $route.when('/foo/:fooId', {controller: FooCtrl, reloadOnSearch: false}); -      scope.$on('$beforeRouteChange', onRouteChange); +      $rootScope.$on('$beforeRouteChange', onRouteChange);        function FooCtrl() {          reloaded(); @@ -340,27 +337,28 @@ describe('$route', function() {        expect(onRouteChange).not.toHaveBeenCalled();        $location.path('/foo/aaa'); -      scope.$digest(); +      $rootScope.$digest();        expect(reloaded).toHaveBeenCalled();        expect(onRouteChange).toHaveBeenCalled();        reloaded.reset();        onRouteChange.reset();        $location.path('/foo/bbb'); -      scope.$digest(); +      $rootScope.$digest();        expect(reloaded).toHaveBeenCalled();        expect(onRouteChange).toHaveBeenCalled();        reloaded.reset();        onRouteChange.reset();        $location.search({foo: 'bar'}); -      scope.$digest(); +      $rootScope.$digest();        expect(reloaded).not.toHaveBeenCalled();        expect(onRouteChange).not.toHaveBeenCalled(); -    }); +    })); -    it('should update params when reloadOnSearch is disabled and .search() changes', function() { +    it('should update params when reloadOnSearch is disabled and .search() changes', +        inject(function($route, $location, $rootScope) {        var routeParams = jasmine.createSpy('routeParams');        $route.when('/foo', {controller: FooCtrl}); @@ -377,32 +375,32 @@ describe('$route', function() {        expect(routeParams).not.toHaveBeenCalled();        $location.path('/foo'); -      scope.$digest(); +      $rootScope.$digest();        expect(routeParams).toHaveBeenCalledWith({});        routeParams.reset();        // trigger reload        $location.search({foo: 'bar'}); -      scope.$digest(); +      $rootScope.$digest();        expect(routeParams).toHaveBeenCalledWith({foo: 'bar'});        routeParams.reset();        $location.path('/bar/123').search({}); -      scope.$digest(); +      $rootScope.$digest();        expect(routeParams).toHaveBeenCalledWith({barId: '123'});        routeParams.reset();        // don't trigger reload        $location.search({foo: 'bar'}); -      scope.$digest(); +      $rootScope.$digest();        expect(routeParams).toHaveBeenCalledWith({barId: '123', foo: 'bar'}); -    }); +    }));      describe('reload', function() { -      it('should reload even if reloadOnSearch is false', function() { -        var $routeParams = scope.$service('$routeParams'), +      it('should reload even if reloadOnSearch is false', inject(function($route, $location, $rootScope) { +        var $routeParams = $rootScope.$service('$routeParams'),              count = 0;          $route.when('/bar/:barId', {controller: FooCtrl, reloadOnSearch: false}); @@ -410,20 +408,20 @@ describe('$route', function() {          function FooCtrl() { count ++; }          $location.path('/bar/123'); -        scope.$digest(); +        $rootScope.$digest();          expect($routeParams).toEqual({barId:'123'});          expect(count).toEqual(1);          $location.path('/bar/123').search('a=b'); -        scope.$digest(); +        $rootScope.$digest();          expect($routeParams).toEqual({barId:'123', a:'b'});          expect(count).toEqual(1);          $route.reload(); -        scope.$digest(); +        $rootScope.$digest();          expect($routeParams).toEqual({barId:'123', a:'b'});          expect(count).toEqual(2); -      }); +      }));      });    });  }); diff --git a/test/service/scopeSpec.js b/test/service/scopeSpec.js index d3f58918..2cd2f635 100644 --- a/test/service/scopeSpec.js +++ b/test/service/scopeSpec.js @@ -1,70 +1,66 @@  'use strict';  describe('Scope', function() { -  var root = null, mockHandler = null; -  beforeEach(function() { -    root = createScope(angular.service, { -      '$exceptionHandler': $exceptionHandlerMockFactory() -    }); -    mockHandler = root.$service('$exceptionHandler'); -  }); +  beforeEach(inject(function(service) { +    service('$exceptionHandler', $exceptionHandlerMockFactory); +  }));    describe('$root', function() { -    it('should point to itself', function() { -      expect(root.$root).toEqual(root); -      expect(root.hasOwnProperty('$root')).toBeTruthy(); -    }); +    it('should point to itself', inject(function($rootScope) { +      expect($rootScope.$root).toEqual($rootScope); +      expect($rootScope.hasOwnProperty('$root')).toBeTruthy(); +    })); -    it('should not have $root on children, but should inherit', function() { -      var child = root.$new(); -      expect(child.$root).toEqual(root); +    it('should not have $root on children, but should inherit', inject(function($rootScope) { +      var child = $rootScope.$new(); +      expect(child.$root).toEqual($rootScope);        expect(child.hasOwnProperty('$root')).toBeFalsy(); -    }); +    }));    });    describe('$parent', function() { -    it('should point to itself in root', function() { -      expect(root.$root).toEqual(root); -    }); +    it('should point to itself in root', inject(function($rootScope) { +      expect($rootScope.$root).toEqual($rootScope); +    })); -    it('should point to parent', function() { -      var child = root.$new(); -      expect(root.$parent).toEqual(null); -      expect(child.$parent).toEqual(root); +    it('should point to parent', inject(function($rootScope) { +      var child = $rootScope.$new(); +      expect($rootScope.$parent).toEqual(null); +      expect(child.$parent).toEqual($rootScope);        expect(child.$new().$parent).toEqual(child); -    }); +    }));    });    describe('$id', function() { -    it('should have a unique id', function() { -      expect(root.$id < root.$new().$id).toBeTruthy(); -    }); +    it('should have a unique id', inject(function($rootScope) { +      expect($rootScope.$id < $rootScope.$new().$id).toBeTruthy(); +    }));    });    describe('this', function() { -    it('should have a \'this\'', function() { -      expect(root['this']).toEqual(root); -    }); +    it('should have a \'this\'', inject(function($rootScope) { +      expect($rootScope['this']).toEqual($rootScope); +    }));    });    describe('$new()', function() { -    it('should create a child scope', function() { -      var child = root.$new(); -      root.a = 123; +    it('should create a child scope', inject(function($rootScope) { +      var child = $rootScope.$new(); +      $rootScope.a = 123;        expect(child.a).toEqual(123); -    }); +    })); -    it('should instantiate controller and bind functions', function() { +    it('should instantiate controller and bind functions', inject(function($rootScope) {        function Cntl($browser, name){          this.$browser = $browser;          this.callCount = 0; @@ -79,10 +75,10 @@ describe('Scope', function() {          }        }; -      var cntl = root.$new(Cntl, ['misko']); +      var cntl = $rootScope.$new(Cntl, ['misko']); -      expect(root.$browser).toBeUndefined(); -      expect(root.myFn).toBeUndefined(); +      expect($rootScope.$browser).toBeUndefined(); +      expect($rootScope.myFn).toBeUndefined();        expect(cntl.$browser).toBeDefined();        expect(cntl.name).toEqual('misko'); @@ -90,96 +86,89 @@ describe('Scope', function() {        cntl.myFn();        cntl.$new().myFn();        expect(cntl.callCount).toEqual(2); -    }); -  }); - - -  describe('$service', function() { -    it('should have it on root', function() { -      expect(root.hasOwnProperty('$service')).toBeTruthy(); -    }); +    }));    });    describe('$watch/$digest', function() { -    it('should watch and fire on simple property change', function() { +    it('should watch and fire on simple property change', inject(function($rootScope) {        var spy = jasmine.createSpy(); -      root.$watch('name', spy); -      root.$digest(); +      $rootScope.$watch('name', spy); +      $rootScope.$digest();        spy.reset();        expect(spy).not.wasCalled(); -      root.$digest(); +      $rootScope.$digest();        expect(spy).not.wasCalled(); -      root.name = 'misko'; -      root.$digest(); -      expect(spy).wasCalledWith(root, 'misko', undefined); -    }); +      $rootScope.name = 'misko'; +      $rootScope.$digest(); +      expect(spy).wasCalledWith($rootScope, 'misko', undefined); +    })); -    it('should watch and fire on expression change', function() { +    it('should watch and fire on expression change', inject(function($rootScope) {        var spy = jasmine.createSpy(); -      root.$watch('name.first', spy); -      root.$digest(); +      $rootScope.$watch('name.first', spy); +      $rootScope.$digest();        spy.reset(); -      root.name = {}; +      $rootScope.name = {};        expect(spy).not.wasCalled(); -      root.$digest(); +      $rootScope.$digest();        expect(spy).not.wasCalled(); -      root.name.first = 'misko'; -      root.$digest(); +      $rootScope.name.first = 'misko'; +      $rootScope.$digest();        expect(spy).wasCalled(); -    }); +    })); -    it('should delegate exceptions', function() { -      root.$watch('a', function() {throw new Error('abc');}); -      root.a = 1; -      root.$digest(); -      expect(mockHandler.errors[0].message).toEqual('abc'); +    it('should delegate exceptions', inject(function($rootScope, $exceptionHandler) { +      $rootScope.$watch('a', function() {throw new Error('abc');}); +      $rootScope.a = 1; +      $rootScope.$digest(); +      expect($exceptionHandler.errors[0].message).toEqual('abc');        $logMock.error.logs.length = 0; -    }); +    })); -    it('should fire watches in order of addition', function() { +    it('should fire watches in order of addition', inject(function($rootScope) {        // this is not an external guarantee, just our own sanity        var log = ''; -      root.$watch('a', function() { log += 'a'; }); -      root.$watch('b', function() { log += 'b'; }); -      root.$watch('c', function() { log += 'c'; }); -      root.a = root.b = root.c = 1; -      root.$digest(); +      $rootScope.$watch('a', function() { log += 'a'; }); +      $rootScope.$watch('b', function() { log += 'b'; }); +      $rootScope.$watch('c', function() { log += 'c'; }); +      $rootScope.a = $rootScope.b = $rootScope.c = 1; +      $rootScope.$digest();        expect(log).toEqual('abc'); -    }); +    })); -    it('should call child $watchers in addition order', function() { +    it('should call child $watchers in addition order', inject(function($rootScope) {        // this is not an external guarantee, just our own sanity        var log = ''; -      var childA = root.$new(); -      var childB = root.$new(); -      var childC = root.$new(); +      var childA = $rootScope.$new(); +      var childB = $rootScope.$new(); +      var childC = $rootScope.$new();        childA.$watch('a', function() { log += 'a'; });        childB.$watch('b', function() { log += 'b'; });        childC.$watch('c', function() { log += 'c'; });        childA.a = childB.b = childC.c = 1; -      root.$digest(); +      $rootScope.$digest();        expect(log).toEqual('abc'); -    }); +    })); -    it('should allow $digest on a child scope with and without a right sibling', function() { +    it('should allow $digest on a child scope with and without a right sibling', inject(function($rootScope) {        // tests a traversal edge case which we originally missed        var log = '', -          childA = root.$new(), -          childB = root.$new(); +          childA = $rootScope.$new(), +          childB = $rootScope.$new(); -      root.$watch(function() { log += 'r'; }); +      $rootScope.$watch(function() { log += 'r'; });        childA.$watch(function() { log += 'a'; });        childB.$watch(function() { log += 'b'; });        // init -      root.$digest(); +      $rootScope.$digest();        expect(log).toBe('rabrab');        log = ''; @@ -189,114 +178,114 @@ describe('Scope', function() {        log = '';        childB.$digest();        expect(log).toBe('b'); -    }); +    })); -    it('should repeat watch cycle while model changes are identified', function() { +    it('should repeat watch cycle while model changes are identified', inject(function($rootScope) {        var log = ''; -      root.$watch('c', function(self, v){self.d = v; log+='c'; }); -      root.$watch('b', function(self, v){self.c = v; log+='b'; }); -      root.$watch('a', function(self, v){self.b = v; log+='a'; }); -      root.$digest(); +      $rootScope.$watch('c', function(self, v){self.d = v; log+='c'; }); +      $rootScope.$watch('b', function(self, v){self.c = v; log+='b'; }); +      $rootScope.$watch('a', function(self, v){self.b = v; log+='a'; }); +      $rootScope.$digest();        log = ''; -      root.a = 1; -      root.$digest(); -      expect(root.b).toEqual(1); -      expect(root.c).toEqual(1); -      expect(root.d).toEqual(1); +      $rootScope.a = 1; +      $rootScope.$digest(); +      expect($rootScope.b).toEqual(1); +      expect($rootScope.c).toEqual(1); +      expect($rootScope.d).toEqual(1);        expect(log).toEqual('abc'); -    }); +    })); -    it('should repeat watch cycle from the root elemnt', function() { +    it('should repeat watch cycle from the root elemnt', inject(function($rootScope) {        var log = ''; -      var child = root.$new(); -      root.$watch(function() { log += 'a'; }); +      var child = $rootScope.$new(); +      $rootScope.$watch(function() { log += 'a'; });        child.$watch(function() { log += 'b'; }); -      root.$digest(); +      $rootScope.$digest();        expect(log).toEqual('abab'); -    }); +    })); -    it('should prevent infinite recursion and print watcher expression', function() { -      root.$watch('a', function(self){self.b++;}); -      root.$watch('b', function(self){self.a++;}); -      root.a = root.b = 0; +    it('should prevent infinite recursion and print watcher expression',inject(function($rootScope) { +      $rootScope.$watch('a', function(self){self.b++;}); +      $rootScope.$watch('b', function(self){self.a++;}); +      $rootScope.a = $rootScope.b = 0;        expect(function() { -        root.$digest(); +        $rootScope.$digest();        }).toThrow('100 $digest() iterations reached. Aborting!\n'+            'Watchers fired in the last 5 iterations: ' +            '[["a","b"],["a","b"],["a","b"],["a","b"],["a","b"]]'); -    }); +    }));      it('should prevent infinite recurcion and print print watcher function name or body', -        function() { -      root.$watch(function watcherA() {return root.a;}, function(self){self.b++;}); -      root.$watch(function() {return root.b;}, function(self){self.a++;}); -      root.a = root.b = 0; +        inject(function($rootScope) { +      $rootScope.$watch(function watcherA() {return $rootScope.a;}, function(self){self.b++;}); +      $rootScope.$watch(function() {return $rootScope.b;}, function(self){self.a++;}); +      $rootScope.a = $rootScope.b = 0;        try { -        root.$digest(); +        $rootScope.$digest();          throw Error('Should have thrown exception');        } catch(e) {          expect(e.message.match(/"fn: (watcherA|function)/g).length).toBe(10);        } -    }); +    })); -    it('should not fire upon $watch registration on initial $digest', function() { +    it('should not fire upon $watch registration on initial $digest', inject(function($rootScope) {        var log = ''; -      root.a = 1; -      root.$watch('a', function() { log += 'a'; }); -      root.$watch('b', function() { log += 'b'; }); -      root.$digest(); +      $rootScope.a = 1; +      $rootScope.$watch('a', function() { log += 'a'; }); +      $rootScope.$watch('b', function() { log += 'b'; }); +      $rootScope.$digest();        log = ''; -      root.$digest(); +      $rootScope.$digest();        expect(log).toEqual(''); -    }); +    })); -    it('should watch objects', function() { +    it('should watch objects', inject(function($rootScope) {        var log = ''; -      root.a = []; -      root.b = {}; -      root.$watch('a', function(scope, value){ +      $rootScope.a = []; +      $rootScope.b = {}; +      $rootScope.$watch('a', function(scope, value){          log +='.'; -        expect(value).toBe(root.a); +        expect(value).toBe($rootScope.a);        }); -      root.$watch('b', function(scope, value){ +      $rootScope.$watch('b', function(scope, value){          log +='!'; -        expect(value).toBe(root.b); +        expect(value).toBe($rootScope.b);        }); -      root.$digest(); +      $rootScope.$digest();        log = ''; -      root.a.push({}); -      root.b.name = ''; +      $rootScope.a.push({}); +      $rootScope.b.name = ''; -      root.$digest(); +      $rootScope.$digest();        expect(log).toEqual('.!'); -    }); +    })); -    it('should prevent recursion', function() { +    it('should prevent recursion', inject(function($rootScope) {        var callCount = 0; -      root.$watch('name', function() { +      $rootScope.$watch('name', function() {          expect(function() { -          root.$digest(); +          $rootScope.$digest();          }).toThrow('$digest already in progress');          callCount++;        }); -      root.name = 'a'; -      root.$digest(); +      $rootScope.name = 'a'; +      $rootScope.$digest();        expect(callCount).toEqual(1); -    }); +    })); -    it('should return a function that allows listeners to be unregistered', function() { -      var root = angular.scope(), +    it('should return a function that allows listeners to be unregistered', inject(function($rootScope) { +      var root = angular.injector()('$rootScope'),            listener = jasmine.createSpy('watch listener'),            listenerRemove; @@ -315,166 +304,162 @@ describe('Scope', function() {        listenerRemove();        root.$digest(); //trigger        expect(listener).not.toHaveBeenCalled(); -    }); +    }));    });    describe('$destroy', function() {      var first = null, middle = null, last = null, log = null; -    beforeEach(function() { +    beforeEach(inject(function($rootScope) {        log = ''; -      first = root.$new(); -      middle = root.$new(); -      last = root.$new(); +      first = $rootScope.$new(); +      middle = $rootScope.$new(); +      last = $rootScope.$new();        first.$watch(function() { log += '1';});        middle.$watch(function() { log += '2';});        last.$watch(function() { log += '3';}); -      root.$digest(); +      $rootScope.$digest();        log = ''; -    }); +    })); -    it('should ignore remove on root', function() { -      root.$destroy(); -      root.$digest(); +    it('should ignore remove on root', inject(function($rootScope) { +      $rootScope.$destroy(); +      $rootScope.$digest();        expect(log).toEqual('123'); -    }); +    })); -    it('should remove first', function() { +    it('should remove first', inject(function($rootScope) {        first.$destroy(); -      root.$digest(); +      $rootScope.$digest();        expect(log).toEqual('23'); -    }); +    })); -    it('should remove middle', function() { +    it('should remove middle', inject(function($rootScope) {        middle.$destroy(); -      root.$digest(); +      $rootScope.$digest();        expect(log).toEqual('13'); -    }); +    })); -    it('should remove last', function() { +    it('should remove last', inject(function($rootScope) {        last.$destroy(); -      root.$digest(); +      $rootScope.$digest();        expect(log).toEqual('12'); -    }); +    })); -    it('should fire a $destroy event', function() { +    it('should fire a $destroy event', inject(function($rootScope) {        var destructedScopes = [];        middle.$on('$destroy', function(event) {          destructedScopes.push(event.currentScope);        });        middle.$destroy();        expect(destructedScopes).toEqual([middle]); -    }); +    }));    });    describe('$eval', function() { -    it('should eval an expression', function() { -      expect(root.$eval('a=1')).toEqual(1); -      expect(root.a).toEqual(1); +    it('should eval an expression', inject(function($rootScope) { +      expect($rootScope.$eval('a=1')).toEqual(1); +      expect($rootScope.a).toEqual(1); -      root.$eval(function(self){self.b=2;}); -      expect(root.b).toEqual(2); -    }); +      $rootScope.$eval(function(self){self.b=2;}); +      expect($rootScope.b).toEqual(2); +    }));    });    describe('$evalAsync', function() { -    it('should run callback before $watch', function() { +    it('should run callback before $watch', inject(function($rootScope) {        var log = ''; -      var child = root.$new(); -      root.$evalAsync(function(scope){ log += 'parent.async;'; }); -      root.$watch('value', function() { log += 'parent.$digest;'; }); +      var child = $rootScope.$new(); +      $rootScope.$evalAsync(function(scope){ log += 'parent.async;'; }); +      $rootScope.$watch('value', function() { log += 'parent.$digest;'; });        child.$evalAsync(function(scope){ log += 'child.async;'; });        child.$watch('value', function() { log += 'child.$digest;'; }); -      root.$digest(); +      $rootScope.$digest();        expect(log).toEqual('parent.async;parent.$digest;child.async;child.$digest;'); -    }); - -    it('should cause a $digest rerun', function() { -      root.log = ''; -      root.value = 0; -      root.$watch('value', 'log = log + ".";'); -      root.$watch('init', function() { -        root.$evalAsync('value = 123; log = log + "=" '); -        expect(root.value).toEqual(0); +    })); + +    it('should cause a $digest rerun', inject(function($rootScope) { +      $rootScope.log = ''; +      $rootScope.value = 0; +      $rootScope.$watch('value', 'log = log + ".";'); +      $rootScope.$watch('init', function() { +        $rootScope.$evalAsync('value = 123; log = log + "=" '); +        expect($rootScope.value).toEqual(0);        }); -      root.$digest(); -      expect(root.log).toEqual('.=.'); -    }); - -    it('should run async in the same order as added', function() { -      root.log = ''; -      root.$evalAsync("log = log + 1"); -      root.$evalAsync("log = log + 2"); -      root.$digest(); -      expect(root.log).toBe('12'); -    }); +      $rootScope.$digest(); +      expect($rootScope.log).toEqual('.=.'); +    })); + +    it('should run async in the same order as added', inject(function($rootScope) { +      $rootScope.log = ''; +      $rootScope.$evalAsync("log = log + 1"); +      $rootScope.$evalAsync("log = log + 2"); +      $rootScope.$digest(); +      expect($rootScope.log).toBe('12'); +    }));    });    describe('$apply', function() { -    it('should apply expression with full lifecycle', function() { +    it('should apply expression with full lifecycle', inject(function($rootScope) {        var log = ''; -      var child = root.$new(); -      root.$watch('a', function(scope, a){ log += '1'; }); +      var child = $rootScope.$new(); +      $rootScope.$watch('a', function(scope, a){ log += '1'; });        child.$apply('$parent.a=0');        expect(log).toEqual('1'); -    }); +    })); -    it('should catch exceptions', function() { +    it('should catch exceptions', inject(function($rootScope, $exceptionHandler) {        var log = ''; -      var child = root.$new(); -      root.$watch('a', function(scope, a){ log += '1'; }); -      root.a = 0; +      var child = $rootScope.$new(); +      $rootScope.$watch('a', function(scope, a){ log += '1'; }); +      $rootScope.a = 0;        child.$apply(function() { throw new Error('MyError'); });        expect(log).toEqual('1'); -      expect(mockHandler.errors[0].message).toEqual('MyError'); +      expect($exceptionHandler.errors[0].message).toEqual('MyError');        $logMock.error.logs.shift(); -    }); +    }));      describe('exceptions', function() { -      var $exceptionHandler, log; -      beforeEach(function() { +      var log; +      beforeEach(inject(function($rootScope) {          log = ''; -        $exceptionHandler = jasmine.createSpy('$exceptionHandler'); -        root.$service = function(name) { -          return {$exceptionHandler:$exceptionHandler}[name]; -        }; -        root.$watch(function() { log += '$digest;'; }); -        root.$digest(); +        $rootScope.$watch(function() { log += '$digest;'; }); +        $rootScope.$digest();          log = ''; -      }); +      })); -      it('should execute and return value and update', function() { -        root.name = 'abc'; -        expect(root.$apply(function(scope){ +      it('should execute and return value and update', inject(function($rootScope, $exceptionHandler) { +        $rootScope.name = 'abc'; +        expect($rootScope.$apply(function(scope){            return scope.name;          })).toEqual('abc');          expect(log).toEqual('$digest;'); -        expect($exceptionHandler).not.wasCalled(); -      }); +        expect($exceptionHandler.errors).toEqual([]); +      })); -      it('should catch exception and update', function() { +      it('should catch exception and update', inject(function($rootScope, $exceptionHandler) {          var error = new Error('MyError'); -        root.$apply(function() { throw error; }); +        $rootScope.$apply(function() { throw error; });          expect(log).toEqual('$digest;'); -        expect($exceptionHandler).wasCalledWith(error); -      }); +        expect($exceptionHandler.errors).toEqual([error]); +      }));      });    }); @@ -483,9 +468,9 @@ describe('Scope', function() {      describe('$on', function() { -      it('should add listener for both $emit and $broadcast events', function() { +      it('should add listener for both $emit and $broadcast events', inject(function($rootScope) {          var log = '', -            root = angular.scope(), +            root = angular.injector()('$rootScope'),              child = root.$new();          function eventFn() { @@ -500,12 +485,12 @@ describe('Scope', function() {          child.$broadcast('abc');          expect(log).toEqual('XX'); -      }); +      })); -      it('should return a function that deregisters the listener', function() { +      it('should return a function that deregisters the listener', inject(function($rootScope) {          var log = '', -            root = angular.scope(), +            root = angular.injector()('$rootScope'),              child = root.$new(),              listenerRemove; @@ -526,7 +511,7 @@ describe('Scope', function() {          child.$emit('abc');          child.$broadcast('abc');          expect(log).toEqual(''); -      }); +      }));      }); @@ -537,55 +522,56 @@ describe('Scope', function() {          log += event.currentScope.id + '>';        } -      beforeEach(function() { +      beforeEach(inject(function($rootScope) {          log = ''; -        child = root.$new(); +        child = $rootScope.$new();          grandChild = child.$new();          greatGrandChild = grandChild.$new(); -        root.id = 0; +        $rootScope.id = 0;          child.id = 1;          grandChild.id = 2;          greatGrandChild.id = 3; -        root.$on('myEvent', logger); +        $rootScope.$on('myEvent', logger);          child.$on('myEvent', logger);          grandChild.$on('myEvent', logger);          greatGrandChild.$on('myEvent', logger); -      }); +      })); -      it('should bubble event up to the root scope', function() { +      it('should bubble event up to the root scope', inject(function($rootScope) {          grandChild.$emit('myEvent');          expect(log).toEqual('2>1>0>'); -      }); +      })); -      it('should dispatch exceptions to the $exceptionHandler', function() { +      it('should dispatch exceptions to the $exceptionHandler', +          inject(function($rootScope, $exceptionHandler) {          child.$on('myEvent', function() { throw 'bubbleException'; });          grandChild.$emit('myEvent');          expect(log).toEqual('2>1>0>'); -        expect(mockHandler.errors).toEqual(['bubbleException']); -      }); +        expect($exceptionHandler.errors).toEqual(['bubbleException']); +      })); -      it('should allow cancelation of event propagation', function() { +      it('should allow cancelation of event propagation', inject(function($rootScope) {          child.$on('myEvent', function(event){ event.cancel(); });          grandChild.$emit('myEvent');          expect(log).toEqual('2>1>'); -      }); +      })); -      it('should forward method arguments', function() { +      it('should forward method arguments', inject(function($rootScope) {          child.$on('abc', function(event, arg1, arg2){            expect(event.name).toBe('abc');            expect(arg1).toBe('arg1');            expect(arg2).toBe('arg2');          });          child.$emit('abc', 'arg1', 'arg2'); -      }); +      }));        describe('event object', function() { -        it('should have methods/properties', function() { +        it('should have methods/properties', inject(function($rootScope) {            var event;            child.$on('myEvent', function(e){              expect(e.targetScope).toBe(grandChild); @@ -595,7 +581,7 @@ describe('Scope', function() {            });            grandChild.$emit('myEvent');            expect(event).toBeDefined(); -        }); +        }));        });      }); @@ -609,18 +595,18 @@ describe('Scope', function() {            log += event.currentScope.id + '>';          } -        beforeEach(function() { +        beforeEach(inject(function($rootScope) {            log = ''; -          child1 = root.$new(); -          child2 = root.$new(); -          child3 = root.$new(); +          child1 = $rootScope.$new(); +          child2 = $rootScope.$new(); +          child3 = $rootScope.$new();            grandChild11 = child1.$new();            grandChild21 = child2.$new();            grandChild22 = child2.$new();            grandChild23 = child2.$new();            greatGrandChild211 = grandChild21.$new(); -          root.id = 0; +          $rootScope.id = 0;            child1.id = 1;            child2.id = 2;            child3.id = 3; @@ -630,7 +616,7 @@ describe('Scope', function() {            grandChild23.id = 23;            greatGrandChild211.id = 211; -          root.$on('myEvent', logger); +          $rootScope.$on('myEvent', logger);            child1.$on('myEvent', logger);            child2.$on('myEvent', logger);            child3.$on('myEvent', logger); @@ -647,43 +633,43 @@ describe('Scope', function() {            //   11  21 22 23            //       |            //      211 -        }); +        })); -        it('should broadcast an event from the root scope', function() { -          root.$broadcast('myEvent'); +        it('should broadcast an event from the root scope', inject(function($rootScope) { +          $rootScope.$broadcast('myEvent');            expect(log).toBe('0>1>11>2>21>211>22>23>3>'); -        }); +        })); -        it('should broadcast an event from a child scope', function() { +        it('should broadcast an event from a child scope', inject(function($rootScope) {            child2.$broadcast('myEvent');            expect(log).toBe('2>21>211>22>23>'); -        }); +        })); -        it('should broadcast an event from a leaf scope with a sibling', function() { +        it('should broadcast an event from a leaf scope with a sibling', inject(function($rootScope) {            grandChild22.$broadcast('myEvent');            expect(log).toBe('22>'); -        }); +        })); -        it('should broadcast an event from a leaf scope without a sibling', function() { +        it('should broadcast an event from a leaf scope without a sibling', inject(function($rootScope) {            grandChild23.$broadcast('myEvent');            expect(log).toBe('23>'); -        }); +        })); -        it('should not not fire any listeners for other events', function() { -          root.$broadcast('fooEvent'); +        it('should not not fire any listeners for other events', inject(function($rootScope) { +          $rootScope.$broadcast('fooEvent');            expect(log).toBe(''); -        }); +        }));        });        describe('listener', function() { -        it('should receive event object', function() { -          var scope = angular.scope(), +        it('should receive event object', inject(function($rootScope) { +          var scope = angular.injector()('$rootScope'),                child = scope.$new(),                event; @@ -695,11 +681,11 @@ describe('Scope', function() {            expect(event.name).toBe('fooEvent');            expect(event.targetScope).toBe(scope);            expect(event.currentScope).toBe(child); -        }); +        })); -        it('should support passing messages as varargs', function() { -          var scope = angular.scope(), +        it('should support passing messages as varargs', inject(function($rootScope) { +          var scope = angular.injector()('$rootScope'),                child = scope.$new(),                args; @@ -710,7 +696,7 @@ describe('Scope', function() {            expect(args.length).toBe(5);            expect(sliceArgs(args, 1)).toEqual(['do', 're', 'me', 'fa']); -        }); +        }));        });      });    }); diff --git a/test/service/windowSpec.js b/test/service/windowSpec.js index c539e285..3b847146 100644 --- a/test/service/windowSpec.js +++ b/test/service/windowSpec.js @@ -1,19 +1,7 @@  'use strict';  describe('$window', function() { -  var scope; - -  beforeEach(function() { -    scope = angular.scope(); -  }); - - -  afterEach(function() { -    dealoc(scope); -  }); - - -  it("should inject $window", function() { -    expect(scope.$service('$window')).toBe(window); -  }); +  it("should inject $window", inject(function($window) { +    expect($window).toBe(window); +  }));  }); diff --git a/test/service/xhr.bulkSpec.js b/test/service/xhr.bulkSpec.js index 6b99fbba..6f273f64 100644 --- a/test/service/xhr.bulkSpec.js +++ b/test/service/xhr.bulkSpec.js @@ -1,24 +1,16 @@  'use strict';  describe('$xhr.bulk', function() { -  var scope, $browser, $browserXhr, $log, $xhrBulk, $xhrError, log; +  var log; -  beforeEach(function() { -    scope = angular.scope(angular.service, { -      '$xhr.error': $xhrError = jasmine.createSpy('$xhr.error'), -      '$log': $log = {} +  beforeEach(inject(function(service) { +    service('$xhr.error', function(){ +      return jasmine.createSpy('$xhr.error');      }); -    $browser = scope.$service('$browser'); -    $browserXhr = $browser.xhr; -    $xhrBulk = scope.$service('$xhr.bulk'); -    $log = scope.$service('$log'); +    service.alias('$xhr.error', '$xhrError'); +    service.alias('$xhr.bulk', '$xhrBulk');      log = ''; -  }); - - -  afterEach(function() { -    dealoc(scope); -  }); +  }));    function callback(code, response) { @@ -27,12 +19,12 @@ describe('$xhr.bulk', function() {    } -  it('should collect requests', function() { +  it('should collect requests', inject(function($browser, $xhrBulk) {      $xhrBulk.urls["/"] = {match:/.*/};      $xhrBulk('GET', '/req1', null, callback);      $xhrBulk('POST', '/req2', {post:'data'}, callback); -    $browserXhr.expectPOST('/', { +    $browser.xhr.expectPOST('/', {        requests:[{method:'GET',  url:'/req1', data: null},                  {method:'POST', url:'/req2', data:{post:'data'} }]      }).respond([ @@ -40,17 +32,18 @@ describe('$xhr.bulk', function() {        {status:200, response:'second'}      ]);      $xhrBulk.flush(function() { log += 'DONE';}); -    $browserXhr.flush(); +    $browser.xhr.flush();      expect(log).toEqual('"first";"second";DONE'); -  }); +  })); -  it('should handle non 200 status code by forwarding to error handler', function() { +  it('should handle non 200 status code by forwarding to error handler', +      inject(function($browser, $xhrBulk, $xhrError) {      $xhrBulk.urls['/'] = {match:/.*/};      $xhrBulk('GET', '/req1', null, callback);      $xhrBulk('POST', '/req2', {post:'data'}, callback); -    $browserXhr.expectPOST('/', { +    $browser.xhr.expectPOST('/', {        requests:[{method:'GET',  url:'/req1', data: null},                  {method:'POST', url:'/req2', data:{post:'data'} }]      }).respond([ @@ -58,7 +51,7 @@ describe('$xhr.bulk', function() {        {status:200, response:'second'}      ]);      $xhrBulk.flush(function() { log += 'DONE';}); -    $browserXhr.flush(); +    $browser.xhr.flush();      expect($xhrError).toHaveBeenCalled();      var cb = $xhrError.mostRecentCall.args[0].success; @@ -68,22 +61,23 @@ describe('$xhr.bulk', function() {          {status: 404, response: 'NotFound'});      expect(log).toEqual('"second";DONE'); -  }); +  })); -  it('should handle non 200 status code by calling error callback if provided', function() { +  it('should handle non 200 status code by calling error callback if provided', +      inject(function($browser, $xhrBulk, $xhrError) {      var callback = jasmine.createSpy('error');      $xhrBulk.urls['/'] = {match: /.*/};      $xhrBulk('GET', '/req1', null, noop, callback); -    $browserXhr.expectPOST('/', { +    $browser.xhr.expectPOST('/', {        requests:[{method: 'GET',  url: '/req1', data: null}]      }).respond([{status: 404, response: 'NotFound'}]);      $xhrBulk.flush(); -    $browserXhr.flush(); +    $browser.xhr.flush();      expect($xhrError).not.toHaveBeenCalled();      expect(callback).toHaveBeenCalledWith(404, 'NotFound'); -  }); +  }));  }); diff --git a/test/service/xhr.cacheSpec.js b/test/service/xhr.cacheSpec.js index 0c77e629..328dfe3a 100644 --- a/test/service/xhr.cacheSpec.js +++ b/test/service/xhr.cacheSpec.js @@ -1,20 +1,17 @@  'use strict';  describe('$xhr.cache', function() { -  var scope, $browser, $browserXhr, $xhrErr, cache, log; - -  beforeEach(function() { -    scope = angular.scope(angularService, {'$xhr.error': $xhrErr = jasmine.createSpy('$xhr.error')}); -    $browser = scope.$service('$browser'); -    $browserXhr = $browser.xhr; -    cache = scope.$service('$xhr.cache'); +  var log; + +  beforeEach(inject(function(service) { +    service('$xhr.error', function(){ +      return jasmine.createSpy('$xhr.error'); +    }); +    service.alias('$xhr.cache', '$xhrCache'); +    service.alias('$xhr.bulk', '$xhrBulk'); +    service.alias('$xhr.error', '$xhrError');      log = ''; -  }); - - -  afterEach(function() { -    dealoc(scope); -  }); +  }));    function callback(code, response) { @@ -23,156 +20,158 @@ describe('$xhr.cache', function() {    } -  it('should cache requests', function() { -    $browserXhr.expectGET('/url').respond('first'); -    cache('GET', '/url', null, callback); -    $browserXhr.flush(); +  it('should cache requests', inject(function($browser, $xhrCache) { +    $browser.xhr.expectGET('/url').respond('first'); +    $xhrCache('GET', '/url', null, callback); +    $browser.xhr.flush(); -    $browserXhr.expectGET('/url').respond('ERROR'); -    cache('GET', '/url', null, callback); +    $browser.xhr.expectGET('/url').respond('ERROR'); +    $xhrCache('GET', '/url', null, callback);      $browser.defer.flush();      expect(log).toEqual('"first";"first";'); -    cache('GET', '/url', null, callback, false); +    $xhrCache('GET', '/url', null, callback, false);      $browser.defer.flush();      expect(log).toEqual('"first";"first";"first";'); -  }); +  })); -  it('should first return cache request, then return server request', function() { -    $browserXhr.expectGET('/url').respond('first'); -    cache('GET', '/url', null, callback, true); -    $browserXhr.flush(); +  it('should first return cache request, then return server request', inject(function($browser, $xhrCache) { +    $browser.xhr.expectGET('/url').respond('first'); +    $xhrCache('GET', '/url', null, callback, true); +    $browser.xhr.flush(); -    $browserXhr.expectGET('/url').respond('ERROR'); -    cache('GET', '/url', null, callback, true); +    $browser.xhr.expectGET('/url').respond('ERROR'); +    $xhrCache('GET', '/url', null, callback, true);      $browser.defer.flush();      expect(log).toEqual('"first";"first";'); -    $browserXhr.flush(); +    $browser.xhr.flush();      expect(log).toEqual('"first";"first";"ERROR";'); -  }); +  })); -  it('should serve requests from cache', function() { -    cache.data.url = {value:'123'}; -    cache('GET', 'url', null, callback); +  it('should serve requests from cache', inject(function($browser, $xhrCache) { +    $xhrCache.data.url = {value:'123'}; +    $xhrCache('GET', 'url', null, callback);      $browser.defer.flush();      expect(log).toEqual('"123";'); -    cache('GET', 'url', null, callback, false); +    $xhrCache('GET', 'url', null, callback, false);      $browser.defer.flush();      expect(log).toEqual('"123";"123";'); -  }); +  })); -  it('should keep track of in flight requests and request only once', function() { -    scope.$service('$xhr.bulk').urls['/bulk'] = { +  it('should keep track of in flight requests and request only once', inject(function($browser, $xhrCache, $xhrBulk) { +    $xhrBulk.urls['/bulk'] = {        match:function(url){          return url == '/url';        }      }; -    $browserXhr.expectPOST('/bulk', { +    $browser.xhr.expectPOST('/bulk', {        requests:[{method:'GET',  url:'/url', data: null}]      }).respond([        {status:200, response:'123'}      ]); -    cache('GET', '/url', null, callback); -    cache('GET', '/url', null, callback); -    cache.delegate.flush(); -    $browserXhr.flush(); +    $xhrCache('GET', '/url', null, callback); +    $xhrCache('GET', '/url', null, callback); +    $xhrCache.delegate.flush(); +    $browser.xhr.flush();      expect(log).toEqual('"123";"123";'); -  }); +  })); -  it('should clear cache on non GET', function() { -    $browserXhr.expectPOST('abc', {}).respond({}); -    cache.data.url = {value:123}; -    cache('POST', 'abc', {}); -    expect(cache.data.url).toBeUndefined(); -  }); +  it('should clear cache on non GET', inject(function($browser, $xhrCache) { +    $browser.xhr.expectPOST('abc', {}).respond({}); +    $xhrCache.data.url = {value:123}; +    $xhrCache('POST', 'abc', {}); +    expect($xhrCache.data.url).toBeUndefined(); +  })); -  it('should call callback asynchronously for both cache hit and cache miss', function() { -    $browserXhr.expectGET('/url').respond('+'); -    cache('GET', '/url', null, callback); +  it('should call callback asynchronously for both cache hit and cache miss', inject(function($browser, $xhrCache) { +    $browser.xhr.expectGET('/url').respond('+'); +    $xhrCache('GET', '/url', null, callback);      expect(log).toEqual(''); //callback hasn't executed -    $browserXhr.flush(); +    $browser.xhr.flush();      expect(log).toEqual('"+";'); //callback has executed -    cache('GET', '/url', null, callback); +    $xhrCache('GET', '/url', null, callback);      expect(log).toEqual('"+";'); //callback hasn't executed      $browser.defer.flush();      expect(log).toEqual('"+";"+";'); //callback has executed -  }); +  })); -  it('should call callback synchronously when sync flag is on', function() { -    $browserXhr.expectGET('/url').respond('+'); -    cache('GET', '/url', null, callback, false, true); +  it('should call callback synchronously when sync flag is on', inject(function($browser, $xhrCache) { +    $browser.xhr.expectGET('/url').respond('+'); +    $xhrCache('GET', '/url', null, callback, false, true);      expect(log).toEqual(''); //callback hasn't executed -    $browserXhr.flush(); +    $browser.xhr.flush();      expect(log).toEqual('"+";'); //callback has executed -    cache('GET', '/url', null, callback, false, true); +    $xhrCache('GET', '/url', null, callback, false, true);      expect(log).toEqual('"+";"+";'); //callback has executed      $browser.defer.flush();      expect(log).toEqual('"+";"+";'); //callback was not called again any more -  }); +  })); -  it('should call eval after callbacks for both cache hit and cache miss execute', function() { -    var flushSpy = this.spyOn(scope, '$digest').andCallThrough(); +  it('should call eval after callbacks for both cache hit and cache miss execute', +      inject(function($browser, $xhrCache, $rootScope) { +    var flushSpy = this.spyOn($rootScope, '$digest').andCallThrough(); -    $browserXhr.expectGET('/url').respond('+'); -    cache('GET', '/url', null, callback); +    $browser.xhr.expectGET('/url').respond('+'); +    $xhrCache('GET', '/url', null, callback);      expect(flushSpy).not.toHaveBeenCalled(); -    $browserXhr.flush(); +    $browser.xhr.flush();      expect(flushSpy).toHaveBeenCalled();      flushSpy.reset(); //reset the spy -    cache('GET', '/url', null, callback); +    $xhrCache('GET', '/url', null, callback);      expect(flushSpy).not.toHaveBeenCalled();      $browser.defer.flush();      expect(flushSpy).toHaveBeenCalled(); -  }); +  })); -  it('should call the error callback on error if provided', function() { +  it('should call the error callback on error if provided', inject(function($browser, $xhrCache) {      var errorSpy = jasmine.createSpy('error'),          successSpy = jasmine.createSpy('success'); -    $browserXhr.expectGET('/url').respond(500, 'error'); +    $browser.xhr.expectGET('/url').respond(500, 'error'); -    cache('GET', '/url', null, successSpy, errorSpy, false, true); -    $browserXhr.flush(); +    $xhrCache('GET', '/url', null, successSpy, errorSpy, false, true); +    $browser.xhr.flush();      expect(errorSpy).toHaveBeenCalledWith(500, 'error');      expect(successSpy).not.toHaveBeenCalled();      errorSpy.reset(); -    cache('GET', '/url', successSpy, errorSpy, false, true); -    $browserXhr.flush(); +    $xhrCache('GET', '/url', successSpy, errorSpy, false, true); +    $browser.xhr.flush();      expect(errorSpy).toHaveBeenCalledWith(500, 'error');      expect(successSpy).not.toHaveBeenCalled(); -  }); +  })); -  it('should call the $xhr.error on error if error callback not provided', function() { +  it('should call the $xhr.error on error if error callback not provided', +      inject(function($browser, $xhrCache, $xhrError) {      var errorSpy = jasmine.createSpy('error'),          successSpy = jasmine.createSpy('success'); -    $browserXhr.expectGET('/url').respond(500, 'error'); -    cache('GET', '/url', null, successSpy, false, true); -    $browserXhr.flush(); +    $browser.xhr.expectGET('/url').respond(500, 'error'); +    $xhrCache('GET', '/url', null, successSpy, false, true); +    $browser.xhr.flush();      expect(successSpy).not.toHaveBeenCalled(); -    expect($xhrErr).toHaveBeenCalledWith( +    expect($xhrError).toHaveBeenCalledWith(        {method: 'GET', url: '/url', data: null, success: successSpy},        {status: 500, body: 'error'}); -  }); +  }));  }); diff --git a/test/service/xhr.errorSpec.js b/test/service/xhr.errorSpec.js index 49b63fd0..0ed5ab59 100644 --- a/test/service/xhr.errorSpec.js +++ b/test/service/xhr.errorSpec.js @@ -1,22 +1,15 @@  'use strict';  describe('$xhr.error', function() { -  var scope, $browser, $browserXhr, $xhr, $xhrError, log; +  var log; -  beforeEach(function() { -    scope = angular.scope(angular.service, { -      '$xhr.error': $xhrError = jasmine.createSpy('$xhr.error') +  beforeEach(inject(function(service) { +    service('$xhr.error', function(){ +      return jasmine.createSpy('$xhr.error');      }); -    $browser = scope.$service('$browser'); -    $browserXhr = $browser.xhr; -    $xhr = scope.$service('$xhr'); +    service.alias('$xhr.error', '$xhrError');      log = ''; -  }); - - -  afterEach(function() { -    dealoc(scope); -  }); +  }));    function callback(code, response) { @@ -25,14 +18,14 @@ describe('$xhr.error', function() {    } -  it('should handle non 200 status codes by forwarding to error handler', function() { -    $browserXhr.expectPOST('/req', 'MyData').respond(500, 'MyError'); +  it('should handle non 200 status codes by forwarding to error handler', inject(function($browser, $xhr, $xhrError) { +    $browser.xhr.expectPOST('/req', 'MyData').respond(500, 'MyError');      $xhr('POST', '/req', 'MyData', callback); -    $browserXhr.flush(); +    $browser.xhr.flush();      var cb = $xhrError.mostRecentCall.args[0].success;      expect(typeof cb).toEqual('function');      expect($xhrError).toHaveBeenCalledWith(          {url: '/req', method: 'POST', data: 'MyData', success: cb},          {status: 500, body: 'MyError'}); -  }); +  }));  }); diff --git a/test/service/xhrSpec.js b/test/service/xhrSpec.js index 2a552403..997994d7 100644 --- a/test/service/xhrSpec.js +++ b/test/service/xhrSpec.js @@ -1,22 +1,16 @@  'use strict';  describe('$xhr', function() { -  var scope, $browser, $browserXhr, $log, $xhr, $xhrErr, log; - -  beforeEach(function() { -    var scope = angular.scope(angular.service, { -        '$xhr.error': $xhrErr = jasmine.createSpy('xhr.error')}); -    $log = scope.$service('$log'); -    $browser = scope.$service('$browser'); -    $browserXhr = $browser.xhr; -    $xhr = scope.$service('$xhr'); -    log = ''; -  }); +  var log; -  afterEach(function() { -    dealoc(scope); -  }); +  beforeEach(inject(function(service) { +    log = ''; +    service('$xhr.error', function(){ +      return jasmine.createSpy('xhr.error'); +    }); +    service.alias('$xhr.error', '$xhrError'); +  }));    function callback(code, response) { @@ -24,246 +18,246 @@ describe('$xhr', function() {    } -  it('should forward the request to $browser and decode JSON', function() { -    $browserXhr.expectGET('/reqGET').respond('first'); -    $browserXhr.expectGET('/reqGETjson').respond('["second"]'); -    $browserXhr.expectPOST('/reqPOST', {post:'data'}).respond('third'); +  it('should forward the request to $browser and decode JSON', inject(function($browser, $xhr) { +    $browser.xhr.expectGET('/reqGET').respond('first'); +    $browser.xhr.expectGET('/reqGETjson').respond('["second"]'); +    $browser.xhr.expectPOST('/reqPOST', {post:'data'}).respond('third');      $xhr('GET', '/reqGET', null, callback);      $xhr('GET', '/reqGETjson', null, callback);      $xhr('POST', '/reqPOST', {post:'data'}, callback); -    $browserXhr.flush(); +    $browser.xhr.flush();      expect(log).toEqual(          '{code=200; response="third"}' +          '{code=200; response=["second"]}' +          '{code=200; response="first"}'); -  }); +  })); -  it('should allow all 2xx requests', function() { -    $browserXhr.expectGET('/req1').respond(200, '1'); +  it('should allow all 2xx requests', inject(function($browser, $xhr) { +    $browser.xhr.expectGET('/req1').respond(200, '1');      $xhr('GET', '/req1', null, callback); -    $browserXhr.flush(); +    $browser.xhr.flush(); -    $browserXhr.expectGET('/req2').respond(299, '2'); +    $browser.xhr.expectGET('/req2').respond(299, '2');      $xhr('GET', '/req2', null, callback); -    $browserXhr.flush(); +    $browser.xhr.flush();      expect(log).toEqual(          '{code=200; response="1"}' +          '{code=299; response="2"}'); -  }); +  })); -  it('should handle exceptions in callback', function() { -    $browserXhr.expectGET('/reqGET').respond('first'); +  it('should handle exceptions in callback', inject(function($browser, $xhr, $log) { +    $browser.xhr.expectGET('/reqGET').respond('first');      $xhr('GET', '/reqGET', null, function() { throw "MyException"; }); -    $browserXhr.flush(); +    $browser.xhr.flush();      expect($log.error.logs.shift()).toContain('MyException'); -  }); +  })); -  it('should automatically deserialize json objects', function() { +  it('should automatically deserialize json objects', inject(function($browser, $xhr) {      var response; -    $browserXhr.expectGET('/foo').respond('{"foo":"bar","baz":23}'); +    $browser.xhr.expectGET('/foo').respond('{"foo":"bar","baz":23}');      $xhr('GET', '/foo', function(code, resp) {        response = resp;      }); -    $browserXhr.flush(); +    $browser.xhr.flush();      expect(response).toEqual({foo:'bar', baz:23}); -  }); +  })); -  it('should automatically deserialize json arrays', function() { +  it('should automatically deserialize json arrays', inject(function($browser, $xhr) {      var response; -    $browserXhr.expectGET('/foo').respond('[1, "abc", {"foo":"bar"}]'); +    $browser.xhr.expectGET('/foo').respond('[1, "abc", {"foo":"bar"}]');      $xhr('GET', '/foo', function(code, resp) {        response = resp;      }); -    $browserXhr.flush(); +    $browser.xhr.flush();      expect(response).toEqual([1, 'abc', {foo:'bar'}]); -  }); +  })); -  it('should automatically deserialize json with security prefix', function() { +  it('should automatically deserialize json with security prefix', inject(function($browser, $xhr) {      var response; -    $browserXhr.expectGET('/foo').respond(')]}\',\n[1, "abc", {"foo":"bar"}]'); +    $browser.xhr.expectGET('/foo').respond(')]}\',\n[1, "abc", {"foo":"bar"}]');      $xhr('GET', '/foo', function(code, resp) {        response = resp;      }); -    $browserXhr.flush(); +    $browser.xhr.flush();      expect(response).toEqual([1, 'abc', {foo:'bar'}]); -  }); +  })); -  it('should call $xhr.error on error if no error callback provided', function() { +  it('should call $xhr.error on error if no error callback provided', inject(function($browser, $xhr, $xhrError) {      var successSpy = jasmine.createSpy('success'); -    $browserXhr.expectGET('/url').respond(500, 'error'); +    $browser.xhr.expectGET('/url').respond(500, 'error');      $xhr('GET', '/url', null, successSpy); -    $browserXhr.flush(); +    $browser.xhr.flush();      expect(successSpy).not.toHaveBeenCalled(); -    expect($xhrErr).toHaveBeenCalledWith( +    expect($xhrError).toHaveBeenCalledWith(        {method: 'GET', url: '/url', data: null, success: successSpy},        {status: 500, body: 'error'}      ); -  }); +  })); -  it('should call the error callback on error if provided', function() { +  it('should call the error callback on error if provided', inject(function($browser, $xhr) {      var errorSpy = jasmine.createSpy('error'),          successSpy = jasmine.createSpy('success'); -    $browserXhr.expectGET('/url').respond(500, 'error'); +    $browser.xhr.expectGET('/url').respond(500, 'error');      $xhr('GET', '/url', null, successSpy, errorSpy); -    $browserXhr.flush(); +    $browser.xhr.flush();      expect(errorSpy).toHaveBeenCalledWith(500, 'error');      expect(successSpy).not.toHaveBeenCalled();      errorSpy.reset();      $xhr('GET', '/url', successSpy, errorSpy); -    $browserXhr.flush(); +    $browser.xhr.flush();      expect(errorSpy).toHaveBeenCalledWith(500, 'error');      expect(successSpy).not.toHaveBeenCalled(); -  }); +  }));    describe('http headers', function() {      describe('default headers', function() { -      it('should set default headers for GET request', function() { +      it('should set default headers for GET request', inject(function($browser, $xhr) {          var callback = jasmine.createSpy('callback'); -        $browserXhr.expectGET('URL', '', {'Accept': 'application/json, text/plain, */*', +        $browser.xhr.expectGET('URL', '', {'Accept': 'application/json, text/plain, */*',                                            'X-Requested-With': 'XMLHttpRequest'}).                      respond(234, 'OK');          $xhr('GET', 'URL', callback); -        $browserXhr.flush(); +        $browser.xhr.flush();          expect(callback).toHaveBeenCalled(); -      }); +      })); -      it('should set default headers for POST request', function() { +      it('should set default headers for POST request', inject(function($browser, $xhr) {          var callback = jasmine.createSpy('callback'); -        $browserXhr.expectPOST('URL', 'xx', {'Accept': 'application/json, text/plain, */*', +        $browser.xhr.expectPOST('URL', 'xx', {'Accept': 'application/json, text/plain, */*',                                               'X-Requested-With': 'XMLHttpRequest',                                               'Content-Type': 'application/x-www-form-urlencoded'}).                      respond(200, 'OK');          $xhr('POST', 'URL', 'xx', callback); -        $browserXhr.flush(); +        $browser.xhr.flush();          expect(callback).toHaveBeenCalled(); -      }); +      })); -      it('should set default headers for custom HTTP method', function() { +      it('should set default headers for custom HTTP method', inject(function($browser, $xhr) {          var callback = jasmine.createSpy('callback'); -        $browserXhr.expect('FOO', 'URL', '', {'Accept': 'application/json, text/plain, */*', +        $browser.xhr.expect('FOO', 'URL', '', {'Accept': 'application/json, text/plain, */*',                                                'X-Requested-With': 'XMLHttpRequest'}).                      respond(200, 'OK');          $xhr('FOO', 'URL', callback); -        $browserXhr.flush(); +        $browser.xhr.flush();          expect(callback).toHaveBeenCalled(); -      }); +      }));        describe('custom headers', function() { -        it('should allow appending a new header to the common defaults', function() { +        it('should allow appending a new header to the common defaults', inject(function($browser, $xhr) {            var callback = jasmine.createSpy('callback'); -          $browserXhr.expectGET('URL', '', {'Accept': 'application/json, text/plain, */*', +          $browser.xhr.expectGET('URL', '', {'Accept': 'application/json, text/plain, */*',                                              'X-Requested-With': 'XMLHttpRequest',                                              'Custom-Header': 'value'}).                        respond(200, 'OK');            $xhr.defaults.headers.common['Custom-Header'] = 'value';            $xhr('GET', 'URL', callback); -          $browserXhr.flush(); +          $browser.xhr.flush();            expect(callback).toHaveBeenCalled();            callback.reset(); -          $browserXhr.expectPOST('URL', 'xx', {'Accept': 'application/json, text/plain, */*', +          $browser.xhr.expectPOST('URL', 'xx', {'Accept': 'application/json, text/plain, */*',                                                 'X-Requested-With': 'XMLHttpRequest',                                                 'Content-Type': 'application/x-www-form-urlencoded',                                                 'Custom-Header': 'value'}).                        respond(200, 'OK');           $xhr('POST', 'URL', 'xx', callback); -         $browserXhr.flush(); +         $browser.xhr.flush();           expect(callback).toHaveBeenCalled(); -        }); +        })); -        it('should allow appending a new header to a method specific defaults', function() { +        it('should allow appending a new header to a method specific defaults', inject(function($browser, $xhr) {            var callback = jasmine.createSpy('callback'); -          $browserXhr.expectGET('URL', '', {'Accept': 'application/json, text/plain, */*', +          $browser.xhr.expectGET('URL', '', {'Accept': 'application/json, text/plain, */*',                                              'X-Requested-With': 'XMLHttpRequest',                                              'Content-Type': 'application/json'}).                        respond(200, 'OK');            $xhr.defaults.headers.get['Content-Type'] = 'application/json';            $xhr('GET', 'URL', callback); -          $browserXhr.flush(); +          $browser.xhr.flush();            expect(callback).toHaveBeenCalled();            callback.reset(); -          $browserXhr.expectPOST('URL', 'x', {'Accept': 'application/json, text/plain, */*', +          $browser.xhr.expectPOST('URL', 'x', {'Accept': 'application/json, text/plain, */*',                                                'X-Requested-With': 'XMLHttpRequest',                                                'Content-Type': 'application/x-www-form-urlencoded'}).                        respond(200, 'OK');           $xhr('POST', 'URL', 'x', callback); -         $browserXhr.flush(); +         $browser.xhr.flush();           expect(callback).toHaveBeenCalled(); -        }); +        })); -        it('should support overwriting and deleting default headers', function() { +        it('should support overwriting and deleting default headers', inject(function($browser, $xhr) {            var callback = jasmine.createSpy('callback'); -          $browserXhr.expectGET('URL', '', {'Accept': 'application/json, text/plain, */*'}). +          $browser.xhr.expectGET('URL', '', {'Accept': 'application/json, text/plain, */*'}).                        respond(200, 'OK');            //delete a default header            delete $xhr.defaults.headers.common['X-Requested-With'];            $xhr('GET', 'URL', callback); -          $browserXhr.flush(); +          $browser.xhr.flush();            expect(callback).toHaveBeenCalled();            callback.reset(); -          $browserXhr.expectPOST('URL', 'xx', {'Accept': 'application/json, text/plain, */*', +          $browser.xhr.expectPOST('URL', 'xx', {'Accept': 'application/json, text/plain, */*',                                                 'Content-Type': 'application/json'}).                        respond(200, 'OK');           //overwrite a default header           $xhr.defaults.headers.post['Content-Type'] = 'application/json';           $xhr('POST', 'URL', 'xx', callback); -         $browserXhr.flush(); +         $browser.xhr.flush();           expect(callback).toHaveBeenCalled(); -        }); +        }));        });      });    });    describe('xsrf', function() { -    it('should copy the XSRF cookie into a XSRF Header', function() { +    it('should copy the XSRF cookie into a XSRF Header', inject(function($browser, $xhr) {        var code, response; -      $browserXhr +      $browser.xhr          .expectPOST('URL', 'DATA', {'X-XSRF-TOKEN': 'secret'})          .respond(234, 'OK');        $browser.cookies('XSRF-TOKEN', 'secret'); @@ -271,9 +265,9 @@ describe('$xhr', function() {          code = c;          response = r;        }); -      $browserXhr.flush(); +      $browser.xhr.flush();        expect(code).toEqual(234);        expect(response).toEqual('OK'); -    }); +    }));    });  }); diff --git a/test/testabilityPatch.js b/test/testabilityPatch.js index a8b1c06b..3d964149 100644 --- a/test/testabilityPatch.js +++ b/test/testabilityPatch.js @@ -17,7 +17,7 @@ if (window.jstestdriver) {        if (isElement(arg)) {          arg = sortedHtml(arg);        } else if (isObject(arg)) { -        if (arg.$eval == Scope.prototype.$eval) { +        if (isFunction(arg.$eval) && isFunction(arg.$apply)) {            arg = dumpScope(arg);          } else {            arg = toJson(arg, true); @@ -79,9 +79,79 @@ beforeEach(function() {    $logMock.warn.logs = [];    $logMock.info.logs = [];    $logMock.error.logs = []; + +  resetAngularPublic()  }); -afterEach(function() { +function inject(){ +  var blockFns = sliceArgs(arguments); +  return function(){ +    var spec = this; +    angular.forEach(blockFns, function(fn){ +      fn.$inject = inferInjectionArgs(fn); +      if (equals(fn.$inject, [])) { +        fn.apply(spec); +      } else if (equals(fn.$inject, ['service'])) { +        if (spec.$injector) { +          throw Error('$injector already created for this test'); +        } +        if (!spec.$service) { +          spec.$service = function(name, fn) { +            if (fn) { spec.$service[name] = fn; } +            return spec.$service[name]; +          } +          spec.$service.alias = function (name, alias) { +            spec.$service(alias, extend(function(x){ return x; }, {$inject:[name]})); +          }; +          forEach(angularService, function(value, key){ +            spec.$service(key, value); +          }); +        } +        fn.call(spec, spec.$service); +      } else { +        if (!spec.$injector) { +          spec.$injector = angular.injector(spec.$service); +        } +        spec.$injector.invoke(spec, fn); +      } +    }); +  }; +} + +/** + * This method republishes the public angular API. It should probably be cleaned up somehow. + * //TODO: remove this method and merge it with the angularPublic.js class + */ +function resetAngularPublic() { +  extend(angular, { +    'element': jqLite, +    'compile': compile, +    'copy': copy, +    'extend': extend, +    'equals': equals, +    'forEach': forEach, +    'noop': noop, +    'bind': bind, +    'toJson': toJson, +    'fromJson': fromJson, +    'identity':identity, +    'injector': createInjector, +    'isUndefined': isUndefined, +    'isDefined': isDefined, +    'isString': isString, +    'isFunction': isFunction, +    'isObject': isObject, +    'isNumber': isNumber, +    'isArray': isArray +  }); +} + +resetAngularPublic(); + +afterEach(inject(function($rootScope) { +  // release the injector +  dealoc($rootScope); +    // check $log mock    forEach(['error', 'warn', 'info', 'log'], function(logLevel) {      if ($logMock[logLevel].logs.length) { @@ -104,7 +174,7 @@ afterEach(function() {    });    clearJqCache(); -}); +}));  function clearJqCache() {    var count = 0; @@ -140,29 +210,6 @@ function dealoc(obj) {    }  } -extend(angular, { -  'element': jqLite, -  'compile': compile, -  'scope': createScope, -  'copy': copy, -  'extend': extend, -  'equals': equals, -  'forEach': forEach, -  'noop':noop, -  'bind':bind, -  'toJson': toJson, -  'fromJson': fromJson, -  'identity':identity, -  'injector': createInjector, -  'isUndefined': isUndefined, -  'isDefined': isDefined, -  'isString': isString, -  'isFunction': isFunction, -  'isObject': isObject, -  'isNumber': isNumber, -  'isArray': isArray -}); -  function sortedHtml(element, showNgClass) {    var html = ""; diff --git a/test/widget/formSpec.js b/test/widget/formSpec.js index 4f5630ba..a30f21eb 100644 --- a/test/widget/formSpec.js +++ b/test/widget/formSpec.js @@ -8,17 +8,17 @@ describe('form', function() {    }); -  it('should attach form to DOM', function() { +  it('should attach form to DOM', inject(function($rootScope) {      doc = angular.element('<form>'); -    var scope = angular.compile(doc)(); +    angular.compile(doc)($rootScope);      expect(doc.data('$form')).toBeTruthy(); -  }); +  })); -  it('should prevent form submission', function() { +  it('should prevent form submission', inject(function($rootScope) {      var startingUrl = '' + window.location;      doc = angular.element('<form name="myForm"><input type=submit val=submit>'); -    var scope = angular.compile(doc)(); +    angular.compile(doc)($rootScope);      browserTrigger(doc.find('input'));      waitsFor(          function() { return true; }, @@ -26,44 +26,44 @@ describe('form', function() {      runs(function() {        expect('' + window.location).toEqual(startingUrl);      }); -  }); +  })); -  it('should publish form to scope', function() { +  it('should publish form to scope', inject(function($rootScope) {      doc = angular.element('<form name="myForm"></form>'); -    var scope = angular.compile(doc)(); -    expect(scope.myForm).toBeTruthy(); +    angular.compile(doc)($rootScope); +    expect($rootScope.myForm).toBeTruthy();      expect(doc.data('$form')).toBeTruthy(); -    expect(doc.data('$form')).toEqual(scope.myForm); -  }); +    expect(doc.data('$form')).toEqual($rootScope.myForm); +  })); -  it('should have ng-valide/ng-invalid style', function() { +  it('should have ng-valide/ng-invalid style', inject(function($rootScope) {      doc = angular.element('<form name="myForm"><input type=text ng:model=text required>'); -    var scope = angular.compile(doc)(); -    scope.text = 'misko'; -    scope.$digest(); +    angular.compile(doc)($rootScope); +    $rootScope.text = 'misko'; +    $rootScope.$digest();      expect(doc.hasClass('ng-valid')).toBe(true);      expect(doc.hasClass('ng-invalid')).toBe(false); -    scope.text = ''; -    scope.$digest(); +    $rootScope.text = ''; +    $rootScope.$digest();      expect(doc.hasClass('ng-valid')).toBe(false);      expect(doc.hasClass('ng-invalid')).toBe(true); -  }); +  })); -  it('should chain nested forms', function() { +  it('should chain nested forms', inject(function($rootScope) {      doc = angular.element(          '<ng:form name=parent>' +            '<ng:form name=child>' +              '<input type=text ng:model=text name=text>' +            '</ng:form>' +          '</ng:form>'); -    var scope = angular.compile(doc)(); -    var parent = scope.parent; -    var child = scope.child; +    angular.compile(doc)($rootScope); +    var parent = $rootScope.parent; +    var child = $rootScope.child;      var input = child.text;      input.$emit('$invalid', 'MyError'); @@ -73,21 +73,21 @@ describe('form', function() {      input.$emit('$valid', 'MyError');      expect(parent.$error.MyError).toBeUndefined();      expect(child.$error.MyError).toBeUndefined(); -  }); +  })); -  it('should chain nested forms in repeater', function() { +  it('should chain nested forms in repeater', inject(function($rootScope) {      doc = angular.element(         '<ng:form name=parent>' +          '<ng:form ng:repeat="f in forms" name=child>' +            '<input type=text ng:model=text name=text>' +           '</ng:form>' +         '</ng:form>'); -    var scope = angular.compile(doc)(); -    scope.forms = [1]; -    scope.$digest(); +    angular.compile(doc)($rootScope); +    $rootScope.forms = [1]; +    $rootScope.$digest(); -    var parent = scope.parent; +    var parent = $rootScope.parent;      var child = doc.find('input').scope().child;      var input = child.text;      expect(parent).toBeDefined(); @@ -102,5 +102,5 @@ describe('form', function() {      input.$emit('$valid', 'myRule');      expect(parent.$error.myRule).toBeUndefined();      expect(child.$error.myRule).toBeUndefined(); -  }); +  }));  }); diff --git a/test/widget/inputSpec.js b/test/widget/inputSpec.js index 179f8156..a889212f 100644 --- a/test/widget/inputSpec.js +++ b/test/widget/inputSpec.js @@ -4,8 +4,8 @@ describe('widget: input', function() {    var compile = null, element = null, scope = null, defer = null;    var doc = null; -  beforeEach(function() { -    scope = null; +  beforeEach(inject(function($rootScope) { +    scope = $rootScope;      element = null;      compile = function(html, parent) {        if (parent) { @@ -14,12 +14,12 @@ describe('widget: input', function() {        } else {          element = jqLite(html);        } -      scope = angular.compile(element)(); +      angular.compile(element)(scope);        scope.$apply();        defer = scope.$service('$browser').defer;        return scope;      }; -  }); +  }));    afterEach(function() {      dealoc(element); @@ -28,8 +28,7 @@ describe('widget: input', function() {    describe('text', function() { -    var scope = null, -        form = null, +    var form = null,          formElement = null,          inputElement = null; @@ -41,7 +40,7 @@ describe('widget: input', function() {        formElement = doc = angular.element('<form name="form"><input ' + prefix +            'type="text" ng:model="name" name="name" ng:change="change()"></form>');        inputElement = formElement.find('input'); -      scope = angular.compile(doc)(); +      angular.compile(doc)(scope);        form = formElement.inheritedData('$form');      }; @@ -91,18 +90,18 @@ describe('widget: input', function() {      }); -    it('should change non-html5 types to text', function() { +    it('should change non-html5 types to text', inject(function($rootScope) {        doc = angular.element('<form name="form"><input type="abc" ng:model="name"></form>'); -      scope = angular.compile(doc)(); +      angular.compile(doc)($rootScope);        expect(doc.find('input').attr('type')).toEqual('text'); -    }); +    })); -    it('should not change html5 types to text', function() { +    it('should not change html5 types to text', inject(function($rootScope) {        doc = angular.element('<form name="form"><input type="number" ng:model="name"></form>'); -      scope = angular.compile(doc)(); +      angular.compile(doc)($rootScope);        expect(doc.find('input')[0].getAttribute('type')).toEqual('number'); -    }); +    }));    }); @@ -455,35 +454,33 @@ describe('widget: input', function() {    describe('scope declaration', function() { -    it('should read the declaration from scope', function() { +    it('should read the declaration from scope', inject(function($rootScope) {        var input, $formFactory;        element = angular.element('<input type="@MyType" ng:model="abc">'); -      scope = angular.scope(); -      scope.MyType = function($f, i) { +      $rootScope.MyType = function($f, i) {          input = i;          $formFactory = $f;        }; -      scope.MyType.$inject = ['$formFactory']; +      $rootScope.MyType.$inject = ['$formFactory']; -      angular.compile(element)(scope); +      angular.compile(element)($rootScope); -      expect($formFactory).toBe(scope.$service('$formFactory')); +      expect($formFactory).toBe($rootScope.$service('$formFactory'));        expect(input[0]).toBe(element[0]); -    }); +    })); -    it('should throw an error of Cntoroller not declared in scope', function() { +    it('should throw an error of Controller not declared in scope', inject(function($rootScope) {        var input, $formFactory;        element = angular.element('<input type="@DontExist" ng:model="abc">');        var error;        try { -        scope = angular.scope(); -        angular.compile(element)(scope); +        angular.compile(element)($rootScope);          error = 'no error thrown';        } catch (e) {          error = e;        }        expect(error.message).toEqual("Argument 'DontExist' is not a function, got undefined"); -    }); +    }));    }); @@ -580,16 +577,16 @@ describe('widget: input', function() {          {'ng:maxlength': 3}); -    it('should throw an error when scope pattern can\'t be found', function() { -      var el = jqLite('<input ng:model="foo" ng:pattern="fooRegexp">'), -          scope = angular.compile(el)(); +    it('should throw an error when scope pattern can\'t be found', inject(function($rootScope) { +      var el = jqLite('<input ng:model="foo" ng:pattern="fooRegexp">'); +      angular.compile(el)($rootScope);        el.val('xx');        browserTrigger(el, 'keydown'); -      expect(function() { scope.$service('$browser').defer.flush(); }). +      expect(function() { $rootScope.$service('$browser').defer.flush(); }).          toThrow('Expected fooRegexp to be a RegExp but was undefined');        dealoc(el); -    }); +    }));    });  }); diff --git a/test/widget/selectSpec.js b/test/widget/selectSpec.js index c3fdc2e6..31e5223d 100644 --- a/test/widget/selectSpec.js +++ b/test/widget/selectSpec.js @@ -1,10 +1,10 @@  'use strict';  describe('select', function() { -  var compile = null, element = null, scope = null, $formFactory = null; +  var compile = null, element = null, scope = null; -  beforeEach(function() { -    scope = null; +  beforeEach(inject(function($rootScope) { +    scope = $rootScope;      element = null;      compile = function(html, parent) {        if (parent) { @@ -13,12 +13,11 @@ describe('select', function() {        } else {          element = jqLite(html);        } -      scope = angular.compile(element)(); +      angular.compile(element)($rootScope);        scope.$apply(); -      $formFactory = scope.$service('$formFactory');        return scope;      }; -  }); +  }));    afterEach(function() {      dealoc(element); @@ -41,7 +40,7 @@ describe('select', function() {        expect(scope.$element.text()).toBe('foobarC');      }); -    it('should require', function() { +    it('should require', inject(function($formFactory) {        compile('<select name="select" ng:model="selection" required ng:change="log=log+\'change;\'">' +            '<option value=""></option>' +            '<option value="c">C</option>' + @@ -65,7 +64,7 @@ describe('select', function() {        expect(element).toBeValid();        expect(element).toBeDirty();        expect(scope.log).toEqual('change;'); -    }); +    }));      it('should not be invalid if no require', function() {        compile('<select name="select" ng:model="selection">' + @@ -91,7 +90,7 @@ describe('select', function() {        expect(element[0].childNodes[0].selected).toEqual(true);      }); -    it('should require', function() { +    it('should require', inject(function($formFactory) {        compile('<select name="select" ng:model="selection" multiple required>' +            '<option>A</option>' +            '<option>B</option>' + @@ -112,7 +111,7 @@ describe('select', function() {        browserTrigger(element, 'change');        expect(element).toBeValid();        expect(element).toBeDirty(); -    }); +    }));    }); @@ -157,12 +156,12 @@ describe('select', function() {        dealoc(scope);      }); -    it('should throw when not formated "? for ? in ?"', function() { +    it('should throw when not formated "? for ? in ?"', inject(function($rootScope, $exceptionHandler) {        expect(function() {          compile('<select ng:model="selected" ng:options="i dont parse"></select>');        }).toThrow("Expected ng:options in form of '_select_ (as _label_)? for (_key_,)?_value_ in" +                   " _collection_' but got 'i dont parse'."); -    }); +    }));      it('should render a list', function() {        createSingleSelect(); diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js index e75b2592..1a4c5e6c 100644 --- a/test/widgetsSpec.js +++ b/test/widgetsSpec.js @@ -1,165 +1,145 @@  'use strict';  describe("widget", function() { -  var compile = null, element = null, scope = null; - -  beforeEach(function() { -    scope = null; -    element = null; -    compile = function(html, parent) { -      if (parent) { -        parent.html(html); -        element = parent.children(); -      } else { -        element = jqLite(html); -      } -      scope = angular.compile(element)(); -      scope.$apply(); -      return scope; -    }; -  }); - -  afterEach(function() { -    dealoc(element); -  }); - - -  describe('ng:switch', function() { -    it('should switch on value change', function() { -      compile('<ng:switch on="select">' + +  describe('ng:switch', inject(function($rootScope) { +    it('should switch on value change', inject(function($rootScope) { +      var element = angular.compile('<ng:switch on="select">' +            '<div ng:switch-when="1">first:{{name}}</div>' +            '<div ng:switch-when="2">second:{{name}}</div>' +            '<div ng:switch-when="true">true:{{name}}</div>' + -        '</ng:switch>'); +        '</ng:switch>')($rootScope);        expect(element.html()).toEqual(''); -      scope.select = 1; -      scope.$apply(); +      $rootScope.select = 1; +      $rootScope.$apply();        expect(element.text()).toEqual('first:'); -      scope.name="shyam"; -      scope.$apply(); +      $rootScope.name="shyam"; +      $rootScope.$apply();        expect(element.text()).toEqual('first:shyam'); -      scope.select = 2; -      scope.$apply(); +      $rootScope.select = 2; +      $rootScope.$apply();        expect(element.text()).toEqual('second:shyam'); -      scope.name = 'misko'; -      scope.$apply(); +      $rootScope.name = 'misko'; +      $rootScope.$apply();        expect(element.text()).toEqual('second:misko'); -      scope.select = true; -      scope.$apply(); +      $rootScope.select = true; +      $rootScope.$apply();        expect(element.text()).toEqual('true:misko'); -    }); - -    it('should switch on switch-when-default', function() { -      compile('<ng:switch on="select">' + -                '<div ng:switch-when="1">one</div>' + -                '<div ng:switch-default>other</div>' + -              '</ng:switch>'); -      scope.$apply(); +    })); +     + +    it('should switch on switch-when-default', inject(function($rootScope) { +      var element = angular.compile( +        '<ng:switch on="select">' + +          '<div ng:switch-when="1">one</div>' + +          '<div ng:switch-default>other</div>' + +        '</ng:switch>')($rootScope); +      $rootScope.$apply();        expect(element.text()).toEqual('other'); -      scope.select = 1; -      scope.$apply(); +      $rootScope.select = 1; +      $rootScope.$apply();        expect(element.text()).toEqual('one'); -    }); - -    it('should call change on switch', function() { -      var scope = angular.compile('<ng:switch on="url" change="name=\'works\'"><div ng:switch-when="a">{{name}}</div></ng:switch>')(); -      scope.url = 'a'; -      scope.$apply(); -      expect(scope.name).toEqual(undefined); -      expect(scope.$element.text()).toEqual('works'); -      dealoc(scope); -    }); - -  }); - - -  describe('ng:include', function() { -    it('should include on external file', function() { +    })); + +     +    it('should call change on switch', inject(function($rootScope) { +      var element = angular.compile( +        '<ng:switch on="url" change="name=\'works\'">' + +          '<div ng:switch-when="a">{{name}}</div>' + +        '</ng:switch>')($rootScope); +      $rootScope.url = 'a'; +      $rootScope.$apply(); +      expect($rootScope.name).toEqual(undefined); +      expect(element.text()).toEqual('works'); +    })); +  })); + + +  describe('ng:include', inject(function($rootScope) { +    it('should include on external file', inject(function($rootScope) {        var element = jqLite('<ng:include src="url" scope="childScope"></ng:include>'); -      var scope = angular.compile(element)(); -      scope.childScope = scope.$new(); -      scope.childScope.name = 'misko'; -      scope.url = 'myUrl'; -      scope.$service('$xhr.cache').data.myUrl = {value:'{{name}}'}; -      scope.$digest(); +      var element = angular.compile(element)($rootScope); +      $rootScope.childScope = $rootScope.$new(); +      $rootScope.childScope.name = 'misko'; +      $rootScope.url = 'myUrl'; +      $rootScope.$service('$xhr.cache').data.myUrl = {value:'{{name}}'}; +      $rootScope.$digest();        expect(element.text()).toEqual('misko'); -      dealoc(scope); -    }); +    })); -    it('should remove previously included text if a falsy value is bound to src', function() { +     +    it('should remove previously included text if a falsy value is bound to src', inject(function($rootScope) {        var element = jqLite('<ng:include src="url" scope="childScope"></ng:include>'); -      var scope = angular.compile(element)(); -      scope.childScope = scope.$new(); -      scope.childScope.name = 'igor'; -      scope.url = 'myUrl'; -      scope.$service('$xhr.cache').data.myUrl = {value:'{{name}}'}; -      scope.$digest(); +      var element = angular.compile(element)($rootScope); +      $rootScope.childScope = $rootScope.$new(); +      $rootScope.childScope.name = 'igor'; +      $rootScope.url = 'myUrl'; +      $rootScope.$service('$xhr.cache').data.myUrl = {value:'{{name}}'}; +      $rootScope.$digest();        expect(element.text()).toEqual('igor'); -      scope.url = undefined; -      scope.$digest(); +      $rootScope.url = undefined; +      $rootScope.$digest();        expect(element.text()).toEqual(''); -      dealoc(scope); -    }); +    })); -    it('should allow this for scope', function() { +     +    it('should allow this for scope', inject(function($rootScope) {        var element = jqLite('<ng:include src="url" scope="this"></ng:include>'); -      var scope = angular.compile(element)(); -      scope.url = 'myUrl'; -      scope.$service('$xhr.cache').data.myUrl = {value:'{{"abc"}}'}; -      scope.$digest(); +      var element = angular.compile(element)($rootScope); +      $rootScope.url = 'myUrl'; +      $rootScope.$service('$xhr.cache').data.myUrl = {value:'{{"abc"}}'}; +      $rootScope.$digest();        // TODO(misko): because we are using scope==this, the eval gets registered        // during the flush phase and hence does not get called.        // I don't think passing 'this' makes sense. Does having scope on ng:include makes sense? -      // should we make scope="this" ilegal? -      scope.$digest(); +      // should we make scope="this" illegal? +      $rootScope.$digest();        expect(element.text()).toEqual('abc'); -      dealoc(element); -    }); +    })); -    it('should evaluate onload expression when a partial is loaded', function() { +     +    it('should evaluate onload expression when a partial is loaded', inject(function($rootScope) {        var element = jqLite('<ng:include src="url" onload="loaded = true"></ng:include>'); -      var scope = angular.compile(element)(); +      var element = angular.compile(element)($rootScope); -      expect(scope.loaded).not.toBeDefined(); +      expect($rootScope.loaded).not.toBeDefined(); -      scope.url = 'myUrl'; -      scope.$service('$xhr.cache').data.myUrl = {value:'my partial'}; -      scope.$digest(); +      $rootScope.url = 'myUrl'; +      $rootScope.$service('$xhr.cache').data.myUrl = {value:'my partial'}; +      $rootScope.$digest();        expect(element.text()).toEqual('my partial'); -      expect(scope.loaded).toBe(true); -      dealoc(element); -    }); +      expect($rootScope.loaded).toBe(true); +    })); -    it('should destroy old scope', function() { +     +    it('should destroy old scope', inject(function($rootScope) {        var element = jqLite('<ng:include src="url"></ng:include>'); -      var scope = angular.compile(element)(); +      var element = angular.compile(element)($rootScope); -      expect(scope.$$childHead).toBeFalsy(); +      expect($rootScope.$$childHead).toBeFalsy(); -      scope.url = 'myUrl'; -      scope.$service('$xhr.cache').data.myUrl = {value:'my partial'}; -      scope.$digest(); -      expect(scope.$$childHead).toBeTruthy(); +      $rootScope.url = 'myUrl'; +      $rootScope.$service('$xhr.cache').data.myUrl = {value:'my partial'}; +      $rootScope.$digest(); +      expect($rootScope.$$childHead).toBeTruthy(); -      scope.url = null; -      scope.$digest(); -      expect(scope.$$childHead).toBeFalsy(); -      dealoc(element); -    }); -  }); +      $rootScope.url = null; +      $rootScope.$digest(); +      expect($rootScope.$$childHead).toBeFalsy(); +    })); +  })); -  describe('a', function() { -    it('should prevent default action to be executed when href is empty', function() { +  describe('a', inject(function($rootScope) { +    it('should prevent default action to be executed when href is empty', inject(function($rootScope) {        var orgLocation = document.location.href,            preventDefaultCalled = false,            event; -      compile('<a href="">empty link</a>'); +      var element = angular.compile('<a href="">empty link</a>')($rootScope);        if (msie < 9) { @@ -185,185 +165,201 @@ describe("widget", function() {        }        expect(document.location.href).toEqual(orgLocation); -    }); -  }); +    })); +  })); -  describe('@ng:repeat', function() { -    it('should ng:repeat over array', function() { -      var scope = compile('<ul><li ng:repeat="item in items" ng:init="suffix = \';\'" ng:bind="item + suffix"></li></ul>'); +  describe('@ng:repeat', inject(function($rootScope) { +    it('should ng:repeat over array', inject(function($rootScope) { +      var element = angular.compile( +        '<ul>' + +          '<li ng:repeat="item in items" ng:init="suffix = \';\'" ng:bind="item + suffix"></li>' + +        '</ul>')($rootScope);        Array.prototype.extraProperty = "should be ignored";        // INIT -      scope.items = ['misko', 'shyam']; -      scope.$digest(); +      $rootScope.items = ['misko', 'shyam']; +      $rootScope.$digest();        expect(element.find('li').length).toEqual(2);        expect(element.text()).toEqual('misko;shyam;');        delete Array.prototype.extraProperty;        // GROW -      scope.items = ['adam', 'kai', 'brad']; -      scope.$digest(); +      $rootScope.items = ['adam', 'kai', 'brad']; +      $rootScope.$digest();        expect(element.find('li').length).toEqual(3);        expect(element.text()).toEqual('adam;kai;brad;');        // SHRINK -      scope.items = ['brad']; -      scope.$digest(); +      $rootScope.items = ['brad']; +      $rootScope.$digest();        expect(element.find('li').length).toEqual(1);        expect(element.text()).toEqual('brad;'); -    }); +    })); -    it('should ng:repeat over object', function() { -      var scope = compile('<ul><li ng:repeat="(key, value) in items" ng:bind="key + \':\' + value + \';\' "></li></ul>'); -      scope.items = {misko:'swe', shyam:'set'}; -      scope.$digest(); +    it('should ng:repeat over object', inject(function($rootScope) { +      var element = angular.compile( +        '<ul>' + +          '<li ng:repeat="(key, value) in items" ng:bind="key + \':\' + value + \';\' "></li>' + +        '</ul>')($rootScope); +      $rootScope.items = {misko:'swe', shyam:'set'}; +      $rootScope.$digest();        expect(element.text()).toEqual('misko:swe;shyam:set;'); -    }); +    })); -    it('should not ng:repeat over parent properties', function() { +    it('should not ng:repeat over parent properties', inject(function($rootScope) {        var Class = function() {};        Class.prototype.abc = function() {};        Class.prototype.value = 'abc'; -      var scope = compile('<ul><li ng:repeat="(key, value) in items" ng:bind="key + \':\' + value + \';\' "></li></ul>'); -      scope.items = new Class(); -      scope.items.name = 'value'; -      scope.$digest(); +      var element = angular.compile( +        '<ul>' + +          '<li ng:repeat="(key, value) in items" ng:bind="key + \':\' + value + \';\' "></li>' + +        '</ul>')($rootScope); +      $rootScope.items = new Class(); +      $rootScope.items.name = 'value'; +      $rootScope.$digest();        expect(element.text()).toEqual('name:value;'); -    }); +    })); -    it('should error on wrong parsing of ng:repeat', function() { +    it('should error on wrong parsing of ng:repeat', inject(function($rootScope) {        expect(function() { -        compile('<ul><li ng:repeat="i dont parse"></li></ul>'); +        var element = angular.compile('<ul><li ng:repeat="i dont parse"></li></ul>')($rootScope);        }).toThrow("Expected ng:repeat in form of '_item_ in _collection_' but got 'i dont parse'.");        $logMock.error.logs.shift(); -    }); +    })); -    it('should expose iterator offset as $index when iterating over arrays', function() { -      var scope = compile('<ul><li ng:repeat="item in items" ' + -                                  'ng:bind="item + $index + \'|\'"></li></ul>'); -      scope.items = ['misko', 'shyam', 'frodo']; -      scope.$digest(); +    it('should expose iterator offset as $index when iterating over arrays', inject(function($rootScope) { +      var element = angular.compile( +        '<ul>' + +          '<li ng:repeat="item in items" ng:bind="item + $index + \'|\'"></li>' + +        '</ul>')($rootScope); +      $rootScope.items = ['misko', 'shyam', 'frodo']; +      $rootScope.$digest();        expect(element.text()).toEqual('misko0|shyam1|frodo2|'); -    }); +    })); -    it('should expose iterator offset as $index when iterating over objects', function() { -      var scope = compile('<ul><li ng:repeat="(key, val) in items" ' + -                                  'ng:bind="key + \':\' + val + $index + \'|\'"></li></ul>'); -      scope.items = {'misko':'m', 'shyam':'s', 'frodo':'f'}; -      scope.$digest(); +    it('should expose iterator offset as $index when iterating over objects', inject(function($rootScope) { +      var element = angular.compile( +        '<ul>' + +          '<li ng:repeat="(key, val) in items" ng:bind="key + \':\' + val + $index + \'|\'"></li>' + +        '</ul>')($rootScope); +      $rootScope.items = {'misko':'m', 'shyam':'s', 'frodo':'f'}; +      $rootScope.$digest();        expect(element.text()).toEqual('frodo:f0|misko:m1|shyam:s2|'); -    }); +    })); -    it('should expose iterator position as $position when iterating over arrays', function() { -      var scope = compile('<ul><li ng:repeat="item in items" ' + -                                  'ng:bind="item + \':\' + $position + \'|\'"></li></ul>'); -      scope.items = ['misko', 'shyam', 'doug']; -      scope.$digest(); +    it('should expose iterator position as $position when iterating over arrays', +        inject(function($rootScope) { +      var element = angular.compile( +        '<ul>' + +          '<li ng:repeat="item in items" ng:bind="item + \':\' + $position + \'|\'"></li>' + +        '</ul>')($rootScope); +      $rootScope.items = ['misko', 'shyam', 'doug']; +      $rootScope.$digest();        expect(element.text()).toEqual('misko:first|shyam:middle|doug:last|'); -      scope.items.push('frodo'); -      scope.$digest(); +      $rootScope.items.push('frodo'); +      $rootScope.$digest();        expect(element.text()).toEqual('misko:first|shyam:middle|doug:middle|frodo:last|'); -      scope.items.pop(); -      scope.items.pop(); -      scope.$digest(); +      $rootScope.items.pop(); +      $rootScope.items.pop(); +      $rootScope.$digest();        expect(element.text()).toEqual('misko:first|shyam:last|'); -    }); +    })); -    it('should expose iterator position as $position when iterating over objects', function() { -      var scope = compile( +    it('should expose iterator position as $position when iterating over objects', inject(function($rootScope) { +      var element = angular.compile(          '<ul>' +            '<li ng:repeat="(key, val) in items" ng:bind="key + \':\' + val + \':\' + $position + \'|\'">' +            '</li>' + -        '</ul>'); -      scope.items = {'misko':'m', 'shyam':'s', 'doug':'d', 'frodo':'f'}; -      scope.$digest(); +        '</ul>')($rootScope); +      $rootScope.items = {'misko':'m', 'shyam':'s', 'doug':'d', 'frodo':'f'}; +      $rootScope.$digest();        expect(element.text()).toEqual('doug:d:first|frodo:f:middle|misko:m:middle|shyam:s:last|'); -      delete scope.items.doug; -      delete scope.items.frodo; -      scope.$digest(); +      delete $rootScope.items.doug; +      delete $rootScope.items.frodo; +      $rootScope.$digest();        expect(element.text()).toEqual('misko:m:first|shyam:s:last|'); -    }); +    })); -    it('should ignore $ and $$ properties', function() { -      var scope = compile('<ul><li ng:repeat="i in items">{{i}}|</li></ul>'); -      scope.items = ['a', 'b', 'c']; -      scope.items.$$hashkey = 'xxx'; -      scope.items.$root = 'yyy'; -      scope.$digest(); +    it('should ignore $ and $$ properties', inject(function($rootScope) { +      var element = angular.compile('<ul><li ng:repeat="i in items">{{i}}|</li></ul>')($rootScope); +      $rootScope.items = ['a', 'b', 'c']; +      $rootScope.items.$$hashkey = 'xxx'; +      $rootScope.items.$root = 'yyy'; +      $rootScope.$digest();        expect(element.text()).toEqual('a|b|c|'); -    }); +    })); -    it('should repeat over nested arrays', function() { -      var scope = compile('<ul>' + -                            '<li ng:repeat="subgroup in groups">' + -                              '<div ng:repeat="group in subgroup">{{group}}|</div>X' + -                            '</li>' + -                           '</ul>'); -      scope.groups = [['a', 'b'], ['c','d']]; -      scope.$digest(); +    it('should repeat over nested arrays', inject(function($rootScope) { +      var element = angular.compile( +        '<ul>' + +          '<li ng:repeat="subgroup in groups">' + +            '<div ng:repeat="group in subgroup">{{group}}|</div>X' + +          '</li>' + +        '</ul>')($rootScope); +      $rootScope.groups = [['a', 'b'], ['c','d']]; +      $rootScope.$digest();        expect(element.text()).toEqual('a|b|Xc|d|X'); -    }); +    })); -    it('should ignore non-array element properties when iterating over an array', function() { -      var scope = compile('<ul><li ng:repeat="item in array">{{item}}|</li></ul>'); -      scope.array = ['a', 'b', 'c']; -      scope.array.foo = '23'; -      scope.array.bar = function() {}; -      scope.$digest(); +    it('should ignore non-array element properties when iterating over an array', inject(function($rootScope) { +      var element = angular.compile('<ul><li ng:repeat="item in array">{{item}}|</li></ul>')($rootScope); +      $rootScope.array = ['a', 'b', 'c']; +      $rootScope.array.foo = '23'; +      $rootScope.array.bar = function() {}; +      $rootScope.$digest();        expect(element.text()).toBe('a|b|c|'); -    }); +    })); -    it('should iterate over non-existent elements of a sparse array', function() { -      var scope = compile('<ul><li ng:repeat="item in array">{{item}}|</li></ul>'); -      scope.array = ['a', 'b']; -      scope.array[4] = 'c'; -      scope.array[6] = 'd'; -      scope.$digest(); +    it('should iterate over non-existent elements of a sparse array', inject(function($rootScope) { +      var element = angular.compile('<ul><li ng:repeat="item in array">{{item}}|</li></ul>')($rootScope); +      $rootScope.array = ['a', 'b']; +      $rootScope.array[4] = 'c'; +      $rootScope.array[6] = 'd'; +      $rootScope.$digest();        expect(element.text()).toBe('a|b|||c||d|'); -    }); +    }));      describe('stability', function() { -      var a, b, c, d, scope, lis; +      var a, b, c, d, lis, element; -      beforeEach(function() { -        scope = compile( +      beforeEach(inject(function($rootScope) { +        element = angular.compile(            '<ul>' + -            '<li ng:repeat="item in items" ng:bind="key + \':\' + val + \':\' + $position + \'|\'">' + -            '</li>' + -          '</ul>'); +            '<li ng:repeat="item in items" ng:bind="key + \':\' + val + \':\' + $position + \'|\'"></li>' + +          '</ul>')($rootScope);          a = {};          b = {};          c = {};          d = {}; -        scope.items = [a, b, c]; -        scope.$digest(); +        $rootScope.items = [a, b, c]; +        $rootScope.$digest();          lis = element.find('li'); -      }); +      })); -      it('should preserve the order of elements', function() { -        scope.items = [a, c, d]; -        scope.$digest(); +      it('should preserve the order of elements', inject(function($rootScope) { +        $rootScope.items = [a, c, d]; +        $rootScope.$digest();          var newElements = element.find('li');          expect(newElements[0]).toEqual(lis[0]);          expect(newElements[1]).toEqual(lis[2]);          expect(newElements[2]).not.toEqual(lis[1]); -      }); +      })); -      it('should support duplicates', function() { -        scope.items = [a, a, b, c]; -        scope.$digest(); +      it('should support duplicates', inject(function($rootScope) { +        $rootScope.items = [a, a, b, c]; +        $rootScope.$digest();          var newElements = element.find('li');          expect(newElements[0]).toEqual(lis[0]);          expect(newElements[1]).not.toEqual(lis[0]); @@ -371,170 +367,163 @@ describe("widget", function() {          expect(newElements[3]).toEqual(lis[2]);          lis = newElements; -        scope.$digest(); +        $rootScope.$digest();          newElements = element.find('li');          expect(newElements[0]).toEqual(lis[0]);          expect(newElements[1]).toEqual(lis[1]);          expect(newElements[2]).toEqual(lis[2]);          expect(newElements[3]).toEqual(lis[3]); -        scope.$digest(); +        $rootScope.$digest();          newElements = element.find('li');          expect(newElements[0]).toEqual(lis[0]);          expect(newElements[1]).toEqual(lis[1]);          expect(newElements[2]).toEqual(lis[2]);          expect(newElements[3]).toEqual(lis[3]); -      }); +      })); -      it('should remove last item when one duplicate instance is removed', function() { -        scope.items = [a, a, a]; -        scope.$digest(); +      it('should remove last item when one duplicate instance is removed', inject(function($rootScope) { +        $rootScope.items = [a, a, a]; +        $rootScope.$digest();          lis = element.find('li'); -        scope.items = [a, a]; -        scope.$digest(); +        $rootScope.items = [a, a]; +        $rootScope.$digest();          var newElements = element.find('li');          expect(newElements.length).toEqual(2);          expect(newElements[0]).toEqual(lis[0]);          expect(newElements[1]).toEqual(lis[1]); -      }); +      })); -      it('should reverse items when the collection is reversed', function() { -        scope.items = [a, b, c]; -        scope.$digest(); +      it('should reverse items when the collection is reversed', inject(function($rootScope) { +        $rootScope.items = [a, b, c]; +        $rootScope.$digest();          lis = element.find('li'); -        scope.items = [c, b, a]; -        scope.$digest(); +        $rootScope.items = [c, b, a]; +        $rootScope.$digest();          var newElements = element.find('li');          expect(newElements.length).toEqual(3);          expect(newElements[0]).toEqual(lis[2]);          expect(newElements[1]).toEqual(lis[1]);          expect(newElements[2]).toEqual(lis[0]); -      }); +      }));      }); -  }); +  }));    describe('@ng:non-bindable', function() { -    it('should prevent compilation of the owning element and its children', function() { -      var scope = compile('<div ng:non-bindable><span ng:bind="name"></span></div>'); -      scope.name =  'misko'; -      scope.$digest(); +    it('should prevent compilation of the owning element and its children', inject(function($rootScope) { +      var element = angular.compile('<div ng:non-bindable><span ng:bind="name"></span></div>')($rootScope); +      $rootScope.name =  'misko'; +      $rootScope.$digest();        expect(element.text()).toEqual(''); -    }); +    }));    });    describe('ng:view', function() { -    var rootScope, $route, $location, $browser; - -    beforeEach(function() { -      rootScope = angular.compile('<ng:view></ng:view>')(); -      $route = rootScope.$service('$route'); -      $location = rootScope.$service('$location'); -      $browser = rootScope.$service('$browser'); -    }); - -    afterEach(function() { -      dealoc(rootScope); -    }); +    var element; +    beforeEach(inject(function($rootScope) { +      element = angular.compile('<ng:view></ng:view>')($rootScope); +    })); -    it('should do nothing when no routes are defined', function() { +    it('should do nothing when no routes are defined', inject(function($rootScope, $location) {        $location.path('/unknown'); -      rootScope.$digest(); -      expect(rootScope.$element.text()).toEqual(''); -    }); +      $rootScope.$digest(); +      expect(element.text()).toEqual(''); +    })); -    it('should load content via xhr when route changes', function() { +    it('should load content via xhr when route changes', inject(function($rootScope, $browser, $location, $route) {        $route.when('/foo', {template: 'myUrl1'});        $route.when('/bar', {template: 'myUrl2'}); -      expect(rootScope.$element.text()).toEqual(''); +      expect(element.text()).toEqual('');        $location.path('/foo');        $browser.xhr.expectGET('myUrl1').respond('<div>{{1+3}}</div>'); -      rootScope.$digest(); +      $rootScope.$digest();        $browser.xhr.flush(); -      expect(rootScope.$element.text()).toEqual('4'); +      expect(element.text()).toEqual('4');        $location.path('/bar');        $browser.xhr.expectGET('myUrl2').respond('angular is da best'); -      rootScope.$digest(); +      $rootScope.$digest();        $browser.xhr.flush(); -      expect(rootScope.$element.text()).toEqual('angular is da best'); -    }); +      expect(element.text()).toEqual('angular is da best'); +    })); -    it('should remove all content when location changes to an unknown route', function() { +    it('should remove all content when location changes to an unknown route', +        inject(function($rootScope, $location, $browser, $route) {        $route.when('/foo', {template: 'myUrl1'});        $location.path('/foo');        $browser.xhr.expectGET('myUrl1').respond('<div>{{1+3}}</div>'); -      rootScope.$digest(); +      $rootScope.$digest();        $browser.xhr.flush(); -      expect(rootScope.$element.text()).toEqual('4'); +      expect(element.text()).toEqual('4');        $location.path('/unknown'); -      rootScope.$digest(); -      expect(rootScope.$element.text()).toEqual(''); -    }); +      $rootScope.$digest(); +      expect($rootScope.$element.text()).toEqual(''); +    })); -    it('should chain scopes and propagate evals to the child scope', function() { +    it('should chain scopes and propagate evals to the child scope', +        inject(function($rootScope, $location, $browser, $route) {        $route.when('/foo', {template: 'myUrl1'}); -      rootScope.parentVar = 'parent'; +      $rootScope.parentVar = 'parent';        $location.path('/foo');        $browser.xhr.expectGET('myUrl1').respond('<div>{{parentVar}}</div>'); -      rootScope.$digest(); +      $rootScope.$digest();        $browser.xhr.flush(); -      expect(rootScope.$element.text()).toEqual('parent'); - -      rootScope.parentVar = 'new parent'; -      rootScope.$digest(); -      expect(rootScope.$element.text()).toEqual('new parent'); -    }); +      expect(element.text()).toEqual('parent'); -    it('should be possible to nest ng:view in ng:include', function() { -      dealoc(rootScope); // we are about to override it. +      $rootScope.parentVar = 'new parent'; +      $rootScope.$digest(); +      expect($rootScope.$element.text()).toEqual('new parent'); +    })); -      var myApp = angular.scope(); +    it('should be possible to nest ng:view in ng:include', inject(function() { +      var injector = createInjector(); +      var myApp = injector('$rootScope');        var $browser = myApp.$service('$browser');        $browser.xhr.expectGET('includePartial.html').respond('view: <ng:view></ng:view>'); -      myApp.$service('$location').path('/foo'); +      injector('$location').path('/foo'); -      var $route = myApp.$service('$route'); +      var $route = injector('$route');        $route.when('/foo', {controller: angular.noop, template: 'viewPartial.html'}); -      rootScope = angular.compile( +      var element = angular.compile(            '<div>' + -            'include: <ng:include src="\'includePartial.html\'">' + -          '</ng:include></div>')(myApp); -      rootScope.$apply(); +            'include: <ng:include src="\'includePartial.html\'"> </ng:include>' + +          '</div>')(myApp); +      myApp.$apply();        $browser.xhr.expectGET('viewPartial.html').respond('content'); -      rootScope.$digest(); +      myApp.$digest();        $browser.xhr.flush(); -      expect(rootScope.$element.text()).toEqual('include: view: content'); +      expect(myApp.$element.text()).toEqual('include: view: content');        expect($route.current.template).toEqual('viewPartial.html'); -      dealoc($route.current.scope); -    }); +      dealoc(myApp); +    }));      it('should initialize view template after the view controller was initialized even when ' + -       'templates were cached', function() { +       'templates were cached', inject(function($rootScope, $location, $browser, $route) {        // this is a test for a regression that was introduced by making the ng:view cache sync        $route.when('/foo', {controller: ParentCtrl, template: 'viewPartial.html'}); -      rootScope.log = []; +      $rootScope.log = [];        function ParentCtrl() {          this.log.push('parent');        } -      rootScope.ChildCtrl = function() { +      $rootScope.ChildCtrl = function() {          this.log.push('child');        }; @@ -543,42 +532,42 @@ describe("widget", function() {            respond('<div ng:init="log.push(\'init\')">' +                      '<div ng:controller="ChildCtrl"></div>' +                    '</div>'); -      rootScope.$apply(); +      $rootScope.$apply();        $browser.xhr.flush(); -      expect(rootScope.log).toEqual(['parent', 'init', 'child']); +      expect($rootScope.log).toEqual(['parent', 'init', 'child']);        $location.path('/'); -      rootScope.$apply(); -      expect(rootScope.log).toEqual(['parent', 'init', 'child']); +      $rootScope.$apply(); +      expect($rootScope.log).toEqual(['parent', 'init', 'child']); -      rootScope.log = []; +      $rootScope.log = [];        $location.path('/foo'); -      rootScope.$apply(); +      $rootScope.$apply();        $browser.defer.flush(); -      expect(rootScope.log).toEqual(['parent', 'init', 'child']); -    }); +      expect($rootScope.log).toEqual(['parent', 'init', 'child']); +    }));      it('should discard pending xhr callbacks if a new route is requested before the current ' + -        'finished loading', function() { +        'finished loading', inject(function($route, $rootScope, $location, $browser) {        // this is a test for a bad race condition that affected feedback        $route.when('/foo', {template: 'myUrl1'});        $route.when('/bar', {template: 'myUrl2'}); -      expect(rootScope.$element.text()).toEqual(''); +      expect($rootScope.$element.text()).toEqual('');        $location.path('/foo');        $browser.xhr.expectGET('myUrl1').respond('<div>{{1+3}}</div>'); -      rootScope.$digest(); +      $rootScope.$digest();        $location.path('/bar');        $browser.xhr.expectGET('myUrl2').respond('<div>{{1+1}}</div>'); -      rootScope.$digest(); +      $rootScope.$digest();        $browser.xhr.flush(); // now that we have to requests pending, flush! -      expect(rootScope.$element.text()).toEqual('2'); -    }); +      expect($rootScope.$element.text()).toEqual('2'); +    }));    }); @@ -586,125 +575,128 @@ describe("widget", function() {      describe('deal with pluralized strings without offset', function() { -       beforeEach(function() { -          compile('<ng:pluralize count="email"' + -                                 "when=\"{'0': 'You have no new email'," + -                                         "'one': 'You have one new email'," + -                                         "'other': 'You have {} new emails'}\">" + -                  '</ng:pluralize>'); -        }); - -        it('should show single/plural strings', function() { -          scope.email = 0; -          scope.$digest(); +       var element; +       beforeEach(inject(function($rootScope) { +          element = angular.compile( +            '<ng:pluralize count="email"' + +                           "when=\"{'0': 'You have no new email'," + +                                   "'one': 'You have one new email'," + +                                   "'other': 'You have {} new emails'}\">" + +            '</ng:pluralize>')($rootScope); +        })); + +        it('should show single/plural strings', inject(function($rootScope) { +          $rootScope.email = 0; +          $rootScope.$digest();            expect(element.text()).toBe('You have no new email'); -          scope.email = '0'; -          scope.$digest(); +          $rootScope.email = '0'; +          $rootScope.$digest();            expect(element.text()).toBe('You have no new email'); -          scope.email = 1; -          scope.$digest(); +          $rootScope.email = 1; +          $rootScope.$digest();            expect(element.text()).toBe('You have one new email'); -          scope.email = 0.01; -          scope.$digest(); +          $rootScope.email = 0.01; +          $rootScope.$digest();            expect(element.text()).toBe('You have 0.01 new emails'); -          scope.email = '0.1'; -          scope.$digest(); +          $rootScope.email = '0.1'; +          $rootScope.$digest();            expect(element.text()).toBe('You have 0.1 new emails'); -          scope.email = 2; -          scope.$digest(); +          $rootScope.email = 2; +          $rootScope.$digest();            expect(element.text()).toBe('You have 2 new emails'); -          scope.email = -0.1; -          scope.$digest(); +          $rootScope.email = -0.1; +          $rootScope.$digest();            expect(element.text()).toBe('You have -0.1 new emails'); -          scope.email = '-0.01'; -          scope.$digest(); +          $rootScope.email = '-0.01'; +          $rootScope.$digest();            expect(element.text()).toBe('You have -0.01 new emails'); -          scope.email = -2; -          scope.$digest(); +          $rootScope.email = -2; +          $rootScope.$digest();            expect(element.text()).toBe('You have -2 new emails'); -        }); +        })); -        it('should show single/plural strings with mal-formed inputs', function() { -          scope.email = ''; -          scope.$digest(); +        it('should show single/plural strings with mal-formed inputs', inject(function($rootScope) { +          $rootScope.email = ''; +          $rootScope.$digest();            expect(element.text()).toBe(''); -          scope.email = null; -          scope.$digest(); +          $rootScope.email = null; +          $rootScope.$digest();            expect(element.text()).toBe(''); -          scope.email = undefined; -          scope.$digest(); +          $rootScope.email = undefined; +          $rootScope.$digest();            expect(element.text()).toBe(''); -          scope.email = 'a3'; -          scope.$digest(); +          $rootScope.email = 'a3'; +          $rootScope.$digest();            expect(element.text()).toBe(''); -          scope.email = '011'; -          scope.$digest(); +          $rootScope.email = '011'; +          $rootScope.$digest();            expect(element.text()).toBe('You have 11 new emails'); -          scope.email = '-011'; -          scope.$digest(); +          $rootScope.email = '-011'; +          $rootScope.$digest();            expect(element.text()).toBe('You have -11 new emails'); -          scope.email = '1fff'; -          scope.$digest(); +          $rootScope.email = '1fff'; +          $rootScope.$digest();            expect(element.text()).toBe('You have one new email'); -          scope.email = '0aa22'; -          scope.$digest(); +          $rootScope.email = '0aa22'; +          $rootScope.$digest();            expect(element.text()).toBe('You have no new email'); -          scope.email = '000001'; -          scope.$digest(); +          $rootScope.email = '000001'; +          $rootScope.$digest();            expect(element.text()).toBe('You have one new email'); -        }); +        }));      });      describe('deal with pluralized strings with offset', function() { -      it('should show single/plural strings with offset', function() { -        compile("<ng:pluralize count=\"viewCount\"  offset=2 " + -                    "when=\"{'0': 'Nobody is viewing.'," + -                            "'1': '{{p1}} is viewing.'," + -                            "'2': '{{p1}} and {{p2}} are viewing.'," + -                            "'one': '{{p1}}, {{p2}} and one other person are viewing.'," + -                            "'other': '{{p1}}, {{p2}} and {} other people are viewing.'}\">" + -                "</ng:pluralize>"); -        scope.p1 = 'Igor'; -        scope.p2 = 'Misko'; - -        scope.viewCount = 0; -        scope.$digest(); +      it('should show single/plural strings with offset', inject(function($rootScope) { +        var element = angular.compile( +          "<ng:pluralize count=\"viewCount\"  offset=2 " + +              "when=\"{'0': 'Nobody is viewing.'," + +                      "'1': '{{p1}} is viewing.'," + +                      "'2': '{{p1}} and {{p2}} are viewing.'," + +                      "'one': '{{p1}}, {{p2}} and one other person are viewing.'," + +                      "'other': '{{p1}}, {{p2}} and {} other people are viewing.'}\">" + +          "</ng:pluralize>")($rootScope); +        $rootScope.p1 = 'Igor'; +        $rootScope.p2 = 'Misko'; + +        $rootScope.viewCount = 0; +        $rootScope.$digest();          expect(element.text()).toBe('Nobody is viewing.'); -        scope.viewCount = 1; -        scope.$digest(); +        $rootScope.viewCount = 1; +        $rootScope.$digest();          expect(element.text()).toBe('Igor is viewing.'); -        scope.viewCount = 2; -        scope.$digest(); +        $rootScope.viewCount = 2; +        $rootScope.$digest();          expect(element.text()).toBe('Igor and Misko are viewing.'); -        scope.viewCount = 3; -        scope.$digest(); +        $rootScope.viewCount = 3; +        $rootScope.$digest();          expect(element.text()).toBe('Igor, Misko and one other person are viewing.'); -        scope.viewCount = 4; -        scope.$digest(); +        $rootScope.viewCount = 4; +        $rootScope.$digest();          expect(element.text()).toBe('Igor, Misko and 2 other people are viewing.'); -      }); +      }));      });    });  }); | 
