From 67eafa695ca7ebe456fcf0240a150c6a0204d263 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 1 Nov 2024 14:47:14 +0000 Subject: [PATCH 1/3] Move moon button events to server id folder --- src/buttonEvents/{ => 304276391837302787}/moons.ts | 2 +- src/buttonEvents/{ => 304276391837302787}/moons/list.ts | 6 +++--- src/registry.ts | 2 +- .../{ => 304276391837302787}/moons/list.test.ts | 0 4 files changed, 5 insertions(+), 5 deletions(-) rename src/buttonEvents/{ => 304276391837302787}/moons.ts (88%) rename src/buttonEvents/{ => 304276391837302787}/moons/list.ts (92%) rename tests/buttonEvents/{ => 304276391837302787}/moons/list.test.ts (100%) diff --git a/src/buttonEvents/moons.ts b/src/buttonEvents/304276391837302787/moons.ts similarity index 88% rename from src/buttonEvents/moons.ts rename to src/buttonEvents/304276391837302787/moons.ts index 726f209..e13a4c0 100644 --- a/src/buttonEvents/moons.ts +++ b/src/buttonEvents/304276391837302787/moons.ts @@ -1,5 +1,5 @@ import {ButtonInteraction} from "discord.js"; -import {ButtonEvent} from "../type/buttonEvent"; +import {ButtonEvent} from "../../type/buttonEvent"; import List from "./moons/list"; export default class Moons extends ButtonEvent { diff --git a/src/buttonEvents/moons/list.ts b/src/buttonEvents/304276391837302787/moons/list.ts similarity index 92% rename from src/buttonEvents/moons/list.ts rename to src/buttonEvents/304276391837302787/moons/list.ts index 8f8e45a..a2b3cc4 100644 --- a/src/buttonEvents/moons/list.ts +++ b/src/buttonEvents/304276391837302787/moons/list.ts @@ -1,7 +1,7 @@ import {ActionRowBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, EmbedBuilder} from "discord.js"; -import Moon from "../../database/entities/304276391837302787/Moon"; -import EmbedColours from "../../constants/EmbedColours"; -import UserSetting from "../../database/entities/UserSetting"; +import Moon from "../../../database/entities/304276391837302787/Moon"; +import EmbedColours from "../../../constants/EmbedColours"; +import UserSetting from "../../../database/entities/UserSetting"; export default async function List(interaction: ButtonInteraction) { if (!interaction.guild) return; diff --git a/src/registry.ts b/src/registry.ts index f1080bb..64c5629 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -42,7 +42,7 @@ import MessageCreate from "./events/MessageEvents/MessageCreate"; // Button Event Imports import Verify from "./buttonEvents/verify"; -import MoonsButtonEvent from "./buttonEvents/moons"; +import MoonsButtonEvent from "./buttonEvents/304276391837302787/moons"; export default class Registry { public static RegisterCommands() { diff --git a/tests/buttonEvents/moons/list.test.ts b/tests/buttonEvents/304276391837302787/moons/list.test.ts similarity index 100% rename from tests/buttonEvents/moons/list.test.ts rename to tests/buttonEvents/304276391837302787/moons/list.test.ts From 5ed31d08d1a29bbb61e5a2fc409f03b4e7f77414 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 1 Nov 2024 14:52:38 +0000 Subject: [PATCH 2/3] Plan tests --- .../304276391837302787/moons.test.ts | 3 +++ tests/commands/304276391837302787/moons.test.ts | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 tests/buttonEvents/304276391837302787/moons.test.ts create mode 100644 tests/commands/304276391837302787/moons.test.ts diff --git a/tests/buttonEvents/304276391837302787/moons.test.ts b/tests/buttonEvents/304276391837302787/moons.test.ts new file mode 100644 index 0000000..69ebd42 --- /dev/null +++ b/tests/buttonEvents/304276391837302787/moons.test.ts @@ -0,0 +1,3 @@ +describe("GIVEN interaction action is list", () => { + test.todo("EXPECT List function to be called"); +}); \ No newline at end of file diff --git a/tests/commands/304276391837302787/moons.test.ts b/tests/commands/304276391837302787/moons.test.ts new file mode 100644 index 0000000..187979a --- /dev/null +++ b/tests/commands/304276391837302787/moons.test.ts @@ -0,0 +1,17 @@ +describe("constructor", () => { + test.todo("EXPECT CommandBuilder to be defined correctly"); +}); + +describe("execute", () => { + describe("GIVEN interaction is not a chat input command", () => { + test.todo("EXPECT nothing to happen"); + }); + + describe("GIVEN interaction subcommand is list", () => { + test.todo("EXPECT ListMoons to be called"); + }); + + describe("GIVEN interaction subcommand is add", () => { + test.todo("EXPECT AddMoon to be called"); + }); +}); \ No newline at end of file From 952e7901ba6c3dc8bbc69f13498237fcca7d3714 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 1 Nov 2024 17:05:31 +0000 Subject: [PATCH 3/3] WIP: Create moons list command tests --- package.json | 2 +- .../moons/__snapshots__/list.test.ts.snap | 40 +++ .../304276391837302787/moons/list.test.ts | 246 +++++++++++++++++- 3 files changed, 274 insertions(+), 14 deletions(-) create mode 100644 tests/commands/304276391837302787/moons/__snapshots__/list.test.ts.snap diff --git a/package.json b/package.json index 620c7ce..2540d27 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "clean": "rm -rf node_modules/ dist/", "build": "tsc", "start": "node ./dist/vylbot", - "test": "jest .", + "test": "jest", "db:up": "typeorm migration:run -d dist/database/dataSources/appDataSource.js", "db:down": "typeorm migration:revert -d dist/database/dataSources/appDataSource.js", "db:create": "typeorm migration:create ./src/database/migrations", diff --git a/tests/commands/304276391837302787/moons/__snapshots__/list.test.ts.snap b/tests/commands/304276391837302787/moons/__snapshots__/list.test.ts.snap new file mode 100644 index 0000000..105e226 --- /dev/null +++ b/tests/commands/304276391837302787/moons/__snapshots__/list.test.ts.snap @@ -0,0 +1,40 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`GIVEN happy flow EXPECT interaction to be replied 1`] = ` +{ + "components": [ + { + "components": [ + { + "custom_id": "moons list userId -1", + "disabled": true, + "emoji": undefined, + "label": "Previous", + "style": 1, + "type": 2, + }, + { + "custom_id": "moons list userId 01", + "disabled": true, + "emoji": undefined, + "label": "Next", + "style": 1, + "type": 2, + }, + ], + "type": 1, + }, + ], + "embeds": [ + { + "color": 3166394, + "description": "**1 -** Test Descriptio", + "footer": { + "icon_url": undefined, + "text": "Page 01 of 1 ยท 0 moons", + }, + "title": "undefined's Moons", + }, + ], +} +`; diff --git a/tests/commands/304276391837302787/moons/list.test.ts b/tests/commands/304276391837302787/moons/list.test.ts index cea7bd5..2d0b6b8 100644 --- a/tests/commands/304276391837302787/moons/list.test.ts +++ b/tests/commands/304276391837302787/moons/list.test.ts @@ -1,29 +1,249 @@ +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(); + }); +}); + describe("GIVEN moons returned is empty", () => { - test.todo("EXPECT none description"); + 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*"); + }); }); describe("GIVEN it is the first page", () => { - test.todo("EXPECT Previous button to be disabled"); + 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; + + expect(repliedWithRow.components[0].data.disabled).toBe(true); + }); }); describe("GIVEN it is the last page", () => { - test.todo("EXPECT Next button to be disabled"); + 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; + + expect(repliedWithRow.components[1].data.disabled).toBe(true); + }); describe("GIVEN moon count is greater than the amount of moons in the database", () => { - test.todo("EXPECT untracked counter to be shown"); + 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"); + }); }); }); describe("GIVEN moon count is empty", () => { - test.todo("EXPECT Next button to be disabled"); + let repliedWith: InteractionReplyOptions | undefined; - describe("GIVEN moon count is greater than the amount of moons in the database", () => { - test.todo("EXPECT untracked counter to be shown"); + 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 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; + + expect(repliedWithRow.components[1].data.disabled).toBe(true); }); }); - -test.todo("EXPECT interaction to be replied"); - -test.todo("EXPECT embed to be replied"); - -test.todo("EXPECT row to be replied");