Skip to content

Commit 4b0ad66

Browse files
committed
test(catcher): Sanitizer covered with tests
1 parent 01ea5af commit 4b0ad66

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import { describe, it, expect } from 'vitest';
2+
import Sanitizer from '../../src/modules/sanitizer';
3+
4+
describe('Sanitizer', () => {
5+
describe('isObject', () => {
6+
it('should return true for a plain object', () => {
7+
expect(Sanitizer.isObject({})).toBe(true);
8+
});
9+
10+
it('should return false for an array', () => {
11+
expect(Sanitizer.isObject([])).toBe(false);
12+
});
13+
14+
it('should return false for a string', () => {
15+
expect(Sanitizer.isObject('x')).toBe(false);
16+
});
17+
18+
it('should return false for a boolean', () => {
19+
expect(Sanitizer.isObject(true)).toBe(false);
20+
});
21+
22+
it('should return false for null', () => {
23+
expect(Sanitizer.isObject(null)).toBe(false);
24+
});
25+
26+
it('should return false for undefined', () => {
27+
expect(Sanitizer.isObject(undefined)).toBe(false);
28+
});
29+
});
30+
31+
describe('sanitize', () => {
32+
it('should pass through strings within the length limit', () => {
33+
expect(Sanitizer.sanitize('hello')).toBe('hello');
34+
});
35+
36+
it('should trim strings longer than maxStringLen', () => {
37+
const long = 'a'.repeat(201);
38+
const result = Sanitizer.sanitize(long);
39+
40+
expect(result).toBe('a'.repeat(200) + '…');
41+
});
42+
43+
it('should pass through short arrays unchanged', () => {
44+
expect(Sanitizer.sanitize([1, 2, 3])).toEqual([1, 2, 3]);
45+
});
46+
47+
it('should truncate arrays over maxArrayLength items and append placeholder', () => {
48+
const arr = Array.from({ length: 12 }, (_, i) => i);
49+
const result = Sanitizer.sanitize(arr);
50+
51+
expect(result).toHaveLength(11);
52+
expect(result[10]).toBe('<2 more items...>');
53+
});
54+
55+
it('should sanitize nested objects recursively', () => {
56+
const longStr = 'a'.repeat(201);
57+
const longArr = Array.from({ length: 12 }, (_, i) => i);
58+
const obj = {
59+
foo: 'x',
60+
bar: longStr,
61+
baz: longArr
62+
}
63+
const result = Sanitizer.sanitize(obj);
64+
65+
expect(result.foo).toBe('x');
66+
expect(result.bar).toBe('a'.repeat(200) + '…');
67+
expect(result.baz).toHaveLength(11);
68+
expect(result.baz[10]).toBe('<2 more items...>');
69+
});
70+
71+
it('should replace objects with more than 20 keys with placeholder', () => {
72+
const big: Record<string, number> = {};
73+
74+
for (let i = 0; i < 21; i++) {
75+
big[`k${i}`] = i;
76+
}
77+
78+
expect(Sanitizer.sanitize(big)).toBe('<big object>');
79+
});
80+
81+
it('should replace deeply nested objects with placeholder', () => {
82+
const deep = { a: { b: { c: { d: { e: { f: 'bottom' } } } } } };
83+
const result = Sanitizer.sanitize(deep);
84+
85+
expect(result.a.b.c.d.e).toBe('<deep object>');
86+
});
87+
88+
it('should format HTML elements as a string starting with tag', () => {
89+
const el = document.createElement('div');
90+
const result = Sanitizer.sanitize(el);
91+
92+
expect(typeof result).toBe('string');
93+
expect(result).toMatch(/^<div/);
94+
});
95+
96+
it('should format a class (not constructed) as "<class Name>"', () => {
97+
class Foo {}
98+
99+
expect(Sanitizer.sanitize(Foo)).toBe('<class Foo>');
100+
});
101+
102+
it('should format a class instance as "<instance of Name>"', () => {
103+
class Foo {}
104+
105+
expect(Sanitizer.sanitize(new Foo())).toBe('<instance of Foo>');
106+
});
107+
108+
it('should replace circular references with placeholder', () => {
109+
const obj: any = { a: 1 };
110+
111+
obj.self = obj;
112+
113+
const result = Sanitizer.sanitize(obj);
114+
115+
expect(result.self).toBe('<circular>');
116+
});
117+
118+
it.each([
119+
{ label: 'number', value: 42 },
120+
{ label: 'boolean', value: true },
121+
{ label: 'null', value: null },
122+
])('should pass through $label primitives unchanged', ({ value }) => {
123+
expect(Sanitizer.sanitize(value)).toBe(value);
124+
});
125+
});
126+
});

0 commit comments

Comments
 (0)