card-drop/tests/buttonEvents/Effects.test.ts

128 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-12-01 20:13:17 +00:00
import {ButtonInteraction} from "discord.js";
import Effects from "../../src/buttonEvents/Effects";
import EffectHelper from "../../src/helpers/EffectHelper";
2024-11-30 16:38:02 +00:00
describe("execute", () => {
describe("GIVEN action in custom id is list", () => {
2024-12-06 18:08:15 +00:00
const interaction = {
2024-12-01 20:13:17 +00:00
customId: "effects list",
} as unknown as ButtonInteraction;
2024-12-06 18:08:15 +00:00
let listSpy: jest.SpyInstance;
2024-12-01 20:13:17 +00:00
beforeAll(async () => {
const effects = new Effects();
2024-12-06 18:08:15 +00:00
listSpy = jest.spyOn(effects as unknown as {"List": () => object}, "List")
.mockImplementation();
2024-12-01 20:13:17 +00:00
await effects.execute(interaction);
});
test("EXPECT list function to be called", () => {
expect(listSpy).toHaveBeenCalledTimes(1);
expect(listSpy).toHaveBeenCalledWith(interaction);
});
2024-11-30 16:38:02 +00:00
});
});
describe("List", () => {
2024-12-06 18:08:15 +00:00
let interaction: ButtonInteraction;
2024-12-01 20:13:17 +00:00
const embed = {
name: "Embed",
};
const row = {
name: "Row",
};
beforeEach(() => {
interaction = {
customId: "effects list",
user: {
id: "userId",
},
update: jest.fn(),
reply: jest.fn(),
2024-12-06 18:08:15 +00:00
} as unknown as ButtonInteraction;
2024-12-01 20:13:17 +00:00
});
2024-11-30 16:38:02 +00:00
describe("GIVEN page is a valid number", () => {
2024-12-01 20:13:17 +00:00
beforeEach(async () => {
interaction.customId += " 1";
2024-11-30 16:38:02 +00:00
2024-12-01 20:13:17 +00:00
EffectHelper.GenerateEffectEmbed = jest.fn()
.mockResolvedValue({
embed,
row,
});
const effects = new Effects();
await effects.execute(interaction);
});
test("EXPECT EffectHelper.GenerateEffectEmbed to be called", () => {
expect(EffectHelper.GenerateEffectEmbed).toHaveBeenCalledTimes(1);
expect(EffectHelper.GenerateEffectEmbed).toHaveBeenCalledWith("userId", 1);
});
test("EXPECT interaction to be updated", () => {
expect(interaction.update).toHaveBeenCalledTimes(1);
expect(interaction.update).toHaveBeenCalledWith({
embeds: [ embed ],
components: [ row ],
});
});
2024-11-30 16:38:02 +00:00
});
describe("GIVEN page in custom id is not supplied", () => {
2024-12-01 20:13:17 +00:00
beforeEach(async () => {
EffectHelper.GenerateEffectEmbed = jest.fn()
.mockResolvedValue({
embed,
row,
});
const effects = new Effects();
await effects.execute(interaction);
});
2024-11-30 16:38:02 +00:00
2024-12-01 20:13:17 +00:00
test("EXPECT interaction to be replied with error", () => {
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Page option is not a valid number");
});
test("EXPECT interaction to not be updated", () => {
expect(interaction.update).not.toHaveBeenCalled();
});
2024-11-30 16:38:02 +00:00
});
describe("GIVEN page in custom id is not a number", () => {
2024-12-01 20:13:17 +00:00
beforeEach(async () => {
interaction.customId += " test";
EffectHelper.GenerateEffectEmbed = jest.fn()
.mockResolvedValue({
embed,
row,
});
const effects = new Effects();
await effects.execute(interaction);
});
test("EXPECT interaction to be replied with error", () => {
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Page option is not a valid number");
});
2024-11-30 16:38:02 +00:00
2024-12-01 20:13:17 +00:00
test("EXPECT interaction to not be updated", () => {
expect(interaction.update).not.toHaveBeenCalled();
});
2024-11-30 16:38:02 +00:00
});
});