Unverified Commit 49f0996c authored by Allison Suarez Miranda's avatar Allison Suarez Miranda Committed by GitHub

fix: accept non int stats values (#748)

* added logic to surface any string
Signed-off-by: 's avatarAllison Suarez Miranda <asuarezmiranda@lyft.com>

* lint fix and adde dunit tests
Signed-off-by: 's avatarAllison Suarez Miranda <asuarezmiranda@lyft.com>

* lint
Signed-off-by: 's avatarAllison Suarez Miranda <asuarezmiranda@lyft.com>

* typed and always return something
Signed-off-by: 's avatarAllison Suarez Miranda <asuarezmiranda@lyft.com>

* added tests for component
Signed-off-by: 's avatarAllison Suarez Miranda <asuarezmiranda@lyft.com>
parent 54123892
...@@ -114,5 +114,27 @@ describe('ColumnStats', () => { ...@@ -114,5 +114,27 @@ describe('ColumnStats', () => {
expect(actual).toEqual(expected); expect(actual).toEqual(expected);
}); });
}); });
describe('when different stat values are passed', () => {
const { stats } = dataBuilder.withNonNumericStats().build();
it('displays formatted number', () => {
const { wrapper } = setup({ stats });
const actual = wrapper.find('.stat-value').first().text();
const expected = '12,345';
expect(actual).toEqual(expected);
});
it('displays date string', () => {
const { wrapper } = setup({ stats });
const actual = wrapper.find('.stat-value').last().text();
const expected = '2020-11-03';
expect(actual).toEqual(expected);
});
});
}); });
}); });
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
import * as React from 'react'; import * as React from 'react';
import { TableColumnStats } from 'interfaces/index'; import { TableColumnStats } from 'interfaces/index';
import { formatNumber } from 'utils/numberUtils'; import { formatNumber, isNumber } from 'utils/numberUtils';
import { getStatsInfoText } from '../utils'; import { getStatsInfoText } from '../utils';
import { COLUMN_STATS_TITLE } from '../constants'; import { COLUMN_STATS_TITLE } from '../constants';
...@@ -24,12 +24,12 @@ const ColumnStatRow: React.FC<ColumnStatRowProps> = ({ ...@@ -24,12 +24,12 @@ const ColumnStatRow: React.FC<ColumnStatRowProps> = ({
stat_type, stat_type,
stat_val, stat_val,
}: ColumnStatRowProps) => { }: ColumnStatRowProps) => {
const numberVal = +stat_val;
return ( return (
<div className="column-stat-row"> <div className="column-stat-row">
<div className="stat-name body-3">{stat_type.toUpperCase()}</div> <div className="stat-name body-3">{stat_type.toUpperCase()}</div>
<div className="stat-value">{formatNumber(numberVal)}</div> <div className="stat-value">
{isNumber(stat_val) ? formatNumber(+stat_val) : stat_val}
</div>
</div> </div>
); );
}; };
......
...@@ -135,6 +135,26 @@ function TestDataBuilder(config = {}) { ...@@ -135,6 +135,26 @@ function TestDataBuilder(config = {}) {
return new this.Klass(attr); return new this.Klass(attr);
}; };
this.withNonNumericStats = () => {
const attr = {
stats: [
{
end_epoch: 1571616000,
start_epoch: 1571616000,
stat_type: 'count',
stat_val: '12345',
},
{
end_epoch: 1571616000,
start_epoch: 1571616000,
stat_type: 'date',
stat_val: '2020-11-03',
},
],
};
return new this.Klass(attr);
};
this.withEmptyStats = () => { this.withEmptyStats = () => {
const attr = { stats: [] }; const attr = { stats: [] };
......
import { ResourceType } from 'interfaces/Resources'; import { ResourceType } from 'interfaces/Resources';
import { assert } from 'console';
import { access } from 'fs';
import { sys } from 'typescript';
import * as DateUtils from './dateUtils'; import * as DateUtils from './dateUtils';
import * as LogUtils from './logUtils'; import * as LogUtils from './logUtils';
import * as NavigationUtils from './navigationUtils'; import * as NavigationUtils from './navigationUtils';
import * as TextUtils from './textUtils'; import * as TextUtils from './textUtils';
import * as NumberUtils from './numberUtils';
describe('textUtils', () => { describe('textUtils', () => {
describe('convertText', () => { describe('convertText', () => {
...@@ -48,6 +52,41 @@ describe('textUtils', () => { ...@@ -48,6 +52,41 @@ describe('textUtils', () => {
}); });
}); });
describe('numberUtils', () => {
describe('isNumber', () => {
it('returns true if string is number', () => {
const actual = NumberUtils.isNumber('1234');
const expected = true;
expect(actual).toEqual(expected);
});
it('returns false if string is not number', () => {
const actualString = NumberUtils.isNumber('abcd');
const actualDate = NumberUtils.isNumber('2020-11-03');
const actualAlphaNum = NumberUtils.isNumber('1a2b3c');
const expected = false;
expect(actualString).toEqual(expected);
expect(actualDate).toEqual(expected);
expect(actualAlphaNum).toEqual(expected);
});
});
describe('formatNumber', () => {
it('returns formatted number', () => {
const actual = NumberUtils.formatNumber('1998');
const expected = '1,998';
expect(actual).toEqual(expected);
});
it('get NaN on non numbers', () => {
const actual = NumberUtils.formatNumber('2020-11-03');
const expected = 'NaN';
expect(actual).toEqual(expected);
});
});
});
describe('navigationUtils', () => { describe('navigationUtils', () => {
describe('updateSearchUrl', () => { describe('updateSearchUrl', () => {
let mockUrl; let mockUrl;
......
...@@ -12,3 +12,7 @@ export function formatNumber(value) { ...@@ -12,3 +12,7 @@ export function formatNumber(value) {
} }
return Intl.NumberFormat().format(value); return Intl.NumberFormat().format(value);
} }
export function isNumber(value): boolean {
return !Number.isNaN(Number(value));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment