| 1 |
import { describe, expect, it } from 'vitest'; |
| 2 |
import { parseTemplate } from '../../src/util.js'; |
| 3 |
|
| 4 |
describe('parseTemplate', () => { |
| 5 |
it('replaces placeholders with values', () => { |
| 6 |
const template = 'My friend {name} is {age} years old'; |
| 7 |
const replacements = { name: 'John', age: '30' }; |
| 8 |
const result = parseTemplate(template, replacements); |
| 9 |
expect(result).toBe('My friend John is 30 years old'); |
| 10 |
}); |
| 11 |
|
| 12 |
it('accepts any-case value keys', () => { |
| 13 |
const template = 'My friend {Name} is {Age} years old'; |
| 14 |
const replacements = { Name: 'John', Age: '30' }; |
| 15 |
const result = parseTemplate(template, replacements); |
| 16 |
expect(result).toBe('My friend John is 30 years old'); |
| 17 |
}); |
| 18 |
|
| 19 |
it('ignores missing value keys', () => { |
| 20 |
const template = 'Hello, {name}!'; |
| 21 |
const replacements = {}; |
| 22 |
const result = parseTemplate(template, replacements); |
| 23 |
expect(result).toBe('Hello, {name}!'); |
| 24 |
}); |
| 25 |
|
| 26 |
it('throws when no data is provided', () => { |
| 27 |
// @ts-expect-error missing argument |
| 28 |
expect(() => parseTemplate('Hello, {name}!')).toThrow(); |
| 29 |
}); |
| 30 |
}); |