forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilterSpec.js
144 lines (112 loc) · 4.62 KB
/
filterSpec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'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];
expect(filter(items, '').length).toBe(4);
expect(filter(items, undefined).length).toBe(4);
expect(filter(items, 'iSk').length).toBe(1);
expect(filter(items, 'isk')[0]).toBe('MIsKO');
expect(filter(items, 'yam').length).toBe(1);
expect(filter(items, 'yam')[0]).toEqual(items[1]);
expect(filter(items, 'da').length).toBe(1);
expect(filter(items, 'da')[0]).toEqual(items[2]);
expect(filter(items, '34').length).toBe(1);
expect(filter(items, '34')[0]).toBe(1234);
expect(filter(items, "I don't exist").length).toBe(0);
});
it('should not read $ properties', function() {
expect(''.charAt(0)).toBe(''); // assumption
var items = [{$name: 'misko'}];
expect(filter(items, 'misko').length).toBe(0);
});
it('should filter on specific property', function() {
var items = [{ignore: 'a', name: 'a'}, {ignore: 'a', name: 'abc'}];
expect(filter(items, {}).length).toBe(2);
expect(filter(items, {name: 'a'}).length).toBe(2);
expect(filter(items, {name: 'b'}).length).toBe(1);
expect(filter(items, {name: 'b'})[0].name).toBe('abc');
});
it('should take function as predicate', function() {
var items = [{name: 'a'}, {name: 'abc', done: true}];
expect(filter(items, function(i) {return i.done;}).length).toBe(1);
});
it('should take object as perdicate', function() {
var items = [{first: 'misko', last: 'hevery'},
{first: 'adam', last: 'abrons'}];
expect(filter(items, {first:'', last:''}).length).toBe(2);
expect(filter(items, {first:'', last:'hevery'}).length).toBe(1);
expect(filter(items, {first:'adam', last:'hevery'}).length).toBe(0);
expect(filter(items, {first:'misko', last:'hevery'}).length).toBe(1);
expect(filter(items, {first:'misko', last:'hevery'})[0]).toEqual(items[0]);
});
it('should match any properties for given "$" property', function() {
var items = [{first: 'tom', last: 'hevery'},
{first: 'adam', last: 'hevery', alias: 'tom', done: false},
{first: 'john', last: 'clark', middle: 'tommy'}];
expect(filter(items, {$: 'tom'}).length).toBe(3);
expect(filter(items, {$: 'a'}).length).toBe(2);
expect(filter(items, {$: false}).length).toBe(1);
expect(filter(items, {$: 10}).length).toBe(0);
expect(filter(items, {$: 'hevery'})[0]).toEqual(items[0]);
});
it('should support boolean properties', function() {
var items = [{name: 'tom', current: true},
{name: 'demi', current: false},
{name: 'sofia'}];
expect(filter(items, {current:true}).length).toBe(1);
expect(filter(items, {current:true})[0].name).toBe('tom');
expect(filter(items, {current:false}).length).toBe(1);
expect(filter(items, {current:false})[0].name).toBe('demi');
});
it('should support negation operator', function() {
var items = ['misko', 'adam'];
expect(filter(items, '!isk').length).toBe(1);
expect(filter(items, '!isk')[0]).toEqual(items[1]);
});
describe('should support comparator', function() {
it('as equality when true', function() {
var items = ['misko', 'adam', 'adamson'];
var expr = 'adam';
expect(filter(items, expr, true)).toEqual([items[1]]);
expect(filter(items, expr, false)).toEqual([items[1], items[2]]);
var items = [
{key: 'value1', nonkey: 1},
{key: 'value2', nonkey: 2},
{key: 'value12', nonkey: 3},
{key: 'value1', nonkey:4},
{key: 'Value1', nonkey:5}
];
var expr = {key: 'value1'};
expect(filter(items, expr, true)).toEqual([items[0], items[3]]);
var items = [
{key: 1, nonkey: 1},
{key: 2, nonkey: 2},
{key: 12, nonkey: 3},
{key: 1, nonkey:4}
];
var expr = { key: 1 };
expect(filter(items, expr, true)).toEqual([items[0], items[3]]);
var expr = 12;
expect(filter(items, expr, true)).toEqual([items[2]]);
});
it('and use the function given to compare values', function() {
var items = [
{key: 1, nonkey: 1},
{key: 2, nonkey: 2},
{key: 12, nonkey: 3},
{key: 1, nonkey:14}
];
var expr = {key: 10};
var comparator = function (obj,value) {
return obj > value;
}
expect(filter(items, expr, comparator)).toEqual([items[2]]);
expr = 10;
expect(filter(items, expr, comparator)).toEqual([items[2], items[3]]);
});
});
});