Skip to content

Commit

Permalink
tests: write unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vsetka committed Sep 9, 2017
1 parent 21050e3 commit b3d2b3e
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
12 changes: 12 additions & 0 deletions __mocks__/request-helper.js
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])
);
});
};
37 changes: 37 additions & 0 deletions __mocks__/translationMap.js
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: [],
},
},
};
42 changes: 42 additions & 0 deletions __tests__/index.test.js
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":[]}}')
);
});
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
"description":
"This module provides promised methods for translating text using DeepL Translator (https://www.deepl.com/translator) undocumented API.",
"main": "index.js",
"scripts": {
"test": "jest",
"collectCoverage": "true"
},
"jest": {
"verbose": true
},
"repository": "https://github.com/vsetka/deepl-translator.git",
"homepage": "https://github.com/vsetka/deepl-translator#readme",
"author": "Vladimir Setka <vsetka@gmail.com>",
Expand All @@ -15,5 +22,9 @@
"engineStrict": true,
"engines": {
"node": ">=6.0.0"
},
"devDependencies": {
"coveralls": "^2.13.1",
"jest": "^21.0.2"
}
}

0 comments on commit b3d2b3e

Please sign in to comment.