Skip to content
Open
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
16 changes: 16 additions & 0 deletions app/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
export default {
preset: 'ts-jest',
testEnvironment: 'jsdom',
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
},
transform: {
'^.+\\.tsx?$': ['ts-jest', {
useESM: true,
}],
},
extensionsToTreatAsEsm: ['.ts'],
moduleFileExtensions: ['ts', 'js', 'json', 'node'],
testMatch: ['**/__tests__/**/*.test.ts'],
};
6 changes: 5 additions & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
"lint:eslint": "eslint --ext \".js,.ts,.html\"",
"fix:eslint": "npm run lint:eslint --fix",
"prettier": "prettier --write .",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest"
},
"devDependencies": {
"@google/generative-ai": "^0.2.1",
"@types/google-apps-script": "^1.0.82",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.20",
"@typescript-eslint/eslint-plugin": "^7.0.2",
"@typescript-eslint/parser": "^7.0.2",
Expand All @@ -31,6 +32,9 @@
"eslint-plugin-typescript-sort-keys": "^3.1.0",
"eslint-plugin-unused-imports": "^3.1.0",
"gas-webpack-plugin": "^2.5.0",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"ts-jest": "^29.1.2",
"html-webpack-plugin": "^5.6.0",
"prettier": "^3.2.5",
"terser-webpack-plugin": "^5.3.10",
Expand Down
48 changes: 48 additions & 0 deletions app/src/__mocks__/google-apps-script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Google Apps Scriptのグローバルモック

// PropertiesServiceのモック
const scriptProperties = {
properties: new Map<string, string>(),
getProperty: function(key: string) {
return this.properties.get(key) || null;
},
setProperty: function(key: string, value: string) {
this.properties.set(key, value);
return this;
},
};

export const PropertiesService = {
getScriptProperties: () => scriptProperties,
};

// Loggerのモック
export const Logger = {
log: jest.fn(),
};

// SpreadsheetAppのモック
export const SpreadsheetApp = {
getUi: jest.fn().mockReturnValue({
createMenu: jest.fn().mockReturnThis(),
addItem: jest.fn().mockReturnThis(),
addToUi: jest.fn(),
showModalDialog: jest.fn(),
}),
};

// HtmlServiceのモック
export const HtmlService = {
createHtmlOutputFromFile: jest.fn().mockReturnValue({
setWidth: jest.fn().mockReturnThis(),
setHeight: jest.fn().mockReturnThis(),
}),
};

// グローバルオブジェクト
export const global = {
onOpen: jest.fn(),
showApiKeyInputForm: jest.fn(),
saveApiKey: jest.fn(),
GEMINI: jest.fn(),
};
75 changes: 75 additions & 0 deletions app/src/lib/__tests__/properties.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import properties from '../properties';
import { PropertiesService } from '@/__mocks__/google-apps-script';

// PropertiesServiceのモックをスパイ
const getPropertySpy = jest.spyOn(PropertiesService.getScriptProperties(), 'getProperty');
const setPropertySpy = jest.spyOn(PropertiesService.getScriptProperties(), 'setProperty');

describe('properties', () => {
beforeEach(() => {
// テスト前にスパイをリセット
getPropertySpy.mockClear();
setPropertySpy.mockClear();

// プロパティをクリア
const scriptProperties = PropertiesService.getScriptProperties();
(scriptProperties as any).properties.clear();
});

describe('get', () => {
it('指定したキーの値を取得できること', () => {
// テスト用のデータをセット
const scriptProperties = PropertiesService.getScriptProperties();
(scriptProperties as any).properties.set('testKey', 'testValue');

// テスト対象の関数を実行
const result = properties.get('testKey');

// 期待する結果を検証
expect(result).toBe('testValue');
expect(getPropertySpy).toHaveBeenCalledWith('testKey');
expect(getPropertySpy).toHaveBeenCalledTimes(1);
});

it('存在しないキーの場合はnullを返すこと', () => {
// テスト対象の関数を実行
const result = properties.get('nonExistentKey');

// 期待する結果を検証
expect(result).toBeNull();
expect(getPropertySpy).toHaveBeenCalledWith('nonExistentKey');
expect(getPropertySpy).toHaveBeenCalledTimes(1);
});
});

describe('set', () => {
it('指定したキーに値をセットできること', () => {
// テスト対象の関数を実行
const result = properties.set('newKey', 'newValue');

// 期待する結果を検証
expect(setPropertySpy).toHaveBeenCalledWith('newKey', 'newValue');
expect(setPropertySpy).toHaveBeenCalledTimes(1);

// 実際に値がセットされたことを確認
const scriptProperties = PropertiesService.getScriptProperties();
expect((scriptProperties as any).properties.get('newKey')).toBe('newValue');
});

it('既存のキーの値を上書きできること', () => {
// テスト用のデータをセット
const scriptProperties = PropertiesService.getScriptProperties();
(scriptProperties as any).properties.set('existingKey', 'oldValue');

// テスト対象の関数を実行
const result = properties.set('existingKey', 'updatedValue');

// 期待する結果を検証
expect(setPropertySpy).toHaveBeenCalledWith('existingKey', 'updatedValue');
expect(setPropertySpy).toHaveBeenCalledTimes(1);

// 実際に値が上書きされたことを確認
expect((scriptProperties as any).properties.get('existingKey')).toBe('updatedValue');
});
});
});