-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add sumObjectValues helper and test
- Loading branch information
1 parent
dbd09ba
commit 245848a
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { sumObjectValues } from '../helpers.js' | ||
|
||
describe('sumObjectValues', () => { | ||
it('should return 0 for an empty object', () => { | ||
expect(sumObjectValues({})).toBe(0) | ||
}) | ||
|
||
it('should sum flat numeric values in an object', () => { | ||
expect(sumObjectValues({ a: 1, b: 2, c: 3 })).toBe(6) | ||
}) | ||
|
||
it('should handle nested objects correctly', () => { | ||
const obj = { | ||
a: 1, | ||
b: { c: 2, d: { e: 3 } }, | ||
} | ||
expect(sumObjectValues(obj)).toBe(6) | ||
}) | ||
|
||
it('should ignore non-numeric values', () => { | ||
const obj = { | ||
a: 1, | ||
b: 'string', | ||
c: { d: 2, e: null, f: true }, | ||
} | ||
expect(sumObjectValues(obj)).toBe(3) | ||
}) | ||
|
||
it('should handle an object with only non-numeric values', () => { | ||
const obj = { | ||
a: 'string', | ||
b: null, | ||
c: undefined, | ||
} | ||
expect(sumObjectValues(obj)).toBe(0) | ||
}) | ||
|
||
it('should handle arrays as object values', () => { | ||
const obj = { | ||
a: [1, 2, 3], | ||
b: { c: [4, 5], d: 6 }, | ||
} | ||
expect(sumObjectValues(obj)).toBe(21) // Note: Array elements aren't handled differently, treated as objects. | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters