🐛 Fixed error rendering count with no number (#13)

refs https://github.com/TryGhost/Team/issues/2221

- added guard to `formatNumber` helper used in the `<Count>` component via the `<ContentTitle>` component so a missing count prop is handled gracefully
This commit is contained in:
Kevin Ansfield 2022-11-07 22:40:58 +00:00 committed by GitHub
parent cb6bf40f56
commit fcf8570393
2 changed files with 25 additions and 2 deletions

View File

@ -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;

View File

@ -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('');
});
});