diff --git a/apps/comments-ui/src/utils/helpers.js b/apps/comments-ui/src/utils/helpers.js index d35ed71bd5..b1827fe413 100644 --- a/apps/comments-ui/src/utils/helpers.js +++ b/apps/comments-ui/src/utils/helpers.js @@ -23,6 +23,10 @@ export function isSentryEventAllowed({event: sentryEvent}) { } export function formatNumber(number) { + if (number !== 0 && !number) { + return ''; + } + // Adds in commas for separators return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); } @@ -128,7 +132,7 @@ export function isCommentPublished(comment) { * Returns the y scroll position (top) of the main window of a given element that is in one or multiple stacked iframes */ export const getScrollToPosition = (element) => { - let yOffset = 0; + let yOffset = 0; // Because we are working in an iframe, we need to resolve the position inside this iframe to the position in the top window // Get the window of the element, not the window (which is the top window) @@ -170,7 +174,7 @@ export const scrollToElement = (element) => { // Trigger scrolling when yMin and yMax is closer than this to the border of the viewport const offset = 64; - + const viewportHeight = window.innerHeight; const viewPortYMin = window.scrollY; const viewPortYMax = viewPortYMin + viewportHeight; diff --git a/apps/comments-ui/src/utils/helpers.test.js b/apps/comments-ui/src/utils/helpers.test.js new file mode 100644 index 0000000000..5484a4e86d --- /dev/null +++ b/apps/comments-ui/src/utils/helpers.test.js @@ -0,0 +1,19 @@ +import * as helpers from './helpers'; + +describe('formatNumber', function () { + it('adds commas to large numbers', function () { + expect(helpers.formatNumber(1234567)).toEqual('1,234,567'); + }); + + it('handles 0', function () { + expect(helpers.formatNumber(0)).toEqual('0'); + }); + + it('handles undefined', function () { + expect(helpers.formatNumber()).toEqual(''); + }); + + it('handles null', function () { + expect(helpers.formatNumber(null)).toEqual(''); + }); +});