From 8140fddc8b351b4dccc51fecdb80676f76e0b2c3 Mon Sep 17 00:00:00 2001 From: AlacrisDevs Date: Sat, 7 Feb 2026 09:32:57 +0200 Subject: [PATCH] test: add 20 unit tests for markdown utils (mentions, emoji-only, formatTime, formatFileSize) --- .../components/message/utils/markdown.test.ts | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/lib/components/message/utils/markdown.test.ts diff --git a/src/lib/components/message/utils/markdown.test.ts b/src/lib/components/message/utils/markdown.test.ts new file mode 100644 index 0000000..0ad8a9e --- /dev/null +++ b/src/lib/components/message/utils/markdown.test.ts @@ -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'); + }); + + 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'); + }); + }); +});