Skip to content

Commit

Permalink
Merge pull request #150 from ttsukagoshi/update-jest-tests
Browse files Browse the repository at this point in the history
Update jest tests
  • Loading branch information
ttsukagoshi authored Feb 7, 2024
2 parents eb16c11 + 6414950 commit 12590d1
Show file tree
Hide file tree
Showing 3 changed files with 408 additions and 6 deletions.
86 changes: 86 additions & 0 deletions tests/deepLTranslate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { deepLTranslate } from '../src/sheetsl';

describe('deepLTranslate', () => {
beforeEach(() => {
global.PropertiesService = {
getUserProperties: jest.fn(() => ({
getProperty: jest.fn(() => 'SampleApiKey:fx'),
})),
} as unknown as GoogleAppsScript.Properties.PropertiesService;
global.encodeURIComponent = jest.fn(
(text: string) => text,
) as unknown as typeof encodeURIComponent;
});
afterEach(() => {
jest.clearAllMocks();
});
const mockSourceObjects = [
{
note: 'with source language specified',
sourceLang: 'EN-US',
targetLang: 'DE',
sourceText: 'Hello, World!',
translatedText: 'Hallo, Welt!',
},
{
note: 'without source language specified',
sourceLang: null,
targetLang: 'DE',
sourceText: 'Hello, World!',
translatedText: 'Hallo, Welt!',
},
{
note: 'in an array of strings',
sourceLang: null,
targetLang: 'DE',
sourceText: ['Hello, World!', 'Hello, World!'],
translatedText: ['Hallo, Welt!', 'Hallo, Welt!'],
},
];
it.each(mockSourceObjects)(
'should translate text $note',
// eslint-disable-next-line @typescript-eslint/no-unused-vars
({ note, sourceLang, targetLang, sourceText, translatedText }) => {
global.UrlFetchApp = {
fetch: jest.fn(() => ({
getContentText: jest.fn(() =>
JSON.stringify({
translations: Array.isArray(translatedText)
? translatedText.map((text) => ({ text: text }))
: [{ text: translatedText }],
}),
),
getResponseCode: jest.fn(() => 200),
})),
} as unknown as GoogleAppsScript.URL_Fetch.UrlFetchApp;
const translated = deepLTranslate(sourceText, sourceLang, targetLang);
expect(translated).toStrictEqual(
Array.isArray(translatedText) ? translatedText : [translatedText],
);
expect(global.UrlFetchApp.fetch).toHaveBeenCalled();
},
);
const mockSourceObjectsError = [
{
note: 'when source text is null',
sourceLang: 'EN-US',
targetLang: 'DE',
sourceText: null,
},
{
note: 'when source text is empty',
sourceLang: 'EN-US',
targetLang: 'DE',
sourceText: '',
},
];
it.each(mockSourceObjectsError)(
'should throw an error $note',
// eslint-disable-next-line @typescript-eslint/no-unused-vars
({ note, sourceLang, targetLang, sourceText }) => {
expect(() => deepLTranslate(sourceText, sourceLang, targetLang)).toThrow(
new Error('[SheetsL] Empty input.'),
);
},
);
});
8 changes: 2 additions & 6 deletions tests/getDeepLApiBaseUrl.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { getDeepLApiBaseUrl } from '../src/sheetsl';

const DEEPL_API_VERSION = 'v2';
const DEEPL_API_BASE_URL_FREE = `https://api-free.deepl.com/${DEEPL_API_VERSION}/`;
const DEEPL_API_BASE_URL_PRO = `https://api.deepl.com/${DEEPL_API_VERSION}/`;

const patterns = [
{
title: 'DeepL API Free account',
input: 'xxxxxxxxxxx:fx',
expectedOutput: DEEPL_API_BASE_URL_FREE,
expectedOutput: 'https://api-free.deepl.com/v2/',
},
{
title: 'DeepL API Pro account',
input: 'xxxxxxxxxxx',
expectedOutput: DEEPL_API_BASE_URL_PRO,
expectedOutput: 'https://api.deepl.com/v2/',
},
];

Expand Down
Loading

0 comments on commit 12590d1

Please sign in to comment.