svelte-i18n/test/cli/extract.test.ts

241 lines
6.8 KiB
TypeScript
Raw Normal View History

2019-11-19 17:18:42 +01:00
// TODO: better tests. these are way too generic.
import { parse } from 'svelte/compiler'
import {
collectFormatCalls,
collectMessageDefinitions,
collectMessages,
extractMessages,
} from '../../src/cli/extract'
describe('collecting format calls', () => {
test('returns nothing if there are no script tag', () => {
const ast = parse(`<div>Hey</div>`)
const calls = collectFormatCalls(ast)
expect(calls).toHaveLength(0)
})
2019-11-29 02:57:05 +01:00
test('returns nothing if there are no imports', () => {
const ast = parse(`<script>
import Foo from 'foo';
const $_ = () => 0; $_();
</script>`)
2019-11-19 17:18:42 +01:00
const calls = collectFormatCalls(ast)
expect(calls).toHaveLength(0)
})
2019-11-29 02:57:05 +01:00
test('returns nothing if there are no format imports', () => {
const ast = parse(
`<script>
import { init } from 'svelte-i18n';
init({})
</script>`
)
const calls = collectFormatCalls(ast)
expect(calls).toHaveLength(0)
})
test('collects all format calls in the instance script', () => {
2019-11-19 17:18:42 +01:00
const ast = parse(`<script>
import { format, _ } from 'svelte-i18n'
2019-11-29 02:57:05 +01:00
$format('foo')
format('bar')
let label = $_({id:'bar'})
const a = { b: () => 0}
2019-11-19 17:18:42 +01:00
</script>`)
const calls = collectFormatCalls(ast)
expect(calls).toHaveLength(2)
expect(calls[0]).toMatchObject({ type: 'CallExpression' })
expect(calls[1]).toMatchObject({ type: 'CallExpression' })
})
2019-11-29 02:57:05 +01:00
test('collects all format calls with renamed imports', () => {
2019-11-19 17:18:42 +01:00
const ast = parse(`<script>
2019-11-29 02:57:05 +01:00
import { format as _x, _ as intl, t as f } from 'svelte-i18n'
$_x('foo')
$intl({ id: 'bar' })
$f({ id: 'bar' })
2019-11-19 17:18:42 +01:00
</script>`)
const calls = collectFormatCalls(ast)
2019-11-29 02:57:05 +01:00
expect(calls).toHaveLength(3)
2019-11-19 17:18:42 +01:00
expect(calls[0]).toMatchObject({ type: 'CallExpression' })
expect(calls[1]).toMatchObject({ type: 'CallExpression' })
2019-11-29 02:57:05 +01:00
expect(calls[2]).toMatchObject({ type: 'CallExpression' })
2019-11-19 17:18:42 +01:00
})
})
describe('collecting message definitions', () => {
2019-11-29 02:57:05 +01:00
test('returns nothing if there are no imports from the library', () => {
2019-11-19 17:18:42 +01:00
const ast = parse(
`<script>
2019-11-29 02:57:05 +01:00
import foo from 'foo';
import { dictionary } from 'svelte-i18n';
</script>`
2019-11-19 17:18:42 +01:00
)
expect(collectMessageDefinitions(ast)).toHaveLength(0)
})
2019-11-29 02:57:05 +01:00
test('gets all message definition objects', () => {
2019-11-19 17:18:42 +01:00
const ast = parse(`<script>
2019-11-29 02:57:05 +01:00
import { defineMessages } from 'svelte-i18n';
defineMessages({ foo: { id: 'foo' }, bar: { id: 'bar' } })
defineMessages({ baz: { id: 'baz' }, quix: { id: 'qux' } })
2019-11-19 17:18:42 +01:00
</script>`)
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' })
})
})
describe('collecting messages', () => {
2019-11-29 02:57:05 +01:00
test('collects all messages in both instance and html ASTs', () => {
2019-11-19 17:18:42 +01:00
const markup = `
<script>
import { _, defineMessages } from 'svelte-i18n';
2019-11-29 02:57:05 +01:00
2019-11-19 17:18:42 +01:00
console.log($_({ id: 'foo' }))
console.log($_.title({ id: 'page.title' }))
const messages = defineMessages({
enabled: { id: 'enabled' , default: 'Enabled' },
disabled: { id: 'disabled', default: 'Disabled' }
})
</script>
2019-11-29 02:57:05 +01:00
2019-11-19 17:18:42 +01:00
<div>{$_('msg_1')}</div>
<div>{$_({id: 'msg_2'})}</div>
<div>{$_('msg_3', { default: 'Message'})}</div>`
2019-11-29 02:57:05 +01:00
2019-11-19 17:18:42 +01:00
const messages = collectMessages(markup)
2019-11-29 02:57:05 +01:00
2019-11-19 17:18:42 +01:00
expect(messages).toHaveLength(7)
expect(messages).toEqual(
expect.arrayContaining([
expect.objectContaining({ meta: { id: 'foo' } }),
expect.objectContaining({ meta: { id: 'msg_1' } }),
expect.objectContaining({ meta: { id: 'msg_2' } }),
expect.objectContaining({ meta: { id: 'msg_3', default: 'Message' } }),
expect.objectContaining({ meta: { id: 'page.title' } }),
expect.objectContaining({
meta: { id: 'disabled', default: 'Disabled' },
}),
expect.objectContaining({
meta: { id: 'enabled', default: 'Enabled' },
}),
2019-11-29 02:57:05 +01:00
])
2019-11-19 17:18:42 +01:00
)
})
})
describe('messages extraction', () => {
2019-11-29 02:57:05 +01:00
test('returns a object built based on all found message paths', () => {
const markup = `<script>
import { _ } from 'svelte-i18n';
</script>
2019-11-19 17:18:42 +01:00
<h1>{$_.title('title')}</h1>
<h2>{$_({ id: 'subtitle'})}</h2>
`
const dict = extractMessages(markup)
expect(dict).toMatchObject({ title: '', subtitle: '' })
})
2019-11-29 02:57:05 +01:00
test('creates deep nested properties', () => {
2019-11-19 17:18:42 +01:00
const markup = `
<script>import { _ } from 'svelte-i18n';</script>
<h1>{$_.title('home.page.title')}</h1>
<h2>{$_({ id: 'home.page.subtitle'})}</h2>
<ul>
2019-11-29 02:57:05 +01:00
<li>{$_('list.0')}</li>
<li>{$_('list.1')}</li>
<li>{$_('list.2')}</li>
2019-11-19 17:18:42 +01:00
</ul>
`
const dict = extractMessages(markup)
expect(dict).toMatchObject({
home: { page: { title: '', subtitle: '' } },
list: ['', '', ''],
})
})
2019-11-29 02:57:05 +01:00
test('creates a shallow dictionary', () => {
2019-11-19 17:18:42 +01:00
const markup = `
<script>import { _ } from 'svelte-i18n';</script>
<h1>{$_.title('home.page.title')}</h1>
<h2>{$_({ id: 'home.page.subtitle'})}</h2>
<ul>
2019-11-29 02:57:05 +01:00
<li>{$_('list.0')}</li>
<li>{$_('list.1')}</li>
<li>{$_('list.2')}</li>
2019-11-19 17:18:42 +01:00
</ul>
`
const dict = extractMessages(markup, { shallow: true })
expect(dict).toMatchObject({
'home.page.title': '',
'home.page.subtitle': '',
2019-11-29 02:57:05 +01:00
'list.0': '',
'list.1': '',
'list.2': '',
2019-11-19 17:18:42 +01:00
})
})
2019-11-29 02:57:05 +01:00
test('allow to pass a initial dictionary and only append non-existing props', () => {
2019-11-19 17:18:42 +01:00
const markup = `
<script>import { _ } from 'svelte-i18n';</script>
<h1>{$_.title('home.page.title')}</h1>
<h2>{$_({ id: 'home.page.subtitle'})}</h2>
<ul>
2019-11-29 02:57:05 +01:00
<li>{$_('list.0')}</li>
<li>{$_('list.1')}</li>
<li>{$_('list.2')}</li>
2019-11-19 17:18:42 +01:00
</ul>
`
const dict = extractMessages(markup, {
overwrite: false,
accumulator: {
home: {
page: {
title: 'Page title',
},
},
},
})
expect(dict).toMatchObject({
home: {
page: {
title: 'Page title',
subtitle: '',
},
},
list: ['', '', ''],
})
})
2019-11-29 02:57:05 +01:00
test('allow to pass a initial dictionary and only append shallow non-existing props', () => {
2019-11-19 17:18:42 +01:00
const markup = `
<script>import { _ } from 'svelte-i18n';</script>
<h1>{$_.title('home.page.title')}</h1>
<h2>{$_({ id: 'home.page.subtitle'})}</h2>
`
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': '',
})
})
})