Skip to content

Commit

Permalink
chore: Update handleMessageCreate to conditionally call fetch based o…
Browse files Browse the repository at this point in the history
…n environment variable
  • Loading branch information
k-taro56 committed May 31, 2024
1 parent b711251 commit ddecacb
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
});

Expand All @@ -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 ?? '',
);
Expand Down

0 comments on commit ddecacb

Please sign in to comment.