vylbot-app/tests/commands/bunny.test.ts

125 lines
4.2 KiB
TypeScript
Raw Permalink Normal View History

2024-03-30 16:53:18 +00:00
import { CommandInteraction, SlashCommandBuilder } from "discord.js";
import Bunny from "../../src/commands/bunny";
import * as randomBunny from "random-bunny";
import { EmbedBuilder } from "@discordjs/builders";
import EmbedColours from "../../src/constants/EmbedColours";
beforeEach(() => {
jest.resetAllMocks();
jest.resetModules();
})
2024-02-03 19:29:28 +00:00
describe('constructor', () => {
2024-03-30 16:53:18 +00:00
test("EXPECT properties to be set", () => {
const bunny = new Bunny();
expect(bunny.CommandBuilder).toBeDefined();
const commandBuilder = bunny.CommandBuilder as SlashCommandBuilder;
expect(commandBuilder.name).toBe("bunny");
expect(commandBuilder.description).toBe("Get a random picture of a rabbit.");
});
2024-02-03 19:29:28 +00:00
});
describe('execute', () => {
2024-03-30 16:53:18 +00:00
test("EXPECT embed to be sent", async () => {
let repliedWith;
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
2024-05-01 18:36:31 +01:00
editReply: jest.fn().mockImplementation((options) => {
2024-03-30 16:53:18 +00:00
repliedWith = options;
}),
2024-05-01 18:36:31 +01:00
deferReply: jest.fn(),
2024-03-30 16:53:18 +00:00
} as unknown as CommandInteraction;
jest.spyOn(randomBunny, "default").mockResolvedValue({
IsSuccess: true,
Result: {
Title: "title",
Permalink: "/r/permalink",
Url: "https://permalink.com/url.png",
Ups: 1,
Archived: false,
Downs: 1,
Hidden: false,
Subreddit: "rabbits",
SubredditSubscribers: 1,
},
2024-05-01 18:36:31 +01:00
Error: undefined,
Query: {
subreddit: "rabbits",
sortBy: "hot",
},
2024-03-30 16:53:18 +00:00
});
Math.floor = jest.fn().mockReturnValue(0);
const bunny = new Bunny();
await bunny.execute(interaction);
expect(randomBunny.default).toHaveBeenCalledTimes(1);
expect(randomBunny.default).toHaveBeenCalledWith("rabbits", "hot");
2024-05-01 18:36:31 +01:00
expect(interaction.editReply).toHaveBeenCalledTimes(1);
expect(interaction.deferReply).toHaveBeenCalledTimes(1);
2024-03-30 16:53:18 +00:00
expect(repliedWith).toBeDefined();
expect(repliedWith!.embeds).toBeDefined();
expect(repliedWith!.embeds.length).toBe(1);
const embed = repliedWith!.embeds[0] as EmbedBuilder;
expect(embed.data.color).toBe(EmbedColours.Ok);
expect(embed.data.title).toBe("title");
expect(embed.data.description).toBe("/r/permalink");
expect(embed.data.image).toBeDefined();
expect(embed.data.image!.url).toBe("https://permalink.com/url.png");
expect(embed.data.url).toBe("https://reddit.com/r/permalink");
expect(embed.data.footer).toBeDefined();
expect(embed.data.footer!.text).toBe("r/rabbits · 1 upvotes");
});
test("GIVEN interaction is not a chat input command, EXPECT nothing to happen", async () => {
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(false),
reply: jest.fn(),
} as unknown as CommandInteraction;
const bunny = new Bunny();
await bunny.execute(interaction);
expect(interaction.reply).not.toHaveBeenCalled();
});
test("GIVEN a result from random-bunny is failure, EXPECT error", async () => {
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
2024-05-01 18:36:31 +01:00
editReply: jest.fn(),
deferReply: jest.fn(),
2024-03-30 16:53:18 +00:00
} as unknown as CommandInteraction;
jest.spyOn(randomBunny, "default").mockResolvedValue({
IsSuccess: false,
2024-05-01 18:36:31 +01:00
Result: undefined,
Error: undefined,
Query: {
subreddit: "rabbits",
sortBy: "hot",
},
2024-03-30 16:53:18 +00:00
});
Math.floor = jest.fn().mockReturnValue(0);
const bunny = new Bunny();
await bunny.execute(interaction);
2024-02-03 19:29:28 +00:00
2024-03-30 16:53:18 +00:00
expect(randomBunny.default).toHaveBeenCalledTimes(1);
2024-02-03 19:29:28 +00:00
2024-05-01 18:36:31 +01:00
expect(interaction.editReply).toHaveBeenCalledTimes(1);
expect(interaction.editReply).toHaveBeenCalledWith("There was an error running this command.");
expect(interaction.deferReply).toHaveBeenCalledTimes(1);
2024-03-30 16:53:18 +00:00
});
2024-02-03 19:29:28 +00:00
});