From 9c0639437607a4fcea379bbaf610600d05d8a9b7 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Tue, 8 Nov 2011 17:40:52 -0800 Subject: chore(scenario tests): make scenario tests pass again --- docs/content/cookbook/advancedform.ngdoc | 29 ++++- docs/content/cookbook/form.ngdoc | 16 ++- docs/content/guide/dev_guide.expressions.ngdoc | 17 ++- docs/content/guide/dev_guide.forms.ngdoc | 26 ++-- .../dev_guide.services.injecting_controllers.ngdoc | 24 ++-- ..._guide.templates.filters.creating_filters.ngdoc | 32 ++--- docs/src/templates/docs.css | 8 ++ example/personalLog/personalLog.html | 4 +- src/Angular.js | 132 +-------------------- src/angular-mocks.js | 4 +- src/scenario/Application.js | 2 +- src/service/filter/filter.js | 6 +- src/service/filter/filters.js | 4 - src/service/filter/limitTo.js | 10 +- src/service/filter/orderBy.js | 2 +- src/service/formFactory.js | 12 +- test/scenario/ApplicationSpec.js | 17 ++- test/scenario/dslSpec.js | 20 +++- test/scenario/mocks.js | 16 +-- 19 files changed, 156 insertions(+), 225 deletions(-) diff --git a/docs/content/cookbook/advancedform.ngdoc b/docs/content/cookbook/advancedform.ngdoc index 19b8284f..e973e30f 100644 --- a/docs/content/cookbook/advancedform.ngdoc +++ b/docs/content/cookbook/advancedform.ngdoc @@ -34,7 +34,28 @@ detection, and preventing invalid form submission. save: function() { this.master = this.form; this.cancel(); + }, + + addContact: function() { + this.form.contacts.push({type:'', value:''}); + }, + + removeContact: function(contact) { + for ( var i = 0, ii = this.form.contacts.length; i < ii; i++) { + if (contact === this.form.contacts[i]) { + this.form.contacts.splice(i, 1); + } + } + }, + + isCancelDisabled: function() { + return angular.equals(this.master, this.form); + }, + + isSaveDisabled: function() { + return this.myForm.$invalid || angular.equals(this.master, this.form); } + };
@@ -53,7 +74,7 @@ detection, and preventing invalid form submission. ng:pattern="zip" required/>

- [ add ] + [ add ]
- [ X ] + [ X ]
- - + +
diff --git a/docs/content/cookbook/form.ngdoc b/docs/content/cookbook/form.ngdoc index 6cd1d83a..1b5bf32b 100644 --- a/docs/content/cookbook/form.ngdoc +++ b/docs/content/cookbook/form.ngdoc @@ -18,6 +18,18 @@ allow a user to enter data. }; this.state = /^\w\w$/; this.zip = /^\d\d\d\d\d$/; + + this.addContact = function() { + this.user.contacts.push({type:'', value:''}); + }; + + this.removeContact = function(contact) { + for ( var i = 0, ii = this.user.contacts.length; i < ii; i++) { + if (contact === this.user.contacts[i]) { + this.user.contacts.splice(i, 1); + } + } + }; }
@@ -34,7 +46,7 @@ allow a user to enter data. ng:pattern="zip" required>

- [ add ] + [ add ]
- [ X ] + [ X ]

Debug View: diff --git a/docs/content/guide/dev_guide.expressions.ngdoc b/docs/content/guide/dev_guide.expressions.ngdoc index 420a6f98..b9417b38 100644 --- a/docs/content/guide/dev_guide.expressions.ngdoc +++ b/docs/content/guide/dev_guide.expressions.ngdoc @@ -54,15 +54,26 @@ You can try evaluating different expressions here: function Cntl2() { this.exprs = []; this.expr = '3*10|currency'; + this.addExp = function(expr) { + this.exprs.push(expr); + }; + + this.removeExp = function(contact) { + for ( var i = 0, ii = this.exprs.length; i < ii; i++) { + if (contact === this.exprs[i]) { + this.exprs.splice(i, 1); + } + } + }; }
Expression: - +
  • - [ X ] + [ X ] {{expr}} =>
@@ -175,7 +186,7 @@ Extensions: You can further extend the expression vocabulary by adding new metho Search: - + diff --git a/docs/content/guide/dev_guide.forms.ngdoc b/docs/content/guide/dev_guide.forms.ngdoc index 980b6c17..aaad66a5 100644 --- a/docs/content/guide/dev_guide.forms.ngdoc +++ b/docs/content/guide/dev_guide.forms.ngdoc @@ -134,7 +134,16 @@ The following example demonstrates: save: function() { this.master = this.form; this.cancel(); + }, + + isCancelDisabled: function() { + return angular.equals(this.master, this.form); + }, + + isSaveDisabled: function() { + return this.userForm.$invalid || angular.equals(this.master, this.form); } + };
@@ -172,10 +181,9 @@ The following example demonstrates: + ng:disabled="{{isCancelDisabled()}}">Cancel + ng:disabled="{{isSaveDisabled()}}">Save
@@ -278,9 +286,9 @@ This example shows how to implement a custom HTML editor widget in Angular. this.htmlContent = 'Hello World!'; } - function HTMLEditorWidget(element) { + HTMLEditorWidget.$inject = ['$element', 'html$Filter']; + function HTMLEditorWidget(element, htmlFilter) { var self = this; - var htmlFilter = angular.filter('html'); this.$parseModel = function() { // need to protect for script injection @@ -309,7 +317,7 @@ This example shows how to implement a custom HTML editor widget in Angular. } angular.directive('ng:html-editor-model', function() { - function linkFn($formFactory, element) { + return ['$formFactory', '$element', function ($formFactory, element) { var exp = element.attr('ng:html-editor-model'), form = $formFactory.forElement(element), widget; @@ -318,15 +326,13 @@ This example shows how to implement a custom HTML editor widget in Angular. scope: this, model: exp, controller: HTMLEditorWidget, - controllerArgs: [element]}); + controllerArgs: {$element: element}}); // if the element is destroyed, then we need to // notify the form. element.bind('$destroy', function() { widget.$destroy(); }); - } - linkFn.$inject = ['$formFactory']; - return linkFn; + }]; });
diff --git a/docs/content/guide/dev_guide.services.injecting_controllers.ngdoc b/docs/content/guide/dev_guide.services.injecting_controllers.ngdoc index d58f2c3c..56823eb9 100644 --- a/docs/content/guide/dev_guide.services.injecting_controllers.ngdoc +++ b/docs/content/guide/dev_guide.services.injecting_controllers.ngdoc @@ -31,16 +31,18 @@ myController.$inject = ['$location', '$log']; -
+

Let's try this simple notify service, injected into the controller...

diff --git a/docs/content/guide/dev_guide.templates.filters.creating_filters.ngdoc b/docs/content/guide/dev_guide.templates.filters.creating_filters.ngdoc index e57c5509..9404ad65 100644 --- a/docs/content/guide/dev_guide.templates.filters.creating_filters.ngdoc +++ b/docs/content/guide/dev_guide.templates.filters.creating_filters.ngdoc @@ -19,28 +19,32 @@ text upper-case and assigns color. -
+

No filter: {{greeting}}
Reverse: {{greeting|reverse}}
diff --git a/docs/src/templates/docs.css b/docs/src/templates/docs.css index d42ec093..5c7a064a 100644 --- a/docs/src/templates/docs.css +++ b/docs/src/templates/docs.css @@ -474,3 +474,11 @@ td.empty-corner-lt { .error { color: red; } + +.odd { + background-color: #808080; +} + +.even { + background-color: #d3d3d3; +} diff --git a/example/personalLog/personalLog.html b/example/personalLog/personalLog.html index bc76b263..4f74a402 100644 --- a/example/personalLog/personalLog.html +++ b/example/personalLog/personalLog.html @@ -20,11 +20,11 @@

Logs:

    -
  • +
  • {{log.at | date:'yy-MM-dd HH:mm'}} {{log.msg}} [x]
- \ No newline at end of file + diff --git a/src/Angular.js b/src/Angular.js index a191c0c7..bb75bc4e 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -498,10 +498,6 @@ function map(obj, iterator, context) { /** - * @ngdoc function - * @name angular.Object.size - * @function - * * @description * Determines the number of elements in an array, the number of properties an object has, or * the length of a string. @@ -512,29 +508,6 @@ function map(obj, iterator, context) { * @param {Object|Array|string} obj Object, array, or string to inspect. * @param {boolean} [ownPropsOnly=false] Count only "own" properties in an object * @returns {number} The size of `obj` or `0` if `obj` is neither an object nor an array. - * - * @example - * - * - * - *
- * Number of items in array: {{ [1,2].$size() }}
- * Number of items in object: {{ {a:1, b:2, c:3}.$size() }}
- * String length: {{fooStringLength}} - *
- *
- * - * it('should print correct sizes for an array and an object', function() { - * expect(binding('[1,2].$size()')).toBe('2'); - * expect(binding('{a:1, b:2, c:3}.$size()')).toBe('3'); - * expect(binding('fooStringLength')).toBe('3'); - * }); - * - *
*/ function size(obj, ownPropsOnly) { var size = 0, key; @@ -566,11 +539,11 @@ function indexOf(array, obj) { } function arrayRemove(array, value) { - var index = indexOf(array, value); - if (index >=0) - array.splice(index, 1); - return value; - } + var index = indexOf(array, value); + if (index >=0) + array.splice(index, 1); + return value; +} function isLeafNode (node) { if (node) { @@ -590,15 +563,6 @@ function isLeafNode (node) { * @function * * @description - * Alias for {@link angular.Object.copy} - */ - -/** - * @ngdoc function - * @name angular.Object.copy - * @function - * - * @description * Creates a deep copy of `source`, which should be an object or an array. * * * If no destination is supplied, a copy of the object or array is created. @@ -614,46 +578,6 @@ function isLeafNode (node) { * @param {(Object|Array)=} destination Destination into which the source is copied. If * provided, must be of the same type as `source`. * @returns {*} The copy or updated `destination`, if `destination` was specified. - * - * @example - * - * - -
- Salutation:
- Name:
- -
- - The master object is NOT equal to the form object. - -
master={{master}}
-
form={{form}}
-
- *
- * - it('should print that initialy the form object is NOT equal to master', function() { - expect(element('.doc-example-live input[ng\\:model="master.salutation"]').val()).toBe('Hello'); - expect(element('.doc-example-live input[ng\\:model="master.name"]').val()).toBe('world'); - expect(element('.doc-example-live span').css('display')).toBe('inline'); - }); - - it('should make form and master equal when the copy button is clicked', function() { - element('.doc-example-live button').click(); - expect(element('.doc-example-live span').css('display')).toBe('none'); - }); - * - *
*/ function copy(source, destination){ if (!destination) { @@ -693,15 +617,6 @@ function copy(source, destination){ * @function * * @description - * Alias for {@link angular.Object.equals} - */ - -/** - * @ngdoc function - * @name angular.Object.equals - * @function - * - * @description * Determines if two objects or two values are equivalent. Supports value types, arrays and * objects. * @@ -720,43 +635,6 @@ function copy(source, destination){ * @param {*} o2 Object or value to compare. * @returns {boolean} True if arguments are equal. * - * @example - * - * - -
- Salutation:
- Name:
-
- - The greeting object is - NOT equal to - {salutation:'Hello', name:'world'}. - -
greeting={{greeting}}
-
- *
- * - it('should print that initialy greeting is equal to the hardcoded value object', function() { - expect(element('.doc-example-live input[ng\\:model="greeting.salutation"]').val()).toBe('Hello'); - expect(element('.doc-example-live input[ng\\:model="greeting.name"]').val()).toBe('world'); - expect(element('.doc-example-live span').css('display')).toBe('none'); - }); - - it('should say that the objects are not equal when the form is modified', function() { - input('greeting.name').enter('kitty'); - expect(element('.doc-example-live span').css('display')).toBe('inline'); - }); - * - *
*/ function equals(o1, o2) { if (o1 === o2) return true; diff --git a/src/angular-mocks.js b/src/angular-mocks.js index 907b0492..1757b941 100644 --- a/src/angular-mocks.js +++ b/src/angular-mocks.js @@ -344,7 +344,9 @@ angular.mock.$Browser.prototype = { } }, - addJs: function() {} + notifyWhenNoOutstandingRequests: function(fn) { + fn(); + } }; diff --git a/src/scenario/Application.js b/src/scenario/Application.js index ef778975..e255041b 100644 --- a/src/scenario/Application.js +++ b/src/scenario/Application.js @@ -90,7 +90,7 @@ angular.scenario.Application.prototype.executeAction = function(action) { if (!$window.angular) { return action.call(this, $window, _jQuery($window.document)); } - var element = $window.angular.element($window.document.body); + var element = $window.angular.element($window.document); var $injector = element.inheritedData('$injector'); $injector(function($browser){ $browser.notifyWhenNoOutstandingRequests(function() { diff --git a/src/service/filter/filter.js b/src/service/filter/filter.js index 0a0f5706..05ae6bfa 100644 --- a/src/service/filter/filter.js +++ b/src/service/filter/filter.js @@ -2,7 +2,7 @@ /** * @ngdoc function - * @name angular.Array.filter + * @name angular.service.filter.filter * @function * * @description @@ -44,7 +44,7 @@ Search:
NamePhone
{{friend.name}} {{friend.phone}}
- + @@ -55,7 +55,7 @@ Phone only
NamePhone
{{friend.name}} {{friend.phone}}
- + diff --git a/src/service/filter/filters.js b/src/service/filter/filters.js index a411bf03..1034896b 100644 --- a/src/service/filter/filters.js +++ b/src/service/filter/filters.js @@ -40,8 +40,6 @@ * @param {string=} symbol Currency symbol or identifier to be displayed. * @returns {string} Formatted number. * - * @css ng-format-negative - * When the value is negative, this css class is applied to the binding making it (by default) red. * * @example @@ -66,8 +64,6 @@ input('amount').enter('-1234'); expect(binding('amount | currency')).toBe('($1,234.00)'); expect(binding('amount | currency:"USD$"')).toBe('(USD$1,234.00)'); - expect(element('.doc-example-live .ng-binding').prop('className')). - toMatch(/ng-format-negative/); }); diff --git a/src/service/filter/limitTo.js b/src/service/filter/limitTo.js index 9bb5cf4d..219322f4 100644 --- a/src/service/filter/limitTo.js +++ b/src/service/filter/limitTo.js @@ -2,7 +2,7 @@ /** * @ngdoc function - * @name angular.Array.limitTo + * @name angular.service.filter.limitTo * @function * * @description @@ -32,23 +32,23 @@
Limit {{numbers}} to: -

Output: {{ numbers.$limitTo(limit) | json }}

+

Output: {{ numbers | limitTo:limit | json }}

it('should limit the numer array to first three items', function() { expect(element('.doc-example-live input[ng\\:model=limit]').val()).toBe('3'); - expect(binding('numbers.$limitTo(limit) | json')).toEqual('[1,2,3]'); + expect(binding('numbers | limitTo:limit | json')).toEqual('[1,2,3]'); }); it('should update the output when -3 is entered', function() { input('limit').enter(-3); - expect(binding('numbers.$limitTo(limit) | json')).toEqual('[7,8,9]'); + expect(binding('numbers | limitTo:limit | json')).toEqual('[7,8,9]'); }); it('should not exceed the maximum size of input array', function() { input('limit').enter(100); - expect(binding('numbers.$limitTo(limit) | json')).toEqual('[1,2,3,4,5,6,7,8,9]'); + expect(binding('numbers | limitTo:limit | json')).toEqual('[1,2,3,4,5,6,7,8,9]'); }); diff --git a/src/service/filter/orderBy.js b/src/service/filter/orderBy.js index 07c69af3..08b86743 100644 --- a/src/service/filter/orderBy.js +++ b/src/service/filter/orderBy.js @@ -53,7 +53,7 @@ - + diff --git a/src/service/formFactory.js b/src/service/formFactory.js index 972b46ee..8ba8ce79 100644 --- a/src/service/formFactory.js +++ b/src/service/formFactory.js @@ -29,9 +29,9 @@ this.html = 'HelloWorld!'; } - function HTMLEditorWidget(element) { + HTMLEditorWidget.$inject = ['$element', 'html$Filter']; + function HTMLEditorWidget(element, htmlFilter) { var self = this; - var htmlFilter = angular.filter('html'); this.$parseModel = function() { // need to protect for script injection @@ -59,7 +59,7 @@ } angular.directive('ng:contenteditable', function() { - function linkFn($formFactory, element) { + return ['$formFactory', '$element', function ($formFactory, element) { var exp = element.attr('ng:contenteditable'), form = $formFactory.forElement(element), widget; @@ -68,14 +68,12 @@ scope: this, model: exp, controller: HTMLEditorWidget, - controllerArgs: [element]}); + controllerArgs: {$element: element}}); // if the element is destroyed, then we need to notify the form. element.bind('$destroy', function() { widget.$destroy(); }); - } - linkFn.$inject = ['$formFactory']; - return linkFn; + }]; }); diff --git a/test/scenario/ApplicationSpec.js b/test/scenario/ApplicationSpec.js index 8caf1651..86023438 100644 --- a/test/scenario/ApplicationSpec.js +++ b/test/scenario/ApplicationSpec.js @@ -112,22 +112,20 @@ describe('angular.scenario.Application', function() { expect(called).toBeTruthy(); }); - it('should wait for pending requests in executeAction', function() { + it('should wait for pending requests in executeAction', inject(function($injector, $browser) { var called, polled; var handlers = []; var testWindow = { - document: _jQuery('
'), + document: jqLite('
'), angular: { + element: jqLite, service: {} } }; - testWindow.angular.service.$browser = function() { - return { - notifyWhenNoOutstandingRequests: function(fn) { - handlers.push(fn); - } - }; + $browser.notifyWhenNoOutstandingRequests = function(fn) { + handlers.push(fn); }; + testWindow.document.data('$injector', $injector); app.getWindow_ = function() { return testWindow; }; @@ -138,5 +136,6 @@ describe('angular.scenario.Application', function() { }); expect(handlers.length).toEqual(1); handlers[0](); - }); + dealoc(testWindow.document); + })); }); diff --git a/test/scenario/dslSpec.js b/test/scenario/dslSpec.js index 411320e8..29956801 100644 --- a/test/scenario/dslSpec.js +++ b/test/scenario/dslSpec.js @@ -4,13 +4,14 @@ describe("angular.scenario.dsl", function() { var $window, $root; var application, eventLog; - beforeEach(function() { + beforeEach(inject(function($injector) { eventLog = []; $window = { - document: _jQuery("
"), + document: jqLite('
'), angular: new angular.scenario.testing.MockAngular() }; - $root = angular.injector('NG')('$rootScope'); + $window.document.data('$injector', $injector); + $root = $injector('$rootScope'); $root.emit = function(eventName) { eventLog.push(eventName); }; @@ -45,6 +46,10 @@ describe("angular.scenario.dsl", function() { // Just use the real one since it delegates to this.addFuture $root.addFutureAction = angular.scenario. SpecRunner.prototype.addFutureAction; + })); + + afterEach(function(){ + jqLite($window.document).removeData('$injector'); }); describe('Pause', function() { @@ -201,11 +206,14 @@ describe("angular.scenario.dsl", function() { // ex. jQuery('#foo').find('[name="bar"]') // fails // ex. jQuery('#foo [name="bar"]') // works, wtf? // - beforeEach(function() { + beforeEach(inject(function($injector) { doc = _jQuery('
'); _jQuery(document.body).html('').append(doc); - $window.document = window.document; - }); + + dealoc($window.document); // we are about to override it + $window.document = window.document; + jqLite($window.document).data('$injector', $injector); + })); afterEach(function() { _jQuery(document.body). diff --git a/test/scenario/mocks.js b/test/scenario/mocks.js index 2db8577a..e135390f 100644 --- a/test/scenario/mocks.js +++ b/test/scenario/mocks.js @@ -4,32 +4,18 @@ angular.scenario.testing = angular.scenario.testing || {}; angular.scenario.testing.MockAngular = function() { this.reset(); - this.service = this; + this.element = jqLite; }; angular.scenario.testing.MockAngular.prototype.reset = function() { this.log = []; }; -angular.scenario.testing.MockAngular.prototype.element = function(e) { - return jqLite(e); -}; - -angular.scenario.testing.MockAngular.prototype.$browser = function() { - this.log.push('$brower()'); - return this; -}; - angular.scenario.testing.MockAngular.prototype.poll = function() { this.log.push('$brower.poll()'); return this; }; -angular.scenario.testing.MockAngular.prototype.notifyWhenNoOutstandingRequests = function(fn) { - this.log.push('$brower.notifyWhenNoOutstandingRequests()'); - fn(); -}; - angular.scenario.testing.MockRunner = function() { this.listeners = []; }; -- cgit v1.2.3
NamePhone
{{friend.name}} {{friend.phone}}
Phone Number Age
{{friend.name}} {{friend.phone}} {{friend.age}}