|
1 | | -import { deepCompare } from "./deepCompare"; |
| 1 | +import { deepCompare, stripNullValues } from "./deepCompare"; |
2 | 2 | import { describe, it, expect } from "bun:test"; |
3 | 3 |
|
4 | 4 | describe("deepCompare", () => { |
@@ -130,3 +130,72 @@ describe("deepCompare", () => { |
130 | 130 | expect(deepCompare(obj1, obj2)).toBe(true); |
131 | 131 | }); |
132 | 132 | }); |
| 133 | + |
| 134 | +describe("stripNullValues", () => { |
| 135 | + it("should return the same object if no null values exist", () => { |
| 136 | + const obj = { a: 1, b: 'test', c: true }; |
| 137 | + expect(stripNullValues(obj)).toEqual(obj); |
| 138 | + }); |
| 139 | + |
| 140 | + it("should remove null values from an object", () => { |
| 141 | + const obj = { a: 1, b: null, c: 'test' }; |
| 142 | + expect(stripNullValues(obj)).toEqual({ a: 1, c: 'test' }); |
| 143 | + }); |
| 144 | + |
| 145 | + it("should handle nested objects", () => { |
| 146 | + const obj = { |
| 147 | + a: 1, |
| 148 | + b: { |
| 149 | + c: null, |
| 150 | + d: 'test', |
| 151 | + e: { |
| 152 | + f: null, |
| 153 | + g: 42 |
| 154 | + } |
| 155 | + } |
| 156 | + }; |
| 157 | + |
| 158 | + // Type assertion is needed to avoid TypeScript errors about missing properties |
| 159 | + const expected = { |
| 160 | + a: 1, |
| 161 | + b: { |
| 162 | + d: 'test', |
| 163 | + e: { |
| 164 | + g: 42 |
| 165 | + } |
| 166 | + } |
| 167 | + }; |
| 168 | + |
| 169 | + expect(stripNullValues(obj)).toEqual(expected as any); |
| 170 | + }); |
| 171 | + |
| 172 | + it("should handle arrays as values", () => { |
| 173 | + const obj = { a: [1, 2, 3], b: null }; |
| 174 | + expect(stripNullValues(obj)).toEqual({ a: [1, 2, 3] }); |
| 175 | + }); |
| 176 | + |
| 177 | + it("should return empty object when all values are null", () => { |
| 178 | + const obj = { a: null, b: null }; |
| 179 | + expect(stripNullValues(obj)).toEqual({}); |
| 180 | + }); |
| 181 | + |
| 182 | + it("should handle undefined values", () => { |
| 183 | + const obj = { a: undefined, b: null }; |
| 184 | + expect(stripNullValues(obj)).toEqual({ a: undefined }); |
| 185 | + }); |
| 186 | + |
| 187 | + it("should not modify the original object", () => { |
| 188 | + const obj = { a: 1, b: null }; |
| 189 | + const result = stripNullValues(obj); |
| 190 | + expect(obj).toEqual({ a: 1, b: null }); |
| 191 | + expect(result).toEqual({ a: 1 }); |
| 192 | + }); |
| 193 | + |
| 194 | + it("should work with deepCompare to treat null properties as non-existent", () => { |
| 195 | + const obj1 = { a: 1, b: null }; |
| 196 | + const obj2 = { a: 1 }; |
| 197 | + |
| 198 | + // Using stripNullValues should make these objects equal for comparison |
| 199 | + expect(deepCompare(stripNullValues(obj1), stripNullValues(obj2))).toBe(true); |
| 200 | + }); |
| 201 | +}); |
0 commit comments