-
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.
refactor: use helper function for computing totals
- Loading branch information
Showing
3 changed files
with
111 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
98 changes: 98 additions & 0 deletions
98
src/modules/pivotTable/__tests__/processTotalValue.spec.js
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,98 @@ | ||
import { processTotalValue } from '../processTotalValue.js' | ||
|
||
const field = 'test' | ||
const tests = [ | ||
{ | ||
testName: 'negative value', | ||
value: -1, | ||
totalObj: {}, | ||
field, | ||
expected: -1, | ||
}, | ||
{ | ||
testName: 'zero value', | ||
value: 0, | ||
totalObj: {}, | ||
field, | ||
expected: 0, | ||
}, | ||
{ | ||
testName: 'positive value', | ||
value: 1, | ||
totalObj: {}, | ||
field, | ||
expected: 1, | ||
}, | ||
{ | ||
testName: 'null value', | ||
value: null, | ||
totalObj: {}, | ||
field, | ||
expected: undefined, | ||
}, | ||
{ | ||
testName: 'undefined value', | ||
value: undefined, | ||
totalObj: {}, | ||
field, | ||
expected: undefined, | ||
}, | ||
{ | ||
testName: 'string value', | ||
value: 'string', | ||
totalObj: {}, | ||
field, | ||
expected: undefined, | ||
}, | ||
{ | ||
testName: 'negative value with existing total', | ||
value: -1, | ||
totalObj: { [field]: 100 }, | ||
field, | ||
expected: 99, | ||
}, | ||
{ | ||
testName: 'zero value with existing total', | ||
value: 0, | ||
totalObj: { [field]: 100 }, | ||
field, | ||
expected: 100, | ||
}, | ||
{ | ||
testName: 'positive value with existing total', | ||
value: 1, | ||
totalObj: { [field]: 100 }, | ||
field, | ||
expected: 101, | ||
}, | ||
{ | ||
testName: 'null value with existing total', | ||
value: null, | ||
totalObj: { [field]: 100 }, | ||
field, | ||
expected: 100, | ||
}, | ||
{ | ||
testName: 'undefined value with existing total', | ||
value: undefined, | ||
totalObj: { [field]: 100 }, | ||
field, | ||
expected: 100, | ||
}, | ||
{ | ||
testName: 'string value with existing total', | ||
value: 'string', | ||
totalObj: { [field]: 100 }, | ||
field, | ||
expected: 100, | ||
}, | ||
] | ||
|
||
describe('processTotalValue', () => { | ||
tests.forEach((t) => { | ||
it(t.testName, () => { | ||
processTotalValue(t.value, t.totalObj, t.field) | ||
expect(t.totalObj[field]).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,5 @@ | ||
export const processTotalValue = (value, totalObj, field) => { | ||
if (typeof value === 'number' && Number.isFinite(value)) { | ||
totalObj[field] = (totalObj[field] ?? 0) + value | ||
} | ||
} |