-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
(cherry picked from commit d93bc3e)
- Loading branch information
Showing
3 changed files
with
117 additions
and
27 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
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,84 @@ | ||
import { addToTotalIfNumber } from '../addToTotalIfNumber.js' | ||
|
||
const tests = [ | ||
{ | ||
testName: 'negative value', | ||
value: -1, | ||
total: undefined, | ||
expected: -1, | ||
}, | ||
{ | ||
testName: 'zero value', | ||
value: 0, | ||
total: undefined, | ||
expected: 0, | ||
}, | ||
{ | ||
testName: 'positive value', | ||
value: 1, | ||
total: undefined, | ||
expected: 1, | ||
}, | ||
{ | ||
testName: 'null value', | ||
value: null, | ||
total: undefined, | ||
expected: undefined, | ||
}, | ||
{ | ||
testName: 'undefined value', | ||
value: undefined, | ||
total: undefined, | ||
expected: undefined, | ||
}, | ||
{ | ||
testName: 'string value', | ||
value: 'string', | ||
total: undefined, | ||
expected: undefined, | ||
}, | ||
{ | ||
testName: 'negative value with existing total', | ||
value: -1, | ||
total: 100, | ||
expected: 99, | ||
}, | ||
{ | ||
testName: 'zero value with existing total', | ||
value: 0, | ||
total: 100, | ||
expected: 100, | ||
}, | ||
{ | ||
testName: 'positive value with existing total', | ||
value: 1, | ||
total: 100, | ||
expected: 101, | ||
}, | ||
{ | ||
testName: 'null value with existing total', | ||
value: null, | ||
total: 100, | ||
expected: 100, | ||
}, | ||
{ | ||
testName: 'undefined value with existing total', | ||
value: undefined, | ||
total: 100, | ||
expected: 100, | ||
}, | ||
{ | ||
testName: 'string value with existing total', | ||
value: 'string', | ||
total: 100, | ||
expected: 100, | ||
}, | ||
] | ||
|
||
describe('addToTotalIfNumber', () => { | ||
tests.forEach((t) => { | ||
it(t.testName, () => { | ||
expect(addToTotalIfNumber(t.value, t.total)).toEqual(t.expected) | ||
}) | ||
}) | ||
}) |
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,4 @@ | ||
export const addToTotalIfNumber = (value, total) => | ||
typeof value === 'number' && Number.isFinite(value) | ||
? (total ?? 0) + value | ||
: total |