Skip to content

Commit edbfcf1

Browse files
committed
test: add tests for withAxisTickLabelTruncation
1 parent f22ab83 commit edbfcf1

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
import { withTickLabelTruncation } from './axis_tick_formatter';
10+
import * as textUtils from '../../../../common/text_utils';
11+
import { LIGHT_THEME } from '../../../../utils/themes/light_theme';
12+
13+
const {
14+
axes: { tickLabel },
15+
} = LIGHT_THEME;
16+
17+
describe('withTickLabelTruncation', () => {
18+
const measure = jest.fn((text: string) => ({ width: text.length, height: tickLabel.fontSize }));
19+
const fitTextSpy = jest.spyOn(textUtils, 'fitText').mockReturnValue({ width: 0, text: 'tickLabel' });
20+
21+
it('does not call fitText when maxLength is undefined', () => {
22+
withTickLabelTruncation(measure, tickLabel, 200)((v: number) => `${v}`);
23+
expect(fitTextSpy).not.toHaveBeenCalled();
24+
});
25+
26+
it("calls fitText with half the container width when maxLength is '50%'", () => {
27+
const containerWidth = 200;
28+
const format = withTickLabelTruncation(measure, { ...tickLabel, maxLength: '50%' }, containerWidth)((v) => `${v}`);
29+
format('tickLabel');
30+
expect(fitTextSpy).toHaveBeenCalledWith(
31+
measure,
32+
'tickLabel',
33+
containerWidth / 2,
34+
tickLabel.fontSize,
35+
expect.any(Object),
36+
tickLabel.truncate ?? 'end',
37+
);
38+
});
39+
40+
it('calls fitText with the numeric maxLength as maximum width', () => {
41+
const maxLengthPx = 72;
42+
const format = withTickLabelTruncation(measure, { ...tickLabel, maxLength: maxLengthPx }, 400)((v) => `${v}`);
43+
format('tickLabel');
44+
45+
expect(fitTextSpy).toHaveBeenCalledWith(
46+
measure,
47+
'tickLabel',
48+
maxLengthPx,
49+
tickLabel.fontSize,
50+
expect.any(Object),
51+
tickLabel.truncate ?? 'end',
52+
);
53+
});
54+
});

packages/charts/src/chart_types/xy_chart/state/selectors/axis_tick_formatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function withTickLabelTruncation(
3434
): <V>(formatter: AxisLabelFormatter<V>) => AxisLabelFormatter<V> {
3535
const { fontSize, fontStyle, fontFamily, fill, truncate, maxLength } = tickLabel;
3636

37-
const maxWidth = maxLength ? getPercentageValue(maxLength, containerWidth, NaN) : undefined;
37+
const maxWidth = maxLength ? getPercentageValue(maxLength, containerWidth, 0) : undefined;
3838
if (maxWidth === undefined || maxWidth <= 0 || maxWidth > containerWidth) return (formatter) => formatter;
3939

4040
const font: Font = {

0 commit comments

Comments
 (0)