Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

4.8 KB
Raw Blame
/**
 * Copyright 2023-present DreamNum Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { describe, expect, it } from 'vitest';
import {
    applyEmojiSkinTone,
    getDefaultRecentEmojis,
    getRandomEmoji,
    hasMixedSkinToneVariants,
    parseStoredEmojiSkinTone,
    parseStoredRecentEmojis,
    promoteRecentEmoji,
    searchEmojis,
} from '../emoji-picker-utils';
import { emojis } from '../emojis.generated';

describe('emoji picker utils', () => {
    it('falls back to frequent emojis when recents are empty or invalid', () => {
        expect(parseStoredRecentEmojis(null)).toEqual(getDefaultRecentEmojis());
        expect(parseStoredRecentEmojis('broken')).toEqual(getDefaultRecentEmojis());
    });

    it('accepts recent emojis already decoded by local storage service', () => {
        expect(parseStoredRecentEmojis([{ emoji: 'πŸ’‘', title: 'Light Bulb' }])).toEqual([{ emoji: 'πŸ’‘', title: 'Light Bulb' }]);
    });

    it('promotes the selected emoji to the front and removes duplicates', () => {
        const result = promoteRecentEmoji([
            { emoji: 'πŸ˜€', title: 'Grinning Face' },
            { emoji: 'πŸ’‘', title: 'Light Bulb' },
        ], { emoji: 'πŸ’‘', title: 'Light Bulb' });

        expect(result.map((item) => item.emoji)).toEqual(['πŸ’‘', 'πŸ˜€']);
    });

    it('groups skin tones into one generated emoji family', () => {
        const wavingHand = emojis.people.find((item) => item.emoji === 'πŸ‘‹');

        expect(wavingHand?.skinToneVariants?.map((item) => item.emoji)).toEqual([
            'πŸ‘‹πŸ»',
            'πŸ‘‹πŸΌ',
            'πŸ‘‹πŸ½',
            'πŸ‘‹πŸΎ',
            'πŸ‘‹πŸΏ',
        ]);
        expect(emojis.people.some((item) => item.emoji === 'πŸ‘‹πŸ½')).toBe(false);
    });

    it('applies one preferred skin tone and falls back for unsupported emoji', () => {
        const wavingHand = emojis.people.find((item) => item.emoji === 'πŸ‘‹')!;
        const lightBulb = emojis.objects.find((item) => item.emoji === 'πŸ’‘')!;

        expect(applyEmojiSkinTone(wavingHand, '🏽').emoji).toBe('πŸ‘‹πŸ½');
        expect(applyEmojiSkinTone(lightBulb, '🏽').emoji).toBe('πŸ’‘');
        expect(applyEmojiSkinTone(wavingHand, '').emoji).toBe('πŸ‘‹');
    });

    it('deduplicates recent skin tone variants by family', () => {
        const result = promoteRecentEmoji([
            { emoji: 'πŸ‘‹πŸ»', title: 'Waving Hand: Light Skin Tone' },
            { emoji: 'πŸ’‘', title: 'Light Bulb' },
        ], { emoji: 'πŸ‘‹πŸΏ', title: 'Waving Hand: Dark Skin Tone' });

        expect(result.map((item) => item.emoji)).toEqual(['πŸ‘‹πŸΏ', 'πŸ’‘']);
    });

    it('keeps mixed skin tone combinations inside their family', () => {
        const handshake = emojis.people.find((item) => item.emoji === '🀝')!;

        expect(hasMixedSkinToneVariants(handshake)).toBe(true);
        expect(handshake.skinToneVariants).toHaveLength(25);
    });

    it('randomizes families before applying the preferred skin tone', () => {
        const families = Object.entries(emojis)
            .filter(([category]) => category !== 'frequent')
            .flatMap(([, items]) => items);
        const wavingHandIndex = families.findIndex((item) => item.emoji === 'πŸ‘‹');
        const random = () => (wavingHandIndex + 0.1) / families.length;

        expect(getRandomEmoji(random, '🏽').emoji).toBe('πŸ‘‹πŸ½');
    });

    it('validates stored skin tone preferences', () => {
        expect(parseStoredEmojiSkinTone('🏾')).toBe('🏾');
        expect(parseStoredEmojiSkinTone('invalid')).toBe('');
        expect(parseStoredEmojiSkinTone(null)).toBe('');
    });

    it('searches emoji title case-insensitively', () => {
        const result = searchEmojis('light bulb');

        expect(result.some((item) => item.emoji === 'πŸ’‘')).toBe(true);
    });

    it('searches localized titles, aliases and skin tone names with the compact index', async () => {
        const { default: zhCNEmojiLocale } = await import('../../../locale/emoji-locale/zh-CN.generated');

        for (const query of ['灯泑', '主意', ' LIGHT BULB ']) {
            expect(searchEmojis(query, zhCNEmojiLocale).some((item) => item.emoji === 'πŸ’‘')).toBe(true);
        }
        expect(searchEmojis('ζŒ₯手: 较桅肀色', zhCNEmojiLocale).map((item) => item.emoji)).toEqual(['πŸ‘‹']);
        expect(searchEmojis('   ', zhCNEmojiLocale)).toEqual([]);
    });
});