|
| 1 | +/** |
| 2 | + * Test deepSort Function |
| 3 | + * |
| 4 | + * @group unit/idempotency/deepSort |
| 5 | + */ |
| 6 | +import { deepSort } from '../../src/deepSort'; |
| 7 | + |
| 8 | +describe('Function: deepSort', () => { |
| 9 | + test('can sort string correctly', () => { |
| 10 | + expect(deepSort('test')).toEqual('test'); |
| 11 | + }); |
| 12 | + |
| 13 | + test('can sort number correctly', () => { |
| 14 | + expect(deepSort(5)).toEqual(5); |
| 15 | + }); |
| 16 | + |
| 17 | + test('can sort boolean correctly', () => { |
| 18 | + expect(deepSort(true)).toEqual(true); |
| 19 | + expect(deepSort(false)).toEqual(false); |
| 20 | + }); |
| 21 | + |
| 22 | + test('can sort null correctly', () => { |
| 23 | + expect(deepSort(null)).toEqual(null); |
| 24 | + }); |
| 25 | + |
| 26 | + test('can sort undefined correctly', () => { |
| 27 | + expect(deepSort(undefined)).toEqual(undefined); |
| 28 | + }); |
| 29 | + |
| 30 | + test('can sort object with nested keys correctly', () => { |
| 31 | + // Prepare |
| 32 | + const input = { |
| 33 | + name: 'John', |
| 34 | + age: 30, |
| 35 | + city: 'New York', |
| 36 | + address: { |
| 37 | + street: '5th Avenue', |
| 38 | + number: 123, |
| 39 | + }, |
| 40 | + }; |
| 41 | + |
| 42 | + // Act |
| 43 | + const result = deepSort(input); |
| 44 | + |
| 45 | + // Assess |
| 46 | + expect(JSON.stringify(result)).toEqual( |
| 47 | + JSON.stringify({ |
| 48 | + address: { |
| 49 | + number: 123, |
| 50 | + street: '5th Avenue', |
| 51 | + }, |
| 52 | + age: 30, |
| 53 | + city: 'New York', |
| 54 | + name: 'John', |
| 55 | + }) |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + test('can sort deeply nested structures', () => { |
| 60 | + // Prepare |
| 61 | + const input = { |
| 62 | + z: [{ b: { d: 4, c: 3 }, a: { f: 6, e: 5 } }], |
| 63 | + a: { c: 3, b: 2, a: 1 }, |
| 64 | + }; |
| 65 | + |
| 66 | + // Act |
| 67 | + const result = deepSort(input); |
| 68 | + |
| 69 | + //Assess |
| 70 | + expect(JSON.stringify(result)).toEqual( |
| 71 | + JSON.stringify({ |
| 72 | + a: { a: 1, b: 2, c: 3 }, |
| 73 | + z: [{ a: { e: 5, f: 6 }, b: { c: 3, d: 4 } }], |
| 74 | + }) |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + test('can sort JSON array with objects containing words as keys and nested objects/arrays correctly', () => { |
| 79 | + // Prepare |
| 80 | + const input = [ |
| 81 | + { |
| 82 | + transactions: [ |
| 83 | + 50, |
| 84 | + 40, |
| 85 | + { field: 'a', category: 'x', purpose: 's' }, |
| 86 | + [ |
| 87 | + { |
| 88 | + zone: 'c', |
| 89 | + warehouse: 'd', |
| 90 | + attributes: { region: 'a', quality: 'x', batch: 's' }, |
| 91 | + }, |
| 92 | + ], |
| 93 | + ], |
| 94 | + totalAmount: 30, |
| 95 | + customerName: 'John', |
| 96 | + location: 'New York', |
| 97 | + transactionType: 'a', |
| 98 | + }, |
| 99 | + { |
| 100 | + customerName: 'John', |
| 101 | + location: 'New York', |
| 102 | + transactionDetails: [ |
| 103 | + { field: 'a', category: 'x', purpose: 's' }, |
| 104 | + null, |
| 105 | + 50, |
| 106 | + [{ zone: 'c', warehouse: 'd', attributes: 't' }], |
| 107 | + 40, |
| 108 | + ], |
| 109 | + amount: 30, |
| 110 | + }, |
| 111 | + ]; |
| 112 | + |
| 113 | + // Act |
| 114 | + const result = deepSort(input); |
| 115 | + |
| 116 | + // Assess |
| 117 | + expect(JSON.stringify(result)).toEqual( |
| 118 | + JSON.stringify([ |
| 119 | + { |
| 120 | + customerName: 'John', |
| 121 | + location: 'New York', |
| 122 | + totalAmount: 30, |
| 123 | + transactions: [ |
| 124 | + 50, |
| 125 | + 40, |
| 126 | + { category: 'x', field: 'a', purpose: 's' }, |
| 127 | + [ |
| 128 | + { |
| 129 | + attributes: { batch: 's', quality: 'x', region: 'a' }, |
| 130 | + warehouse: 'd', |
| 131 | + zone: 'c', |
| 132 | + }, |
| 133 | + ], |
| 134 | + ], |
| 135 | + transactionType: 'a', |
| 136 | + }, |
| 137 | + { |
| 138 | + amount: 30, |
| 139 | + customerName: 'John', |
| 140 | + location: 'New York', |
| 141 | + transactionDetails: [ |
| 142 | + { category: 'x', field: 'a', purpose: 's' }, |
| 143 | + null, |
| 144 | + 50, |
| 145 | + [{ attributes: 't', warehouse: 'd', zone: 'c' }], |
| 146 | + 40, |
| 147 | + ], |
| 148 | + }, |
| 149 | + ]) |
| 150 | + ); |
| 151 | + }); |
| 152 | + |
| 153 | + test('handles empty objects and arrays correctly', () => { |
| 154 | + expect(deepSort({})).toEqual({}); |
| 155 | + expect(deepSort([])).toEqual([]); |
| 156 | + }); |
| 157 | +}); |
0 commit comments