From ddecacb79d4dd40fa0e8c005274eae80e017497e Mon Sep 17 00:00:00 2001 From: k-taro56 <121674121+k-taro56@users.noreply.github.com> Date: Sat, 1 Jun 2024 01:42:16 +0900 Subject: [PATCH] chore: Update handleMessageCreate to conditionally call fetch based on environment variable --- test/index.test.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/test/index.test.ts b/test/index.test.ts index 78aa669..17bb558 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -19,8 +19,13 @@ const expectReactionsToHaveBeenCalled = (mockReact: jest.Mock) => { expect(mockReact).toHaveBeenCalledWith('🔥'); }; +const isSetEnv = (envVar: string) => { + return process.env[envVar] !== undefined; +}; + describe('handleMessageCreate', () => { global.fetch = jest.fn(); + const isSetAuditLogWebHook = isSetEnv('AUDIT_LOG_WEBHOOK'); const mockReact = jest.fn(); const mockReply = jest.fn(); const mockDisplayAvatarURL = jest.fn(); @@ -59,7 +64,11 @@ describe('handleMessageCreate', () => { const message = createMockMessage('Hello 代表', true, ChannelType.DM); await handleMessageCreateCurried(message); - expect(fetch).toHaveBeenCalled(); + if (isSetAuditLogWebHook) { + expect(fetch).toHaveBeenCalled(); + } else { + expect(fetch).not.toHaveBeenCalled(); + } expectReactionsToHaveBeenCalled(mockReact); }); @@ -79,7 +88,11 @@ describe('handleMessageCreate', () => { const message = createMockMessage('Hello', false, ChannelType.DM); await handleMessageCreateCurried(message); - expect(fetch).toHaveBeenCalled(); + if (isSetAuditLogWebHook) { + expect(fetch).toHaveBeenCalled(); + } else { + expect(fetch).not.toHaveBeenCalled(); + } expect(mockReply).toHaveBeenCalledWith( process.env.DM_MESSAGE_CONTENT ?? '', );