test: add 20 unit tests for markdown utils (mentions, emoji-only, formatTime, formatFileSize)
This commit is contained in:
111
src/lib/components/message/utils/markdown.test.ts
Normal file
111
src/lib/components/message/utils/markdown.test.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { renderMentions, isEmojiOnly, formatTime, formatFileSize } from './markdown';
|
||||
|
||||
describe('markdown utils', () => {
|
||||
describe('renderMentions', () => {
|
||||
it('renders @user:server.com as a mention button', () => {
|
||||
const result = renderMentions('Hello @alice:matrix.org');
|
||||
expect(result).toContain('class="mention-ping"');
|
||||
expect(result).toContain('data-user-id="@alice:matrix.org"');
|
||||
expect(result).toContain('@alice</button>');
|
||||
});
|
||||
|
||||
it('renders @everyone as a special mention', () => {
|
||||
const result = renderMentions('Hey @everyone');
|
||||
expect(result).toContain('mention-everyone');
|
||||
expect(result).toContain('@everyone');
|
||||
});
|
||||
|
||||
it('renders @here as a special mention', () => {
|
||||
const result = renderMentions('Attention @here');
|
||||
expect(result).toContain('mention-everyone');
|
||||
expect(result).toContain('@here');
|
||||
});
|
||||
|
||||
it('renders @room as a special mention', () => {
|
||||
const result = renderMentions('FYI @room');
|
||||
expect(result).toContain('mention-everyone');
|
||||
expect(result).toContain('@room');
|
||||
});
|
||||
|
||||
it('leaves plain text unchanged', () => {
|
||||
const result = renderMentions('Hello world');
|
||||
expect(result).toBe('Hello world');
|
||||
});
|
||||
|
||||
it('handles multiple mentions', () => {
|
||||
const result = renderMentions('@alice:matrix.org and @bob:example.com');
|
||||
expect(result).toContain('data-user-id="@alice:matrix.org"');
|
||||
expect(result).toContain('data-user-id="@bob:example.com"');
|
||||
});
|
||||
});
|
||||
|
||||
describe('isEmojiOnly', () => {
|
||||
it('returns true for single emoji', () => {
|
||||
expect(isEmojiOnly('😀')).toBe(true);
|
||||
});
|
||||
|
||||
it('returns true for multiple emojis', () => {
|
||||
expect(isEmojiOnly('😀🎉🔥')).toBe(true);
|
||||
});
|
||||
|
||||
it('returns true for emojis with spaces', () => {
|
||||
expect(isEmojiOnly('😀 🎉')).toBe(true);
|
||||
});
|
||||
|
||||
it('returns false for text with emoji', () => {
|
||||
expect(isEmojiOnly('hello 😀')).toBe(false);
|
||||
});
|
||||
|
||||
it('returns false for plain text', () => {
|
||||
expect(isEmojiOnly('hello world')).toBe(false);
|
||||
});
|
||||
|
||||
it('returns false for empty string', () => {
|
||||
expect(isEmojiOnly('')).toBe(false);
|
||||
});
|
||||
|
||||
it('returns false for whitespace only', () => {
|
||||
expect(isEmojiOnly(' ')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatTime', () => {
|
||||
it('formats timestamp to HH:MM', () => {
|
||||
// Create a date at 14:30
|
||||
const date = new Date(2024, 0, 15, 14, 30, 0);
|
||||
const result = formatTime(date.getTime());
|
||||
expect(result).toMatch(/14:30/);
|
||||
});
|
||||
|
||||
it('formats midnight correctly', () => {
|
||||
const date = new Date(2024, 0, 15, 0, 0, 0);
|
||||
const result = formatTime(date.getTime());
|
||||
expect(result).toMatch(/00:00/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatFileSize', () => {
|
||||
it('returns empty string for undefined', () => {
|
||||
expect(formatFileSize(undefined)).toBe('');
|
||||
});
|
||||
|
||||
it('returns empty string for 0', () => {
|
||||
expect(formatFileSize(0)).toBe('');
|
||||
});
|
||||
|
||||
it('formats bytes', () => {
|
||||
expect(formatFileSize(500)).toBe('500 B');
|
||||
});
|
||||
|
||||
it('formats kilobytes', () => {
|
||||
expect(formatFileSize(1024)).toBe('1.0 KB');
|
||||
expect(formatFileSize(1536)).toBe('1.5 KB');
|
||||
});
|
||||
|
||||
it('formats megabytes', () => {
|
||||
expect(formatFileSize(1024 * 1024)).toBe('1.0 MB');
|
||||
expect(formatFileSize(1.5 * 1024 * 1024)).toBe('1.5 MB');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user