An open source design system that's fully customizable and agent ready
TypeScript
// Copyright (c) Meta Platforms, Inc. and affiliates.
/**
* @file characters.test.ts
* @input character-counting utilities
* @output Tests for characterCount, firstCharacter, truncateCharacters
*/
import {describe, it, expect, vi, afterEach} from 'vitest';
import {characterCount, firstCharacter, truncateCharacters} from './characters';
// Multi-code-unit fixtures: an emoji surrogate pair (2 units), a flag
// sequence (4 units), a ZWJ family (11 units), and a combining mark (2 units).
const EMOJI = '\u{1F600}'; // π
const FLAG = '\u{1F1F9}\u{1F1F7}'; // πΉπ·
const FAMILY = '\u{1F468}β\u{1F469}β\u{1F467}β\u{1F466}'; // π¨βπ©βπ§βπ¦
// Γ© as base + combining mark, spelled as an escape so NFC-normalizing
// tools cannot silently precompose the fixture into single-code-unit Γ©.
const E_ACUTE = 'e\u0301';
describe('characterCount', () => {
it('returns 0 for the empty string', () => {
expect(characterCount('')).toBe(0);
});
it('counts ASCII one per character', () => {
expect(characterCount('hello')).toBe(5);
});
it('counts an emoji surrogate pair as one', () => {
expect(characterCount(EMOJI.repeat(2))).toBe(2);
expect(EMOJI.repeat(2).length).toBe(4); // sanity: code units differ
});
it('counts a flag sequence as one', () => {
expect(characterCount(FLAG)).toBe(1);
});
it('counts a ZWJ family emoji as one', () => {
expect(characterCount(FAMILY)).toBe(1);
expect(FAMILY.length).toBe(11); // sanity: code units differ
});
it('counts a combining-mark character as one', () => {
expect(characterCount(E_ACUTE)).toBe(1);
expect(E_ACUTE.length).toBe(2); // sanity: code units differ
});
it('counts mixed content by user-perceived characters', () => {
expect(characterCount(`a${EMOJI}b`)).toBe(3);
});
});
describe('firstCharacter', () => {
it('returns the empty string for the empty string', () => {
expect(firstCharacter('')).toBe('');
});
it('returns the first ASCII character', () => {
expect(firstCharacter('abc')).toBe('a');
});
it('returns a whole emoji, not half a surrogate pair', () => {
expect(firstCharacter(`${EMOJI}x`)).toBe(EMOJI);
});
it('returns a whole ZWJ family emoji', () => {
expect(firstCharacter(`${FAMILY}x`)).toBe(FAMILY);
});
it('keeps a combining mark attached to its base', () => {
expect(firstCharacter(`${E_ACUTE}cole`)).toBe(E_ACUTE);
});
});
describe('truncateCharacters', () => {
it('returns strings within max unchanged', () => {
expect(truncateCharacters('abc', 5)).toBe('abc');
});
it('returns strings exactly at max unchanged', () => {
expect(truncateCharacters('abcde', 5)).toBe('abcde');
});
it('truncates so the result, including the ellipsis, is max characters', () => {
expect(truncateCharacters('abcdefghij', 5)).toBe('abcdβ¦');
});
it('counts a multi-character ellipsis against max', () => {
// "..." is 3 characters, so 5 content characters remain.
expect(truncateCharacters('aaaaaaaaaa', 8, '...')).toBe('aaaaa...');
});
it('passes strings within max through under a multi-character ellipsis', () => {
expect(truncateCharacters('aaaaaaaa', 8, '...')).toBe('aaaaaaaa');
});
it('cuts between characters, never splitting an emoji', () => {
// The exact-string assertion also proves no surrogate pair was split.
expect(truncateCharacters(EMOJI.repeat(4), 3)).toBe(`${EMOJI.repeat(2)}β¦`);
});
it('treats a ZWJ family emoji as a single unit when cutting', () => {
expect(truncateCharacters(`${FAMILY}abc`, 2)).toBe(`${FAMILY}β¦`);
});
it('returns the empty string unchanged', () => {
expect(truncateCharacters('', 5)).toBe('');
});
it('degrades to just the ellipsis when max is smaller than the ellipsis', () => {
// Documented degenerate behavior: no consumer passes max below the
// ellipsis length, but the clamp must stay predictable.
expect(truncateCharacters('abcdef', 2, '...')).toBe('...');
expect(truncateCharacters('abcdef', 0)).toBe('β¦');
});
});
describe('code-point fallback (no Intl.Segmenter)', () => {
afterEach(() => {
vi.unstubAllGlobals();
vi.resetModules();
});
it('keeps surrogate pairs intact while counting by code points', async () => {
vi.resetModules();
// characters.ts only reads Intl.Segmenter, so a minimal stub suffices.
vi.stubGlobal('Intl', {Segmenter: undefined});
const fallback = await import('./characters');
expect(fallback.characterCount(EMOJI.repeat(2))).toBe(2);
expect(fallback.firstCharacter(`${EMOJI}x`)).toBe(EMOJI);
expect(fallback.firstCharacter('')).toBe('');
expect(fallback.truncateCharacters(EMOJI.repeat(4), 3)).toBe(
`${EMOJI.repeat(2)}β¦`,
);
// Documented degradation: a flag sequence splits into its two
// regional-indicator code points under the fallback.
expect(fallback.characterCount(FLAG)).toBe(2);
});
});