From fcf85703930a948012079e30b238b24d08d9470b Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Mon, 7 Nov 2022 22:40:58 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20error=20rendering=20coun?= =?UTF-8?q?t=20with=20no=20number=20(#13)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refs https://github.com/TryGhost/Team/issues/2221 - added guard to `formatNumber` helper used in the `` component via the `` component so a missing count prop is handled gracefully --- apps/comments-ui/src/utils/helpers.js | 8 ++++++-- apps/comments-ui/src/utils/helpers.test.js | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 apps/comments-ui/src/utils/helpers.test.js 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(''); + }); +});