Initial commit
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
// Test utilities for Discord.js bot testing
|
||||
|
||||
/**
|
||||
* Creates a mock interaction object with common properties and methods
|
||||
* @param {Object} options - Customization options for the mock interaction
|
||||
* @returns {Object} Mock interaction object
|
||||
*/
|
||||
const createMockInteraction = (options = {}) => ({
|
||||
commandName: options.commandName || 'test-command',
|
||||
user: {
|
||||
id: options.userId || 'mock-user-id',
|
||||
username: options.username || 'MockUser',
|
||||
tag: options.userTag || 'MockUser#0000',
|
||||
},
|
||||
guild: {
|
||||
id: options.guildId || 'mock-guild-id',
|
||||
name: options.guildName || 'Mock Guild',
|
||||
},
|
||||
channel: {
|
||||
id: options.channelId || 'mock-channel-id',
|
||||
name: options.channelName || 'mock-channel',
|
||||
send: jest.fn().mockResolvedValue({ id: 'mock-message-id' }),
|
||||
bulkDelete: jest.fn().mockResolvedValue({ size: 0 }),
|
||||
},
|
||||
client: {
|
||||
commands: new Map(),
|
||||
user: {
|
||||
id: 'mock-client-id',
|
||||
username: 'MockBot',
|
||||
setActivity: jest.fn(),
|
||||
},
|
||||
},
|
||||
reply: jest.fn().mockImplementation(async _response => {
|
||||
if (options.replyFails) {
|
||||
throw new Error('Failed to reply');
|
||||
}
|
||||
return { id: 'mock-reply-id' };
|
||||
}),
|
||||
deferReply: jest.fn().mockImplementation(async () => {
|
||||
if (options.deferFails) {
|
||||
throw new Error('Failed to defer reply');
|
||||
}
|
||||
return true;
|
||||
}),
|
||||
editReply: jest.fn().mockResolvedValue({ id: 'mock-edit-id' }),
|
||||
followUp: jest.fn().mockImplementation(async _response => {
|
||||
if (options.followUpFails) {
|
||||
throw new Error('Failed to follow up');
|
||||
}
|
||||
return { id: 'mock-followup-id' };
|
||||
}),
|
||||
options: {
|
||||
getString: jest.fn(name => options.stringOptions?.[name]),
|
||||
getInteger: jest.fn(name => options.integerOptions?.[name]),
|
||||
getBoolean: jest.fn(name => options.booleanOptions?.[name]),
|
||||
getUser: jest.fn(name => options.userOptions?.[name]),
|
||||
getMember: jest.fn(name => options.memberOptions?.[name]),
|
||||
get: jest.fn(name => options.options?.[name]),
|
||||
},
|
||||
member: {
|
||||
permissions: {
|
||||
has: jest.fn().mockReturnValue(options.hasPermission ?? true),
|
||||
},
|
||||
roles: {
|
||||
cache: new Map(),
|
||||
},
|
||||
},
|
||||
isCommand: () => true,
|
||||
isChatInputCommand: () => true,
|
||||
});
|
||||
|
||||
/**
|
||||
* Creates a mock client object with common properties and methods
|
||||
* @param {Object} options - Customization options for the mock client
|
||||
* @returns {Object} Mock client object
|
||||
*/
|
||||
const createMockClient = (options = {}) => ({
|
||||
user: {
|
||||
id: options.clientId || 'mock-client-id',
|
||||
username: options.clientUsername || 'MockBot',
|
||||
setActivity: jest.fn(),
|
||||
},
|
||||
guilds: {
|
||||
cache: new Map(),
|
||||
},
|
||||
commands: new Map(),
|
||||
on: jest.fn(),
|
||||
once: jest.fn(),
|
||||
login: jest.fn().mockResolvedValue('token'),
|
||||
destroy: jest.fn().mockResolvedValue(),
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
createMockInteraction,
|
||||
createMockClient,
|
||||
};
|
||||
@@ -0,0 +1,61 @@
|
||||
const { createMockInteraction, createMockClient } = require('./testUtils');
|
||||
|
||||
describe('Test Utilities', () => {
|
||||
describe('createMockInteraction', () => {
|
||||
it('should create a mock interaction with default values', () => {
|
||||
const interaction = createMockInteraction();
|
||||
|
||||
expect(interaction.commandName).toBe('test-command');
|
||||
expect(interaction.user.id).toBe('mock-user-id');
|
||||
expect(interaction.guild.id).toBe('mock-guild-id');
|
||||
expect(interaction.channel.id).toBe('mock-channel-id');
|
||||
expect(typeof interaction.reply).toBe('function');
|
||||
expect(typeof interaction.deferReply).toBe('function');
|
||||
expect(typeof interaction.editReply).toBe('function');
|
||||
expect(typeof interaction.followUp).toBe('function');
|
||||
});
|
||||
|
||||
it('should create a mock interaction with custom values', () => {
|
||||
const customOptions = {
|
||||
commandName: 'custom-command',
|
||||
userId: 'custom-user-id',
|
||||
username: 'CustomUser',
|
||||
guildId: 'custom-guild-id',
|
||||
channelId: 'custom-channel-id',
|
||||
};
|
||||
|
||||
const interaction = createMockInteraction(customOptions);
|
||||
|
||||
expect(interaction.commandName).toBe('custom-command');
|
||||
expect(interaction.user.id).toBe('custom-user-id');
|
||||
expect(interaction.user.username).toBe('CustomUser');
|
||||
expect(interaction.guild.id).toBe('custom-guild-id');
|
||||
expect(interaction.channel.id).toBe('custom-channel-id');
|
||||
});
|
||||
});
|
||||
|
||||
describe('createMockClient', () => {
|
||||
it('should create a mock client with default values', () => {
|
||||
const client = createMockClient();
|
||||
|
||||
expect(client.user.id).toBe('mock-client-id');
|
||||
expect(client.user.username).toBe('MockBot');
|
||||
expect(typeof client.login).toBe('function');
|
||||
expect(typeof client.destroy).toBe('function');
|
||||
expect(typeof client.on).toBe('function');
|
||||
expect(typeof client.once).toBe('function');
|
||||
});
|
||||
|
||||
it('should create a mock client with custom values', () => {
|
||||
const customOptions = {
|
||||
clientId: 'custom-client-id',
|
||||
clientUsername: 'CustomBot',
|
||||
};
|
||||
|
||||
const client = createMockClient(customOptions);
|
||||
|
||||
expect(client.user.id).toBe('custom-client-id');
|
||||
expect(client.user.username).toBe('CustomBot');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user