diff options
| author | Misko Hevery | 2011-11-03 15:59:18 -0700 |
|---|---|---|
| committer | Misko Hevery | 2011-11-14 20:31:09 -0800 |
| commit | cb6f832f38b11499a6d1dd2caf14d15e68211635 (patch) | |
| tree | adb837d60cb080712319122d3a7103e20e07c2e0 /test | |
| parent | 6022f3df399a5d98830dfe7904f0ad2baaa308c7 (diff) | |
| download | angular.js-cb6f832f38b11499a6d1dd2caf14d15e68211635.tar.bz2 | |
refactor(filter): filters are now injectable and services
BREAK:
- removed CSS support from filters
Diffstat (limited to 'test')
| -rw-r--r-- | test/directivesSpec.js | 22 | ||||
| -rw-r--r-- | test/service/filter/filtersSpec.js | 72 | ||||
| -rw-r--r-- | test/service/parseSpec.js | 19 |
3 files changed, 42 insertions, 71 deletions
diff --git a/test/directivesSpec.js b/test/directivesSpec.js index 7acf87bc..93682fa0 100644 --- a/test/directivesSpec.js +++ b/test/directivesSpec.js @@ -41,25 +41,15 @@ describe("directive", function() { expect(lowercase(element.html())).toEqual('<div onclick="">hello</div>'); })); - it('should set element element', inject(function($rootScope, $compile) { - angularFilter.myElement = function() { + it('should set element element', inject(function($rootScope, $compile, $provide) { + $provide.filter('myElement', valueFn(function() { return jqLite('<a>hello</a>'); - }; + })); var element = $compile('<div ng:bind="0|myElement"></div>')($rootScope); $rootScope.$digest(); expect(lowercase(element.html())).toEqual('<a>hello</a>'); })); - it('should have $element set to current bind element', inject(function($rootScope, $compile) { - angularFilter.myFilter = function() { - this.$element.addClass("filter"); - return 'HELLO'; - }; - var element = $compile('<div>before<div ng:bind="0|myFilter"></div>after</div>')($rootScope); - $rootScope.$digest(); - expect(sortedHtml(element)).toEqual('<div>before<div class="filter" ng:bind="0|myFilter">HELLO</div>after</div>'); - })); - it('should suppress rendering of falsy values', inject(function($rootScope, $compile) { var element = $compile('<div>{{ null }}{{ undefined }}{{ "" }}-{{ 0 }}{{ false }}</div>')($rootScope); @@ -83,12 +73,12 @@ describe("directive", function() { expect(element.text()).toEqual('Hello Misko!'); })); - it('should have $element set to current bind element', inject(function($rootScope, $compile) { + it('should have $element set to current bind element', inject(function($rootScope, $compile, $provide) { var innerText; - angularFilter.myFilter = function(text) { + $provide.filter('myFilter', valueFn(function(text) { innerText = innerText || this.$element.text(); return text; - }; + })); var element = $compile('<div>before<span ng:bind-template="{{\'HELLO\'|myFilter}}">INNER</span>after</div>')($rootScope); $rootScope.$digest(); expect(element.text()).toEqual("beforeHELLOafter"); diff --git a/test/service/filter/filtersSpec.js b/test/service/filter/filtersSpec.js index 8c567441..39d034f5 100644 --- a/test/service/filter/filtersSpec.js +++ b/test/service/filter/filtersSpec.js @@ -2,27 +2,18 @@ describe('filter', function() { - var filter = angular.filter; + var filter; - it('should called the filter when evaluating expression', inject(function($rootScope) { - filter.fakeFilter = function() {}; - spyOn(filter, 'fakeFilter'); - - $rootScope.$eval('10|fakeFilter'); - expect(filter.fakeFilter).toHaveBeenCalledWith(10); - delete filter['fakeFilter']; + beforeEach(inject(function($filter){ + filter = $filter; })); - 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(); + it('should called the filter when evaluating expression', inject(function($rootScope, $provide) { + var filter = jasmine.createSpy('myFilter'); + $provide.filter('myFilter', valueFn(filter)); - $rootScope.$eval('10|fakeFilter'); - expect(filter.fakeFilter).toHaveBeenCalled(); - delete filter['fakeFilter']; + $rootScope.$eval('10|myFilter'); + expect(filter).toHaveBeenCalledWith(10); })); describe('formatNumber', function() { @@ -81,40 +72,31 @@ describe('filter', function() { }); describe('currency', function() { - var currency, html, context; + var currency; - beforeEach(inject(function($rootScope) { - html = jqLite('<span></span>'); - context = $rootScope; - context.$element = html; - currency = bind(context, filter.currency); - })); + beforeEach(function() { + currency = filter('currency'); + }); it('should do basic currency filtering', function() { expect(currency(0)).toEqual('$0.00'); - expect(html.hasClass('ng-format-negative')).toBeFalsy(); expect(currency(-999)).toEqual('($999.00)'); - expect(html.hasClass('ng-format-negative')).toBeTruthy(); expect(currency(1234.5678, "USD$")).toEqual('USD$1,234.57'); - expect(html.hasClass('ng-format-negative')).toBeFalsy(); }); it('should return empty string for non-numbers', function() { expect(currency()).toBe(''); - expect(html.hasClass('ng-format-negative')).toBeFalsy(); expect(currency('abc')).toBe(''); - expect(html.hasClass('ng-format-negative')).toBeFalsy(); }); }); describe('number', function() { - var context, number; + var number; beforeEach(inject(function($rootScope) { - context = $rootScope; - number = bind(context, filter.number); + number = filter('number'); })); @@ -151,34 +133,39 @@ describe('filter', function() { describe('json', function () { it('should do basic filter', function() { - expect(filter.json.call({$element:jqLite('<div></div>')}, {a:"b"})).toEqual(toJson({a:"b"}, true)); + expect(filter('json')({a:"b"})).toEqual(toJson({a:"b"}, true)); }); }); describe('lowercase', function() { it('should do basic filter', function() { - expect(filter.lowercase('AbC')).toEqual('abc'); - expect(filter.lowercase(null)).toBeNull(); + expect(filter('lowercase')('AbC')).toEqual('abc'); + expect(filter('lowercase')(null)).toBeNull(); }); }); describe('uppercase', function() { it('should do basic filter', function() { - expect(filter.uppercase('AbC')).toEqual('ABC'); - expect(filter.uppercase(null)).toBeNull(); + expect(filter('uppercase')('AbC')).toEqual('ABC'); + expect(filter('uppercase')(null)).toBeNull(); }); }); describe('html', function() { it('should do basic filter', function() { - var html = filter.html("a<b>c</b>d"); + var html = filter('html')("a<b>c</b>d"); expect(html instanceof HTML).toBeTruthy(); expect(html.html).toEqual("a<b>c</b>d"); }); }); describe('linky', function() { - var linky = filter.linky; + var linky; + + beforeEach(inject(function($filter){ + linky = $filter('linky') + })); + it('should do basic filter', function() { expect(linky("http://ab/ (http://a/) <http://a/> http://1.2/v:~-123. c").html). toEqual('<a href="http://ab/">http://ab/</a> ' + @@ -205,11 +192,10 @@ describe('filter', function() { var midnight = new angular.mock.TzDate(+5, '2010-09-03T05:05:08.000Z'); //12am var earlyDate = new angular.mock.TzDate(+5, '0001-09-03T05:05:08.000Z'); - var context, date; + var date; - beforeEach(inject(function($rootScope) { - context = $rootScope; - date = bind(context, filter.date); + beforeEach(inject(function($filter) { + date = $filter('date'); })); it('should ignore falsy inputs', function() { diff --git a/test/service/parseSpec.js b/test/service/parseSpec.js index 5045ec9e..506d3373 100644 --- a/test/service/parseSpec.js +++ b/test/service/parseSpec.js @@ -191,24 +191,19 @@ describe('parser', function() { expect(scope.$eval("'a' + 'b c'")).toEqual("ab c"); }); - it('should parse filters', function() { - angular.filter.substring = function(input, start, end) { + it('should parse filters', inject(function($provide) { + $provide.filter('substring', valueFn(function(input, start, end) { return input.substring(start, end); - }; - - angular.filter.upper = {_case: function(input) { - return input.toUpperCase(); - }}; + })); expect(function() { - scope.$eval("1|nonExistant"); - }).toThrow(new Error("Syntax Error: Token 'nonExistant' should be a function at column 3 of the expression [1|nonExistant] starting at [nonExistant].")); + scope.$eval("1|nonexistent"); + }).toThrow(new Error("Unknown provider for 'nonexistent$Filter'.")); scope.offset = 3; - expect(scope.$eval("'abcd'|upper._case")).toEqual("ABCD"); expect(scope.$eval("'abcd'|substring:1:offset")).toEqual("bc"); - expect(scope.$eval("'abcd'|substring:1:3|upper._case")).toEqual("BC"); - }); + expect(scope.$eval("'abcd'|substring:1:3|uppercase")).toEqual("BC"); + })); it('should access scope', function() { scope.a = 123; |
