From 3e198308b98d38872a35251e6b1858748c196410 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 18 May 2024 10:07:54 +0100 Subject: [PATCH 1/2] Add poll command tests --- tests/commands/poll.test.ts | 123 ++++++++++++++++++++++++++++++++++-- 1 file changed, 117 insertions(+), 6 deletions(-) diff --git a/tests/commands/poll.test.ts b/tests/commands/poll.test.ts index 36a50d7..ccf8b04 100644 --- a/tests/commands/poll.test.ts +++ b/tests/commands/poll.test.ts @@ -117,15 +117,126 @@ describe('Execute', () => { expect(message.react).toHaveBeenCalledWith("5️⃣"); }); - test.todo("GIVEN title is not supplied, EXPECT nothing to happen"); + test("GIVEN option3 is not supplied, EXPECT a 2 option poll to be created", async () => { + let sentEmbed: EmbedBuilder | undefined; - test.todo("GIVEN option1 is not supplied, EXPECT nothing to happen"); + // Arrange + const message = { + react: jest.fn(), + } as unknown as Message; - test.todo("GIVEN option2 is not supplied, EXPECT nothing to happen"); + const response = { + fetch: jest.fn().mockResolvedValue(message), + } as unknown as InteractionResponse; - test.todo("GIVEN option3 is not supplied, EXPECT a 2 option poll to be created"); + const interaction = { + options: { + get: jest.fn().mockReturnValueOnce({ value: "Title" }) + .mockReturnValueOnce({ value: "Option 1" }) + .mockReturnValueOnce({ value: "Option 2" }), + }, + reply: jest.fn().mockImplementation((options: any) => { + sentEmbed = options.embeds[0]; - test.todo("GIVEN option4 is not supplied, EXPECT a 3 option poll to be created"); + return response; + }), + user: { + username: "username", + avatarURL: jest.fn().mockReturnValue("https://avatarurl.com/user.png"), + }, + } as unknown as CommandInteraction; - test.todo("GIVEN option5 is not supplied, EXPECT a 4 option poll to be created"); + // Act + const command = new Command(); + await command.execute(interaction); + + // Assert + expect(sentEmbed).toBeDefined(); + expect(sentEmbed!.data.description).toBe("1️⃣ Option 1\n2️⃣ Option 2"); + + expect(message.react).toHaveBeenCalledTimes(2); + }); + + test("GIVEN option4 is not supplied, EXPECT a 3 option poll to be created", async () => { + let sentEmbed: EmbedBuilder | undefined; + + // Arrange + const message = { + react: jest.fn(), + } as unknown as Message; + + const response = { + fetch: jest.fn().mockResolvedValue(message), + } as unknown as InteractionResponse; + + const interaction = { + options: { + get: jest.fn().mockReturnValueOnce({ value: "Title" }) + .mockReturnValueOnce({ value: "Option 1" }) + .mockReturnValueOnce({ value: "Option 2" }) + .mockReturnValueOnce({ value: "Option 3" }), + }, + reply: jest.fn().mockImplementation((options: any) => { + sentEmbed = options.embeds[0]; + + return response; + }), + user: { + username: "username", + avatarURL: jest.fn().mockReturnValue("https://avatarurl.com/user.png"), + }, + } as unknown as CommandInteraction; + + // Act + const command = new Command(); + await command.execute(interaction); + + // Assert + expect(sentEmbed).toBeDefined(); + expect(sentEmbed!.data.description).toBe("1️⃣ Option 1\n2️⃣ Option 2\n3️⃣ Option 3"); + + expect(message.react).toHaveBeenCalledTimes(3); + }); + + test("GIVEN option5 is not supplied, EXPECT a 4 option poll to be created", async () => { + let sentEmbed: EmbedBuilder | undefined; + + // Arrange + const message = { + react: jest.fn(), + } as unknown as Message; + + const response = { + fetch: jest.fn().mockResolvedValue(message), + } as unknown as InteractionResponse; + + const interaction = { + options: { + get: jest.fn().mockReturnValueOnce({ value: "Title" }) + .mockReturnValueOnce({ value: "Option 1" }) + .mockReturnValueOnce({ value: "Option 2" }) + .mockReturnValueOnce({ value: "Option 3" }) + .mockReturnValueOnce({ value: "Option 4" }), + }, + reply: jest.fn().mockImplementation((options: any) => { + sentEmbed = options.embeds[0]; + + return response; + }), + user: { + username: "username", + avatarURL: jest.fn().mockReturnValue("https://avatarurl.com/user.png"), + }, + } as unknown as CommandInteraction; + + // Act + const command = new Command(); + await command.execute(interaction); + + // Assert + expect(sentEmbed).toBeDefined(); + expect(sentEmbed!.data.description).toBe("1️⃣ Option 1\n2️⃣ Option 2\n3️⃣ Option 3\n4️⃣ Option 4"); + + expect(message.react).toHaveBeenCalledTimes(4); + }); }); \ No newline at end of file From 581c275adf04196b20805ba8c4c353d350e51752 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 18 May 2024 10:14:57 +0100 Subject: [PATCH 2/2] WIP: Start of rules command tests --- .../commands/__snapshots__/rules.test.ts.snap | 32 +++++++++++++++++++ tests/commands/rules.test.ts | 8 ++++- 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/commands/__snapshots__/rules.test.ts.snap diff --git a/tests/commands/__snapshots__/rules.test.ts.snap b/tests/commands/__snapshots__/rules.test.ts.snap new file mode 100644 index 0000000..c613f4d --- /dev/null +++ b/tests/commands/__snapshots__/rules.test.ts.snap @@ -0,0 +1,32 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Constructor EXPECT properties to be set 1`] = ` +{ + "default_member_permissions": "8", + "default_permission": undefined, + "description": "Rules-related commands", + "description_localizations": undefined, + "dm_permission": undefined, + "name": "rules", + "name_localizations": undefined, + "nsfw": undefined, + "options": [ + { + "description": "Send the rules embeds for this server", + "description_localizations": undefined, + "name": "embeds", + "name_localizations": undefined, + "options": [], + "type": 1, + }, + { + "description": "Send the server verification embed button", + "description_localizations": undefined, + "name": "access", + "name_localizations": undefined, + "options": [], + "type": 1, + }, + ], +} +`; diff --git a/tests/commands/rules.test.ts b/tests/commands/rules.test.ts index 014c682..e3ba58a 100644 --- a/tests/commands/rules.test.ts +++ b/tests/commands/rules.test.ts @@ -1,9 +1,15 @@ +import Command from "../../src/commands/rules"; + beforeEach(() => { process.env = {}; }); describe('Constructor', () => { - test.todo('EXPECT properties to be set'); + test('EXPECT properties to be set', () => { + const command = new Command(); + + expect(command.CommandBuilder).toMatchSnapshot(); + }); }); describe('Execute', () => {