openpencil/tests/engine/render/canvas/component-label-text.test.ts

27 lines
836 B
TypeScript
Raw Normal View History

import { describe, expect, test } from 'bun:test'
2026-05-18 00:07:46 +00:00
import { ellipsizeLabelText } from '#core/canvas/labels/text'
const fixedWidthFont = {
getGlyphIDs(text: string) {
return [...text].map((_, index) => index)
},
getGlyphWidths(glyphs: number[]) {
return glyphs.map(() => 10)
}
2026-05-18 00:07:46 +00:00
} as Parameters<typeof ellipsizeLabelText>[0]
2026-05-18 00:07:46 +00:00
describe('label text', () => {
test('keeps labels that fit inside the component width', () => {
2026-05-18 00:07:46 +00:00
expect(ellipsizeLabelText(fixedWidthFont, 'Button', 60)).toBe('Button')
})
test('ellipsizes labels that exceed the component width', () => {
2026-05-18 00:07:46 +00:00
expect(ellipsizeLabelText(fixedWidthFont, 'Very long component', 55)).toBe('Very…')
})
test('returns only ellipsis when only the ellipsis fits', () => {
2026-05-18 00:07:46 +00:00
expect(ellipsizeLabelText(fixedWidthFont, 'Component', 10)).toBe('…')
})
})