@@ -130,25 +130,86 @@ describe('Filters', function () {
130130
131131 describe ( 'filterBy' , function ( ) {
132132
133- var filter = filters . filterBy
133+ var filter = filters . filterBy ,
134+ arr = [
135+ { a : 1 , b : { c : 'hello' } } ,
136+ { a : 1 , b : 'hello' } ,
137+ { a : 1 , b : 2 }
138+ ] ,
139+ vm = { search : { key : 'hello' , datakey : 'b.c' } }
134140
135141 it ( 'should be computed' , function ( ) {
136142 assert . ok ( filter . computed )
137143 } )
138144
139- // TODO
145+ it ( 'should recursively check for searchKey if no dataKey is provided' , function ( ) {
146+ var res = filter . call ( vm , arr , 'search.key' )
147+ assert . strictEqual ( res . length , 2 )
148+ assert . deepEqual ( res , arr . slice ( 0 , 2 ) )
149+ } )
150+
151+ it ( 'should check for datakey only if provided' , function ( ) {
152+ var res = filter . call ( vm , arr , 'search.key' , 'search.datakey' )
153+ assert . strictEqual ( res . length , 1 )
154+ assert . strictEqual ( res [ 0 ] , arr [ 0 ] )
155+ } )
156+
157+ it ( 'should use literal searchKey if in single quotes' , function ( ) {
158+ var res = filter . call ( vm , arr , "'hello'" , "'b.c'" )
159+ assert . strictEqual ( res . length , 1 )
160+ assert . strictEqual ( res [ 0 ] , arr [ 0 ] )
161+ } )
162+
163+ it ( 'should accept optional delimiter' , function ( ) {
164+ var res = filter . call ( vm , arr , 'search.key' , 'in' , 'search.datakey' )
165+ assert . strictEqual ( res . length , 1 )
166+ assert . strictEqual ( res [ 0 ] , arr [ 0 ] )
167+ } )
140168
141169 } )
142170
143171 describe ( 'orderBy' , function ( ) {
144172
145- var filter = filters . orderBy
173+ var filter = filters . orderBy ,
174+ arr = [
175+ { a : { b : 0 } , c : 'b' } ,
176+ { a : { b : 2 } , c : 'c' } ,
177+ { a : { b : 1 } , c : 'a' }
178+ ]
146179
147180 it ( 'should be computed' , function ( ) {
148181 assert . ok ( filter . computed )
149182 } )
150183
151- // TODO
184+ it ( 'should sort based on sortKey' , function ( ) {
185+ var vm = { sortby : 'a.b' }
186+ var res = filter . call ( vm , arr , 'sortby' )
187+ assert . strictEqual ( res [ 0 ] . a . b , 0 )
188+ assert . strictEqual ( res [ 1 ] . a . b , 1 )
189+ assert . strictEqual ( res [ 2 ] . a . b , 2 )
190+ } )
191+
192+ it ( 'should sort based on sortKey and reverseKey' , function ( ) {
193+ var vm = { sortby : 'a.b' , reverse : true }
194+ var res = filter . call ( vm , arr , 'sortby' , 'reverse' )
195+ assert . strictEqual ( res [ 0 ] . a . b , 2 )
196+ assert . strictEqual ( res [ 1 ] . a . b , 1 )
197+ assert . strictEqual ( res [ 2 ] . a . b , 0 )
198+ } )
199+
200+ it ( 'should sort with literal args and special -1 syntax' , function ( ) {
201+ var res = filter . call ( { } , arr , "'c'" , '-1' )
202+ assert . strictEqual ( res [ 0 ] . c , 'c' )
203+ assert . strictEqual ( res [ 1 ] . c , 'b' )
204+ assert . strictEqual ( res [ 2 ] . c , 'a' )
205+ } )
206+
207+ it ( 'should accept negate reverse key' , function ( ) {
208+ var res = filter . call ( { reverse : true } , arr , "'c'" , '!reverse' )
209+ assert . strictEqual ( res [ 0 ] . c , 'a' )
210+ assert . strictEqual ( res [ 1 ] . c , 'b' )
211+ assert . strictEqual ( res [ 2 ] . c , 'c' )
212+ } )
152213
153214 } )
154215
0 commit comments