Skip to content

Commit

Permalink
test(googleService&yandexService): Fixing unit tests and increasing c…
Browse files Browse the repository at this point in the history
…overage
  • Loading branch information
KhaledMohamedP committed Jul 1, 2017
1 parent e171c34 commit 325f7af
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 7 deletions.
6 changes: 4 additions & 2 deletions __mocks__/google-translate.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module.exports = function () {
return {
translate: (value, lang, fn) => {
if (value) {
if (value && lang) {
var translatedList = {translatedText: value + '-' + lang};
fn(null, translatedList);
} else if (lang === null) {
fn('You missed to pass the languge');
} else {
fn({error: 'provide value'});
fn('Something went wrong');
}
}
};
Expand Down
6 changes: 4 additions & 2 deletions __mocks__/yandex-translate.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module.exports = function () {
return {
translate: (value, lang, fn) => {
if (value) {
if (value && lang.to) {
var translatedList = value + '-' + lang.to;
fn(null, {code: 200, text: [translatedList]});
} else if (lang.to === null) {
fn(null, {status: 404, error: 'Invalid language'});
} else {
fn({error: 'provide value'});
fn('Something went wrong');
}
}
};
Expand Down
67 changes: 67 additions & 0 deletions lib/service/__tests__/google.service.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var translate = require('../google');

// Mocked external node_modules
jest.mock('google-translate');

// Mocked internal public methods
jest.fn(translate.init);
jest.fn(translate.object);
jest.fn(translate.string);

describe('google service existance', () => {
it('should exist', () => {
expect(translate).toBeDefined();
});

it('should include init', () => {
expect(translate.init).toBeDefined();
});

it('should include string', () => {
expect(translate.string).toBeDefined();
});

it('should include array', () => {
expect(translate.object).toBeDefined();
});
});

describe('Google translate.object: ', () => {
it('should fail due missing language param', () => {
translate.init({googleApiKey: 'google_token'});
return translate
.object(null, {}, {}, ['key1', 'key2'], ['list', 'of', 'elements'])
.catch(err => {
expect(err).toBe('You missed to pass the languge');
});
});

it('should fail to transalte object due to invalid value', () => {
translate.init({googleApiKey: 'google_token'});
return translate
.object('ar', {}, {}, [], [])
.catch(err => {
expect(err).toBe('Something went wrong');
});
});
});

describe('Google translate.string: ', () => {
it('should fail due missing language param', () => {
translate.init({googleApiKey: 'google_token'});
return translate
.string(null, 'key', {}, '')
.catch(err => {
expect(err).toBe('You missed to pass the languge');
});
});

it('should failed due to invalid value', () => {
translate.init({googleApiKey: 'google_token'});
return translate
.string('es', 'key', {}, null)
.catch(err => {
expect(err).toBe('Something went wrong');
});
});
});
70 changes: 70 additions & 0 deletions lib/service/__tests__/yandex.service.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
var translate = require('../yandex');

// Mocked external node_modules
jest.mock('yandex-translate');

// Mocked internal public methods
jest.fn(translate.init);
jest.fn(translate.object);
jest.fn(translate.string);

describe('Yandex service public API', () => {
it('should exist', () => {
expect(translate).toBeDefined();
});

it('should include init', () => {
expect(translate.init).toBeDefined();
});

it('should include string', () => {
expect(translate.string).toBeDefined();
});

it('should include array', () => {
expect(translate.object).toBeDefined();
});
});

describe('Yandex translate.object: ', () => {
it('should fail due missing language param', () => {
translate.init({yandexApiKey: 'yandex_token'});
return translate
.object(null, {}, {}, [], ['list', 'of', 'elements'])
.catch(err => {
expect(err.status).toBe(404);
expect(err.error).toBe('Invalid language');
});
});

it('should failed due to invalid value', () => {
translate.init({yandexApiKey: 'yandex_token'});
return translate
.object('ar', {}, {}, [], [])
.catch(err => {
expect(err).toBe('Something went wrong');
});
});
});

describe('Yandex translate.string: ', () => {
it('should fail due missing language param', () => {
translate.init({yandexApiKey: 'yandex_token'});
return translate
.string(null, 'key', {}, '')
.catch(err => {
expect(err.status).toBe(404);
expect(err.error).toBe('Invalid language');
});
});

it('should failed due to invalid value', () => {
translate.init({yandexApiKey: 'yandex_token'});
return translate
.string('es', 'key', {}, null)
.catch(err => {
expect(err).toBe('Something went wrong');
});
});
});

7 changes: 4 additions & 3 deletions lib/translate-json-object.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var _ = require('lodash');
var Promise = require('promise');
var API_MISSED_ERROR_MESSAGE = 'Please provide a option.googleApiKey, or option.yandexApiKey (token key) via invoking the tjo.init()';

var MISSING_TOKEN_ERROR_MESSAGE = 'Please provide a option.googleApiKey, or option.yandexApiKey (token key) via invoking the [init] method';

/**
* TranslateJSONObject - A Node.js module to translate a JSON object from a detectable language to any other language currently via google or yandex translate API
Expand All @@ -25,7 +26,7 @@ function TranslateJSONObject() {
setting = options || {};

if (!setting.googleApiKey && !setting.yandexApiKey) {
console.warn(API_MISSED_ERROR_MESSAGE);
console.warn(MISSING_TOKEN_ERROR_MESSAGE);
return false;
} else if (setting.yandexApiKey) {
translateSrv = require('./service/yandex.js');
Expand All @@ -46,7 +47,7 @@ function TranslateJSONObject() {
*/
function translate(srcObj, language) {
if (!setting.googleApiKey && !setting.yandexApiKey) {
return Promise.reject(API_MISSED_ERROR_MESSAGE);
return Promise.reject(MISSING_TOKEN_ERROR_MESSAGE);
}

if (!_.isString(language)) {
Expand Down

0 comments on commit 325f7af

Please sign in to comment.