[Lens] Fix multi-value formatting for metric (#187982)

## Summary

Fixes #187968

It avoids the default JSON stringify behaviour around formatted values
in multi-values scenarios for the new Metric chart type


<img width="1504" alt="Screenshot 2024-07-10 at 15 28 23"
src="c52c5c5c-3004-4a3f-bdbf-612fdedd853a">

Added also a couple of unit tests to spot regressions in the area.

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
This commit is contained in:
Marco Liberati 2024-07-11 10:20:17 +02:00 committed by GitHub
parent b11e9eeb6d
commit 61de0b022b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 59 additions and 3 deletions

View file

@ -405,6 +405,61 @@ describe('MetricVisComponent', function () {
expect(tileConfig.trend).toEqual(trends[DEFAULT_TRENDLINE_NAME]);
expect(tileConfig.trendShape).toEqual('area');
});
it('should display multi-values non-numeric values formatted and without quotes', () => {
const newTable: Datatable = {
...table,
// change the format id for the columns
columns: table.columns.map((column) =>
[basePriceColumnId, minPriceColumnId].includes(column.id)
? {
...column,
meta: { ...column.meta, params: { id: 'text' } },
}
: column
),
rows: table.rows.map((row) => ({
...row,
[basePriceColumnId]: [String(row[basePriceColumnId]), String(100)],
[minPriceColumnId]: [String(row[minPriceColumnId]), String(10)],
})),
};
const component = shallow(<MetricVis config={config} data={newTable} {...defaultProps} />);
const [[visConfig]] = component.find(Metric).props().data!;
expect(visConfig!.value).toMatchInlineSnapshot(
`
Array [
"text-28.984375",
"text-100",
]
`
);
});
it('should display multi-values numeric values formatted and without quotes', () => {
const newTable = {
...table,
rows: table.rows.map((row) => ({
...row,
[basePriceColumnId]: [row[basePriceColumnId], 100],
[minPriceColumnId]: [row[minPriceColumnId], 10],
})),
};
const component = shallow(<MetricVis config={config} data={newTable} {...defaultProps} />);
const [[visConfig]] = component.find(Metric).props().data!;
expect(visConfig!.value).toMatchInlineSnapshot(
`
Array [
"number-28.984375",
"number-100",
]
`
);
});
});
describe('metric grid', () => {

View file

@ -208,15 +208,16 @@ export const MetricVis = ({
const subtitle = breakdownByColumn ? primaryMetricColumn.name : config.metric.subtitle;
if (typeof value !== 'number') {
const nonNumericMetric: MetricWText = {
value: formatPrimaryMetric(value),
const nonNumericMetricBase: Omit<MetricWText, 'value'> = {
title: String(title),
subtitle,
icon: config.metric?.icon ? getIcon(config.metric?.icon) : undefined,
extra: renderSecondaryMetric(data.columns, row, config),
color: config.metric.color ?? defaultColor,
};
return nonNumericMetric;
return Array.isArray(value)
? { ...nonNumericMetricBase, value: value.map((v) => formatPrimaryMetric(v)) }
: { ...nonNumericMetricBase, value: formatPrimaryMetric(value) };
}
const baseMetric: MetricWNumber = {