这是indexloc提供的服务,不要输入任何密码
Skip to content

Update jest tests #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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