parent
0f0dbee7de
commit
9c13ee6099
3 changed files with 282 additions and 15 deletions
tests/client/interactionCreate
|
@ -1,9 +1,168 @@
|
|||
import { ChatInputCommandInteraction } from 'discord.js';
|
||||
import ChatInputComamnd from '../../../src/client/interactionCreate/chatInputCommand';
|
||||
import { CoreClient } from '../../../src/client/client';
|
||||
import ICommandItem from '../../../src/contracts/ICommandItem';
|
||||
|
||||
beforeEach(() => {
|
||||
CoreClient.commandItems = [];
|
||||
});
|
||||
|
||||
describe('onChatInput', () => {
|
||||
test.todo("GIVEN command is registered globally AND command is found, execute the global command");
|
||||
test("GIVEN command is registered globally AND command is found, execute the global command", async () => {
|
||||
// Arrange
|
||||
const interaction = {
|
||||
reply: jest.fn(),
|
||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
||||
commandName: "test",
|
||||
} as unknown as ChatInputCommandInteraction;
|
||||
|
||||
test.todo("GIVEN command is registered to a single server, execute the server command");
|
||||
const testCommand: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: {
|
||||
CommandBuilder: jest.fn(),
|
||||
execute: jest.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
test.todo("GIVEN interaction is not a chat input command, EXPECT nothing to happen");
|
||||
const testServerCommand: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: {
|
||||
CommandBuilder: jest.fn(),
|
||||
execute: jest.fn(),
|
||||
},
|
||||
ServerId: "123",
|
||||
};
|
||||
|
||||
test.todo("GIVEN command is registered globally AND command is not found, EXPECT error");
|
||||
CoreClient.commandItems = [ testCommand, testServerCommand ];
|
||||
|
||||
// Act
|
||||
await ChatInputComamnd.onChatInput(interaction);
|
||||
|
||||
// Assert
|
||||
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
|
||||
expect(interaction.reply).not.toHaveBeenCalled();
|
||||
|
||||
expect(testCommand.Command.execute).toHaveBeenCalledTimes(1);
|
||||
expect(testCommand.Command.execute).toHaveBeenCalledWith(interaction);
|
||||
expect(testServerCommand.Command.execute).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("GIVEN command is registered to a single server, execute the server command", async () => {
|
||||
// Arrange
|
||||
const interaction = {
|
||||
reply: jest.fn(),
|
||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
||||
commandName: "test",
|
||||
guildId: "123",
|
||||
} as unknown as ChatInputCommandInteraction;
|
||||
|
||||
const testCommand: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: {
|
||||
CommandBuilder: jest.fn(),
|
||||
execute: jest.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
const testServerCommand: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: {
|
||||
CommandBuilder: jest.fn(),
|
||||
execute: jest.fn(),
|
||||
},
|
||||
ServerId: "123",
|
||||
};
|
||||
|
||||
CoreClient.commandItems = [ testCommand, testServerCommand ];
|
||||
|
||||
// Act
|
||||
await ChatInputComamnd.onChatInput(interaction);
|
||||
|
||||
// Assert
|
||||
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
|
||||
expect(interaction.reply).not.toHaveBeenCalled();
|
||||
|
||||
expect(testServerCommand.Command.execute).toHaveBeenCalledTimes(1);
|
||||
expect(testServerCommand.Command.execute).toHaveBeenCalledWith(interaction);
|
||||
expect(testCommand.Command.execute).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("GIVEN interaction is not a chat input command, EXPECT nothing to happen", async () => {
|
||||
// Arrange
|
||||
const interaction = {
|
||||
reply: jest.fn(),
|
||||
isChatInputCommand: jest.fn().mockReturnValue(false),
|
||||
commandName: "test",
|
||||
guildId: "123",
|
||||
} as unknown as ChatInputCommandInteraction;
|
||||
|
||||
const testCommand: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: {
|
||||
CommandBuilder: jest.fn(),
|
||||
execute: jest.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
const testServerCommand: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: {
|
||||
CommandBuilder: jest.fn(),
|
||||
execute: jest.fn(),
|
||||
},
|
||||
ServerId: "123",
|
||||
};
|
||||
|
||||
CoreClient.commandItems = [ testCommand, testServerCommand ];
|
||||
|
||||
// Act
|
||||
await ChatInputComamnd.onChatInput(interaction);
|
||||
|
||||
// Assert
|
||||
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
|
||||
expect(interaction.reply).not.toHaveBeenCalled();
|
||||
|
||||
expect(testServerCommand.Command.execute).not.toHaveBeenCalled();
|
||||
expect(testCommand.Command.execute).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("GIVEN command is registered globally AND command is not found, EXPECT error", async () => {
|
||||
// Arrange
|
||||
const interaction = {
|
||||
reply: jest.fn(),
|
||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
||||
commandName: "other",
|
||||
guildId: "123",
|
||||
} as unknown as ChatInputCommandInteraction;
|
||||
|
||||
const testCommand: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: {
|
||||
CommandBuilder: jest.fn(),
|
||||
execute: jest.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
const testServerCommand: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: {
|
||||
CommandBuilder: jest.fn(),
|
||||
execute: jest.fn(),
|
||||
},
|
||||
ServerId: "123",
|
||||
};
|
||||
|
||||
CoreClient.commandItems = [ testCommand, testServerCommand ];
|
||||
|
||||
// Act
|
||||
await ChatInputComamnd.onChatInput(interaction);
|
||||
|
||||
// Assert
|
||||
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
|
||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||
expect(interaction.reply).toHaveBeenCalledWith("Command not found.");
|
||||
|
||||
expect(testServerCommand.Command.execute).not.toHaveBeenCalled();
|
||||
expect(testCommand.Command.execute).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue