Skip to content

Commit

Permalink
[Lens] Fix bug in terms formatting (#82776) (#82843)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
Wylie Conlon and kibanamachine committed Nov 12, 2020
1 parent 55ff015 commit e316601
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ export interface TermsIndexPatternColumn extends FieldBasedIndexPatternColumn {
size: number;
orderBy: { type: 'alphabetical' } | { type: 'column'; columnId: string };
orderDirection: 'asc' | 'desc';
// Terms on numeric fields can be formatted
format?: {
id: string;
params?: {
decimals: number;
};
};
};
}

Expand Down Expand Up @@ -105,10 +112,16 @@ export const termsOperation: OperationDefinition<TermsIndexPatternColumn, 'field
};
},
onFieldChange: (oldColumn, indexPattern, field) => {
const newParams = { ...oldColumn.params };
if ('format' in newParams && field.type !== 'number') {
delete newParams.format;
}
return {
...oldColumn,
dataType: field.type as DataType,
label: ofName(field.displayName),
sourceField: field.name,
params: newParams,
};
},
onOtherColumnChanged: (currentColumn, columns) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,40 @@ describe('terms', () => {
},
};
const indexPattern = createMockedIndexPattern();
const newDateField = indexPattern.fields.find((i) => i.name === 'dest')!;
const newNumberField = indexPattern.fields.find((i) => i.name === 'bytes')!;

const column = termsOperation.onFieldChange(oldColumn, indexPattern, newDateField);
expect(column).toHaveProperty('sourceField', 'dest');
const column = termsOperation.onFieldChange(oldColumn, indexPattern, newNumberField);
expect(column).toHaveProperty('dataType', 'number');
expect(column).toHaveProperty('sourceField', 'bytes');
expect(column).toHaveProperty('params.size', 5);
expect(column).toHaveProperty('params.orderBy.type', 'alphabetical');
expect(column).toHaveProperty('params.orderDirection', 'asc');
expect(column.label).toContain('dest');
expect(column.label).toContain('bytes');
});

it('should remove numeric parameters when changing away from number', () => {
const oldColumn: TermsIndexPatternColumn = {
operationType: 'terms',
sourceField: 'bytes',
label: 'Top values of bytes',
isBucketed: true,
dataType: 'number',
params: {
size: 5,
orderBy: {
type: 'alphabetical',
},
orderDirection: 'asc',
format: { id: 'number', params: { decimals: 0 } },
},
};
const indexPattern = createMockedIndexPattern();
const newStringField = indexPattern.fields.find((i) => i.name === 'source')!;

const column = termsOperation.onFieldChange(oldColumn, indexPattern, newStringField);
expect(column).toHaveProperty('dataType', 'string');
expect(column).toHaveProperty('sourceField', 'source');
expect(column.params.format).toBeUndefined();
});
});

Expand Down

0 comments on commit e316601

Please sign in to comment.