vylbot-app/tests/commands/304276391837302787/moons/list.test.ts

250 lines
6.9 KiB
TypeScript
Raw Permalink Normal View History

2024-11-01 17:05:31 +00:00
import { ActionRowBuilder, APIEmbed, ButtonBuilder, CommandInteraction, EmbedBuilder, InteractionReplyOptions } from "discord.js";
import List from "../../../../src/commands/304276391837302787/moons/list";
import Moon from "../../../../src/database/entities/304276391837302787/Moon";
import UserSetting from "../../../../src/database/entities/UserSetting";
describe("GIVEN happy flow", () => {
let repliedWith: InteractionReplyOptions | undefined;
const interaction = {
reply: jest.fn((options) => {
repliedWith = options;
}),
options: {
get: jest.fn()
.mockReturnValueOnce(undefined) // User
.mockReturnValue({
value: "0",
}), // Page
},
user: {
id: "userId",
}
} as unknown as CommandInteraction;
beforeAll(async () => {
Moon.FetchPaginatedMoonsByUserId = jest.fn().mockResolvedValue([
[
{
MoonNumber: 1,
Description: "Test Description",
}
],
1,
]);
UserSetting.FetchOneByKey = jest.fn().mockResolvedValue({
Value: "0",
});
await List(interaction);
});
test("EXPECT interaction to be replied", () => {
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(repliedWith).toMatchSnapshot();
});
});
2024-10-31 16:56:36 +00:00
describe("GIVEN moons returned is empty", () => {
2024-11-01 17:05:31 +00:00
let repliedWith: InteractionReplyOptions | undefined;
const interaction = {
reply: jest.fn((options) => {
repliedWith = options;
}),
options: {
get: jest.fn()
.mockReturnValueOnce(undefined) // User
.mockReturnValue({
value: "0",
}), // Page
},
user: {
id: "userId",
}
} as unknown as CommandInteraction;
beforeAll(async () => {
Moon.FetchPaginatedMoonsByUserId = jest.fn().mockResolvedValue([
[],
0,
]);
UserSetting.FetchOneByKey = jest.fn().mockResolvedValue({
Value: "0",
});
await List(interaction);
});
test("EXPECT none description", () => {
expect(repliedWith).toBeDefined();
expect(repliedWith!.embeds).toBeDefined();
expect(repliedWith!.embeds!.length).toBe(1);
const repliedWithEmbed = repliedWith!.embeds![0] as EmbedBuilder;
expect(repliedWithEmbed.data.description).toBe("*none*");
});
2024-10-31 16:56:36 +00:00
});
describe("GIVEN it is the first page", () => {
2024-11-01 17:05:31 +00:00
let repliedWith: InteractionReplyOptions | undefined;
const interaction = {
reply: jest.fn((options) => {
repliedWith = options;
}),
options: {
get: jest.fn()
.mockReturnValueOnce(undefined) // User
.mockReturnValue({
value: "0",
}), // Page
},
user: {
id: "userId",
}
} as unknown as CommandInteraction;
beforeAll(async () => {
Moon.FetchPaginatedMoonsByUserId = jest.fn().mockResolvedValue([
[
{
MoonNumber: 1,
Description: "Test Description",
}
],
1,
]);
UserSetting.FetchOneByKey = jest.fn().mockResolvedValue({
Value: "0",
});
await List(interaction);
});
test("EXPECT Previous button to be disabled", () => {
expect(repliedWith).toBeDefined();
expect(repliedWith!.components).toBeDefined();
expect(repliedWith!.components!.length).toBe(1);
const repliedWithRow = repliedWith!.components![0] as ActionRowBuilder<ButtonBuilder>;
expect(repliedWithRow.components[0].data.disabled).toBe(true);
});
2024-10-31 16:56:36 +00:00
});
describe("GIVEN it is the last page", () => {
2024-11-01 17:05:31 +00:00
let repliedWith: InteractionReplyOptions | undefined;
const interaction = {
reply: jest.fn((options) => {
repliedWith = options;
}),
options: {
get: jest.fn()
.mockReturnValueOnce(undefined) // User
.mockReturnValue({
value: "0",
}), // Page
},
user: {
id: "userId",
}
} as unknown as CommandInteraction;
beforeAll(async () => {
Moon.FetchPaginatedMoonsByUserId = jest.fn().mockResolvedValue([
[
{
MoonNumber: 1,
Description: "Test Description",
}
],
1,
]);
UserSetting.FetchOneByKey = jest.fn().mockResolvedValue({
Value: "0",
});
await List(interaction);
});
test("EXPECT Next button to be disabled", () => {
expect(repliedWith).toBeDefined();
expect(repliedWith!.components).toBeDefined();
expect(repliedWith!.components!.length).toBe(1);
const repliedWithRow = repliedWith!.components![0] as ActionRowBuilder<ButtonBuilder>;
expect(repliedWithRow.components[1].data.disabled).toBe(true);
});
2024-10-31 16:56:36 +00:00
describe("GIVEN moon count is greater than the amount of moons in the database", () => {
2024-11-01 17:05:31 +00:00
beforeAll(async () => {
UserSetting.FetchOneByKey = jest.fn().mockResolvedValue({
Value: "2",
});
await List(interaction);
});
test("EXPECT untracked counter to be shown", () => {
const repliedWithEmbed = repliedWith!.embeds![0] as EmbedBuilder;
expect(repliedWithEmbed.data.description).toContain("...plus 1 more untracked");
});
2024-10-31 16:56:36 +00:00
});
});
describe("GIVEN moon count is empty", () => {
2024-11-01 17:05:31 +00:00
let repliedWith: InteractionReplyOptions | undefined;
2024-10-31 16:56:36 +00:00
2024-11-01 17:05:31 +00:00
const interaction = {
reply: jest.fn((options) => {
repliedWith = options;
}),
options: {
get: jest.fn()
.mockReturnValueOnce(undefined) // User
.mockReturnValue({
value: "0",
}), // Page
},
user: {
id: "userId",
}
} as unknown as CommandInteraction;
beforeAll(async () => {
Moon.FetchPaginatedMoonsByUserId = jest.fn().mockResolvedValue([
[],
0,
]);
UserSetting.FetchOneByKey = jest.fn().mockResolvedValue({
Value: "0",
});
await List(interaction);
2024-10-31 16:56:36 +00:00
});
2024-11-01 17:05:31 +00:00
test("EXPECT Next button to be disabled", () => {
expect(repliedWith).toBeDefined();
2024-10-31 16:56:36 +00:00
2024-11-01 17:05:31 +00:00
expect(repliedWith!.components).toBeDefined();
expect(repliedWith!.components!.length).toBe(1);
2024-10-31 16:56:36 +00:00
2024-11-01 17:05:31 +00:00
const repliedWithRow = repliedWith!.components![0] as ActionRowBuilder<ButtonBuilder>;
expect(repliedWithRow.components[1].data.disabled).toBe(true);
});
});