Files
ToolBox/tests/unit/comparator/html.test.js
T
renjue 11e1b3ca3b
CI / docker (push) Failing after 11s
初始化 ToolBox 开发者工具箱。
包含 JSON 格式化、文本/JSON 对比、编解码、变量名转换、二维码、时间戳与颜色转换等工具;修复编辑器行号同步;补充 Vitest 单元测试、Docker 部署与 Gitea CI;完善开源文档;支持通过环境变量配置站点标题、备案号及第三方集成。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-24 15:11:05 +08:00

42 lines
1.3 KiB
JavaScript

import { describe, it, expect } from 'vitest'
import { escapeHtml, highlightDiff } from '../../../src/utils/comparator/html.js'
describe('comparator/html', () => {
describe('escapeHtml', () => {
it('escapes special HTML characters', () => {
expect(escapeHtml('<script>"\'&</script>')).toBe(
'&lt;script&gt;&quot;&#39;&amp;&lt;/script&gt;'
)
})
it('handles empty string', () => {
expect(escapeHtml('')).toBe('')
})
it('coerces non-string values', () => {
expect(escapeHtml(123)).toBe('123')
})
})
describe('highlightDiff', () => {
it('returns escaped text when no ranges', () => {
expect(highlightDiff('hello', [])).toBe('hello')
expect(highlightDiff('a<b', null)).toBe('a&lt;b')
})
it('wraps diff ranges with highlight span', () => {
const result = highlightDiff('hello', [{ start: 1, end: 4 }])
expect(result).toBe('h<span class="diff-highlight">ell</span>o')
})
it('handles multiple ranges', () => {
const result = highlightDiff('abcdef', [
{ start: 0, end: 1 },
{ start: 4, end: 6 },
])
expect(result).toContain('<span class="diff-highlight">a</span>bcd')
expect(result).toContain('<span class="diff-highlight">ef</span>')
})
})
})