🐛 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) { export function formatNumber(number) {
if (number !== 0 && !number) {
return '';
}
// Adds in commas for separators // Adds in commas for separators
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
} }

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