-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpolate.test.ts
68 lines (60 loc) · 1.78 KB
/
interpolate.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { expect } from '@std/expect';
import { describe, it } from '@std/testing/bdd';
import { interpolate } from './interpolate.ts';
import type { LocalizedUnit } from './LocalizedUnit.ts';
describe('interpolate()', () =>
{
const mockUnit: LocalizedUnit<'en' | 'ja'> = {
en: 'Hello {{name}}',
ja: 'こんにちは {{name}}',
};
it('should interpolate basic parameters', () =>
{
const actual = interpolate('Hello {{name}}', mockUnit, 'en', { name: 'Alice' });
expect(actual).toBe('Hello Alice');
});
it('should handle multiple parameters', () =>
{
const actual = interpolate(
'Hello {{firstName}} {{lastName}}',
mockUnit,
'en',
{ firstName: 'Alice', lastName: 'Smith' },
);
expect(actual).toBe('Hello Alice Smith');
});
it('should handle missing parameters', () =>
{
const actual = interpolate('Hello {{name}}', mockUnit, 'en', {});
expect(actual).toBe('Hello ');
});
it('should handle null/undefined message', () =>
{
const actualNull = interpolate(null, mockUnit, 'en', { name: 'Alice' });
expect(actualNull).toBe('');
const actualUndefined = interpolate(undefined, mockUnit, 'en', { name: 'Alice' });
expect(actualUndefined).toBe('');
});
it('should handle HTML escaping', () =>
{
const actual = interpolate(
'Message: {{message}}',
mockUnit,
'en',
{ message: '<script>alert("xss")</script>' },
true,
);
expect(actual).toBe('Message: <script>alert("xss")</script>');
});
it('should not escape HTML when escapeParam is false', () =>
{
const actual = interpolate(
'Message: {{message}}',
mockUnit,
'en',
{ message: '<b>bold</b>' },
false,
);
expect(actual).toBe('Message: <b>bold</b>');
});
});