Skip to content

Commit

Permalink
Add new tests and refactor existing ones
Browse files Browse the repository at this point in the history
resolve #86 and #87
  • Loading branch information
ttsukagoshi committed Feb 7, 2024
1 parent e1a5ef6 commit b7ef3ed
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 510 deletions.
213 changes: 0 additions & 213 deletions tests/deepL.test.ts

This file was deleted.

35 changes: 35 additions & 0 deletions tests/deepLGetLanguages.test.ts
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 },
],
});
});
});
41 changes: 41 additions & 0 deletions tests/deleteDeeplApiKey.test.ts
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();
});
});
15 changes: 15 additions & 0 deletions tests/getBlobBytes.test.ts
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);
});
});
20 changes: 0 additions & 20 deletions tests/getBytes.test.ts

This file was deleted.

42 changes: 25 additions & 17 deletions tests/getDeepLApiKey.test.ts
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.',
),
);
});
});
Loading

0 comments on commit b7ef3ed

Please sign in to comment.