From db71c76199c91ed2ae2f334e52b41bcbcba4cbd4 Mon Sep 17 00:00:00 2001 From: ttsukagoshi Date: Mon, 15 May 2023 02:42:35 +0900 Subject: [PATCH 1/2] Add SpreadsheetApp to globals --- jest.config.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/jest.config.js b/jest.config.js index d1d8efe..1fb9bf1 100644 --- a/jest.config.js +++ b/jest.config.js @@ -9,11 +9,8 @@ module.exports = { coverageDirectory: 'coverage', coverageProvider: 'v8', globals: { - /* - 'ts-jest': { - diagnostics: false, - },*/ PropertiesService: {}, + SpreadsheetApp: {}, UrlFetchApp: {}, Utilities: {}, }, From 92d50f268149d0e4748eb96e5db77853ea249f0a Mon Sep 17 00:00:00 2001 From: ttsukagoshi Date: Mon, 15 May 2023 02:42:56 +0900 Subject: [PATCH 2/2] Add test for onOpen and onInstall --- __tests__/onInstall.test.ts | 57 ------------------ __tests__/onInstallonOpen.test.ts | 98 +++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 57 deletions(-) delete mode 100644 __tests__/onInstall.test.ts create mode 100644 __tests__/onInstallonOpen.test.ts diff --git a/__tests__/onInstall.test.ts b/__tests__/onInstall.test.ts deleted file mode 100644 index c6af9f4..0000000 --- a/__tests__/onInstall.test.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { onInstall } from '../src/sheetsl'; - -const ADDON_NAME = 'SheetsL'; - -SpreadsheetApp.getUi = jest.fn(() => ({ - createAddonMenu: jest.fn(() => new MockMenu(true)), - createMenu: jest.fn((title: string) => new MockMenu(false, title)), -})) as any; - -class MockMenu { - mockMenu: MockMenuObj; - isAddonMenu: boolean; - constructor(isAddonMenu: boolean, title: string = '') { - this.mockMenu = { - title: isAddonMenu ? ADDON_NAME : title, - menu: [], - isAddonMenu: isAddonMenu, - }; - } - addItem(itemName: string, functionName: string): this { - this.mockMenu.menu.push({ itemName: itemName, functionName: functionName }); - return this; - } - addSeparator(): this { - this.mockMenu.menu.push('---'); - return this; - } - addSubMenu(menu: MockMenuObj): this { - this.mockMenu.menu.push(menu); - return this; - } - addToUi(): MockMenuObj { - return this.mockMenu; - } -} - -type MockMenuObj = { - title: string; - menu: any[]; - isAddonMenu: boolean; -}; - -describe('onOpen/onInstall', () => { - test('onOpen/onInstall test', () => { - expect(onInstall).toEqual({ - title: ADDON_NAME, - menu: [ - { - title: 'Settings', - menu: [{ itemName: 'Set Auth Key', functionName: 'setDeeplAuthKey' }], - isAddonMenu: false, - }, - ], - isAddonMenu: true, - }); - }); -}); diff --git a/__tests__/onInstallonOpen.test.ts b/__tests__/onInstallonOpen.test.ts new file mode 100644 index 0000000..c0782c8 --- /dev/null +++ b/__tests__/onInstallonOpen.test.ts @@ -0,0 +1,98 @@ +import { onInstall } from '../src/sheetsl'; + +type MockMenuObj = { + title?: string; + menu: (MockMenuObj | MockMenuItemObj | string)[]; +}; + +type MockMenuItemObj = { + itemName: string; + functionName: string; +}; + +const ADDON_MENU: MockMenuObj = { + menu: [], +}; +const ADDON_SUB_MENU_SETTINGS: MockMenuObj = { + menu: [], +}; +const SEPARATOR = '---'; + +SpreadsheetApp.getUi = jest.fn(() => new MockUi()) as any; + +class MockUi { + isAddonMenu: boolean; + constructor() {} + addItem(itemName: string, functionName: string): this { + if (this.isAddonMenu) { + ADDON_MENU.menu.push({ + itemName: itemName, + functionName: functionName, + }); + } else { + ADDON_SUB_MENU_SETTINGS.menu.push({ + itemName: itemName, + functionName: functionName, + }); + } + return this; + } + addSeparator(): this { + if (this.isAddonMenu) { + ADDON_MENU.menu.push(SEPARATOR); + } else { + ADDON_SUB_MENU_SETTINGS.menu.push(SEPARATOR); + } + return this; + } + addSubMenu(ui: MockUi): this { + ADDON_MENU.menu.push(ADDON_SUB_MENU_SETTINGS); + this.isAddonMenu = true; + return this; + } + addToUi(): void { + console.log(JSON.stringify(ADDON_MENU)); + } + createAddonMenu(): this { + this.isAddonMenu = true; + return this; + } + createMenu(title: string): this { + this.isAddonMenu = false; + ADDON_SUB_MENU_SETTINGS.title = title; + return this; + } +} + +describe('onOpen/onInstall', () => { + test('onOpen/onInstall test', () => { + onInstall(); + expect(ADDON_MENU).toEqual({ + menu: [ + { + title: 'Settings', + menu: [ + { + itemName: 'Set Auth Key', + functionName: 'setDeeplAuthKey', + }, + { + itemName: 'Delete Auth Key', + functionName: 'deleteDeeplAuthKey', + }, + '---', + { + itemName: 'Set Language', + functionName: 'setLanguage', + }, + ], + }, + '---', + { + itemName: 'Translate', + functionName: 'translateRange', + }, + ], + }); + }); +});