1
1
import { sort } from '../src'
2
2
3
3
describe ( 'sort' , ( ) => {
4
- it ( 'should sort an object' , ( ) => {
5
- const input = { b : 'b' , a : 'a' }
6
- const expected = { a : 'a' , b : 'b' }
4
+ it ( 'should sort an object in ascending order by default ' , ( ) => {
5
+ const input = { c : 'c' , b : 'b' , a : 'a' }
6
+ const expected = { a : 'a' , b : 'b' , c : 'c' }
7
7
expect ( sort ( input ) ) . toEqual ( expected )
8
8
} )
9
9
10
- it ( 'should not sort an simple array' , ( ) => {
11
- const input = [ 'a' , 'b' ]
12
- const expected = [ 'a' , 'b' ]
13
- expect ( sort ( input ) ) . toEqual ( expected )
10
+ it ( 'should sort an object in descending order when specified' , ( ) => {
11
+ const input = { a : 'a' , b : 'b' , c : 'c' }
12
+ const expected = { c : 'c' , b : 'b' , a : 'a' }
13
+ expect ( sort ( input , false ) ) . toEqual ( expected )
14
+ } )
15
+
16
+ it ( 'should not modify an array of primitives' , ( ) => {
17
+ const input = [ 'b' , 'a' , 'c' ]
18
+ expect ( sort ( input ) ) . toEqual ( input )
14
19
} )
15
20
16
21
it ( 'should sort an array of objects' , ( ) => {
@@ -25,74 +30,41 @@ describe('sort', () => {
25
30
expect ( sort ( input ) ) . toEqual ( expected )
26
31
} )
27
32
28
- it ( 'should sort an object with array values ' , ( ) => {
33
+ it ( 'should sort nested objects ' , ( ) => {
29
34
const input = {
30
- b : [ 'b ', 'a' ] ,
31
- a : [ 'd' , 'c' , 'b ', 'a' ] ,
35
+ b : { z : 'z ', y : 'y' } ,
36
+ a : { x : 'x ', w : 'w' } ,
32
37
}
33
38
const expected = {
34
- a : [ 'd' , 'c' , 'b ', 'a' ] ,
35
- b : [ 'b ', 'a' ] ,
39
+ a : { w : 'w ', x : 'x' } ,
40
+ b : { y : 'y ', z : 'z' } ,
36
41
}
37
42
expect ( sort ( input ) ) . toEqual ( expected )
38
43
} )
39
44
40
- it ( 'should sort an object with nested objects ' , ( ) => {
45
+ it ( 'should handle mixed nested structures ' , ( ) => {
41
46
const input = {
42
- a : 'a' ,
43
- b : {
44
- b : 'b' ,
45
- a : 'a' ,
46
- } ,
47
+ b : [ 'b' , 'a' ] ,
48
+ a : { d : 'd' , c : 'c' } ,
47
49
}
48
50
const expected = {
49
- a : 'a' ,
50
- b : {
51
- a : 'a' ,
52
- b : 'b' ,
53
- } ,
51
+ a : { c : 'c' , d : 'd' } ,
52
+ b : [ 'b' , 'a' ] ,
54
53
}
55
54
expect ( sort ( input ) ) . toEqual ( expected )
56
55
} )
57
56
58
- it ( 'should not sort an array of simple arrays' , ( ) => {
59
- const input = [
60
- [ 'b' , 'a' ] ,
61
- [ 'd' , 'c' , 'b' , 'a' ] ,
62
- ]
63
- const expected = [
64
- [ 'b' , 'a' ] ,
65
- [ 'd' , 'c' , 'b' , 'a' ] ,
66
- ]
67
- expect ( sort ( input ) ) . toEqual ( expected )
57
+ it ( 'should not throw an error for primitive inputs' , ( ) => {
58
+ expect ( ( ) => sort ( 'string' ) ) . not . toThrow ( )
59
+ expect ( ( ) => sort ( 123 ) ) . not . toThrow ( )
60
+ expect ( ( ) => sort ( null ) ) . not . toThrow ( )
61
+ expect ( ( ) => sort ( undefined ) ) . not . toThrow ( )
68
62
} )
69
63
70
- it ( 'should sort an array containing objects' , ( ) => {
71
- const input = [
72
- { b : 'b' , a : 'a' } ,
73
- { d : 'd' , c : 'c' , b : 'b' , a : 'a' } ,
74
- ]
75
- const expected = [
76
- { a : 'a' , b : 'b' } ,
77
- { a : 'a' , b : 'b' , c : 'c' , d : 'd' } ,
78
- ]
79
- expect ( sort ( input ) ) . toEqual ( expected )
80
- } )
81
-
82
- it ( 'should sort an object in descending order' , ( ) => {
83
- const input = { b : 'b' , a : 'a' , c : 'c' }
84
- const expected = { c : 'c' , b : 'b' , a : 'a' }
85
- expect ( sort ( input , false ) ) . toEqual ( expected )
86
- } )
87
-
88
- it ( 'should throw an error for non-object, non-array input' , ( ) => {
89
- const input = 'string'
90
- expect ( ( ) => sort ( input ) ) . toThrow ( 'Invalid data type: expected an object or array of objects.' )
91
-
92
- const inputNumber = 123
93
- expect ( ( ) => sort ( inputNumber ) ) . toThrow ( 'Invalid data type: expected an object or array of objects.' )
94
-
95
- const inputNull = null
96
- expect ( ( ) => sort ( inputNull ) ) . toThrow ( 'Invalid data type: expected an object or array of objects.' )
64
+ it ( 'should return primitive inputs unchanged' , ( ) => {
65
+ expect ( sort ( 'string' ) ) . toBe ( 'string' )
66
+ expect ( sort ( 123 ) ) . toBe ( 123 )
67
+ expect ( sort ( null ) ) . toBe ( null )
68
+ expect ( sort ( undefined ) ) . toBe ( undefined )
97
69
} )
98
70
} )
0 commit comments