From 056783bc44925b30cf13d543287bb563edf95205 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 7 Nov 2024 19:38:16 +0000 Subject: [PATCH] WIP: Create add moon command tests --- .../304276391837302787/moons.test.ts | 20 ++++- .../moons/__snapshots__/list.test.ts.snap | 41 +++++++++ .../304276391837302787/moons/list.test.ts | 85 ++++++++++++------ .../moons/__snapshots__/add.test.ts.snap | 25 ++++++ .../304276391837302787/moons/add.test.ts | 87 +++++++++++++++++-- 5 files changed, 226 insertions(+), 32 deletions(-) create mode 100644 tests/buttonEvents/304276391837302787/moons/__snapshots__/list.test.ts.snap create mode 100644 tests/commands/304276391837302787/moons/__snapshots__/add.test.ts.snap diff --git a/tests/buttonEvents/304276391837302787/moons.test.ts b/tests/buttonEvents/304276391837302787/moons.test.ts index 69ebd42..83e60d7 100644 --- a/tests/buttonEvents/304276391837302787/moons.test.ts +++ b/tests/buttonEvents/304276391837302787/moons.test.ts @@ -1,3 +1,21 @@ +import { ButtonInteraction } from "discord.js"; +import Moons from "../../../src/buttonEvents/304276391837302787/moons"; +import * as List from "../../../src/buttonEvents/304276391837302787/moons/list"; + describe("GIVEN interaction action is list", () => { - test.todo("EXPECT List function to be called"); + const interaction = { + customId: "moons list", + } as unknown as ButtonInteraction; + + const listSpy = jest.spyOn(List, "default"); + + beforeAll(async () => { + const moons = new Moons(); + await moons.execute(interaction); + }); + + test("EXPECT List function to be called", () => { + expect(listSpy).toHaveBeenCalledTimes(1); + expect(listSpy).toHaveBeenCalledWith(interaction); + }); }); \ No newline at end of file diff --git a/tests/buttonEvents/304276391837302787/moons/__snapshots__/list.test.ts.snap b/tests/buttonEvents/304276391837302787/moons/__snapshots__/list.test.ts.snap new file mode 100644 index 0000000..61f75d5 --- /dev/null +++ b/tests/buttonEvents/304276391837302787/moons/__snapshots__/list.test.ts.snap @@ -0,0 +1,41 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`GIVEN happy flow EXPECT embed to be updated 1`] = ` +[ + { + "color": 3166394, + "description": "**1 -** Test Descriptio", + "footer": { + "icon_url": undefined, + "text": "Page 1 of 1 ยท 0 moons", + }, + "title": "username's Moons", + }, +] +`; + +exports[`GIVEN happy flow EXPECT row to be updated 1`] = ` +[ + { + "components": [ + { + "custom_id": "moons list userId -1", + "disabled": true, + "emoji": undefined, + "label": "Previous", + "style": 1, + "type": 2, + }, + { + "custom_id": "moons list userId 1", + "disabled": true, + "emoji": undefined, + "label": "Next", + "style": 1, + "type": 2, + }, + ], + "type": 1, + }, +] +`; diff --git a/tests/buttonEvents/304276391837302787/moons/list.test.ts b/tests/buttonEvents/304276391837302787/moons/list.test.ts index 2114123..fcb80c6 100644 --- a/tests/buttonEvents/304276391837302787/moons/list.test.ts +++ b/tests/buttonEvents/304276391837302787/moons/list.test.ts @@ -1,8 +1,65 @@ -import {ButtonInteraction, EmbedBuilder} from "discord.js"; +import {ActionRowBuilder, ButtonBuilder, ButtonInteraction, EmbedBuilder} from "discord.js"; import List from "../../../../src/buttonEvents/304276391837302787/moons/list"; import UserSetting from "../../../../src/database/entities/UserSetting"; import Moon from "../../../../src/database/entities/304276391837302787/Moon"; +describe("GIVEN happy flow", () => { + let updatedWithEmbeds: EmbedBuilder[] | undefined; + let updatedWithRows: ActionRowBuilder[] | undefined; + + const interaction = { + guild: { + members: { + cache: { + find: jest.fn().mockReturnValue({ + user: { + username: "username", + }, + }), + }, + }, + }, + reply: jest.fn(), + update: jest.fn((options: any) => { + updatedWithEmbeds = options.embeds; + updatedWithRows = options.components; + }), + customId: "moons list userId 0", + } as unknown as ButtonInteraction; + + beforeAll(async () => { + UserSetting.FetchOneByKey = jest.fn(); + Moon.FetchPaginatedMoonsByUserId = jest.fn().mockResolvedValue([ + [ + { + MoonNumber: 1, + Description: "Test Description", + } + ], + 1, + ]); + + await List(interaction); + }); + + test("EXPECT moons to be fetched", () => { + expect(Moon.FetchPaginatedMoonsByUserId).toHaveBeenCalledTimes(1); + expect(Moon.FetchPaginatedMoonsByUserId).toHaveBeenCalledWith("userId", 10, 0); + }); + + test("EXPECT interaction.update to be called", () => { + expect(interaction.update).toHaveBeenCalledTimes(1); + }); + + test("EXPECT embed to be updated", () => { + expect(updatedWithEmbeds).toMatchSnapshot(); + }); + + test("EXPECT row to be updated", () => { + expect(updatedWithRows).toMatchSnapshot(); + }); +}); + describe("GIVEN interaction.guild is null", () => { const interaction = { guild: null, @@ -205,31 +262,7 @@ describe("GIVEN no moons on current page", () => { test("EXPECT description to say so", () => { expect(updatedWith).toBeDefined(); expect(updatedWith?.length).toBe(1); - + expect(updatedWith![0].data.description).toBe("*none*"); }); -}); - -describe("GIVEN happy flow", () => { - test.todo("EXPECT moons to be fetched"); - - test.todo("EXPECT embed to be updated"); - - test.todo("EXPECT row to be updated"); - - describe("GIVEN it is the first page", () => { - test.todo("EXPECT Previous button to be disabled"); - }); - - describe("GIVEN it is the last page", () => { - test.todo("EXPECT Next button to be disabled"); - - describe("GIVEN there are more moons in the counter than in the database", () => { - test.todo("EXPECT untracked counter to be present"); - }); - }); - - describe("GIVEN no moons on the current page", () => { - test.todo("EXPECT Next button to be disabled"); - }); }); \ No newline at end of file diff --git a/tests/commands/304276391837302787/moons/__snapshots__/add.test.ts.snap b/tests/commands/304276391837302787/moons/__snapshots__/add.test.ts.snap new file mode 100644 index 0000000..ace8150 --- /dev/null +++ b/tests/commands/304276391837302787/moons/__snapshots__/add.test.ts.snap @@ -0,0 +1,25 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`GIVEN happy flow EXPECT embed to be replied 1`] = ` +[ + { + "color": 5294200, + "description": "**2 -** Test Description", + "thumbnail": { + "url": "https://cdn.discordapp.com/emojis/374131312182689793.webp?size=96&quality=lossless", + }, + "title": "undefined Got A Moon!", + }, +] +`; + +exports[`GIVEN happy flow EXPECT moon to be saved 1`] = ` +{ + "Description": "Test Description", + "Id": Any, + "MoonNumber": 2, + "UserId": "userId", + "WhenCreated": Any, + "WhenUpdated": Any, +} +`; diff --git a/tests/commands/304276391837302787/moons/add.test.ts b/tests/commands/304276391837302787/moons/add.test.ts index a07e070..cf31842 100644 --- a/tests/commands/304276391837302787/moons/add.test.ts +++ b/tests/commands/304276391837302787/moons/add.test.ts @@ -1,3 +1,84 @@ +import { CommandInteraction, EmbedBuilder } from "discord.js"; +import AddMoon from "../../../../src/commands/304276391837302787/moons/add"; +import Moon from "../../../../src/database/entities/304276391837302787/Moon"; +import UserSetting from "../../../../src/database/entities/UserSetting"; + +describe("GIVEN happy flow", () => { + let repliedWithEmbed: EmbedBuilder[] | undefined; + let savedMoon: Moon | undefined; + + const interaction = { + reply: jest.fn((options: any) => { + repliedWithEmbed = options.embeds; + }), + options: { + get: jest.fn() + .mockReturnValueOnce({ + value: "Test Description", + }), + }, + user: { + id: "userId", + }, + } as unknown as CommandInteraction; + + const userSetting = { + Value: 1, + UpdateValue: jest.fn(), + Save: jest.fn(), + }; + + beforeAll(async () => { + Moon.FetchMoonCountByUserId = jest.fn().mockResolvedValue(1); + Moon.prototype.Save = jest.fn().mockImplementation((_, entity: Moon) => { + savedMoon = entity; + }); + + UserSetting.FetchOneByKey = jest.fn().mockResolvedValue(userSetting); + + await AddMoon(interaction); + }); + + test("EXPECT description option to have been fetched", () => { + expect(interaction.options.get).toHaveBeenCalledTimes(1); + expect(interaction.options.get).toHaveBeenCalledWith("description", true); + }); + + test("EXPECT UserSetting to have been fetched", () => { + expect(UserSetting.FetchOneByKey).toHaveBeenCalledTimes(1); + expect(UserSetting.FetchOneByKey).toHaveBeenCalledWith("userId", "moons"); + }); + + test("EXPECT moonCount to be updated +1", () => { + expect(userSetting.UpdateValue).toHaveBeenCalledTimes(1); + expect(userSetting.UpdateValue).toHaveBeenCalledWith("2"); + }); + + test("EXPECT setting to be saved", () => { + expect(userSetting.Save).toHaveBeenCalledTimes(1); + expect(userSetting.Save).toHaveBeenCalledWith(UserSetting, userSetting); + }); + + test("EXPECT moon to be saved", () => { + expect(Moon.prototype.Save).toHaveBeenCalledTimes(1); + expect(Moon.prototype.Save).toHaveBeenCalledWith(Moon, expect.any(Moon)); + + expect(savedMoon).toBeDefined(); + expect(savedMoon).toMatchSnapshot({ + Id: expect.any(String), + WhenCreated: expect.any(Date), + WhenUpdated: expect.any(Date), + }); + }); + + test("EXPECT embed to be replied", () => { + expect(interaction.reply).toHaveBeenCalledTimes(1); + + expect(repliedWithEmbed).toBeDefined(); + expect(repliedWithEmbed).toMatchSnapshot(); + }); +}); + describe("GIVEN description is null", () => { test.todo("EXPECT error replied"); }); @@ -12,8 +93,4 @@ describe("GIVEN moon count setting exists", () => { describe("GIVEN moon count setting does not exist", () => { test.todo("EXPECT new entity to be created"); -}); - -test.todo("EXPECT setting to be saved"); - -test.todo("EXPECT embed to be replied"); +}); \ No newline at end of file