1
+ describe ( 'unique' , function ( ) {
2
+ var uniqueFilter ;
3
+
4
+ beforeEach ( module ( 'ui.filters' ) ) ;
5
+ beforeEach ( inject ( function ( $filter ) {
6
+ uniqueFilter = $filter ( 'unique' ) ;
7
+ } ) ) ;
8
+
9
+ it ( 'should return unique entries based on object equality' , function ( ) {
10
+ var arrayToFilter = [ { key : 'value' } , { key : 'value2' } , { key : 'value' } ] ;
11
+ expect ( uniqueFilter ( arrayToFilter ) ) . toEqual ( [ { key : 'value' } , { key : 'value2' } ] ) ;
12
+ } ) ;
13
+
14
+ it ( 'should return unique entries based on object equality for complex objects' , function ( ) {
15
+ var arrayToFilter = [ { key : 'value' , other : 'other1' } , { key : 'value2' , other : 'other2' } , { other : 'other1' , key : 'value' } ] ;
16
+ expect ( uniqueFilter ( arrayToFilter ) ) . toEqual ( [ { key : 'value' , other : 'other1' } , { key : 'value2' , other : 'other2' } ] ) ;
17
+ } ) ;
18
+
19
+ it ( 'should return unique entries based on the key provided' , function ( ) {
20
+ var arrayToFilter = [ { key : 'value' } , { key : 'value2' } , { key : 'value' } ] ;
21
+ expect ( uniqueFilter ( arrayToFilter , 'key' ) ) . toEqual ( [ { key : 'value' } , { key : 'value2' } ] ) ;
22
+ } ) ;
23
+
24
+ it ( 'should return unique entries based on the key provided for complex objects' , function ( ) {
25
+ var arrayToFilter = [ { key : 'value' , other : 'other1' } , { key : 'value2' , other : 'other2' } , { key : 'value' , other : 'other3' } ] ;
26
+ expect ( uniqueFilter ( arrayToFilter , 'key' ) ) . toEqual ( [ { key : 'value' , other : 'other1' } , { key : 'value2' , other : 'other2' } ] ) ;
27
+ } ) ;
28
+
29
+ it ( 'should return unique primitives in arrays' , function ( ) {
30
+ expect ( uniqueFilter ( [ 1 , 2 , 1 , 3 ] ) ) . toEqual ( [ 1 , 2 , 3 ] ) ;
31
+ } ) ;
32
+
33
+ it ( 'should work correctly for arrays of mixed elements and object equality' , function ( ) {
34
+ expect ( uniqueFilter ( [ 1 , { key :'value' } , 1 , { key :'value' } , 2 , "string" , 3 ] ) ) . toEqual ( [ 1 , { key :'value' } , 2 , "string" , 3 ] ) ;
35
+ } ) ;
36
+
37
+ it ( 'should work correctly for arrays of mixed elements and a key specified' , function ( ) {
38
+ expect ( uniqueFilter ( [ 1 , { key :'value' } , 1 , { key :'value' } , 2 , "string" , 3 ] , 'key' ) ) . toEqual ( [ 1 , { key :'value' } , 2 , "string" , 3 ] ) ;
39
+ } ) ;
40
+
41
+ it ( 'should return unmodified object if not array' , function ( ) {
42
+ expect ( uniqueFilter ( 'string' , 'someKey' ) ) . toEqual ( 'string' ) ;
43
+ } ) ;
44
+
45
+ it ( 'should return unmodified array if provided key === false' , function ( ) {
46
+ var arrayToFilter = [ { key : 'value1' } , { key : 'value2' } ] ;
47
+ expect ( uniqueFilter ( arrayToFilter , false ) ) . toEqual ( arrayToFilter ) ;
48
+ } ) ;
49
+
50
+ } )
0 commit comments