-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const translationMap = require('./translationMap'); | ||
|
||
module.exports = (options, postBody) => { | ||
return new Promise((resolve, reject) => { | ||
const { params: { jobs: [{ raw_en_sentence: inputText }] } } = postBody; | ||
|
||
process.nextTick( | ||
() => | ||
!translationMap[inputText] ? reject(new Error('This input should throw')) : resolve(translationMap[inputText]) | ||
); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module.exports = { | ||
'Happy birthday!': { | ||
result: { | ||
source_lang: 'EN', | ||
target_lang: 'DE', | ||
translations: [ | ||
{ | ||
beams: [ | ||
{ | ||
postprocessed_sentence: 'Herzlichen Glückwunsch zum Geburtstag!', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}, | ||
'This is a representative chunk of text in english.': { | ||
result: { | ||
source_lang: 'EN', | ||
target_lang: 'DE', | ||
translations: [ | ||
{ | ||
beams: [ | ||
{ | ||
postprocessed_sentence: 'This is a representative chunk of text in english.', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}, | ||
'This mock results in an incorrect reponse format': { | ||
result: { | ||
no_translations_here: [], | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
jest.mock('../request-helper'); | ||
|
||
const { translate, detectLanguage } = require('../index'); | ||
|
||
test('Detects english input language correctly', () => { | ||
return expect(detectLanguage('This is a representative chunk of text in english.')).resolves.toEqual({ | ||
languageCode: 'EN', | ||
languageName: 'English', | ||
}); | ||
}); | ||
|
||
test('Translate input correctly without specifying its language', () => { | ||
return expect(translate('Happy birthday!', 'DE')).resolves.toEqual({ | ||
targetLanguage: 'DE', | ||
resolvedSourceLanguage: 'EN', | ||
translation: 'Herzlichen Glückwunsch zum Geburtstag!', | ||
}); | ||
}); | ||
|
||
test('Rejects on invalid target language', () => { | ||
return expect(translate('Happy birthday!')).rejects.toEqual(new Error('Invalid target language code undefined')); | ||
}); | ||
|
||
test('Rejects on invalid source language', () => { | ||
return expect(translate('Happy birthday!', 'DE', 'XX')).rejects.toEqual(new Error('Invalid source language code XX')); | ||
}); | ||
|
||
test('Rejects when source and target languages are identical', () => { | ||
return expect(translate('Happy birthday!', 'DE', 'DE')).rejects.toEqual( | ||
new Error('Target and source language codes identical') | ||
); | ||
}); | ||
|
||
test('Rejects when input text is not provided', () => { | ||
return expect(translate('', 'DE')).rejects.toEqual(new Error('Must provide text for translation')); | ||
}); | ||
|
||
test('Rejects when response in incorrect format', () => { | ||
return expect(translate('This mock results in an incorrect reponse format', 'DE')).rejects.toEqual( | ||
new Error('Unexpected error when parsing response body: {"result":{"no_translations_here":[]}}') | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters