test: add escapeHtml utility tests
Some checks failed
Build & Deploy to Staging / Build & Deploy to Staging (push) Has been cancelled
Some checks failed
Build & Deploy to Staging / Build & Deploy to Staging (push) Has been cancelled
This commit is contained in:
parent
ecc7b9640c
commit
d976afebc5
1 changed files with 49 additions and 0 deletions
49
src/__tests__/html.test.ts
Normal file
49
src/__tests__/html.test.ts
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import { escapeHtml } from '../utils/html';
|
||||||
|
|
||||||
|
describe('escapeHtml', () => {
|
||||||
|
it('escapes ampersands', () => {
|
||||||
|
expect(escapeHtml('foo & bar')).toBe('foo & bar');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('escapes less-than', () => {
|
||||||
|
expect(escapeHtml('a < b')).toBe('a < b');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('escapes greater-than', () => {
|
||||||
|
expect(escapeHtml('a > b')).toBe('a > b');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('escapes double quotes', () => {
|
||||||
|
expect(escapeHtml('say "hello"')).toBe('say "hello"');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('escapes single quotes', () => {
|
||||||
|
expect(escapeHtml("it's")).toBe('it's');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns empty string unchanged', () => {
|
||||||
|
expect(escapeHtml('')).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('passes through strings with no special chars', () => {
|
||||||
|
expect(escapeHtml('hello world 123')).toBe('hello world 123');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('escapes multiple special chars combined', () => {
|
||||||
|
expect(escapeHtml('<div class="x">&</div>')).toBe('<div class="x">&</div>');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('escapes XSS payload', () => {
|
||||||
|
expect(escapeHtml('<script>alert("xss")</script>')).toBe('<script>alert("xss")</script>');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('double-escapes existing entities', () => {
|
||||||
|
expect(escapeHtml('&')).toBe('&amp;');
|
||||||
|
expect(escapeHtml('<')).toBe('&lt;');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('escapes single quotes in attributes', () => {
|
||||||
|
expect(escapeHtml("data-x='val'")).toBe('data-x='val'');
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue