From ee7f83dc3fa8b93dfa9db007400075945d0d2ed4 Mon Sep 17 00:00:00 2001 From: sakaritoru <129810356+sakaritoru@users.noreply.github.com> Date: Tue, 15 Apr 2025 10:45:30 +0900 Subject: [PATCH] feat: add cache.ts tests - Add Jest configuration - Add test setup file - Add cache.ts tests with 100% coverage Resolves #11 --- app/jest.config.js | 17 ++++++ app/src/lib/__tests__/cache.test.ts | 83 +++++++++++++++++++++++++++++ app/src/setupTests.ts | 2 + 3 files changed, 102 insertions(+) create mode 100644 app/jest.config.js create mode 100644 app/src/lib/__tests__/cache.test.ts create mode 100644 app/src/setupTests.ts diff --git a/app/jest.config.js b/app/jest.config.js new file mode 100644 index 0000000..7d0d84d --- /dev/null +++ b/app/jest.config.js @@ -0,0 +1,17 @@ +/** @type {import('ts-jest').JestConfigWithTsJest} */ +module.exports = { + globals: { + 'ts-jest': { + tsconfig: 'tsconfig.json' + } + }, + moduleDirectories: ['node_modules', 'src'], + preset: 'ts-jest', + roots: ['/src'], + setupFiles: ['/src/setupTests.ts'], + testEnvironment: 'node', + testMatch: ['**/__tests__/**/*.test.ts'], + transform: { + '^.+\.tsx?$': 'ts-jest' + } +} \ No newline at end of file diff --git a/app/src/lib/__tests__/cache.test.ts b/app/src/lib/__tests__/cache.test.ts new file mode 100644 index 0000000..9717f80 --- /dev/null +++ b/app/src/lib/__tests__/cache.test.ts @@ -0,0 +1,83 @@ +import cache from '../cache' + +// Google Apps ScriptのCacheServiceのモック +const mockPut = jest.fn() +const mockGet = jest.fn() + +// グローバルなCacheServiceのモック +;(global as any).CacheService = { + getDocumentCache: () => ({ + get: mockGet, + put: mockPut, + }), +} + +describe('cache', () => { + beforeEach(() => { + // 各テストの前にモックをリセット + mockPut.mockClear() + mockGet.mockClear() + }) + + describe('get', () => { + it('キャッシュから値を取得できる', () => { + const testKey = 'testKey' + const testValue = 'testValue' + mockGet.mockReturnValue(testValue) + + const result = cache(testKey).get() + + expect(result).toBe(testValue) + expect(mockGet).toHaveBeenCalledWith(testKey) + }) + + it('キャッシュが存在しない場合はundefinedを返す', () => { + const testKey = 'nonExistentKey' + mockGet.mockReturnValue(null) + + const result = cache(testKey).get() + + expect(result).toBeNull() + expect(mockGet).toHaveBeenCalledWith(testKey) + }) + }) + + describe('set', () => { + it('デフォルトの有効期限でキャッシュを設定できる', () => { + const testKey = 'testKey' + const testValue = 'testValue' + const defaultExpire = 24 * 60 * 60 * 1000 // 24時間(ミリ秒) + + cache(testKey).set(testValue) + + expect(mockPut).toHaveBeenCalledWith(testKey, testValue, defaultExpire) + }) + + it('カスタムの有効期限でキャッシュを設定できる', () => { + const testKey = 'testKey' + const testValue = 'testValue' + const customExpire = 60 * 60 * 1000 // 1時間(ミリ秒) + + cache(testKey, customExpire).set(testValue) + + expect(mockPut).toHaveBeenCalledWith(testKey, testValue, customExpire) + }) + }) + + describe('エラーケース', () => { + it('CacheServiceが利用できない場合でもエラーにならない', () => { + // CacheServiceをnullに設定 + global.CacheService = null as any + + const testKey = 'testKey' + const testValue = 'testValue' + + // エラーが発生しないことを確認 + expect(() => { + const cacheInstance = cache(testKey) + cacheInstance.set(testValue) + cacheInstance.get() + }).not.toThrow() + }) + }) +}) \ No newline at end of file diff --git a/app/src/setupTests.ts b/app/src/setupTests.ts new file mode 100644 index 0000000..68a8d6a --- /dev/null +++ b/app/src/setupTests.ts @@ -0,0 +1,2 @@ +// テストのグローバル設定をここに記述 +export {} \ No newline at end of file