|
| 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 | +}); |
0 commit comments