diff --git a/angularFiles.js b/angularFiles.js index 3b4d91ee952a..453d9989d28f 100644 --- a/angularFiles.js +++ b/angularFiles.js @@ -2,8 +2,6 @@ angularFiles = { 'angularSrc': [ 'src/Angular.js', 'src/JSON.js', - 'src/Compiler.js', - 'src/Scope.js', 'src/Injector.js', 'src/parser.js', 'src/Resource.js', @@ -12,6 +10,7 @@ angularFiles = { 'src/jqLite.js', 'src/apis.js', 'src/filters.js', + 'src/service/compiler.js', 'src/service/cookieStore.js', 'src/service/cookies.js', 'src/service/defer.js', @@ -23,6 +22,7 @@ angularFiles = { 'src/service/resource.js', 'src/service/route.js', 'src/service/routeParams.js', + 'src/service/scope.js', 'src/service/sniffer.js', 'src/service/window.js', 'src/service/xhr.bulk.js', diff --git a/example/personalLog/test/personalLogSpec.js b/example/personalLog/test/personalLogSpec.js index 3e6935a3d9be..cf80a420a32b 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 256a119c1308..8969d5ad617a 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -855,11 +855,6 @@ function toBoolean(value) { } -/** @name angular.compile */ -function compile(element) { - return new Compiler(angularTextMarkup, angularAttrMarkup, angularDirective, angularWidget) - .compile(element); -} ///////////////////////////////////////////////// /** @@ -954,11 +949,12 @@ function angularInit(config, document){ if (autobind) { var element = isString(autobind) ? document.getElementById(autobind) : document, - scope = compile(element)(createScope()), - $browser = scope.$service('$browser'); + injector = createInjector(), + scope = injector('$rootScope'); + injector('$compile')(element)(scope); if (config.css) - $browser.addCss(config.base_url + config.css); + injector('$browser').addCss(config.base_url + config.css); scope.$apply(); } } diff --git a/src/AngularPublic.js b/src/AngularPublic.js index fc8a90fdf978..3ed0eab77694 100644 --- a/src/AngularPublic.js +++ b/src/AngularPublic.js @@ -14,8 +14,6 @@ angularService('$browser', function($log, $sniffer) { 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 deleted file mode 100644 index 12736db7adc1..000000000000 --- a/src/Compiler.js +++ /dev/null @@ -1,329 +0,0 @@ -'use strict'; - -/** - * Template provides directions an how to bind to a given element. - * It contains a list of init functions which need to be called to - * bind to a new instance of elements. It also provides a list - * of child paths which contain child templates - */ -function Template() { - this.paths = []; - this.children = []; - this.linkFns = []; - this.newScope = false; -} - -Template.prototype = { - link: function(element, scope) { - var childScope = scope; - if (this.newScope) { - childScope = isFunction(this.newScope) ? scope.$new(this.newScope(scope)) : scope.$new(); - element.data($$scope, childScope); - } - forEach(this.linkFns, function(fn) { - try { - childScope.$service.invoke(childScope, fn, [element]); - } catch (e) { - childScope.$service('$exceptionHandler')(e); - } - }); - var i, - childNodes = element[0].childNodes, - children = this.children, - paths = this.paths, - length = paths.length; - for (i = 0; i < length; i++) { - // sometimes `element` can be modified by one of the linker functions in `this.linkFns` - // and childNodes may be added or removed - // TODO: element structure needs to be re-evaluated if new children added - // if the childNode still exists - if (childNodes[paths[i]]) - children[i].link(jqLite(childNodes[paths[i]]), childScope); - else - delete paths[i]; // if child no longer available, delete path - } - }, - - - addLinkFn:function(linkingFn) { - if (linkingFn) { - this.linkFns.push(linkingFn); - } - }, - - - addChild: function(index, template) { - if (template) { - this.paths.push(index); - this.children.push(template); - } - }, - - empty: function() { - return this.linkFns.length === 0 && this.paths.length === 0; - } -}; - -/////////////////////////////////// -//Compiler -////////////////////////////////// - -/** - * @ngdoc function - * @name angular.compile - * @function - * - * @description - * Compiles a piece of HTML string or DOM into a template and produces a template function, which - * can then be used to link {@link angular.scope scope} and the template together. - * - * The compilation is a process of walking the DOM tree and trying to match DOM elements to - * {@link angular.markup markup}, {@link angular.attrMarkup attrMarkup}, - * {@link angular.widget widgets}, and {@link angular.directive directives}. For each match it - * executes corresponding markup, attrMarkup, widget or directive template function and collects the - * instance functions into a single template function which is then returned. - * - * The template function can then be used once to produce the view or as it is the case with - * {@link angular.widget.@ng:repeat repeater} many-times, in which case each call results in a view - * that is a DOM clone of the original template. - * -
-    // compile the entire window.document and give me the scope bound to this template.
-    var rootScope = angular.compile(window.document)();
-
-    // compile a piece of html
-    var rootScope2 = angular.compile('
click me
')(); - - // compile a piece of html and retain reference to both the dom and scope - var template = angular.element('
click me
'), - scope = angular.compile(template)(); - // at this point template was transformed into a view -
- * - * - * @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 - * (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. - * * `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 - * called as:
`cloneAttachFn(clonedElement, scope)` where: - * - * * `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. - * - * 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 - * {@link angular.widget widgets} found in the template have been executed yet. This means that the - * view is likely empty and doesn't contain any values that result from evaluation on the scope. To - * bring the view to life, the scope needs to run through a $digest phase which typically is done by - * Angular automatically, except for the case when an application is being - * {@link guide/dev_guide.bootstrap.manual_bootstrap} manually bootstrapped, in which case the - * $digest phase must be invoked by calling {@link angular.scope.$apply}. - * - * If you need access to the bound view, there are two ways to do it: - * - * - 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. - *
- *     var view = angular.element('

{{total}}

'), - * scope = angular.compile(view)(); - *
- * - * - if on the other hand, you need the element to be cloned, the view reference from the original - * example would not point to the clone, but rather to the original template that was cloned. In - * this case, you can access the clone via the cloneAttachFn: - *
- *     var original = angular.element('

{{total}}

'), - * scope = someParentScope.$new(), - * clone; - * - * angular.compile(original)(scope, function(clonedElement, scope) { - * clone = clonedElement; - * //attach the clone to DOM document at the right place - * }); - * - * //now we have reference to the cloned DOM via `clone` - *
- * - * - * Compiler Methods For Widgets and Directives: - * - * The following methods are available for use when you write your own widgets, directives, - * and markup. (Recall that the compile function's this is a reference to the compiler.) - * - * `compile(element)` - returns linker - - * Invoke a new instance of the compiler to compile a DOM element and return a linker function. - * You can apply the linker function to the original element or a clone of the original element. - * The linker function returns a scope. - * - * * `comment(commentText)` - returns element - Create a comment element. - * - * * `element(elementName)` - returns element - Create an element by name. - * - * * `text(text)` - returns element - Create a text element. - * - * * `descend([set])` - returns descend state (true or false). Get or set the current descend - * state. If true the compiler will descend to children elements. - * - * * `directives([set])` - returns directive state (true or false). Get or set the current - * directives processing state. The compiler will process directives only when directives set to - * true. - * - * For information on how the compiler works, see the - * {@link guide/dev_guide.compiler Angular HTML Compiler} section of the Developer Guide. - */ -function Compiler(markup, attrMarkup, directives, widgets){ - this.markup = markup; - this.attrMarkup = attrMarkup; - this.directives = directives; - this.widgets = widgets; -} - -Compiler.prototype = { - compile: function(templateElement) { - templateElement = jqLite(templateElement); - var index = 0, - template, - parent = templateElement.parent(); - if (templateElement.length > 1) { - // https://github.com/angular/angular.js/issues/338 - throw Error("Cannot compile multiple element roots: " + - jqLite('
').append(templateElement.clone()).html()); - } - if (parent && parent[0]) { - parent = parent[0]; - for(var i = 0; i < parent.childNodes.length; i++) { - if (parent.childNodes[i] == templateElement[0]) { - index = i; - } - } - } - template = this.templatize(templateElement, index) || new Template(); - return function(scope, cloneConnectFn){ - // 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; - }; - }, - - templatize: function(element, elementIndex){ - var self = this, - widget, - fn, - directiveFns = self.directives, - descend = true, - directives = true, - elementName = nodeName_(element), - elementNamespace = elementName.indexOf(':') > 0 ? lowercase(elementName).replace(':', '-') : '', - template, - selfApi = { - compile: bind(self, self.compile), - descend: function(value){ if(isDefined(value)) descend = value; return descend;}, - directives: function(value){ if(isDefined(value)) directives = value; return directives;}, - scope: function(value){ if(isDefined(value)) template.newScope = template.newScope || value; return template.newScope;} - }; - element.addClass(elementNamespace); - template = new Template(); - eachAttribute(element, function(value, name){ - if (!widget) { - if ((widget = self.widgets('@' + name))) { - element.addClass('ng-attr-widget'); - widget = bind(selfApi, widget, value, element); - } - } - }); - if (!widget) { - if ((widget = self.widgets(elementName))) { - if (elementNamespace) - element.addClass('ng-widget'); - widget = bind(selfApi, widget, element); - } - } - if (widget) { - descend = false; - directives = false; - var parent = element.parent(); - template.addLinkFn(widget.call(selfApi, element)); - if (parent && parent[0]) { - element = jqLite(parent[0].childNodes[elementIndex]); - } - } - if (descend){ - // process markup for text nodes only - for(var i=0, child=element[0].childNodes; - i=} [factories=angular.service] Map of the service factory * functions. - * @param {Object.=} [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,40 +32,37 @@ * * 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+"'."); + function injector(serviceId, path){ + if (!(serviceId in instanceCache)) { + var factory = factories[serviceId]; + path = path || []; + path.unshift(serviceId); + if (!factory) throw Error("Unknown provider for '" + path.join("' <- '") + "'."); inferInjectionArgs(factory); - instanceCache[value] = invoke(factoryScope, factory); + instanceCache[serviceId] = invoke(null, factory, [], path); + path.shift(); } - return instanceCache[value]; + return instanceCache[serviceId]; } - function invoke(self, fn, args){ + function invoke(self, fn, args, path){ args = args || []; var injectNames = fn.$inject || []; var i = injectNames.length; while(i--) { - args.unshift(injector(injectNames[i])); + args.unshift(injector(injectNames[i], path)); } return fn.apply(self, args); } diff --git a/src/Scope.js b/src/Scope.js deleted file mode 100644 index be5030cc2db8..000000000000 --- a/src/Scope.js +++ /dev/null @@ -1,668 +0,0 @@ -'use strict'; - -/** - * DESIGN NOTES - * - * The design decisions behind the scope ware heavily favored for speed and memory consumption. - * - * The typical use of scope is to watch the expressions, which most of the time return the same - * value as last time so we optimize the operation. - * - * Closures construction is expensive from speed as well as memory: - * - no closures, instead ups prototypical inheritance for API - * - Internal state needs to be stored on scope directly, which means that private state is - * exposed as $$____ properties - * - * Loop operations are optimized by using while(count--) { ... } - * - this means that in order to keep the same order of execution as addition we have to add - * items to the array at the begging (shift) instead of at the end (push) - * - * Child scopes are created and removed often - * - Using array would be slow since inserts in meddle are expensive so we use linked list - * - * There are few watches then a lot of observers. This is why you don't want the observer to be - * implemented in the same way as watch. Watch requires return of initialization function which - * 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. - *
-       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!');
- * 
- * - * # Inheritance - * A scope can inherit from a parent scope, as in this example: - *
-     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');
- * 
- * - * # Dependency Injection - * See {@link guide/dev_guide.di dependency injection}. - * - * - * @param {Object.=} providers Map of service factory which need to be provided - * for the current scope. Defaults to {@link angular.service}. - * @param {Object.=} 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 = { - /** - * @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]); - } - this.$service.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 -
-       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);
-     
- * - * - * - * @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 - }; - - 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 -
-       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);
-     
- * - */ - $digest: function() { - var watch, value, last, - watchers, - asyncQueue, - length, - dirty, ttl = 100, - next, current, target = this; - - 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); - } - } - 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); - } - } 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!'); - } - } 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 -
-       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);
-     
- * - * @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 + // compile the entire window.document and give me the scope bound to this template. + var rootScope = angular.compile(window.document)(); + + // compile a piece of html + var rootScope2 = angular.compile('
click me
')(); + + // compile a piece of html and retain reference to both the dom and scope + var template = angular.element('
click me
'), + scope = angular.compile(template)(); + // at this point template was transformed into a view + + * + * + * @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 + * (a DOM element/tree) to a scope. Where: + * + * * `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 + * called as:
`cloneAttachFn(clonedElement, scope)` where: + * + * * `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 element of the template. It is either the original element + * passed in, or the clone of the element. + * + * 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 + * {@link angular.widget widgets} found in the template have been executed yet. This means that the + * view is likely empty and doesn't contain any values that result from evaluation on the scope. To + * bring the view to life, the scope needs to run through a $digest phase which typically is done by + * Angular automatically, except for the case when an application is being + * {@link guide/dev_guide.bootstrap.manual_bootstrap} manually bootstrapped, in which case the + * $digest phase must be invoked by calling {@link angular.scope.$apply}. + * + * If you need access to the bound view, there are two ways to do it: + * + * - 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. + *
+   *     var scope = angular.injector()('$rootScope');
+   *     var element = angular.compile('

{{total}}

')(scope); + *
+ * + * - if on the other hand, you need the element to be cloned, the view reference from the original + * example would not point to the clone, but rather to the original template that was cloned. In + * this case, you can access the clone via the cloneAttachFn: + *
+   *     var original = angular.element('

{{total}}

'), + * scope = someParentScope.$new(), + * clone; + * + * angular.compile(original)(scope, function(clonedElement, scope) { + * clone = clonedElement; + * //attach the clone to DOM document at the right place + * }); + * + * //now we have reference to the cloned DOM via `clone` + *
+ * + * + * Compiler Methods For Widgets and Directives: + * + * The following methods are available for use when you write your own widgets, directives, + * and markup. (Recall that the compile function's this is a reference to the compiler.) + * + * `compile(element)` - returns linker - + * Invoke a new instance of the compiler to compile a DOM element and return a linker function. + * You can apply the linker function to the original element or a clone of the original element. + * The linker function returns a scope. + * + * * `comment(commentText)` - returns element - Create a comment element. + * + * * `element(elementName)` - returns element - Create an element by name. + * + * * `text(text)` - returns element - Create a text element. + * + * * `descend([set])` - returns descend state (true or false). Get or set the current descend + * state. If true the compiler will descend to children elements. + * + * * `directives([set])` - returns directive state (true or false). Get or set the current + * directives processing state. The compiler will process directives only when directives set to + * true. + * + * For information on how the compiler works, see the + * {@link guide/dev_guide.compiler Angular HTML Compiler} section of the Developer Guide. + */ + function Compiler(markup, attrMarkup, directives, widgets){ + this.markup = markup; + this.attrMarkup = attrMarkup; + this.directives = directives; + this.widgets = widgets; + } + + Compiler.prototype = { + compile: function(templateElement) { + templateElement = jqLite(templateElement); + var index = 0, + template, + parent = templateElement.parent(); + if (templateElement.length > 1) { + // https://github.com/angular/angular.js/issues/338 + throw Error("Cannot compile multiple element roots: " + + jqLite('
').append(templateElement.clone()).html()); + } + if (parent && parent[0]) { + parent = parent[0]; + for(var i = 0; i < parent.childNodes.length; i++) { + if (parent.childNodes[i] == templateElement[0]) { + index = i; + } + } + } + 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; + element.data($$scope, scope); + scope.$element = element; + (cloneConnectFn||noop)(element, scope); + template.link(element, scope); + return element; + }; + }, + + templatize: function(element, elementIndex){ + var self = this, + widget, + fn, + directiveFns = self.directives, + descend = true, + directives = true, + elementName = nodeName_(element), + elementNamespace = elementName.indexOf(':') > 0 ? lowercase(elementName).replace(':', '-') : '', + template, + selfApi = { + compile: bind(self, self.compile), + descend: function(value){ if(isDefined(value)) descend = value; return descend;}, + directives: function(value){ if(isDefined(value)) directives = value; return directives;}, + scope: function(value){ if(isDefined(value)) template.newScope = template.newScope || value; return template.newScope;} + }; + element.addClass(elementNamespace); + template = new Template(); + eachAttribute(element, function(value, name){ + if (!widget) { + if ((widget = self.widgets('@' + name))) { + element.addClass('ng-attr-widget'); + widget = bind(selfApi, widget, value, element); + } + } + }); + if (!widget) { + if ((widget = self.widgets(elementName))) { + if (elementNamespace) + element.addClass('ng-widget'); + widget = bind(selfApi, widget, element); + } + } + if (widget) { + descend = false; + directives = false; + var parent = element.parent(); + template.addLinkFn(widget.call(selfApi, element)); + if (parent && parent[0]) { + element = jqLite(parent[0].childNodes[elementIndex]); + } + } + if (descend){ + // process markup for text nodes only + for(var i=0, child=element[0].childNodes; + i */ -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 d1d34e6762be..c9b76122605d 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 ddc3df493729..3918c251b5b0 100644 --- a/src/service/route.js +++ b/src/service/route.js @@ -62,7 +62,7 @@ */ -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 new file mode 100644 index 000000000000..f059208a74cc --- /dev/null +++ b/src/service/scope.js @@ -0,0 +1,655 @@ +'use strict'; + +/** + * DESIGN NOTES + * + * The design decisions behind the scope ware heavily favored for speed and memory consumption. + * + * The typical use of scope is to watch the expressions, which most of the time return the same + * value as last time so we optimize the operation. + * + * Closures construction is expensive from speed as well as memory: + * - no closures, instead ups prototypical inheritance for API + * - Internal state needs to be stored on scope directly, which means that private state is + * exposed as $$____ properties + * + * Loop operations are optimized by using while(count--) { ... } + * - this means that in order to keep the same order of execution as addition we have to add + * items to the array at the begging (shift) instead of at the end (push) + * + * Child scopes are created and removed often + * - Using array would be slow since inserts in meddle are expensive so we use linked list + * + * There are few watches then a lot of observers. This is why you don't want the observer to be + * implemented in the same way as watch. Watch requires return of initialization function which + * are expensive to construct. + */ + +angularServiceInject('$rootScope', function($injector, $exceptionHandler){ + /** + * @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. + *
+         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!');
+   * 
+ * + * # Inheritance + * A scope can inherit from a parent scope, as in this example: + *
+       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');
+   * 
+ * + * # Dependency Injection + * See {@link guide/dev_guide.di dependency injection}. + * + * + * @param {Object.=} providers Map of service factory which need to be provided + * for the current scope. Defaults to {@link angular.service}. + * @param {Object.=} 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.$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 = { + /** + * @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]); + } + $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 +
+         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);
+       
+ * + * + * + * @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 + }; + + 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 +
+         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);
+       
+ * + */ + $digest: function() { + var watch, value, last, + watchers, + asyncQueue, + length, + dirty, ttl = 100, + next, current, target = this; + + 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) { + $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); + } + } catch (e) { + $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!'); + } + } 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 +
+         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);
+       
+ * + * @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; + + do { + namedListeners = scope.$$listeners[name] || empty; + event.currentScope = scope; + for (i=0, length=namedListeners.length; i */ -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/src/widget/select.js b/src/widget/select.js index 9b9ed1727bc8..2e328b260d63 100644 --- a/src/widget/select.js +++ b/src/widget/select.js @@ -130,7 +130,8 @@ var NG_OPTIONS_REGEXP = /^\s*(.*?)(?:\s+as\s+(.*?))?(?:\s+group\s+by\s+(.*))?\s+ angularWidget('select', function(element){ this.directives(true); this.descend(true); - return element.attr('ng:model') && annotate('$formFactory', function($formFactory, selectElement){ + return element.attr('ng:model') && + annotate('$formFactory', '$compile', function($formFactory, $compile, selectElement){ var modelScope = this, match, form = $formFactory.forElement(selectElement), @@ -245,7 +246,7 @@ angularWidget('select', function(element){ // developer declared null option, so user should be able to select it nullOption = jqLite(option).remove(); // compile the element since there might be bindings in it - compile(nullOption)(modelScope); + $compile(nullOption)(modelScope); } }); selectElement.html(''); // clear contents diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 5b0a3466c9ee..50485a2272c6 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -455,22 +455,16 @@ describe('angular', function() { '
' + '
'); }); - - - 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}); @@ -494,19 +488,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']); }); }); @@ -525,55 +519,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('
{{greeting = "hello world"}}
'); - scope = angular.compile(template)(); - scope.$digest(); + it('should link to existing node and create scope', inject(function($rootScope, $compile) { + var template = angular.element('
{{greeting = "hello world"}}
'); + $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('
{{greeting = "hello world"}}
'); - angular.compile(template)(scope); - scope.$digest(); + it('should link to existing node and given scope', inject(function($rootScope, $compile) { + var template = angular.element('
{{greeting = "hello world"}}
'); + $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('
{{greeting = "hello world"}}
'); + it('should link to new node and given scope', inject(function($rootScope, $compile) { + var template = jqLite('
{{greeting = "hello world"}}
'); - var templateFn = angular.compile(template); + var templateFn = $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('
{{greeting = "hello world"}}
'); - 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, $compile) { + var template = jqLite('
{{greeting = "hello world"}}
'); + var element = $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 bdeed675a683..6a459074cb87 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, $compile){ + content = jqLite(content); + $compile(content)($rootScope); + html = sortedHtml(content); + }).call(this); + return html; }; }); @@ -28,36 +19,39 @@ describe('Binder', function() { } }); - it('BindUpdate', function() { - var scope = this.compile('
'); - scope.$digest(); - assertEquals(123, scope.a); - }); + it('BindUpdate', inject(function($rootScope, $compile) { + $compile('
')($rootScope); + $rootScope.$digest(); + assertEquals(123, $rootScope.a); + })); - it('ExecuteInitialization', function() { - var scope = this.compile('
'); - assertEquals(scope.a, 123); - }); + it('ExecuteInitialization', inject(function($rootScope, $compile) { + $compile('
')($rootScope); + assertEquals($rootScope.a, 123); + })); - it('ExecuteInitializationStatements', function() { - var scope = this.compile('
'); - assertEquals(scope.a, 123); - assertEquals(scope.b, 345); - }); + it('ExecuteInitializationStatements', inject(function($rootScope, $compile) { + $compile('
')($rootScope); + assertEquals($rootScope.a, 123); + assertEquals($rootScope.b, 345); + })); - it('ApplyTextBindings', function() { - var scope = this.compile('
x
'); - scope.model = {a:123}; - scope.$apply(); - assertEquals('123', scope.$element.text()); + it('ApplyTextBindings', inject(function($rootScope, $compile) { + var element = $compile('
x
')($rootScope); + $rootScope.model = {a:123}; + $rootScope.$apply(); + assertEquals('123', element.text()); + })); + + it('ReplaceBindingInTextWithSpan preserve surounding text', function() { + assertEquals(this.compileToHtml("a{{b}}c"), 'ac'); }); it('ReplaceBindingInTextWithSpan', function() { - assertEquals(this.compileToHtml("a{{b}}c"), 'ac'); assertEquals(this.compileToHtml("{{b}}"), ''); }); - it('BindingSpaceConfusesIE', function() { + it('BindingSpaceConfusesIE', inject(function($rootScope, $compile) { if (!msie) return; var span = document.createElement("span"); span.innerHTML = ' '; @@ -65,89 +59,90 @@ describe('Binder', function() { assertEquals( ''+nbsp+'', this.compileToHtml("{{a}} {{b}}")); + dealoc(($rootScope)); assertEquals( ''+nbsp+'x '+nbsp+'()', this.compileToHtml("{{A}} x {{B}} ({{C}})")); - }); + })); - it('BindingOfAttributes', function() { - var scope = this.compile(""); - var attrbinding = scope.$element.attr("ng:bind-attr"); + it('BindingOfAttributes', inject(function($rootScope, $compile) { + var element = $compile("")($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(''); - var attrbinding = scope.$element.attr("ng:bind-attr"); + it('MarkMultipleAttributes', inject(function($rootScope, $compile) { + var element = $compile('')($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(""); - var a = scope.$element; + it('AttributesNoneBound', inject(function($rootScope, $compile) { + var a = $compile("")($rootScope); assertEquals(a[0].nodeName, "A"); assertTrue(!a.attr("ng:bind-attr")); - }); + })); - it('ExistingAttrbindingIsAppended', function() { - var scope = this.compile(""); - var a = scope.$element; + it('ExistingAttrbindingIsAppended', inject(function($rootScope, $compile) { + var a = $compile("")($rootScope); assertEquals('{"b":"{{def}}","href":"http://s/{{abc}}"}', a.attr('ng:bind-attr')); - }); + })); - it('AttributesAreEvaluated', function() { - var scope = this.compile(''); - scope.$eval('a=1;b=2'); - scope.$apply(); - var a = scope.$element; + it('AttributesAreEvaluated', inject(function($rootScope, $compile) { + var a = $compile('')($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, $compile) { var savedCalled = false; - var scope = this.compile(''); - scope.person = {}; - scope.person.save = function() { + var element = $compile( + '')($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, $compile) { var log = ""; - var scope = this.compile(''); - scope.action = function() { + var element = $compile('')($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, $compile) { var savedCalled = false; - var scope = this.compile(''); - scope.person = {}; - scope.person.save = function() { + var element = $compile('')($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('
'); - var form = scope.$element; + it('RepeaterUpdateBindings', inject(function($rootScope, $compile) { + var form = $compile( + '
    ' + + '
  • ' + + '
')($rootScope); var items = [{a:"A"}, {a:"B"}]; - scope.model = {items:items}; + $rootScope.model = {items:items}; - scope.$apply(); + $rootScope.$apply(); assertEquals('
    ' + '<#comment>' + '
  • A
  • ' + @@ -155,7 +150,7 @@ describe('Binder', function() { '
', sortedHtml(form)); items.unshift({a:'C'}); - scope.$apply(); + $rootScope.$apply(); assertEquals('
    ' + '<#comment>' + '
  • C
  • ' + @@ -164,7 +159,7 @@ describe('Binder', function() { '
', sortedHtml(form)); items.shift(); - scope.$apply(); + $rootScope.$apply(); assertEquals('
    ' + '<#comment>' + '
  • A
  • ' + @@ -173,100 +168,113 @@ describe('Binder', function() { items.shift(); items.shift(); - scope.$apply(); - }); - - it('RepeaterContentDoesNotBind', function() { - var scope = this.compile('
    '); - scope.model = {items:[{a:"A"}]}; - scope.$apply(); + $rootScope.$apply(); + })); + + it('RepeaterContentDoesNotBind', inject(function($rootScope, $compile) { + var element = $compile( + '
      ' + + '
    • ' + + '
    ')($rootScope); + $rootScope.model = {items:[{a:"A"}]}; + $rootScope.$apply(); assertEquals('
      ' + '<#comment>' + '
    • A
    • ' + - '
    ', sortedHtml(scope.$element)); - }); + '
', sortedHtml(element)); + })); it('DoNotOverwriteCustomAction', function() { var html = this.compileToHtml(''); assertTrue(html.indexOf('action="foo();"') > 0 ); }); - it('RepeaterAdd', function() { - var scope = this.compile('
'); - 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, $compile) { + var element = $compile('
')($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('
{{i}}
'); + it('ItShouldRemoveExtraChildrenWhenIteratingOverHash', inject(function($rootScope, $compile) { + var element = $compile('
{{i}}
')($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('
{{error.throw()}}
', 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('
', 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, $compile) { + $compile('
{{error.throw()}}
', 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, $compile) { + $compile('
', 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('
' + - '
    ' + - '
    '); + it('NestedRepeater', inject(function($rootScope, $compile) { + var element = $compile( + '
    ' + + '
    ' + + '
      ' + + '
      ' + + '
      ')($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('
      '+ '<#comment>'+ @@ -279,143 +287,154 @@ describe('Binder', function() { '<#comment>'+ '
        '+ '
          '+ - '
          ', sortedHtml(scope.$element)); - }); + '
          ', sortedHtml(element)); + })); - it('HideBindingExpression', function() { - var scope = this.compile('
          '); + it('HideBindingExpression', inject(function($rootScope, $compile) { + var element = $compile('
          ')($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('
          '); + it('HideBinding', inject(function($rootScope, $compile) { + var element = $compile('
          ')($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('
          '); + it('ShowBinding', inject(function($rootScope, $compile) { + var element = $compile('
          ')($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('
          '); + it('BindClass', inject(function($rootScope, $compile) { + var element = $compile('
          ')($rootScope); - scope.clazz = 'testClass'; - scope.$apply(); + $rootScope.clazz = 'testClass'; + $rootScope.$apply(); - assertEquals('
          ', sortedHtml(scope.$element)); + assertEquals('
          ', sortedHtml(element)); - scope.clazz = ['a', 'b']; - scope.$apply(); + $rootScope.clazz = ['a', 'b']; + $rootScope.$apply(); - assertEquals('
          ', sortedHtml(scope.$element)); - }); + assertEquals('
          ', sortedHtml(element)); + })); - it('BindClassEvenOdd', function() { - var scope = this.compile('
          '); - scope.$apply(); - var d1 = jqLite(scope.$element[0].childNodes[1]); - var d2 = jqLite(scope.$element[0].childNodes[2]); + it('BindClassEvenOdd', inject(function($rootScope, $compile) { + var element = $compile( + '
          ' + + '
          ' + + '
          ')($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( '
          <#comment>' + '
          ' + '
          ', - sortedHtml(scope.$element)); - }); - - it('BindStyle', function() { - var scope = this.compile('
          '); - - 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('Add Phone', 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("
          {{a}}" + + sortedHtml(element)); + })); + + it('BindStyle', inject(function($rootScope, $compile) { + var element = $compile('
          ')($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, $compile) { + var input = $compile('Add Phone')($rootScope); + $rootScope.action = function() { + throw new Error('MyError'); + }; + browserTrigger(input, 'click'); + expect($exceptionHandler.errors[0]).toMatch(/MyError/); + }) + ); + + it('ShoulIgnoreVbNonBindable', inject(function($rootScope, $compile) { + var element = $compile( + "
          {{a}}" + "
          {{a}}
          " + "
          {{b}}
          " + - "
          {{c}}
          "); - scope.a = 123; - scope.$apply(); - assertEquals('123{{a}}{{b}}{{c}}', scope.$element.text()); - }); + "
          {{c}}
          " + + "
          ")($rootScope); + $rootScope.a = 123; + $rootScope.$apply(); + assertEquals('123{{a}}{{b}}{{c}}', element.text()); + })); + + it('ShouldTemplateBindPreElements', inject(function ($rootScope, $compile) { + var element = $compile('
          Hello {{name}}!
          ')($rootScope); + $rootScope.name = "World"; + $rootScope.$apply(); - it('ShouldTemplateBindPreElements', function () { - var scope = this.compile('
          Hello {{name}}!
          '); - scope.name = "World"; - scope.$apply(); - - assertEquals('
          Hello World!
          ', sortedHtml(scope.$element)); - }); + assertEquals( + '
          Hello World!
          ', + sortedHtml(element)); + })); - it('FillInOptionValueWhenMissing', function() { - var scope = this.compile( + it('FillInOptionValueWhenMissing', inject(function($rootScope, $compile) { + var element = $compile( ''); - 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); + '')($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('
          ' + + it('DeleteAttributeIfEvaluatesFalse', inject(function($rootScope, $compile) { + var element = $compile( + '
          ' + '' + '' + '' + '' + '' + - '
          '); - scope.$apply(); + '' + + '
          ')($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('
          ' + - '' + - '
          ', 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('
          ' + + })); + + it('ItShouldDisplayErrorWhenActionIsSyntacticlyIncorrect', inject( + function(service){ + service('$exceptionHandler', $exceptionHandlerMockFactory); + }, + function($rootScope, $exceptionHandler, $log, $compile) { + var element = $compile( + '
          ' + + '' + + '' + + '
          ')($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, $compile) { + var element = $compile( + '
          ' + '' + - '
          '); - var female = jqLite(scope.$element[0].childNodes[0]); - var male = jqLite(scope.$element[0].childNodes[1]); + '' + + '
          ')($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('
          '); - scope.$apply(); + })); + + it('ItShouldRepeatOnHashes', inject(function($rootScope, $compile) { + var element = $compile( + '
            ' + + '
          • ' + + '
          ')($rootScope); + $rootScope.$apply(); assertEquals('
            ' + '<#comment>' + '
          • a0
          • ' + '
          • b1
          • ' + '
          ', - sortedHtml(scope.$element)); - }); - - it('ItShouldFireChangeListenersBeforeUpdate', function() { - var scope = this.compile('
          '); - scope.name = ""; - scope.$watch("watched", "name=123"); - scope.watched = "change"; - scope.$apply(); - assertEquals(123, scope.name); + sortedHtml(element)); + })); + + it('ItShouldFireChangeListenersBeforeUpdate', inject(function($rootScope, $compile) { + var element = $compile('
          ')($rootScope); + $rootScope.name = ""; + $rootScope.$watch("watched", "name=123"); + $rootScope.watched = "change"; + $rootScope.$apply(); + assertEquals(123, $rootScope.name); assertEquals( '
          123
          ', - sortedHtml(scope.$element)); - }); - - it('ItShouldHandleMultilineBindings', function() { - var scope = this.compile('
          {{\n 1 \n + \n 2 \n}}
          '); - scope.$apply(); - assertEquals("3", scope.$element.text()); - }); + sortedHtml(element)); + })); + + it('ItShouldHandleMultilineBindings', inject(function($rootScope, $compile) { + var element = $compile('
          {{\n 1 \n + \n 2 \n}}
          ')($rootScope); + $rootScope.$apply(); + assertEquals("3", element.text()); + })); }); diff --git a/test/FiltersSpec.js b/test/FiltersSpec.js index df47ccc3602d..4b69bf03281a 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(''); - 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 2c6c102a2c85..fe1993e5bf34 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'}); }); @@ -67,16 +62,22 @@ describe('injector', function() { }); - it('should provide usefull message if no provider', function() { + it('should provide useful message if no provider', function() { expect(function() { injector('idontexist'); }).toThrow("Unknown provider for 'idontexist'."); }); + it('should proved path to the missing provider', function(){ + expect(function() { + injector('idontexist', ['a', 'b']); + }).toThrow("Unknown provider for 'idontexist' <- 'a' <- 'b'."); + }); + 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 +127,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 975cacc43a7f..ce3b22ca8cb1 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 15bbbdae166a..928b4f6c3ce3 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('
          ')(); - var $browser = scope.$service('$browser'); - var $resource = scope.$service('$resource'); + it('should excersize full stack', inject(function($rootScope, $browser, $resource, $compile) { + $compile('
          ')($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('
          ')(); - var $browser = scope.$service('$browser'); - var $resource = scope.$service('$resource'); + it('should return the same object when verifying the cache', inject(function($rootScope, $compile) { + $compile('
          ')($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 5f83ab930539..986e21217969 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, $compile) { var node = jqLite('
          {{b=a+1}}
          ')[0]; - scope = angular.compile(node)(); - scope.$digest(); - expect(scope.a).toEqual(1); - expect(scope.b).toEqual(2); - }); + $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('
          {{a=123}}
          '))(); - scope.$digest(); - expect(jqLite(scope.$element).text()).toEqual('123'); - }); + it("should compile jQuery node and return scope", inject(function($rootScope, $compile) { + var element = $compile(jqLite('
          {{a=123}}
          '))($rootScope); + $rootScope.$digest(); + expect(jqLite(element).text()).toEqual('123'); + })); - it("should compile text node and return scope", function() { - scope = angular.compile('
          {{a=123}}
          ')(); - scope.$digest(); - expect(jqLite(scope.$element).text()).toEqual('123'); - }); + it("should compile text node and return scope", inject(function($rootScope, $compile) { + var element = $compile('
          {{a=123}}
          ')($rootScope); + $rootScope.$digest(); + expect(jqLite(element).text()).toEqual('123'); + })); }); }); diff --git a/test/angular-mocksSpec.js b/test/angular-mocksSpec.js index c2cffca6d8cc..205461af8b9b 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 8c07cf70a183..2bdefb6814e2 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('
          '); - expect(scope.a).toEqual(123); - }); + it("should ng:init", inject(function($rootScope, $compile) { + var element = $compile('
          ')($rootScope); + expect($rootScope.a).toEqual(123); + })); describe('ng:bind', function() { - it('should set text', function() { - var scope = compile('
          '); + it('should set text', inject(function($rootScope, $compile) { + var element = $compile('
          ')($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('
          '); - scope.a = 'misko'; - scope.$digest(); + it('should set text to blank if undefined', inject(function($rootScope, $compile) { + var element = $compile('
          ')($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('
          '); - scope.html = '
          hello
          '; - scope.$digest(); + it('should set html', inject(function($rootScope, $compile) { + var element = $compile('
          ')($rootScope); + $rootScope.html = '
          hello
          '; + $rootScope.$digest(); expect(lowercase(element.html())).toEqual('
          hello
          '); - }); + })); - it('should set unsafe html', function() { - var scope = compile('
          '); - scope.html = '
          hello
          '; - scope.$digest(); + it('should set unsafe html', inject(function($rootScope, $compile) { + var element = $compile('
          ')($rootScope); + $rootScope.html = '
          hello
          '; + $rootScope.$digest(); expect(lowercase(element.html())).toEqual('
          hello
          '); - }); + })); - it('should set element element', function() { + it('should set element element', inject(function($rootScope, $compile) { angularFilter.myElement = function() { return jqLite('hello'); }; - var scope = compile('
          '); - scope.$digest(); + var element = $compile('
          ')($rootScope); + $rootScope.$digest(); expect(lowercase(element.html())).toEqual('hello'); - }); + })); - it('should have $element set to current bind element', function() { + it('should have $element set to current bind element', inject(function($rootScope, $compile) { angularFilter.myFilter = function() { this.$element.addClass("filter"); return 'HELLO'; }; - var scope = compile('
          before
          after
          '); - scope.$digest(); - expect(sortedHtml(scope.$element)).toEqual('
          before
          HELLO
          after
          '); - }); - - - it('should suppress rendering of falsy values', function() { - var scope = compile('
          {{ null }}{{ undefined }}{{ "" }}-{{ 0 }}{{ false }}
          '); - scope.$digest(); - expect(scope.$element.text()).toEqual('-0false'); - }); - - it('should render object as JSON ignore $$', function() { - var scope = compile('
          {{ {key:"value", $$key:"hide"} }}
          '); - scope.$digest(); - expect(fromJson(scope.$element.text())).toEqual({key:'value'}); - }); + var element = $compile('
          before
          after
          ')($rootScope); + $rootScope.$digest(); + expect(sortedHtml(element)).toEqual('
          before
          HELLO
          after
          '); + })); + + + it('should suppress rendering of falsy values', inject(function($rootScope, $compile) { + var element = $compile('
          {{ null }}{{ undefined }}{{ "" }}-{{ 0 }}{{ false }}
          ')($rootScope); + $rootScope.$digest(); + expect(element.text()).toEqual('-0false'); + })); + + it('should render object as JSON ignore $$', inject(function($rootScope, $compile) { + var element = $compile('
          {{ {key:"value", $$key:"hide"} }}
          ')($rootScope); + $rootScope.$digest(); + expect(fromJson(element.text())).toEqual({key:'value'}); + })); }); describe('ng:bind-template', function() { - it('should ng:bind-template', function() { - var scope = compile('
          '); - scope.name = 'Misko'; - scope.$digest(); + it('should ng:bind-template', inject(function($rootScope, $compile) { + var element = $compile('
          ')($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, $compile) { var innerText; angularFilter.myFilter = function(text) { innerText = innerText || this.$element.text(); return text; }; - var scope = compile('
          beforeINNERafter
          '); - scope.$digest(); - expect(scope.$element.text()).toEqual("beforeHELLOafter"); + var element = $compile('
          beforeINNERafter
          ')($rootScope); + $rootScope.$digest(); + expect(element.text()).toEqual("beforeHELLOafter"); expect(innerText).toEqual('INNER'); - }); + })); - it('should render object as JSON ignore $$', function() { - var scope = compile('
          {{ {key:"value", $$key:"hide"}  }}
          '); - scope.$digest(); - expect(fromJson(scope.$element.text())).toEqual({key:'value'}); - }); + it('should render object as JSON ignore $$', inject(function($rootScope, $compile) { + var element = $compile('
          {{ {key:"value", $$key:"hide"}  }}
          ')($rootScope); + $rootScope.$digest(); + expect(fromJson(element.text())).toEqual({key:'value'}); + })); }); describe('ng:bind-attr', function() { - it('should bind attributes', function() { - var scope = compile('
          '); - scope.$digest(); + it('should bind attributes', inject(function($rootScope, $compile) { + var element = $compile('
          ')($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('{{ {a:1} }}'); - scope.$digest(); + it('should not pretty print JSON in attributes', inject(function($rootScope, $compile) { + var element = $compile('{{ {a:1} }}')($rootScope); + $rootScope.$digest(); expect(element.attr('alt')).toEqual('{"a":1}'); - }); + })); }); - it('should remove special attributes on false', function() { - var scope = compile(''); - var input = scope.$element[0]; + it('should remove special attributes on false', inject(function($rootScope, $compile) { + var element = $compile('')($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('
          '); - scope.$digest(); - expect(scope.clicked).toBeFalsy(); + it('should get called on a click', inject(function($rootScope, $compile) { + var element = $compile('
          ')($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('
          '); - scope.$digest(); - expect(scope.outer).not.toBeDefined(); - expect(scope.inner).not.toBeDefined(); + it('should stop event propagation', inject(function($rootScope, $compile) { + var element = $compile('
          ')($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('
          ' + - '' + - '
          '); - scope.$digest(); - expect(scope.submitted).not.toBeDefined(); + it('should get called on form submit', inject(function($rootScope, $compile) { + var element = $compile('
          ' + + '' + + '
          ')($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('
          '); - scope.dynClass = 'A'; - scope.$digest(); + it('should add new and remove old classes dynamically', inject(function($rootScope, $compile) { + var element = $compile('
          ')($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('
          '); - scope.$digest(); + it('should support adding multiple classes via an array', inject(function($rootScope, $compile) { + var element = $compile('
          ')($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('
          '); - scope.$digest(); + it('should support adding multiple classes via a space delimited string', inject(function($rootScope, $compile) { + var element = $compile('
          ')($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('
          '); - scope.dynClass = 'A'; - scope.$digest(); + it('should preserve class added post compilation with pre-existing classes', inject(function($rootScope, $compile) { + var element = $compile('
          ')($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('
          '); - scope.dynClass = 'A'; - scope.$digest(); + it('should preserve class added post compilation without pre-existing classes"', inject(function($rootScope, $compile) { + var element = $compile('
          ')($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('
          '); - scope.dynCls = 'panel'; - scope.$digest(); - scope.dynCls = 'foo'; - scope.$digest(); + it('should preserve other classes with similar name"', inject(function($rootScope, $compile) { + var element = $compile('
          ')($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('
          '); - scope.dynCls = 'panel'; - scope.$digest(); + it('should not add duplicate classes', inject(function($rootScope, $compile) { + var element = $compile('
          ')($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('
          '); - 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, $compile) { + var element = $compile('
          ')($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('
          '); - scope.dynCls = 'foo'; - scope.$digest(); + it('should remove classes even if they were added by another code', inject(function($rootScope, $compile) { + var element = $compile('
          ')($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('
          '); - scope.dynCls = [undefined, null]; - scope.$digest(); + it('should convert undefined and null values to an empty string', inject(function($rootScope, $compile) { + var element = $compile('
          ')($rootScope); + $rootScope.dynCls = [undefined, null]; + $rootScope.$digest(); expect(element[0].className).toBe('ng-directive'); - }); + })); }); - it('should ng:class odd/even', function() { - var scope = compile('
            • '); - scope.$digest(); + it('should ng:class odd/even', inject(function($rootScope, $compile) { + var element = $compile('
                • ')($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('
                    ' + - '
                  • ' + - '
                      '); - scope.$apply(); + it('should allow both ng:class and ng:class-odd/even on the same element', inject(function($rootScope, $compile) { + var element = $compile('
                        ' + + '
                      • ' + + '
                          ')($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('
                            ' + - '
                          • ' + - '
                              '); - scope.$apply(); + it('should allow both ng:class and ng:class-odd/even with multiple classes', inject(function($rootScope, $compile) { + var element = $compile('
                                ' + + '
                              • ' + + '
                                  ')($rootScope); + $rootScope.$apply(); var e1 = jqLite(element[0].childNodes[1]); var e2 = jqLite(element[0].childNodes[2]); @@ -359,76 +346,73 @@ 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('
                                  '); - scope.$digest(); + it('should set', inject(function($rootScope, $compile) { + var element = $compile('
                                  ')($rootScope); + $rootScope.$digest(); expect(element.css('height')).toEqual('40px'); - }); + })); - it('should silently ignore undefined style', function() { - var scope = compile('
                                  '); - scope.$digest(); + it('should silently ignore undefined style', inject(function($rootScope, $compile) { + var element = $compile('
                                  ')($rootScope); + $rootScope.$digest(); expect(element.hasClass('ng-exception')).toBeFalsy(); - }); + })); - it('should preserve and remove previous style', function() { - var scope = compile('
                                  '); - scope.$digest(); + it('should preserve and remove previous style', inject(function($rootScope, $compile) { + var element = $compile('
                                  ')($rootScope); + $rootScope.$digest(); expect(getStyle(element)).toEqual({height: '10px'}); - scope.myStyle = {height: '20px', width: '10px'}; - scope.$digest(); + $rootScope.myStyle = {height: '20px', width: '10px'}; + $rootScope.$digest(); expect(getStyle(element)).toEqual({height: '20px', width: '10px'}); - scope.myStyle = {}; - scope.$digest(); + $rootScope.myStyle = {}; + $rootScope.$digest(); expect(getStyle(element)).toEqual({height: '10px'}); - }); + })); }); - it('should silently ignore undefined ng:style', function() { - var scope = compile('
                                  '); - scope.$digest(); + it('should silently ignore undefined ng:style', inject(function($rootScope, $compile) { + var element = $compile('
                                  ')($rootScope); + $rootScope.$digest(); expect(element.hasClass('ng-exception')).toBeFalsy(); - }); + })); describe('ng:show', function() { - it('should show and hide an element', function() { - var element = jqLite('
                                  '), - scope = compile(element); - - scope.$digest(); + it('should show and hide an element', inject(function($rootScope, $compile) { + var element = jqLite('
                                  '); + var element = $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('
                                  '), - scope = compile(element); + it('should make hidden element visible', inject(function($rootScope, $compile) { + var element = jqLite('
                                  '); + var element = $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('
                                  '), - scope = compile(element); - + it('should hide an element', inject(function($rootScope, $compile) { + var element = jqLite('
                                  '); + var element = $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() { @@ -453,13 +437,13 @@ describe("directive", function() { window.temp = undefined; }); - it('should bind', function() { - var scope = compile('
                                  '); - expect(scope.greeter.greeting).toEqual('hello'); - expect(scope.greeter.greet('misko')).toEqual('hello misko!'); - }); + it('should bind', inject(function($rootScope, $compile) { + var element = $compile('
                                  ')($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, $compile) { temp.ChildGreeter = function() { this.greeting = 'hey'; this.$root.childGreeter = this; @@ -469,48 +453,48 @@ describe("directive", function() { return this.greeting + ' dude' + this.suffix; } }; - var scope = compile('
                                  {{greet("misko")}}
                                  '); - 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 = $compile('
                                  {{greet("misko")}}
                                  ')($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, $compile) { temp.MyController = function($xhr){ this.$root.someService = $xhr; }; - var scope = compile('
                                  '); - expect(scope.someService).toBe(scope.$service('$xhr')); - }); + var element = $compile('
                                  ')($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, $compile) { var element = jqLite('
                                  '); expect(element.attr('ng:cloak')).toBe(''); - angular.compile(element); + $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, $compile) { var element = jqLite('
                                  '); expect(element.hasClass('foo')).toBe(true); expect(element.hasClass('ng-cloak')).toBe(true); expect(element.hasClass('bar')).toBe(true); - angular.compile(element); + $compile(element); 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 2f9a5fb9ab48..5c54e786cba0 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/manual.html b/test/manual.html deleted file mode 100644 index 8aa64128120c..000000000000 --- a/test/manual.html +++ /dev/null @@ -1,130 +0,0 @@ - - - - Jasmine Test Runner - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/test/markupSpec.js b/test/markupSpec.js index 41255b4a1f75..c23cc62db4db 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('
                                  hello {{name}}!
                                  '); + it('should translate {{}} in text', inject(function($rootScope, $compile) { + var element = $compile('
                                  hello {{name}}!
                                  ')($rootScope) expect(sortedHtml(element)).toEqual('
                                  hello !
                                  '); - scope.name = 'Misko'; - scope.$digest(); + $rootScope.name = 'Misko'; + $rootScope.$digest(); expect(sortedHtml(element)).toEqual('
                                  hello Misko!
                                  '); - }); + })); - it('should translate {{}} in terminal nodes', function() { - compile(''); - scope.$digest(); + it('should translate {{}} in terminal nodes', inject(function($rootScope, $compile) { + var element = $compile('')($rootScope) + $rootScope.$digest(); expect(sortedHtml(element).replace(' selected="true"', '')). toEqual(''); - scope.name = 'Misko'; - scope.$digest(); + $rootScope.name = 'Misko'; + $rootScope.$digest(); expect(sortedHtml(element).replace(' selected="true"', '')). toEqual(''); - }); + })); - it('should translate {{}} in attributes', function() { - compile('
                                  '); + it('should translate {{}} in attributes', inject(function($rootScope, $compile) { + var element = $compile('
                                  ')($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(''); + it('should populate value attribute on OPTION', inject(function($rootScope, $compile) { + var element = $compile('')($rootScope) expect(element).toHaveValue('abc'); - }); + })); - it('should ignore value if already exists', function() { - compile(''); + it('should ignore value if already exists', inject(function($rootScope, $compile) { + var element = $compile('')($rootScope) expect(element).toHaveValue('abc'); - }); + })); - it('should set value even if newlines present', function() { - compile(''); + it('should set value even if newlines present', inject(function($rootScope, $compile) { + var element = $compile('')($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, $compile) { // IE removes the \n from option, which makes this test pointless if (msie) return; - compile(''); + var element = $compile('')($rootScope) expect(element).toHaveValue('\n'); - }); + })); }); - it('should bind href', function() { - compile(''); + it('should bind href', inject(function($rootScope, $compile) { + var element = $compile('')($rootScope) expect(sortedHtml(element)).toEqual(''); - }); + })); - it('should bind disabled', function() { - compile(''); - scope.isDisabled = false; - scope.$digest(); + it('should bind disabled', inject(function($rootScope, $compile) { + var element = $compile('')($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(''); - scope.isChecked = false; - scope.$digest(); + it('should bind checked', inject(function($rootScope, $compile) { + var element = $compile('')($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(''); + it('should bind selected', inject(function($rootScope, $compile) { + var element = $compile('')($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(''); - scope.isReadonly=false; - scope.$digest(); + it('should bind readonly', inject(function($rootScope, $compile) { + var element = $compile('')($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(''); - scope.isMultiple=false; - scope.$digest(); + it('should bind multiple', inject(function($rootScope, $compile) { + var element = $compile('')($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('
                                  '); - scope.url = 'http://localhost/'; - scope.$digest(); + it('should bind src', inject(function($rootScope, $compile) { + var element = $compile('
                                  ')($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(''); + it('should bind href and merge with other attrs', inject(function($rootScope, $compile) { + var element = $compile('')($rootScope) expect(sortedHtml(element)).toEqual(''); - }); - - it('should bind Text with no Bindings', function() { - forEach(['checked', 'disabled', 'multiple', 'readonly', 'selected', 'src', 'href'], - function(name) { - compile('
                                  '); - expect(sortedHtml(element)).toEqual('
                                  '); + })); + + it('should bind Text with no Bindings', inject(function($compile) { + var $rootScope; + function newScope (){ + return $rootScope = angular.injector()('$rootScope'); + } + forEach(['checked', 'disabled', 'multiple', 'readonly', 'selected'], function(name) { + var element = $compile('
                                  ')(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 = $compile('
                                  ')(newScope()) + $rootScope.$digest(); + expect(sortedHtml(element)).toEqual('
                                  '); + dealoc(element); + + var element = $compile('
                                  ')(newScope()) + $rootScope.$digest(); + expect(sortedHtml(element)).toEqual('
                                  '); + dealoc(element); + })); + + it('should Parse Text With No Bindings', inject(function($rootScope, $compile) { 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, $compile) { 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, $compile) { 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, $compile) { 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, $compile) { 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, $compile) { 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, $compile) { 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, $compile) { 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, $compile) { 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, $compile) { 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 92f000bae233..4cffc63aa684 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 a6e8090147db..5e3310f1ca2c 100644 --- a/test/scenario/dslSpec.js +++ b/test/scenario/dslSpec.js @@ -10,7 +10,7 @@ describe("angular.scenario.dsl", function() { document: _jQuery("
                                  "), angular: new angular.scenario.testing.MockAngular() }; - $root = angular.scope(); + $root = angular.injector()('$rootScope'); $root.emit = function(eventName) { eventLog.push(eventName); }; @@ -131,18 +131,16 @@ describe("angular.scenario.dsl", function() { search: '?foo=10', hash: '#bar?x=2' }; - $window.angular.scope = function() { - return { - $service: function(serviceId) { - if (serviceId == '$location') { - return { - hashSearch: {x: 2}, - hashPath: '/bar', - search: {foo: 10} - }; - } else { - throw new Error('unknown service id ' + serviceId); - } + $window.angular.injector = function() { + return function(serviceId) { + if (serviceId == '$location') { + return { + hashSearch: {x: 2}, + hashPath: '/bar', + search: {foo: 10} + }; + } else { + throw new Error('unknown service id ' + serviceId); } }; }; diff --git a/test/CompilerSpec.js b/test/service/compilerSpec.js similarity index 52% rename from test/CompilerSpec.js rename to test/service/compilerSpec.js index 860cea4a0159..4e0dbed63b38 100644 --- a/test/CompilerSpec.js +++ b/test/service/compilerSpec.js @@ -1,10 +1,12 @@ 'use strict'; describe('compiler', function() { - var compiler, markup, attrMarkup, directives, widgets, compile, log, scope; + var compiler, textMmarkup, attrMarkup, directives, widgets, compile, log, $rootScope; - beforeEach(function() { - log = ""; + beforeEach(inject(function(service){ + textMmarkup = []; + attrMarkup = []; + widgets = extensionMap({}, 'widget'); directives = { hello: function(expression, element){ log += "hello "; @@ -23,33 +25,25 @@ describe('compiler', function() { } }; - markup = []; - attrMarkup = []; - widgets = extensionMap({}, 'widget'); - compiler = new Compiler(markup, attrMarkup, directives, widgets); - compile = function(html){ - var e = jqLite("
                                  " + html + "
                                  "); - return scope = compiler.compile(e)(); - }; - }); - - - afterEach(function() { - dealoc(scope); - }); + log = ""; + service('$textMarkup', valueFn(textMmarkup)); + service('$attrMarkup', valueFn(attrMarkup)); + service('$directive', valueFn(directives)); + service('$widget', valueFn(widgets)); + })); - it('should not allow compilation of multiple roots', function() { + it('should not allow compilation of multiple roots', inject(function($rootScope, $compile) { expect(function() { - compiler.compile('
                                  A
                                  '); + $compile('
                                  A
                                  '); }).toThrow("Cannot compile multiple element roots: " + ie("
                                  A
                                  ")); function ie(text) { return msie < 9 ? uppercase(text) : text; } - }); + })); - it('should recognize a directive', function() { + it('should recognize a directive', inject(function($rootScope, $compile) { var e = jqLite('
                                  '); directives.directive = function(expression, element){ log += "found"; @@ -59,79 +53,80 @@ describe('compiler', function() { log += ":init"; }; }; - var template = compiler.compile(e); + var linkFn = $compile(e); expect(log).toEqual("found"); - scope = template(angular.scope()); + linkFn($rootScope); expect(e.hasClass('ng-directive')).toEqual(true); expect(log).toEqual("found:init"); - }); + })); - it('should recurse to children', function() { - scope = compile('
                                  '); + it('should recurse to children', inject(function($rootScope, $compile) { + $compile('
                                  ')($rootScope); expect(log).toEqual("hello misko"); - }); + })); - it('should observe scope', function() { - scope = compile(''); + it('should observe scope', inject(function($rootScope, $compile) { + $compile('')($rootScope); expect(log).toEqual(""); - scope.$digest(); - scope.name = 'misko'; - scope.$digest(); - scope.$digest(); - scope.name = 'adam'; - scope.$digest(); - scope.$digest(); + $rootScope.$digest(); + $rootScope.name = 'misko'; + $rootScope.$digest(); + $rootScope.$digest(); + $rootScope.name = 'adam'; + $rootScope.$digest(); + $rootScope.$digest(); expect(log).toEqual(":misko:adam"); - }); + })); - it('should prevent descend', function() { + it('should prevent descend', inject(function($rootScope, $compile) { directives.stop = function() { this.descend(false); }; - scope = compile(''); + $compile('')($rootScope); expect(log).toEqual("hello misko"); - }); + })); - it('should allow creation of templates', function() { + it('should allow creation of templates', inject(function($rootScope, $compile) { 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); }); }; }; - scope = compile('beforexafter'); - expect(sortedHtml(scope.$element)). + $compile('
                                  beforexafter
                                  ')($rootScope); + expect(sortedHtml($rootScope.$element)). toEqual('
                                  ' + 'before<#comment>' + 'after' + '
                                  '); - scope.value = 1; - scope.$digest(); - expect(sortedHtml(scope.$element)). + $rootScope.value = 1; + $rootScope.$digest(); + expect(sortedHtml($rootScope.$element)). toEqual('
                                  ' + 'before<#comment>' + 'x' + 'after' + '
                                  '); - scope.value = 2; - scope.$digest(); - expect(sortedHtml(scope.$element)). + $rootScope.value = 2; + $rootScope.$digest(); + expect(sortedHtml($rootScope.$element)). toEqual('
                                  ' + 'before<#comment>' + 'x' + 'x' + 'after' + '
                                  '); - scope.value = 3; - scope.$digest(); - expect(sortedHtml(scope.$element)). + $rootScope.value = 3; + $rootScope.$digest(); + expect(sortedHtml($rootScope.$element)). toEqual('
                                  ' + 'before<#comment>' + 'x' + @@ -139,24 +134,25 @@ describe('compiler', function() { 'x' + 'after' + '
                                  '); - }); + })); - it('should process markup before directives', function() { - markup.push(function(text, textNode, parentNode) { + it('should process markup before directives', inject(function($rootScope, $compile) { + textMmarkup.push(function(text, textNode, parentNode) { if (text == 'middle') { expect(textNode.text()).toEqual(text); parentNode.attr('hello', text); textNode[0].nodeValue = 'replaced'; } }); - scope = compile('beforemiddleafter'); - expect(sortedHtml(scope.$element[0], true)).toEqual('
                                  beforereplacedafter
                                  '); + $compile('
                                  beforemiddleafter
                                  ')($rootScope); + expect(sortedHtml($rootScope.$element[0], true)). + toEqual('
                                  beforereplacedafter
                                  '); expect(log).toEqual("hello middle"); - }); + })); - it('should replace widgets', function() { + it('should replace widgets', inject(function($rootScope, $compile) { widgets['NG:BUTTON'] = function(element) { expect(element.hasClass('ng-widget')).toEqual(true); element.replaceWith('
                                  button
                                  '); @@ -164,13 +160,13 @@ describe('compiler', function() { log += 'init'; }; }; - scope = compile('push me'); - expect(lowercase(scope.$element[0].innerHTML)).toEqual('
                                  button
                                  '); + $compile('
                                  push me
                                  ')($rootScope); + expect(lowercase($rootScope.$element[0].innerHTML)).toEqual('
                                  button
                                  '); expect(log).toEqual('init'); - }); + })); - it('should use the replaced element after calling widget', function() { + it('should use the replaced element after calling widget', inject(function($rootScope, $compile) { widgets['H1'] = function(element) { // HTML elements which are augmented by acting as widgets, should not be marked as so expect(element.hasClass('ng-widget')).toEqual(false); @@ -180,17 +176,17 @@ describe('compiler', function() { this.directives(true); return noop; }; - markup.push(function(text, textNode, parent){ + textMmarkup.push(function(text, textNode, parent){ if (text == '{{1+2}}') parent.text('3'); }); - scope = compile('

                                  ignore me

                                  '); - expect(scope.$element.text()).toEqual('3'); - }); + $compile('

                                  ignore me

                                  ')($rootScope); + expect($rootScope.$element.text()).toEqual('3'); + })); - it('should allow multiple markups per text element', function() { - markup.push(function(text, textNode, parent){ + it('should allow multiple markups per text element', inject(function($rootScope, $compile) { + textMmarkup.push(function(text, textNode, parent){ var index = text.indexOf('---'); if (index > -1) { textNode.after(text.substring(index + 3)); @@ -199,7 +195,7 @@ describe('compiler', function() { textNode.remove(); } }); - markup.push(function(text, textNode, parent){ + textMmarkup.push(function(text, textNode, parent){ var index = text.indexOf('==='); if (index > -1) { textNode.after(text.substring(index + 3)); @@ -208,14 +204,13 @@ describe('compiler', function() { textNode.remove(); } }); - scope = compile('A---B---C===D'); - expect(sortedHtml(scope.$element)).toEqual('
                                  A
                                  B
                                  C

                                  D
                                  '); - }); + $compile('
                                  A---B---C===D
                                  ')($rootScope); + expect(sortedHtml($rootScope.$element)).toEqual('
                                  A
                                  B
                                  C

                                  D
                                  '); + })); - it('should add class for namespace elements', function() { - scope = compile('abc'); - var space = jqLite(scope.$element[0].firstChild); - expect(space.hasClass('ng-space')).toEqual(true); - }); + it('should add class for namespace elements', inject(function($rootScope, $compile) { + var element = $compile('abc')($rootScope); + expect(element.hasClass('ng-space')).toEqual(true); + })); }); diff --git a/test/service/cookieStoreSpec.js b/test/service/cookieStoreSpec.js index 0bf7e99d90f2..50ac7797aaf4 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 f078c20cabec..2569645bb678 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 ff48c93ef05d..98ddeac555ce 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 885331e4728e..064904a26db0 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 61e652b5278e..3bfb70c0911e 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 23b8ae0a6d2d..fbe601c6ea03 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('$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 9a7aa94306c9..38df261934ae 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('
                                  '); - attrs = attrs ? ' ' + attrs + ' ' : ''; - content = content || 'link'; - link = jqLite('' + content + '')[0]; - root = jqRoot.append(link)[0]; + function configureService(linkHref, html5Mode, supportHist, attrs, content) { + return function(service){ + var jqRoot = jqLite('
                                  '); + attrs = attrs ? ' ' + attrs + ' ' : ''; + link = jqLite('' + content + '')[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, '', 'link'); - 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, '', 'link'), + 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 c4efb8c57200..8c56d99e13ec 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 972e4314cce5..d4088767ae4b 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 5aba2a1f3ea0..26ae17e58c04 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();;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/ScopeSpec.js b/test/service/scopeSpec.js similarity index 50% rename from test/ScopeSpec.js rename to test/service/scopeSpec.js index b1942646b7ac..acbba630d3ec 100644 --- a/test/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,97 +178,97 @@ 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', function() { - root.$watch('a', function(self, v){self.b++;}); - root.$watch('b', function(self, v){self.a++;}); - root.a = root.b = 0; + it('should prevent infinite recursion', inject(function($rootScope) { + $rootScope.$watch('a', function(self, v){self.b++;}); + $rootScope.$watch('b', function(self, v){self.a++;}); + $rootScope.a = $rootScope.b = 0; expect(function() { - root.$digest(); + $rootScope.$digest(); }).toThrow('100 $digest() iterations reached. Aborting!'); - }); + })); - 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; @@ -298,166 +287,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]); + })); }); }); @@ -466,9 +451,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() { @@ -483,12 +468,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; @@ -509,7 +494,7 @@ describe('Scope', function() { child.$emit('abc'); child.$broadcast('abc'); expect(log).toEqual(''); - }); + })); }); @@ -520,55 +505,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); @@ -578,7 +564,7 @@ describe('Scope', function() { }); grandChild.$emit('myEvent'); expect(event).toBeDefined(); - }); + })); }); }); @@ -592,18 +578,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; @@ -613,7 +599,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); @@ -630,43 +616,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; @@ -678,11 +664,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; @@ -693,7 +679,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 c539e2853f37..3b8471469b3d 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 6b99fbba3ca7..6f273f64f37d 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 0c77e629d571..328dfe3ab87e 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 49b63fd00bb5..0ed5ab59d433 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 2a552403d5c8..997994d7f75f 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 b3316c0c5eed..379e02c04516 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); @@ -164,9 +164,78 @@ 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, + '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) { @@ -189,7 +258,7 @@ afterEach(function() { }); clearJqCache(); -}); +})); function clearJqCache() { var count = 0; @@ -225,29 +294,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 4f5630ba1492..7a92dbf43443 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, $compile) { doc = angular.element('
                                  '); - var scope = angular.compile(doc)(); + $compile(doc)($rootScope); expect(doc.data('$form')).toBeTruthy(); - }); + })); - it('should prevent form submission', function() { + it('should prevent form submission', inject(function($rootScope, $compile) { var startingUrl = '' + window.location; doc = angular.element(''); - var scope = angular.compile(doc)(); + $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, $compile) { doc = angular.element('
                                  '); - var scope = angular.compile(doc)(); - expect(scope.myForm).toBeTruthy(); + $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, $compile) { doc = angular.element('
                                  '); - var scope = angular.compile(doc)(); - scope.text = 'misko'; - scope.$digest(); + $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, $compile) { doc = angular.element( '' + '' + '' + '' + ''); - var scope = angular.compile(doc)(); - var parent = scope.parent; - var child = scope.child; + $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, $compile) { doc = angular.element( '' + '' + '' + '' + ''); - var scope = angular.compile(doc)(); - scope.forms = [1]; - scope.$digest(); + $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 179f8156e47b..6ab53ab0f2a5 100644 --- a/test/widget/inputSpec.js +++ b/test/widget/inputSpec.js @@ -2,10 +2,12 @@ describe('widget: input', function() { var compile = null, element = null, scope = null, defer = null; + var $compile = null; var doc = null; - beforeEach(function() { - scope = null; + beforeEach(inject(function($rootScope, $compile) { + scope = $rootScope; + set$compile($compile); element = null; compile = function(html, parent) { if (parent) { @@ -14,12 +16,14 @@ describe('widget: input', function() { } else { element = jqLite(html); } - scope = angular.compile(element)(); + $compile(element)(scope); scope.$apply(); defer = scope.$service('$browser').defer; return scope; }; - }); + })); + + function set$compile(c) { $compile = c; } afterEach(function() { dealoc(element); @@ -28,8 +32,7 @@ describe('widget: input', function() { describe('text', function() { - var scope = null, - form = null, + var form = null, formElement = null, inputElement = null; @@ -41,7 +44,7 @@ describe('widget: input', function() { formElement = doc = angular.element('
                                  '); inputElement = formElement.find('input'); - scope = angular.compile(doc)(); + $compile(doc)(scope); form = formElement.inheritedData('$form'); }; @@ -91,18 +94,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, $compile) { doc = angular.element('
                                  '); - scope = angular.compile(doc)(); + $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, $compile) { doc = angular.element('
                                  '); - scope = angular.compile(doc)(); + $compile(doc)($rootScope); expect(doc.find('input')[0].getAttribute('type')).toEqual('number'); - }); + })); }); @@ -455,35 +458,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, $compile) { var input, $formFactory; element = angular.element(''); - 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); + $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, $compile) { var input, $formFactory; element = angular.element(''); var error; try { - scope = angular.scope(); - angular.compile(element)(scope); + $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 +581,16 @@ describe('widget: input', function() { {'ng:maxlength': 3}); - it('should throw an error when scope pattern can\'t be found', function() { - var el = jqLite(''), - scope = angular.compile(el)(); + it('should throw an error when scope pattern can\'t be found', inject(function($rootScope, $compile) { + var el = jqLite(''); + $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 c3fdc2e6fc83..5d92e674a693 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($compile, $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)(); + $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('' + @@ -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(''); }).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 905553258898..d4886afa2877 100644 --- a/test/widgetsSpec.js +++ b/test/widgetsSpec.js @@ -1,165 +1,146 @@ '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('' + + describe('ng:switch', inject(function($rootScope, $compile) { + it('should switch on value change', inject(function($rootScope, $compile) { + var element = $compile( + '' + '
                                  first:{{name}}
                                  ' + '
                                  second:{{name}}
                                  ' + '
                                  true:{{name}}
                                  ' + - '
                                  '); + '
                                  ')($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('' + - '
                                  one
                                  ' + - '
                                  other
                                  ' + - '
                                  '); - scope.$apply(); + })); + + + it('should switch on switch-when-default', inject(function($rootScope, $compile) { + var element = $compile( + '' + + '
                                  one
                                  ' + + '
                                  other
                                  ' + + '
                                  ')($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('
                                  {{name}}
                                  ')(); - 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, $compile) { + var element = $compile( + '' + + '
                                  {{name}}
                                  ' + + '
                                  ')($rootScope); + $rootScope.url = 'a'; + $rootScope.$apply(); + expect($rootScope.name).toEqual(undefined); + expect(element.text()).toEqual('works'); + })); + })); + + + describe('ng:include', inject(function($rootScope, $compile) { + it('should include on external file', inject(function($rootScope, $compile) { var element = jqLite(''); - 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 = $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, $compile) { var element = jqLite(''); - 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 = $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, $compile) { var element = jqLite(''); - var scope = angular.compile(element)(); - scope.url = 'myUrl'; - scope.$service('$xhr.cache').data.myUrl = {value:'{{"abc"}}'}; - scope.$digest(); + var element = $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, $compile) { var element = jqLite(''); - var scope = angular.compile(element)(); + var element = $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, $compile) { var element = jqLite(''); - var scope = angular.compile(element)(); + var element = $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, $compile) { + it('should prevent default action to be executed when href is empty', inject(function($rootScope, $compile) { var orgLocation = document.location.href, preventDefaultCalled = false, event; - compile('empty link'); + var element = $compile('empty link')($rootScope); if (msie < 9) { @@ -185,165 +166,179 @@ describe("widget", function() { } expect(document.location.href).toEqual(orgLocation); - }); - }); + })); + })); - describe('@ng:repeat', function() { - it('should ng:repeat over array', function() { - var scope = compile('
                                  '); + describe('@ng:repeat', inject(function($rootScope, $compile) { + it('should ng:repeat over array', inject(function($rootScope, $compile) { + var element = $compile( + '
                                    ' + + '
                                  • ' + + '
                                  ')($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('
                                  '); - scope.items = {misko:'swe', shyam:'set'}; - scope.$digest(); + it('should ng:repeat over object', inject(function($rootScope, $compile) { + var element = $compile( + '
                                    ' + + '
                                  • ' + + '
                                  ')($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, $compile) { var Class = function() {}; Class.prototype.abc = function() {}; Class.prototype.value = 'abc'; - var scope = compile('
                                  '); - scope.items = new Class(); - scope.items.name = 'value'; - scope.$digest(); + var element = $compile( + '
                                    ' + + '
                                  • ' + + '
                                  ')($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, $compile) { expect(function() { - compile('
                                  '); + var element = $compile('
                                  ')($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('
                                  '); - scope.items = ['misko', 'shyam', 'frodo']; - scope.$digest(); + it('should expose iterator offset as $index when iterating over arrays', inject(function($rootScope, $compile) { + var element = $compile( + '
                                    ' + + '
                                  • ' + + '
                                  ')($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('
                                  '); - scope.items = {'misko':'m', 'shyam':'s', 'frodo':'f'}; - scope.$digest(); + it('should expose iterator offset as $index when iterating over objects', inject(function($rootScope, $compile) { + var element = $compile( + '
                                    ' + + '
                                  • ' + + '
                                  ')($rootScope); + $rootScope.items = {'misko':'m', 'shyam':'s', 'frodo':'f'}; + $rootScope.$digest(); expect(element.text()).toEqual('misko:m0|shyam:s1|frodo:f2|'); - }); + })); - it('should expose iterator position as $position when iterating over arrays', function() { - var scope = compile('
                                  '); - scope.items = ['misko', 'shyam', 'doug']; - scope.$digest(); + it('should expose iterator position as $position when iterating over arrays', inject(function($rootScope, $compile) { + var element = $compile( + '
                                    ' + + '
                                  • ' + + '
                                  ')($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, $compile) { + var element = $compile( '
                                    ' + - '
                                  • ' + - '
                                  • ' + - '
                                  '); - scope.items = {'misko':'m', 'shyam':'s', 'doug':'d', 'frodo':'f'}; - scope.$digest(); + '
                                • ' + + '
                                ')($rootScope); + $rootScope.items = {'misko':'m', 'shyam':'s', 'doug':'d', 'frodo':'f'}; + $rootScope.$digest(); expect(element.text()).toEqual('misko:m:first|shyam:s:middle|doug:d:middle|frodo:f: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('
                                • {{i}}|
                                '); - scope.items = ['a', 'b', 'c']; - scope.items.$$hashkey = 'xxx'; - scope.items.$root = 'yyy'; - scope.$digest(); + it('should ignore $ and $$ properties', inject(function($rootScope, $compile) { + var element = $compile('
                                • {{i}}|
                                ')($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('
                                  ' + - '
                                • ' + - '
                                  {{group}}|
                                  X' + - '
                                • ' + - '
                                '); - scope.groups = [['a', 'b'], ['c','d']]; - scope.$digest(); + it('should repeat over nested arrays', inject(function($rootScope, $compile) { + var element = $compile( + '
                                  ' + + '
                                • ' + + '
                                  {{group}}|
                                  X' + + '
                                • ' + + '
                                ')($rootScope); + $rootScope.groups = [['a', 'b'], ['c','d']]; + $rootScope.$digest(); expect(element.text()).toEqual('a|b|Xc|d|X'); - }); + })); 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, $compile) { + element = $compile( '
                                  ' + - '
                                • ' + - '
                                • ' + - '
                                '); + '
                              • ' + + '
                              ')($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, $compile) { + $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, $compile) { + $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]); @@ -351,176 +346,168 @@ 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, $compile) { + $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, $compile) { + $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('
                              '); - scope.name = 'misko'; - scope.$digest(); + it('should prevent compilation of the owning element and its children', inject(function($rootScope, $compile) { + var element = $compile('
                              ')($rootScope); + $rootScope.name = 'misko'; + $rootScope.$digest(); expect(element.text()).toEqual(''); - }); + })); }); describe('ng:view', function() { - var rootScope, $route, $location, $browser; - - beforeEach(function() { - rootScope = angular.compile('')(); - $route = rootScope.$service('$route'); - $location = rootScope.$service('$location'); - $browser = rootScope.$service('$browser'); - }); - - afterEach(function() { - dealoc(rootScope); - }); + var element; + beforeEach(inject(function($rootScope, $compile) { + element = $compile('')($rootScope); + })); - it('should do nothing when no routes are defined', function() { + it('should do nothing when no routes are defined', inject(function($rootScope, $compile, $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, $compile, $browser, $location, $route) { $route.when('/foo', {controller: angular.noop, template: 'myUrl1'}); $route.when('/bar', {controller: angular.noop, template: 'myUrl2'}); - expect(rootScope.$element.text()).toEqual(''); + expect(element.text()).toEqual(''); $location.path('/foo'); $browser.xhr.expectGET('myUrl1').respond('
                              {{1+3}}
                              '); - rootScope.$digest(); - rootScope.$digest(); + $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(); + $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, $compile, $location, $browser, $route) { $route.when('/foo', {controller: angular.noop, template: 'myUrl1'}); $location.path('/foo'); $browser.xhr.expectGET('myUrl1').respond('
                              {{1+3}}
                              '); - rootScope.$digest(); - rootScope.$digest(); + $rootScope.$digest(); $browser.xhr.flush(); - expect(rootScope.$element.text()).toEqual('4'); + expect(element.text()).toEqual('4'); $location.path('/unknown'); - rootScope.$digest(); - rootScope.$digest(); - expect(rootScope.$element.text()).toEqual(''); - }); + $rootScope.$digest(); + $rootScope.$digest(); + expect(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, $compile, $location, $browser, $route) { $route.when('/foo', {controller: angular.noop, template: 'myUrl1'}); - rootScope.parentVar = 'parent'; + $rootScope.parentVar = 'parent'; $location.path('/foo'); $browser.xhr.expectGET('myUrl1').respond('
                              {{parentVar}}
                              '); - rootScope.$digest(); - rootScope.$digest(); + $rootScope.$digest(); + $rootScope.$digest(); $browser.xhr.flush(); - expect(rootScope.$element.text()).toEqual('parent'); + expect(element.text()).toEqual('parent'); - rootScope.parentVar = 'new parent'; - rootScope.$digest(); - rootScope.$digest(); - expect(rootScope.$element.text()).toEqual('new parent'); - }); + $rootScope.parentVar = 'new parent'; + $rootScope.$digest(); + $rootScope.$digest(); + expect(element.text()).toEqual('new parent'); + })); - it('should be possible to nest ng:view in ng:include', function() { - dealoc(rootScope); // we are about to override it. - - 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: '); - 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 = injector('$compile')( '
                              ' + - 'include: ' + - '
                              ')(myApp); - rootScope.$apply(); + 'include: ' + + '
          ')(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, $compile, $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'); }; @@ -529,22 +516,22 @@ describe("widget", function() { respond('
          ' + '
          ' + '
          '); - 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']); + })); }); @@ -552,125 +539,128 @@ describe("widget", function() { describe('deal with pluralized strings without offset', function() { - beforeEach(function() { - compile('" + - ''); - }); - - it('should show single/plural strings', function() { - scope.email = 0; - scope.$digest(); + var element; + beforeEach(inject(function($rootScope, $compile) { + element = $compile( + '" + + '')($rootScope); + })); + + it('should show single/plural strings', inject(function($rootScope, $compile) { + $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, $compile) { + $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("" + - ""); - scope.p1 = 'Igor'; - scope.p2 = 'Misko'; - - scope.viewCount = 0; - scope.$digest(); + it('should show single/plural strings with offset', inject(function($rootScope, $compile) { + var element = $compile( + "" + + "")($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.'); - }); + })); }); }); });