aboutsummaryrefslogtreecommitdiffstats
path: root/test/service
diff options
context:
space:
mode:
Diffstat (limited to 'test/service')
-rw-r--r--test/service/filter/filterSpec.js68
-rw-r--r--test/service/filter/filtersSpec.js2
-rw-r--r--test/service/filter/limitToSpec.js52
-rw-r--r--test/service/filter/orderBySpec.js37
4 files changed, 158 insertions, 1 deletions
diff --git a/test/service/filter/filterSpec.js b/test/service/filter/filterSpec.js
new file mode 100644
index 00000000..9c43144c
--- /dev/null
+++ b/test/service/filter/filterSpec.js
@@ -0,0 +1,68 @@
+'use strict';
+
+describe('Filter: filter', function() {
+ var filter;
+
+ beforeEach(inject(function($filter){
+ filter = $filter('filter');
+ }));
+
+ it('should filter by string', function() {
+ var items = ["MIsKO", {name:"shyam"}, ["adam"], 1234];
+ assertEquals(4, filter(items, "").length);
+ assertEquals(4, filter(items, undefined).length);
+
+ assertEquals(1, filter(items, 'iSk').length);
+ assertEquals("MIsKO", filter(items, 'isk')[0]);
+
+ assertEquals(1, filter(items, 'yam').length);
+ assertEquals(items[1], filter(items, 'yam')[0]);
+
+ assertEquals(1, filter(items, 'da').length);
+ assertEquals(items[2], filter(items, 'da')[0]);
+
+ assertEquals(1, filter(items, '34').length);
+ assertEquals(1234, filter(items, '34')[0]);
+
+ assertEquals(0, filter(items, "I don't exist").length);
+ });
+
+ it('should not read $ properties', function() {
+ assertEquals("", "".charAt(0)); // assumption
+ var items = [{$name:"misko"}];
+ assertEquals(0, filter(items, "misko").length);
+ });
+
+ it('should filter on specific property', function() {
+ var items = [{ignore:"a", name:"a"}, {ignore:"a", name:"abc"}];
+ assertEquals(2, filter(items, {}).length);
+
+ assertEquals(2, filter(items, {name:'a'}).length);
+
+ assertEquals(1, filter(items, {name:'b'}).length);
+ assertEquals("abc", filter(items, {name:'b'})[0].name);
+ });
+
+ it('should take function as predicate', function() {
+ var items = [{name:"a"}, {name:"abc", done:true}];
+ assertEquals(1, filter(items, function(i) {return i.done;}).length);
+ });
+
+ it('should take object as perdicate', function() {
+ var items = [{first:"misko", last:"hevery"},
+ {first:"adam", last:"abrons"}];
+
+ assertEquals(2, filter(items, {first:'', last:''}).length);
+ assertEquals(1, filter(items, {first:'', last:'hevery'}).length);
+ assertEquals(0, filter(items, {first:'adam', last:'hevery'}).length);
+ assertEquals(1, filter(items, {first:'misko', last:'hevery'}).length);
+ assertEquals(items[0], filter(items, {first:'misko', last:'hevery'})[0]);
+ });
+
+ it('should support negation operator', function() {
+ var items = ["misko", "adam"];
+
+ assertEquals(1, filter(items, '!isk').length);
+ assertEquals(items[1], filter(items, '!isk')[0]);
+ });
+});
diff --git a/test/service/filter/filtersSpec.js b/test/service/filter/filtersSpec.js
index 39d034f5..ca510a72 100644
--- a/test/service/filter/filtersSpec.js
+++ b/test/service/filter/filtersSpec.js
@@ -1,6 +1,6 @@
'use strict';
-describe('filter', function() {
+describe('filters', function() {
var filter;
diff --git a/test/service/filter/limitToSpec.js b/test/service/filter/limitToSpec.js
new file mode 100644
index 00000000..b0977235
--- /dev/null
+++ b/test/service/filter/limitToSpec.js
@@ -0,0 +1,52 @@
+'use strict';
+
+describe('Filter: limitTo', function() {
+ var items;
+ var limitTo;
+
+ beforeEach(inject(function($filter) {
+ items = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
+ limitTo = $filter('limitTo');
+ }));
+
+
+ it('should return the first X items when X is positive', function() {
+ expect(limitTo(items, 3)).toEqual(['a', 'b', 'c']);
+ expect(limitTo(items, '3')).toEqual(['a', 'b', 'c']);
+ });
+
+
+ it('should return the last X items when X is negative', function() {
+ expect(limitTo(items, -3)).toEqual(['f', 'g', 'h']);
+ expect(limitTo(items, '-3')).toEqual(['f', 'g', 'h']);
+ });
+
+
+ it('should return an empty array when X cannot be parsed', function() {
+ expect(limitTo(items, 'bogus')).toEqual([]);
+ expect(limitTo(items, 'null')).toEqual([]);
+ expect(limitTo(items, 'undefined')).toEqual([]);
+ expect(limitTo(items, null)).toEqual([]);
+ expect(limitTo(items, undefined)).toEqual([]);
+ });
+
+
+ it('should return an empty array when input is not Array type', function() {
+ expect(limitTo('bogus', 1)).toEqual('bogus');
+ expect(limitTo(null, 1)).toEqual(null);
+ expect(limitTo(undefined, 1)).toEqual(undefined);
+ expect(limitTo(null, 1)).toEqual(null);
+ expect(limitTo(undefined, 1)).toEqual(undefined);
+ expect(limitTo({}, 1)).toEqual({});
+ });
+
+
+ it('should return a copy of input array if X is exceeds array length', function () {
+ expect(limitTo(items, 19)).toEqual(items);
+ expect(limitTo(items, '9')).toEqual(items);
+ expect(limitTo(items, -9)).toEqual(items);
+ expect(limitTo(items, '-9')).toEqual(items);
+
+ expect(limitTo(items, 9)).not.toBe(items);
+ });
+});
diff --git a/test/service/filter/orderBySpec.js b/test/service/filter/orderBySpec.js
new file mode 100644
index 00000000..f59fad55
--- /dev/null
+++ b/test/service/filter/orderBySpec.js
@@ -0,0 +1,37 @@
+'use strict';
+
+describe('Filter: orderBy', function() {
+ var orderBy;
+ beforeEach(inject(function($filter) {
+ orderBy = $filter('orderBy');
+ }));
+
+ it('should return same array if predicate is falsy', function() {
+ var array = [1, 2, 3];
+ expect(orderBy(array)).toBe(array);
+ });
+
+ it('shouldSortArrayInReverse', function() {
+ assertJsonEquals([{a:15},{a:2}], orderBy([{a:15},{a:2}], 'a', true));
+ assertJsonEquals([{a:15},{a:2}], orderBy([{a:15},{a:2}], 'a', "T"));
+ assertJsonEquals([{a:15},{a:2}], orderBy([{a:15},{a:2}], 'a', "reverse"));
+ });
+
+ it('should sort array by predicate', function() {
+ assertJsonEquals([{a:2, b:1},{a:15, b:1}],
+ orderBy([{a:15, b:1},{a:2, b:1}], ['a', 'b']));
+ assertJsonEquals([{a:2, b:1},{a:15, b:1}],
+ orderBy([{a:15, b:1},{a:2, b:1}], ['b', 'a']));
+ assertJsonEquals([{a:15, b:1},{a:2, b:1}],
+ orderBy([{a:15, b:1},{a:2, b:1}], ['+b', '-a']));
+ });
+
+ it('should use function', function() {
+ expect(
+ orderBy(
+ [{a:15, b:1},{a:2, b:1}],
+ function(value) { return value.a; })).
+ toEqual([{a:2, b:1},{a:15, b:1}]);
+ });
+
+});