From cb6f832f38b11499a6d1dd2caf14d15e68211635 Mon Sep 17 00:00:00 2001
From: Misko Hevery
Date: Thu, 3 Nov 2011 15:59:18 -0700
Subject: refactor(filter): filters are now injectable and services
BREAK:
- removed CSS support from filters
---
test/directivesSpec.js | 22 ++++--------
test/service/filter/filtersSpec.js | 72 +++++++++++++++-----------------------
test/service/parseSpec.js | 19 ++++------
3 files changed, 42 insertions(+), 71 deletions(-)
(limited to 'test')
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('
hello
');
}));
- 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('hello');
- };
+ }));
var element = $compile('')($rootScope);
$rootScope.$digest();
expect(lowercase(element.html())).toEqual('hello');
}));
- 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('')($rootScope);
- $rootScope.$digest();
- expect(sortedHtml(element)).toEqual('');
- }));
-
it('should suppress rendering of falsy values', inject(function($rootScope, $compile) {
var element = $compile('{{ null }}{{ undefined }}{{ "" }}-{{ 0 }}{{ false }}
')($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('beforeINNERafter
')($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('');
- 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('')}, {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("acd");
+ var html = filter('html')("acd");
expect(html instanceof HTML).toBeTruthy();
expect(html.html).toEqual("acd");
});
});
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://1.2/v:~-123. c").html).
toEqual('http://ab/ ' +
@@ -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;
--
cgit v1.2.3