From 5302d57a899872ad1a2e2feb9d56a0bd67c77bf4 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 2 Dec 2024 18:40:19 +0000 Subject: [PATCH 1/2] Add effects command tests --- tests/commands/effects.test.ts | 112 +++++++++++++++++++++++++++++++-- 1 file changed, 106 insertions(+), 6 deletions(-) diff --git a/tests/commands/effects.test.ts b/tests/commands/effects.test.ts index 0ab6eab..8819209 100644 --- a/tests/commands/effects.test.ts +++ b/tests/commands/effects.test.ts @@ -1,4 +1,5 @@ import Effects from "../../src/commands/effects"; +import EffectHelper from "../../src/helpers/EffectHelper"; describe("constructor", () => { let effects: Effects; @@ -25,7 +26,8 @@ describe("execute", () => { const effects = new Effects(); - listSpy = jest.spyOn(effects as any, "List"); + listSpy = jest.spyOn(effects as any, "List") + .mockImplementation(); await effects.execute(interaction); }); @@ -40,24 +42,122 @@ describe("execute", () => { }); describe("GIVEN subcommand is list", () => { - test.todo("EXPECT list function to be called"); + let interaction: any; + + let listSpy: any; + + beforeEach(async () => { + interaction = { + isChatInputCommand: jest.fn().mockReturnValue(true), + options: { + getSubcommand: jest.fn().mockReturnValue("list"), + }, + }; + + const effects = new Effects(); + + listSpy = jest.spyOn(effects as any, "List") + .mockImplementation(); + + await effects.execute(interaction); + }); + + test("EXPECT subcommand function to be called", () => { + expect(interaction.options.getSubcommand).toHaveBeenCalledTimes(1); + }); + + test("EXPECT list function to be called", () => { + expect(listSpy).toHaveBeenCalledTimes(1); + expect(listSpy).toHaveBeenCalledWith(interaction); + }); }); }); describe("List", () => { + let effects: Effects = new Effects(); + let interaction: any; + + const embed = { + name: "embed", + }; + + const row = { + name: "row", + }; + + beforeEach(async () => { + interaction = { + isChatInputCommand: jest.fn().mockReturnValue(true), + options: { + getSubcommand: jest.fn().mockReturnValue("list"), + }, + reply: jest.fn(), + user: { + id: "userId", + }, + }; + + const effects = new Effects(); + + EffectHelper.GenerateEffectEmbed = jest.fn().mockReturnValue({ + embed, + row, + }); + + jest.spyOn(effects as any, "List") + .mockImplementation(); + }); + describe("GIVEN page option is supplied", () => { describe("AND page is a valid number", () => { - test.todo("EXPECT EffectHelper.GenerateEffectEmbed to have been called with page"); + beforeEach(async () => { + interaction.options.get = jest.fn().mockReturnValueOnce({ + value: "2", + }); - test.todo("EXPECT interaction to have been replied"); + await effects.execute(interaction); + }); + + test("EXPECT EffectHelper.GenerateEffectEmbed to have been called with page", () => { + expect(EffectHelper.GenerateEffectEmbed).toHaveBeenCalledTimes(1); + expect(EffectHelper.GenerateEffectEmbed).toHaveBeenCalledWith("userId", 2); + }); + + test("EXPECT interaction to have been replied", () => { + expect(interaction.reply).toHaveBeenCalledTimes(1); + expect(interaction.reply).toHaveBeenCalledWith({ + embeds: [ embed ], + components: [ row ], + }); + }); }); describe("AND page is not a valid number", () => { - test.todo("EXPECT EffectHelper.GenerateEffectEmbed to have been called with page of 1"); + beforeEach(async () => { + interaction.options.get = jest.fn().mockReturnValueOnce({ + value: "test", + }); + + await effects.execute(interaction); + }); + + test("EXPECT EffectHelper.GenerateEffectEmbed to have been called with page of 1", () => { + expect(EffectHelper.GenerateEffectEmbed).toHaveBeenCalledTimes(1); + expect(EffectHelper.GenerateEffectEmbed).toHaveBeenCalledWith("userId", 1); + }); }); }); describe("GIVEN page option is not supplied", () => { - test.todo("EXPECT EffectHelper.GenerateEffectEmbed to have been called with a page of 1"); + beforeEach(async () => { + interaction.options.get = jest.fn().mockReturnValueOnce(undefined); + + await effects.execute(interaction); + }); + + test("EXPECT EffectHelper.GenerateEffectEmbed to have been called with page of 1", () => { + expect(EffectHelper.GenerateEffectEmbed).toHaveBeenCalledTimes(1); + expect(EffectHelper.GenerateEffectEmbed).toHaveBeenCalledWith("userId", 1); + }); }); }); From 0584d0304b9320332703470db2dcad2bc9f10fe3 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 2 Dec 2024 18:57:15 +0000 Subject: [PATCH 2/2] Update tests --- tests/helpers/EffectHelper.test.ts | 76 +++++++++++++++++-- .../__snapshots__/EffectHelper.test.ts.snap | 71 +++++++++++++++++ 2 files changed, 140 insertions(+), 7 deletions(-) create mode 100644 tests/helpers/__snapshots__/EffectHelper.test.ts.snap diff --git a/tests/helpers/EffectHelper.test.ts b/tests/helpers/EffectHelper.test.ts index 2adcc9b..bdfe5a1 100644 --- a/tests/helpers/EffectHelper.test.ts +++ b/tests/helpers/EffectHelper.test.ts @@ -281,25 +281,87 @@ describe("HasEffect", () => { }); describe("GenerateEffectEmbed", () => { - test.todo("EXPECT UserEffect.FetchAllByUserIdPaginated to be called"); + beforeEach(async () => { + UserEffect.FetchAllByUserIdPaginated = jest.fn() + .mockResolvedValue([ + [], + 0, + ]); + + await EffectHelper.GenerateEffectEmbed("userId", 1); + }); + + test("EXPECT UserEffect.FetchAllByUserIdPaginated to be called", () => { + expect(UserEffect.FetchAllByUserIdPaginated).toHaveBeenCalledTimes(1); + expect(UserEffect.FetchAllByUserIdPaginated).toHaveBeenCalledWith("userId", 0, 10); + }); describe("GIVEN there are no effects returned", () => { - test.todo("EXPECT embed generated"); + let result: any; - test.todo("EXPECT row generated"); + beforeEach(async () => { + UserEffect.FetchAllByUserIdPaginated = jest.fn() + .mockResolvedValue([ + [], + 0, + ]); + + result = await EffectHelper.GenerateEffectEmbed("userId", 1); + }); + + test("EXPECT result returned", () => { + expect(result).toMatchSnapshot(); + }); }); describe("GIVEN there are effects returned", () => { - test.todo("EXPECT embed generated"); + let result: any; - test.todo("EXPECT row generated"); + beforeEach(async () => { + UserEffect.FetchAllByUserIdPaginated = jest.fn() + .mockResolvedValue([ + [ + { + Name: "name", + Unused: 1, + }, + ], + 1, + ]); + + result = await EffectHelper.GenerateEffectEmbed("userId", 1); + }); + + test("EXPECT result returned", () => { + expect(result).toMatchSnapshot(); + }); describe("AND it is the first page", () => { - test.todo("EXPECT Previous button to be disabled"); + beforeEach(async () => { + result = await EffectHelper.GenerateEffectEmbed("userId", 1) + }); + + test("EXPECT Previous button to be disabled", () => { + const button = result.row.components[0].data; + + expect(button).toBeDefined(); + expect(button.label).toBe("Previous"); + expect(button.disabled).toBe(true); + }); }); describe("AND it is the last page", () => { - test.todo("EXPECT Next button to be disabled"); + beforeEach(async () => { + result = await EffectHelper.GenerateEffectEmbed("userId", 1) + }); + + test("EXPECT Next button to be disabled", () => { + const button = result.row.components[1].data; + + expect(button).toBeDefined(); + expect(button.label).toBe("Next"); + expect(button.disabled).toBe(true); + }); }); }); }); diff --git a/tests/helpers/__snapshots__/EffectHelper.test.ts.snap b/tests/helpers/__snapshots__/EffectHelper.test.ts.snap new file mode 100644 index 0000000..6484acd --- /dev/null +++ b/tests/helpers/__snapshots__/EffectHelper.test.ts.snap @@ -0,0 +1,71 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`GenerateEffectEmbed GIVEN there are effects returned EXPECT result returned 1`] = ` +{ + "embed": { + "color": 3166394, + "description": "name x1", + "footer": { + "icon_url": undefined, + "text": "Page 1 of 1", + }, + "title": "Effects", + }, + "row": { + "components": [ + { + "custom_id": "effects list 0", + "disabled": true, + "emoji": undefined, + "label": "Previous", + "style": 1, + "type": 2, + }, + { + "custom_id": "effects list 2", + "disabled": true, + "emoji": undefined, + "label": "Next", + "style": 1, + "type": 2, + }, + ], + "type": 1, + }, +} +`; + +exports[`GenerateEffectEmbed GIVEN there are no effects returned EXPECT result returned 1`] = ` +{ + "embed": { + "color": 3166394, + "description": "*none*", + "footer": { + "icon_url": undefined, + "text": "Page 1 of 1", + }, + "title": "Effects", + }, + "row": { + "components": [ + { + "custom_id": "effects list 0", + "disabled": true, + "emoji": undefined, + "label": "Previous", + "style": 1, + "type": 2, + }, + { + "custom_id": "effects list 2", + "disabled": true, + "emoji": undefined, + "label": "Next", + "style": 1, + "type": 2, + }, + ], + "type": 1, + }, +} +`;