-
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.
Add new tests and refactor existing ones
- Loading branch information
1 parent
e1a5ef6
commit b7ef3ed
Showing
11 changed files
with
315 additions
and
510 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,35 @@ | ||
import { deepLGetLanguages } from '../src/sheetsl'; | ||
|
||
describe('deepLGetLanguages', () => { | ||
beforeEach(() => { | ||
global.UrlFetchApp = { | ||
fetch: jest.fn(() => ({ | ||
getContentText: jest.fn(() => | ||
JSON.stringify({ | ||
languages: [ | ||
{ language: 'EN', name: 'English', supportsFormality: true }, | ||
{ language: 'DE', name: 'German', supportsFormality: true }, | ||
], | ||
}), | ||
), | ||
getResponseCode: jest.fn(() => 200), | ||
})), | ||
} as unknown as GoogleAppsScript.URL_Fetch.UrlFetchApp; | ||
global.PropertiesService = { | ||
getUserProperties: jest.fn(() => ({ | ||
getProperty: jest.fn(() => 'SampleApiKey:fx'), | ||
})), | ||
} as unknown as GoogleAppsScript.Properties.PropertiesService; | ||
}); | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
it('should return the list of supported languages', () => { | ||
expect(deepLGetLanguages()).toEqual({ | ||
languages: [ | ||
{ language: 'EN', name: 'English', supportsFormality: true }, | ||
{ language: 'DE', name: 'German', supportsFormality: true }, | ||
], | ||
}); | ||
}); | ||
}); |
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,41 @@ | ||
import { deleteDeeplApiKey } from '../src/sheetsl'; | ||
|
||
describe('deleteDeeplApiKey', () => { | ||
beforeEach(() => { | ||
class MockUi { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
constructor() {} | ||
alert: jest.Mock = jest.fn(); | ||
} | ||
global.SpreadsheetApp = { | ||
getUi: jest.fn(() => new MockUi() as unknown as GoogleAppsScript.Base.Ui), | ||
} as unknown as GoogleAppsScript.Spreadsheet.SpreadsheetApp; | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
}); | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
it('should successfully delete the DeepL API key', () => { | ||
global.PropertiesService = { | ||
getUserProperties: jest.fn(() => ({ | ||
deleteProperty: jest.fn(), | ||
})), | ||
} as unknown as GoogleAppsScript.Properties.PropertiesService; | ||
deleteDeeplApiKey(); | ||
expect(PropertiesService.getUserProperties).toHaveBeenCalled(); | ||
expect(console.error).not.toHaveBeenCalled(); | ||
}); | ||
it('should catch an error if something went wrong', () => { | ||
global.PropertiesService = { | ||
getUserProperties: jest.fn(() => ({ | ||
deleteProperty: jest.fn(() => { | ||
throw new Error('Test error'); | ||
}), | ||
})), | ||
} as unknown as GoogleAppsScript.Properties.PropertiesService; | ||
deleteDeeplApiKey(); | ||
expect(PropertiesService.getUserProperties).toHaveBeenCalled(); | ||
expect(console.error).toHaveBeenCalled(); | ||
}); | ||
}); |
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,15 @@ | ||
import { getBlobBytes } from '../src/sheetsl'; | ||
|
||
global.Utilities = { | ||
newBlob: jest.fn((text: string) => ({ | ||
getBytes: jest.fn(() => ({ | ||
length: text.length, | ||
})), | ||
})), | ||
} as unknown as GoogleAppsScript.Utilities.Utilities; | ||
|
||
describe('getBlobBytes', () => { | ||
it('should return the length of the given string in bytes', () => { | ||
expect(getBlobBytes('test string')).toBe(11); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,21 +1,29 @@ | ||
import { getDeepLApiKey } from '../src/sheetsl'; | ||
|
||
const ADDON_NAME = 'SheetsL'; | ||
|
||
PropertiesService.getUserProperties = jest.fn(() => ({ | ||
getProperty: jest.fn(() => undefined), | ||
})) as any; | ||
|
||
const testObj = { | ||
title: | ||
'Case when DeepL API Authentication Key is not saved in the user property', | ||
errorMessage: `[${ADDON_NAME}] API Key Unavailable: Set the DeepL API Authentication Key from the Settings > Set Auth Key of the add-on menu.`, | ||
}; | ||
|
||
describe('getDeepLApiKey Error', () => { | ||
test(testObj.title, () => { | ||
expect(() => { | ||
getDeepLApiKey(); | ||
}).toThrowError(new Error(testObj.errorMessage)); | ||
describe('getDeepLApiKey', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
it('should get the DeepL API key from the user properties and return it', () => { | ||
const mockApiKey = 'Sample-API-key:fx'; | ||
global.PropertiesService = { | ||
getUserProperties: jest.fn(() => ({ | ||
getProperty: jest.fn(() => mockApiKey), | ||
})), | ||
} as unknown as GoogleAppsScript.Properties.PropertiesService; | ||
const result = getDeepLApiKey(); | ||
expect(result).toBe(mockApiKey); | ||
}); | ||
it('should throw an error if the DeepL API key is not saved in the user properties', () => { | ||
global.PropertiesService = { | ||
getUserProperties: jest.fn(() => ({ | ||
getProperty: jest.fn(() => null), | ||
})), | ||
} as unknown as GoogleAppsScript.Properties.PropertiesService; | ||
expect(() => getDeepLApiKey()).toThrow( | ||
new Error( | ||
'[SheetsL] API Key Unavailable: Set the DeepL API Authentication Key from the Settings > Set Auth Key of the add-on menu.', | ||
), | ||
); | ||
}); | ||
}); |
Oops, something went wrong.