dify/web/utils/emoji.spec.ts

79 lines
2.2 KiB
TypeScript
Raw Permalink Normal View History

import type { Mock } from 'vitest'
2025-11-04 13:06:44 +00:00
import { SearchIndex } from 'emoji-mart'
import { searchEmoji } from './emoji'
2025-11-04 13:06:44 +00:00
vi.mock('emoji-mart', () => ({
2025-11-04 13:06:44 +00:00
SearchIndex: {
search: vi.fn(),
2025-11-04 13:06:44 +00:00
},
}))
describe('Emoji Utilities', () => {
describe('searchEmoji', () => {
beforeEach(() => {
vi.clearAllMocks()
2025-11-04 13:06:44 +00:00
})
it('should return emoji natives for search results', async () => {
const mockEmojis = [
{ skins: [{ native: '😀' }] },
{ skins: [{ native: '😃' }] },
{ skins: [{ native: '😄' }] },
]
;(SearchIndex.search as Mock).mockResolvedValue(mockEmojis)
2025-11-04 13:06:44 +00:00
const result = await searchEmoji('smile')
expect(result).toEqual(['😀', '😃', '😄'])
})
it('should return empty array when no results', async () => {
;(SearchIndex.search as Mock).mockResolvedValue([])
2025-11-04 13:06:44 +00:00
const result = await searchEmoji('nonexistent')
expect(result).toEqual([])
})
it('should return empty array when search returns null', async () => {
;(SearchIndex.search as Mock).mockResolvedValue(null)
2025-11-04 13:06:44 +00:00
const result = await searchEmoji('test')
expect(result).toEqual([])
})
it('should handle search with empty string', async () => {
;(SearchIndex.search as Mock).mockResolvedValue([])
2025-11-04 13:06:44 +00:00
const result = await searchEmoji('')
expect(result).toEqual([])
expect(SearchIndex.search).toHaveBeenCalledWith('')
})
it('should extract native from first skin', async () => {
const mockEmojis = [
{
skins: [
{ native: '👍' },
{ native: '👍🏻' },
{ native: '👍🏼' },
],
},
]
;(SearchIndex.search as Mock).mockResolvedValue(mockEmojis)
2025-11-04 13:06:44 +00:00
const result = await searchEmoji('thumbs')
expect(result).toEqual(['👍'])
})
it('should handle multiple search terms', async () => {
const mockEmojis = [
{ skins: [{ native: '❤️' }] },
{ skins: [{ native: '💙' }] },
]
;(SearchIndex.search as Mock).mockResolvedValue(mockEmojis)
2025-11-04 13:06:44 +00:00
const result = await searchEmoji('heart love')
expect(result).toEqual(['❤️', '💙'])
})
})
})