vvlad1973-telegram-framework
Version:
Current version: *7.9.5*
112 lines (99 loc) • 3.02 kB
JavaScript
import { describe, it, expect } from 'vitest';
import { replaceTemplate } from '../src/helpers/utils.js';
describe('replaceTemplate', () => {
const strings = {
TITLE: 'Заголовок',
FOOTER: 'Низ страницы',
};
const data = {
user: {
name: 'Alice',
peers: {
peer_0: 777777,
peer_1: 888888,
},
links: [
{ id: 'l1', href: '/link1' },
{ id: 'l2', href: '/link2' },
],
meta: {
peer_0: { status: 'online' },
},
getLink(id) {
return this.links.find((l) => l.id === id);
},
async getProfile() {
return { nickname: 'Ali', level: 3 };
},
},
numbers: [10, 20, 30],
};
it('replaces simple string key from "strings"', async () => {
const result = await replaceTemplate(
'Welcome to {{TITLE}}!',
strings,
data
);
expect(result).toBe('Welcome to Заголовок!');
});
it('replaces nested path from data object', async () => {
const result = await replaceTemplate('User: {{user.name}}', strings, data);
expect(result).toBe('User: Alice');
});
it('replaces nested path from data object 2', async () => {
const result = await replaceTemplate('User: {{user.peers["peer_0"]}}', strings, data);
expect(result).toBe('User: 777777');
});
it('resolves array access like user.links[1].href', async () => {
const result = await replaceTemplate(
'Second link: {{user.links[1].href}}',
strings,
data
);
expect(result).toBe('Second link: /link2');
});
it('resolves object key access with brackets', async () => {
const result = await replaceTemplate(
'Status: {{user.meta["peer_0"].status}}',
strings,
data
);
expect(result).toBe('Status: online');
});
it('resolves method call with argument', async () => {
const result = await replaceTemplate(
'Link: {{user.getLink("l2").href}}',
strings,
data
);
expect(result).toBe('Link: /link2');
});
it('resolves method call with argument 2', async () => {
const result = await replaceTemplate(
"Link: {{user.getLink('l2').href}}",
strings,
data
);
expect(result).toBe('Link: /link2');
});
it('resolves async method call', async () => {
const result = await replaceTemplate(
'Nickname: {{user.getProfile().nickname}}',
strings,
data
);
expect(result).toBe('Nickname: Ali');
});
it('leaves unknown placeholder as-is', async () => {
const result = await replaceTemplate('Hello {{UNKNOWN}}', strings, data);
expect(result).toBe('Hello {{UNKNOWN}}');
});
it('supports numeric array access', async () => {
const result = await replaceTemplate(
'First number: {{numbers[0]}}',
strings,
data
);
expect(result).toBe('First number: 10');
});
});