Files
dify/web/utils/__tests__/emoji.spec.ts
CodingOnStar d66b0d2d11 refactor(tests): reorganize test files and enhance test coverage for utility functions
- Moved test files for completion parameters, clipboard, app redirection, and emoji utilities to the appropriate directory structure.
- Added comprehensive tests for clipboard functionality, including modern and legacy methods.
- Implemented tests for app redirection logic based on user permissions and app modes.
- Enhanced tests for completion parameters validation and error handling.
- Introduced tests for emoji search functionality with various scenarios.
- Updated icon utility tests to cover edge cases and security concerns.
- Improved formatting tests for numbers, file sizes, and time representation.
2026-03-26 09:42:09 +08:00

79 lines
2.2 KiB
TypeScript

import type { Mock } from 'vitest'
import { SearchIndex } from 'emoji-mart'
import { searchEmoji } from '../emoji'
vi.mock('emoji-mart', () => ({
SearchIndex: {
search: vi.fn(),
},
}))
describe('Emoji Utilities', () => {
describe('searchEmoji', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('should return emoji natives for search results', async () => {
const mockEmojis = [
{ skins: [{ native: '😀' }] },
{ skins: [{ native: '😃' }] },
{ skins: [{ native: '😄' }] },
]
;(SearchIndex.search as Mock).mockResolvedValue(mockEmojis)
const result = await searchEmoji('smile')
expect(result).toEqual(['😀', '😃', '😄'])
})
it('should return empty array when no results', async () => {
;(SearchIndex.search as Mock).mockResolvedValue([])
const result = await searchEmoji('nonexistent')
expect(result).toEqual([])
})
it('should return empty array when search returns null', async () => {
;(SearchIndex.search as Mock).mockResolvedValue(null)
const result = await searchEmoji('test')
expect(result).toEqual([])
})
it('should handle search with empty string', async () => {
;(SearchIndex.search as Mock).mockResolvedValue([])
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)
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)
const result = await searchEmoji('heart love')
expect(result).toEqual(['❤️', '💙'])
})
})
})