Skip to content

Commit

Permalink
[Lens] Improve column type detection in table for alignment (#120007)
Browse files Browse the repository at this point in the history
* 🐛 Fix last value alignment behaviour

* ✅ Add unit test
  • Loading branch information
dej611 authored Dec 2, 2021
1 parent 3232689 commit 976253a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,52 @@ describe('DatatableComponent', () => {
});
});

test('it detect last_value filtered metric type', () => {
const { data, args } = sampleArgs();

const table = data.tables.l1;
const column = table.columns[1];

column.meta = {
...column.meta,
field: undefined,
type: 'number',
sourceParams: { ...column.meta.sourceParams, type: 'filtered_metric' },
};
table.rows[0].b = 'Hello';

const wrapper = shallow(
<DatatableComponent
data={data}
args={{
...args,
columns: [
{ columnId: 'a', alignment: 'center', type: 'lens_datatable_column' },
{ columnId: 'b', type: 'lens_datatable_column' },
{ columnId: 'c', type: 'lens_datatable_column' },
],
sortingColumnId: 'b',
sortingDirection: 'desc',
}}
formatFactory={() => ({ convert: (x) => x } as IFieldFormat)}
dispatchEvent={onDispatchEvent}
getType={jest.fn()}
renderMode="view"
paletteService={chartPluginMock.createPaletteRegistry()}
uiSettings={{ get: jest.fn() } as unknown as IUiSettingsClient}
/>
);

expect(wrapper.find(DataContext.Provider).prop('value').alignments).toEqual({
// set via args
a: 'center',
// default for string
b: 'left',
// default for number
c: 'right',
});
});

test('it should refresh the table header when the datatable data changes', () => {
const { data, args } = sampleArgs();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,14 @@ export const DatatableComponent = (props: DatatableRenderProps) => {
const isNumericMap: Record<string, boolean> = useMemo(() => {
const numericMap: Record<string, boolean> = {};
for (const column of firstLocalTable.columns) {
numericMap[column.id] = column.meta.type === 'number';
// filtered metrics result as "number" type, but have no field
numericMap[column.id] =
(column.meta.type === 'number' && column.meta.field != null) ||
// as fallback check the first available value type
// mind here: date can be seen as numbers, to carefully check that is a filtered metric
(column.meta.field == null &&
typeof firstLocalTable.rows.find((row) => row[column.id] != null)?.[column.id] ===
'number');
}
return numericMap;
}, [firstLocalTable]);
Expand Down

0 comments on commit 976253a

Please sign in to comment.