import { parse } from 'svelte/compiler';
import {
collectFormatCalls,
collectMessageDefinitions,
collectMessages,
extractMessages,
} from '../../src/cli/extract';
describe('collecting format calls', () => {
it('returns nothing if there are no script tag', () => {
const ast = parse(`
Hey
`);
const calls = collectFormatCalls(ast);
expect(calls).toHaveLength(0);
});
it('returns nothing if there are no imports', () => {
const ast = parse(``);
const calls = collectFormatCalls(ast);
expect(calls).toHaveLength(0);
});
it('returns nothing if there are no format imports', () => {
const ast = parse(
``,
);
const calls = collectFormatCalls(ast);
expect(calls).toHaveLength(0);
});
it('collects all format calls in the instance script', () => {
const ast = parse(``);
const calls = collectFormatCalls(ast);
expect(calls).toHaveLength(2);
expect(calls[0]).toMatchObject({ type: 'CallExpression' });
expect(calls[1]).toMatchObject({ type: 'CallExpression' });
});
it('collects all format calls with renamed imports', () => {
const ast = parse(``);
const calls = collectFormatCalls(ast);
expect(calls).toHaveLength(3);
expect(calls[0]).toMatchObject({ type: 'CallExpression' });
expect(calls[1]).toMatchObject({ type: 'CallExpression' });
expect(calls[2]).toMatchObject({ type: 'CallExpression' });
});
});
describe('collecting message definitions', () => {
it('returns nothing if there are no imports from the library', () => {
const ast = parse(
``,
);
expect(collectMessageDefinitions(ast)).toHaveLength(0);
});
it('gets all message definition objects', () => {
const ast = parse(``);
const definitions = collectMessageDefinitions(ast);
expect(definitions).toHaveLength(4);
expect(definitions[0]).toMatchObject({ type: 'ObjectExpression' });
expect(definitions[1]).toMatchObject({ type: 'ObjectExpression' });
expect(definitions[2]).toMatchObject({ type: 'ObjectExpression' });
expect(definitions[3]).toMatchObject({ type: 'ObjectExpression' });
});
it('throws an error if an spread is found', () => {
const ast = parse(``);
expect(() =>
collectMessageDefinitions(ast),
).toThrowErrorMatchingInlineSnapshot(
`"Found invalid 'SpreadElement' at L4:23"`,
);
});
});
describe('collecting messages', () => {
it('collects all messages in both instance and html ASTs', () => {
const markup = `
{$_('msg_1')}
{$_({id: 'msg_2'})}
{$_('msg_3', { default: 'Message'})}
`;
const messages = collectMessages(markup);
expect(messages).toHaveLength(7);
expect(messages).toEqual(
expect.arrayContaining([
expect.objectContaining({ id: 'foo' }),
expect.objectContaining({ id: 'msg_1' }),
expect.objectContaining({ id: 'msg_2' }),
expect.objectContaining({ id: 'msg_3', default: 'Message' }),
expect.objectContaining({ id: 'page.title' }),
expect.objectContaining({
id: 'disabled',
default: 'Disabled',
}),
expect.objectContaining({
id: 'enabled',
default: 'Enabled',
}),
]),
);
});
it('ignores non-static message ids', () => {
const markup = `
`;
const messages = collectMessages(markup);
expect(messages).toHaveLength(0);
});
});
describe('messages extraction', () => {
it('returns a object built based on all found message paths', () => {
const markup = `
{$_('title')}
{$_({ id: 'subtitle'})}
`;
const dict = extractMessages(markup);
expect(dict).toMatchObject({ title: '', subtitle: '' });
});
it('creates deep nested properties', () => {
const markup = `
{$_('home.page.title')}
{$_({ id: 'home.page.subtitle'})}
- {$_('list.0')}
- {$_('list.1')}
- {$_('list.2')}
`;
const dict = extractMessages(markup);
expect(dict).toMatchObject({
home: { page: { title: '', subtitle: '' } },
list: ['', '', ''],
});
});
it('creates a shallow dictionary', () => {
const markup = `
{$_('home.page.title')}
{$_({ id: 'home.page.subtitle'})}
- {$_('list.0')}
- {$_('list.1')}
- {$_('list.2')}
`;
const dict = extractMessages(markup, { shallow: true });
expect(dict).toMatchObject({
'home.page.title': '',
'home.page.subtitle': '',
'list.0': '',
'list.1': '',
'list.2': '',
});
});
it('allow to pass a initial dictionary and only append non-existing props', () => {
const markup = `
{$_('home.page.title')}
{$_({ id: 'home.page.subtitle'})}
- {$_('list.0')}
- {$_('list.1')}
- {$_('list.2')}
`;
const dict = extractMessages(markup, {
overwrite: false,
accumulator: {
home: {
page: {
title: 'Page title',
},
},
},
});
expect(dict).toMatchObject({
home: {
page: {
title: 'Page title',
subtitle: '',
},
},
list: ['', '', ''],
});
});
it('allow to pass a initial dictionary and only append shallow non-existing props', () => {
const markup = `
{$_('home.page.title')}
{$_({ id: 'home.page.subtitle'})}
`;
const dict = extractMessages(markup, {
overwrite: false,
shallow: true,
accumulator: {
'home.page.title': 'Page title',
},
});
expect(dict).toMatchObject({
'home.page.title': 'Page title',
'home.page.subtitle': '',
});
});
});