-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from ttsukagoshi/update-jest-tests
Update jest tests
- Loading branch information
Showing
3 changed files
with
408 additions
and
6 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,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.'), | ||
); | ||
}, | ||
); | ||
}); |
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
Oops, something went wrong.