lzy1960/google-translate

View on GitHub
packages/__tests__/translate.spec.ts

Summary

Maintainability
A
1 hr
Test Coverage
import { describe, expect, it } from 'vitest'
import { translate } from '../src/index'
import { LanguageKey } from '../../types/language'
import { DefaultResult } from '../../types/index'
 
describe('translate', async () => {
it('should translate more words from zh-CN to en', async () => {
const res = await translate('苹果')
expect(res.text).toBe('apple')
 
const res1 = await translate('香蕉')
expect(res1.text).toBe('banana')
 
const res2 = await translate('猴子')
expect(res2.text).toBe('monkey')
 
const res3 = (await translate('你好')) as DefaultResult
expect(res3.text).toBe('Hello')
// 现在好像不支持拼音了
// expect(res3.pronunciation).toBe('Nǐ hǎo')
})
Similar blocks of code found in 3 locations. Consider refactoring.
it('should translate with punctuation', async () => {
const res = await translate('今天,我看到一个程序员!')
expect(res.text).toBe('Today, I saw a programmer!')
})
Similar blocks of code found in 3 locations. Consider refactoring.
it('should translate without char', async () => {
const res = await translate('')
expect(res.text).toBe('')
})
it('should translate with space', async () => {
const res = await translate(' ')
expect(res.text).toBe('')
const res1 = await translate('这是什么?')
const res2 = await translate(' 这 是 什 么?')
const res3 = await translate(' 这 是 什 么?')
const res4 = await translate(' 这是什么? ')
expect(res1.text).toEqual(res2.text)
expect(res2.text).toEqual(res3.text)
expect(res4.text).toBe('What\'s this?')
})
Similar blocks of code found in 3 locations. Consider refactoring.
it('should translate with confusing punctuation', async () => {
const res = await translate('这,...是,..什.么????12123123')
expect(res.text).toBe('What\'s this???? 12123123')
})
it('should translate long sentences', async () => {
const res = await translate(
'床前明月光,疑是地上霜。举头望明月,低头思故乡。',
)
expect(res.text).toBe(
'The bright moonlight in front of the bed is suspected to be frost on the ground. Looking up at the bright moon, looking down at the hometown.',
)
const resMobile = await translate(
'床前明月光,疑是地上霜。举头望明月,低头思故乡。',
{
from: 'auto',
to: 'en',
isMobile: true,
},
)
expect(resMobile.text).toBe(
'The bright moonlight in front of the bed is suspected to be frost on the ground. Looking up at the bright moon, looking down at the hometown.',
)
})
it('should translate from en to zh-CN', async () => {
const res = await translate('ok, Im done!', {
from: 'en',
to: 'zh-CN',
})
expect(res.text).toBe('好,我完成了!')
})
it('should throw Error when get wrong from and to', () => {
const fn = translate('ok, Im done!', {
from: 'notInLanguage' as unknown as LanguageKey,
to: 'zh-CN',
})
expect(fn).rejects.toThrowError('not support this language')
})
})