11import { sort } from '../src'
22
33describe ( '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' }
77 expect ( sort ( input ) ) . toEqual ( expected )
88 } )
99
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 )
1419 } )
1520
1621 it ( 'should sort an array of objects' , ( ) => {
@@ -25,74 +30,41 @@ describe('sort', () => {
2530 expect ( sort ( input ) ) . toEqual ( expected )
2631 } )
2732
28- it ( 'should sort an object with array values ' , ( ) => {
33+ it ( 'should sort nested objects ' , ( ) => {
2934 const input = {
30- b : [ 'b ', 'a' ] ,
31- a : [ 'd' , 'c' , 'b ', 'a' ] ,
35+ b : { z : 'z ', y : 'y' } ,
36+ a : { x : 'x ', w : 'w' } ,
3237 }
3338 const expected = {
34- a : [ 'd' , 'c' , 'b ', 'a' ] ,
35- b : [ 'b ', 'a' ] ,
39+ a : { w : 'w ', x : 'x' } ,
40+ b : { y : 'y ', z : 'z' } ,
3641 }
3742 expect ( sort ( input ) ) . toEqual ( expected )
3843 } )
3944
40- it ( 'should sort an object with nested objects ' , ( ) => {
45+ it ( 'should handle mixed nested structures ' , ( ) => {
4146 const input = {
42- a : 'a' ,
43- b : {
44- b : 'b' ,
45- a : 'a' ,
46- } ,
47+ b : [ 'b' , 'a' ] ,
48+ a : { d : 'd' , c : 'c' } ,
4749 }
4850 const expected = {
49- a : 'a' ,
50- b : {
51- a : 'a' ,
52- b : 'b' ,
53- } ,
51+ a : { c : 'c' , d : 'd' } ,
52+ b : [ 'b' , 'a' ] ,
5453 }
5554 expect ( sort ( input ) ) . toEqual ( expected )
5655 } )
5756
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 ( )
6862 } )
6963
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 )
9769 } )
9870} )
0 commit comments