forked from elastic/elastic-charts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_utils.test.ts
More file actions
56 lines (48 loc) · 1.58 KB
/
text_utils.test.ts
File metadata and controls
56 lines (48 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import type { Font } from './text_utils';
import { fitText } from './text_utils';
import type { TextMeasure } from '../utils/bbox/canvas_text_bbox_calculator';
const monospaceMeasure: TextMeasure = (text) => ({
width: text.length,
height: 12,
});
const font: Font = {
fontStyle: 'normal',
fontVariant: 'normal',
fontWeight: 400,
fontFamily: 'sans-serif',
textColor: '#000',
};
const fontSize = 12;
describe('fitText', () => {
it('returns the full string when it already fits (end)', () => {
expect(fitText(monospaceMeasure, 'abc', 10, fontSize, font, 'end')).toEqual({
width: 3,
text: 'abc',
});
});
it('truncates at the end with an ellipsis when width is constrained', () => {
expect(fitText(monospaceMeasure, 'abcdef', 4, fontSize, font, 'end')).toEqual({
width: 4,
text: 'abc…',
});
});
it('truncates at the start when position is start', () => {
expect(fitText(monospaceMeasure, 'abcdef', 4, fontSize, font, 'start')).toEqual({
width: 4,
text: '…def',
});
});
it('truncates in the middle when position is middle', () => {
expect(fitText(monospaceMeasure, 'abcdef', 4, fontSize, font, 'middle')).toEqual({
width: 4,
text: 'ab…f',
});
});
});