diff --git a/src/libs/DistanceRequestUtils.js b/src/libs/DistanceRequestUtils.js index 7744ee3d8bb1..9b875fb82004 100644 --- a/src/libs/DistanceRequestUtils.js +++ b/src/libs/DistanceRequestUtils.js @@ -102,12 +102,12 @@ const getDistanceMerchant = (hasRoute, distanceInMeters, unit, rate, currency, t * @param {Number} distance - The distance traveled in meters * @param {'mi' | 'km'} unit - The unit of measurement for the distance * @param {Number} rate - Rate used for calculating the request amount - * @returns {Number} The computed request amount. + * @returns {Number} The computed request amount (rounded) in "cents". */ const getDistanceRequestAmount = (distance, unit, rate) => { const convertedDistance = convertDistanceUnit(distance, unit); const roundedDistance = convertedDistance.toFixed(2); - return roundedDistance * rate; + return Math.round(roundedDistance * rate); }; export default {getDefaultMileageRate, getDistanceMerchant, getDistanceRequestAmount}; diff --git a/tests/unit/DistanceRequestUtilsTest.js b/tests/unit/DistanceRequestUtilsTest.js new file mode 100644 index 000000000000..4b657841f2be --- /dev/null +++ b/tests/unit/DistanceRequestUtilsTest.js @@ -0,0 +1,12 @@ +import DistanceRequestUtils from '../../src/libs/DistanceRequestUtils'; + +describe('DistanceRequestUtils', () => { + describe('getDistanceRequestAmount', () => { + test.each([ + [350, 8605.146, 'mi', 65.5], + [561, 8605.146, 'km', 65.1], + ])('Correctly calculates amount %s for %s%s at a rate of %s per unit', (expectedResult, distance, unit, rate) => { + expect(DistanceRequestUtils.getDistanceRequestAmount(distance, unit, rate)).toBe(expectedResult); + }); + }); +});