mirror of
https://github.com/grafana/grafana.git
synced 2024-12-02 13:39:19 -06:00
TextFormatting/chore: update and add test coverage to the value formatting (#63975)
baldm0mma/chore/updateFormattingTests/ update and add tests to formatting module
This commit is contained in:
parent
682a186676
commit
9160f608b4
@ -1,41 +1,96 @@
|
||||
import { toHex, toHex0x } from './arithmeticFormatters';
|
||||
import { sci, toHex, toHex0x, toPercent, toPercentUnit } from './arithmeticFormatters';
|
||||
import { formattedValueToString } from './valueFormats';
|
||||
|
||||
describe('hex', () => {
|
||||
it('positive integer', () => {
|
||||
const str = toHex(100, 0);
|
||||
expect(formattedValueToString(str)).toBe('64');
|
||||
});
|
||||
it('negative integer', () => {
|
||||
const str = toHex(-100, 0);
|
||||
expect(formattedValueToString(str)).toBe('-64');
|
||||
});
|
||||
it('positive float', () => {
|
||||
const str = toHex(50.52, 1);
|
||||
expect(formattedValueToString(str)).toBe('32.8');
|
||||
});
|
||||
it('negative float', () => {
|
||||
const str = toHex(-50.333, 2);
|
||||
expect(formattedValueToString(str)).toBe('-32.547AE147AE14');
|
||||
describe('scientific formatting', () => {
|
||||
describe('sci', () => {
|
||||
it('follows the sad path as expected', () => {
|
||||
const str = sci(null, 0);
|
||||
expect(formattedValueToString(str)).toBe('');
|
||||
});
|
||||
it('renders the correct postive scientific notation as expected', () => {
|
||||
const str = sci(1000, 0);
|
||||
expect(formattedValueToString(str)).toBe('1e+3');
|
||||
});
|
||||
it('renders the correct nagative scientific notation as expected', () => {
|
||||
const str = sci(-1000, 0);
|
||||
expect(formattedValueToString(str)).toBe('-1e+3');
|
||||
});
|
||||
it('renders the correct decimal scientific notation as expected', () => {
|
||||
const str = sci(1000, 2);
|
||||
expect(formattedValueToString(str)).toBe('1.00e+3');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('hex 0x', () => {
|
||||
it('positive integer', () => {
|
||||
const str = toHex0x(7999, 0);
|
||||
expect(formattedValueToString(str)).toBe('0x1F3F');
|
||||
});
|
||||
it('negative integer', () => {
|
||||
const str = toHex0x(-584, 0);
|
||||
expect(formattedValueToString(str)).toBe('-0x248');
|
||||
describe('hexadecimal formatting', () => {
|
||||
describe('toHex', () => {
|
||||
it('follows the sad path as expected', () => {
|
||||
const str = toHex(null, 0);
|
||||
expect(formattedValueToString(str)).toBe('');
|
||||
});
|
||||
it('renders a positive integer', () => {
|
||||
const str = toHex(100, 0);
|
||||
expect(formattedValueToString(str)).toBe('64');
|
||||
});
|
||||
it('negative integer', () => {
|
||||
const str = toHex(-100, 0);
|
||||
expect(formattedValueToString(str)).toBe('-64');
|
||||
});
|
||||
it('positive float', () => {
|
||||
const str = toHex(50.52, 1);
|
||||
expect(formattedValueToString(str)).toBe('32.8');
|
||||
});
|
||||
it('negative float', () => {
|
||||
const str = toHex(-50.333, 2);
|
||||
expect(formattedValueToString(str)).toBe('-32.547AE147AE14');
|
||||
});
|
||||
});
|
||||
describe('toHex0x', () => {
|
||||
it('follows the sad path as expected', () => {
|
||||
const str = toHex0x(null, 0);
|
||||
expect(formattedValueToString(str)).toBe('');
|
||||
});
|
||||
it('positive integer', () => {
|
||||
const str = toHex0x(7999, 0);
|
||||
expect(formattedValueToString(str)).toBe('0x1F3F');
|
||||
});
|
||||
it('negative integer', () => {
|
||||
const str = toHex0x(-584, 0);
|
||||
expect(formattedValueToString(str)).toBe('-0x248');
|
||||
});
|
||||
|
||||
it('positive float', () => {
|
||||
const str = toHex0x(74.443, 3);
|
||||
expect(formattedValueToString(str)).toBe('0x4A.716872B020C4');
|
||||
});
|
||||
it('negative float', () => {
|
||||
const str = toHex0x(-65.458, 1);
|
||||
expect(formattedValueToString(str)).toBe('-0x41.8');
|
||||
it('positive float', () => {
|
||||
const str = toHex0x(74.443, 3);
|
||||
expect(formattedValueToString(str)).toBe('0x4A.716872B020C4');
|
||||
});
|
||||
it('negative float', () => {
|
||||
const str = toHex0x(-65.458, 1);
|
||||
expect(formattedValueToString(str)).toBe('-0x41.8');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('percentage formatting', () => {
|
||||
const size = 33.33333;
|
||||
const decimals = 2;
|
||||
describe('toPercent', () => {
|
||||
it('follows the sad path as expected', () => {
|
||||
const str = toPercent(null, decimals);
|
||||
expect(formattedValueToString(str)).toBe('');
|
||||
});
|
||||
it('renders a percent as expected', () => {
|
||||
const str = toPercent(size, 2);
|
||||
expect(formattedValueToString(str)).toBe('33.33%');
|
||||
});
|
||||
});
|
||||
describe('toPercentUnit', () => {
|
||||
it('follows the sad path as expected', () => {
|
||||
const str = toPercentUnit(null, decimals);
|
||||
expect(formattedValueToString(str)).toBe('');
|
||||
});
|
||||
it('renders a percent unit as expected', () => {
|
||||
const str = toPercentUnit(size, 2);
|
||||
expect(formattedValueToString(str)).toBe('3333.33%');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -2,21 +2,21 @@ import { DecimalCount } from '../types/displayValue';
|
||||
|
||||
import { toFixed, FormattedValue } from './valueFormats';
|
||||
|
||||
export function toPercent(size: number, decimals: DecimalCount): FormattedValue {
|
||||
export function toPercent(size: number | null, decimals: DecimalCount): FormattedValue {
|
||||
if (size === null) {
|
||||
return { text: '' };
|
||||
}
|
||||
return { text: toFixed(size, decimals), suffix: '%' };
|
||||
}
|
||||
|
||||
export function toPercentUnit(size: number, decimals: DecimalCount): FormattedValue {
|
||||
export function toPercentUnit(size: number | null, decimals: DecimalCount): FormattedValue {
|
||||
if (size === null) {
|
||||
return { text: '' };
|
||||
}
|
||||
return { text: toFixed(100 * size, decimals), suffix: '%' };
|
||||
}
|
||||
|
||||
export function toHex0x(value: number, decimals: DecimalCount): FormattedValue {
|
||||
export function toHex0x(value: number | null, decimals: DecimalCount): FormattedValue {
|
||||
if (value == null) {
|
||||
return { text: '' };
|
||||
}
|
||||
@ -29,7 +29,7 @@ export function toHex0x(value: number, decimals: DecimalCount): FormattedValue {
|
||||
return asHex;
|
||||
}
|
||||
|
||||
export function toHex(value: number, decimals: DecimalCount): FormattedValue {
|
||||
export function toHex(value: number | null, decimals: DecimalCount): FormattedValue {
|
||||
if (value == null) {
|
||||
return { text: '' };
|
||||
}
|
||||
@ -38,7 +38,7 @@ export function toHex(value: number, decimals: DecimalCount): FormattedValue {
|
||||
};
|
||||
}
|
||||
|
||||
export function sci(value: number, decimals: DecimalCount): FormattedValue {
|
||||
export function sci(value: number | null, decimals: DecimalCount): FormattedValue {
|
||||
if (value == null) {
|
||||
return { text: '' };
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user