basic tests for language detection

This commit is contained in:
Moriz Wahl 2021-01-19 02:54:14 +01:00 committed by Ulf Gebhardt
parent f3847e5c97
commit 3bd8a531f1
No known key found for this signature in database
GPG Key ID: 81308EFE29ABFEBD
2 changed files with 44 additions and 12 deletions

View File

@ -1,18 +1,15 @@
import LanguageDetect from 'languagedetect'
const setPostLanguage = (text) => {
console.log(text)
const lngDetector = new LanguageDetect()
lngDetector.setLanguageType('iso2')
const result = lngDetector.detect(text, 2)
console.log(result)
return result[0][0]
}
export default {
Mutation: {
CreatePost: async (resolve, root, args, context, info) => {
console.log('CreatePost, language', args)
args.language = await setPostLanguage(args.content)
return resolve(root, args, context, info)
},

View File

@ -23,15 +23,6 @@ beforeAll(async () => {
},
})
mutate = createTestClient(server).mutate
await cleanDatabase()
variables = {}
const user = await Factory.build('user')
authenticatedUser = await user.toJson()
await Factory.build('category', {
id: 'cat9',
name: 'Democracy & Politics',
icon: 'university',
})
})
@ -54,6 +45,17 @@ describe('languagesMiddleware', () => {
categoryIds: ['cat9'],
}
beforeAll(async () => {
await cleanDatabase()
const user = await Factory.build('user')
authenticatedUser = await user.toJson()
await Factory.build('category', {
id: 'cat9',
name: 'Democracy & Politics',
icon: 'university',
})
})
it('detects German', async () => {
variables = {
...variables,
@ -71,4 +73,37 @@ describe('languagesMiddleware', () => {
})
})
it('detects English', async () => {
variables = {
...variables,
content: 'A journey of a thousand miles begins with a single step.',
}
await expect(mutate({
mutation: createPostMutation,
variables,
})).resolves.toMatchObject({
data: {
CreatePost: {
language: 'en',
},
},
})
})
it('detects Spanish', async () => {
variables = {
...variables,
content: 'A caballo regalado, no le mires el diente.',
}
await expect(mutate({
mutation: createPostMutation,
variables,
})).resolves.toMatchObject({
data: {
CreatePost: {
language: 'es',
},
},
})
})
})