42 lines
1.3 KiB
JavaScript
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(
|
|
'<script>"'&</script>'
|
|
)
|
|
})
|
|
|
|
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<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>')
|
|
})
|
|
})
|
|
})
|