From 1f5965ae790b2e127aa7698eed1848de82b65aa5 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 14 Dec 2024 18:04:04 +0000 Subject: [PATCH] Add tests --- .../__snapshots__/moons.test.ts.snap | 69 ++++++++++++ .../commands/304276391837302787/moons.test.ts | 104 +++++++++++++++++- tests/database/entites/UserSetting.test.ts | 60 +++++++++- .../__snapshots__/UserSetting.test.ts.snap | 12 ++ 4 files changed, 238 insertions(+), 7 deletions(-) create mode 100644 tests/commands/304276391837302787/__snapshots__/moons.test.ts.snap create mode 100644 tests/database/entites/__snapshots__/UserSetting.test.ts.snap diff --git a/tests/commands/304276391837302787/__snapshots__/moons.test.ts.snap b/tests/commands/304276391837302787/__snapshots__/moons.test.ts.snap new file mode 100644 index 0000000..ec8a85e --- /dev/null +++ b/tests/commands/304276391837302787/__snapshots__/moons.test.ts.snap @@ -0,0 +1,69 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`constructor EXPECT CommandBuilder to be defined correctly 1`] = ` +{ + "contexts": undefined, + "default_member_permissions": undefined, + "default_permission": undefined, + "description": "View and create moons", + "description_localizations": undefined, + "dm_permission": undefined, + "integration_types": undefined, + "name": "moons", + "name_localizations": undefined, + "nsfw": undefined, + "options": [ + { + "description": "List moons you have obtained", + "description_localizations": undefined, + "name": "list", + "name_localizations": undefined, + "options": [ + { + "description": "The user to view (Defaults to yourself)", + "description_localizations": undefined, + "name": "user", + "name_localizations": undefined, + "required": false, + "type": 6, + }, + { + "autocomplete": undefined, + "choices": undefined, + "description": "The page to start with", + "description_localizations": undefined, + "max_value": undefined, + "min_value": undefined, + "name": "page", + "name_localizations": undefined, + "required": false, + "type": 10, + }, + ], + "type": 1, + }, + { + "description": "Add a moon to your count!", + "description_localizations": undefined, + "name": "add", + "name_localizations": undefined, + "options": [ + { + "autocomplete": undefined, + "choices": undefined, + "description": "What deserved a moon?", + "description_localizations": undefined, + "max_length": undefined, + "min_length": undefined, + "name": "description", + "name_localizations": undefined, + "required": true, + "type": 3, + }, + ], + "type": 1, + }, + ], + "type": 1, +} +`; diff --git a/tests/commands/304276391837302787/moons.test.ts b/tests/commands/304276391837302787/moons.test.ts index 187979a..e122941 100644 --- a/tests/commands/304276391837302787/moons.test.ts +++ b/tests/commands/304276391837302787/moons.test.ts @@ -1,17 +1,113 @@ +import { ChatInputCommandInteraction, CommandInteraction } from "discord.js"; +import Moons from "../../../src/commands/304276391837302787/moons"; +import * as AddMoon from "../../../src/commands/304276391837302787/moons/add"; +import * as ListMoons from "../../../src/commands/304276391837302787/moons/list"; + +beforeEach(() => { + jest.resetAllMocks(); +}); + describe("constructor", () => { - test.todo("EXPECT CommandBuilder to be defined correctly"); + let moons: Moons; + + beforeEach(() => { + moons = new Moons(); + }); + + test("EXPECT CommandBuilder to be defined correctly", () => { + expect(moons.CommandBuilder).toMatchSnapshot(); + }); }); describe("execute", () => { describe("GIVEN interaction is not a chat input command", () => { - test.todo("EXPECT nothing to happen"); + const moons = new Moons(); + + let interaction: CommandInteraction; + + let listMoonsSpy: jest.SpyInstance; + let addMoonSpy: jest.SpyInstance; + + beforeEach(async () => { + listMoonsSpy = jest.spyOn(ListMoons, "default"); + addMoonSpy = jest.spyOn(AddMoon, "default"); + + interaction = { + isChatInputCommand: jest.fn().mockReturnValue(false), + } as unknown as CommandInteraction; + + await moons.execute(interaction); + }); + + test("EXPECT interaction.isChatInputCommand to have been called", () => { + expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1); + }); + + test("EXPECT nothing to happen", () => { + expect(listMoonsSpy).not.toHaveBeenCalled(); + expect(addMoonSpy).not.toHaveBeenCalled(); + }); }); describe("GIVEN interaction subcommand is list", () => { - test.todo("EXPECT ListMoons to be called"); + const moons = new Moons(); + + let interaction: ChatInputCommandInteraction; + + let listMoonsSpy: jest.SpyInstance; + + beforeEach(async () => { + listMoonsSpy = jest.spyOn(ListMoons, "default") + .mockImplementation(); + + interaction = { + isChatInputCommand: jest.fn().mockReturnValue(true), + options: { + getSubcommand: jest.fn().mockReturnValue("list"), + }, + } as unknown as ChatInputCommandInteraction; + + await moons.execute(interaction); + }); + + test("EXPECT interaction.options.getSubcommand to have been called", () => { + expect(interaction.options.getSubcommand).toHaveBeenCalledTimes(1); + }); + + test("EXPECT ListMoons to be called", () => { + expect(listMoonsSpy).toHaveBeenCalledTimes(1); + expect(listMoonsSpy).toHaveBeenCalledWith(interaction); + }); }); describe("GIVEN interaction subcommand is add", () => { - test.todo("EXPECT AddMoon to be called"); + const moons = new Moons(); + + let interaction: ChatInputCommandInteraction; + + let addMoonSpy: jest.SpyInstance; + + beforeEach(async () => { + addMoonSpy = jest.spyOn(AddMoon, "default") + .mockImplementation(); + + interaction = { + isChatInputCommand: jest.fn().mockReturnValue(true), + options: { + getSubcommand: jest.fn().mockReturnValue("add"), + }, + } as unknown as ChatInputCommandInteraction; + + await moons.execute(interaction); + }); + + test("EXPECT interaction.options.getSubcommand to have been called", () => { + expect(interaction.options.getSubcommand).toHaveBeenCalledTimes(1); + }); + + test("EXPECT AddMoon to be called", () => { + expect(addMoonSpy).toHaveBeenCalledTimes(1); + expect(addMoonSpy).toHaveBeenCalledWith(interaction); + }); }); }); \ No newline at end of file diff --git a/tests/database/entites/UserSetting.test.ts b/tests/database/entites/UserSetting.test.ts index 238e23d..f062cc7 100644 --- a/tests/database/entites/UserSetting.test.ts +++ b/tests/database/entites/UserSetting.test.ts @@ -1,11 +1,65 @@ +import AppDataSource from "../../../src/database/dataSources/appDataSource"; +import UserSetting from "../../../src/database/entities/UserSetting"; + describe("constructor", () => { - test.todo("EXPECT settings to be configured"); + let userSetting: UserSetting; + + beforeEach(() => { + userSetting = new UserSetting("userId", "key", "value"); + }); + + test("EXPECT settings to be configured", () => { + expect(userSetting).toMatchSnapshot({ + Id: expect.any(String), + WhenCreated: expect.any(Date), + WhenUpdated: expect.any(Date), + }); + }); }); describe("UpdateValue", () => { - test.todo("EXPECT value to be updated"); + let userSetting: UserSetting; + + beforeEach(() => { + userSetting = new UserSetting("userId", "key", "value"); + + userSetting.UpdateValue("newValue"); + }); + + test("EXPECT value to be updated", () => { + expect(userSetting.Value).toBe("newValue"); + }); }); describe("FetchOneByKey", () => { - test.todo("EXPECT single entity returned"); + let result: UserSetting | null; + let userSetting: UserSetting; + + let findOneMock: jest.Mock; + + beforeEach(async () => { + userSetting = new UserSetting("userId", "key", "value"); + + findOneMock = jest.fn().mockResolvedValue(userSetting); + + AppDataSource.getRepository = jest.fn().mockReturnValue({ + findOne: findOneMock, + }); + + result = await UserSetting.FetchOneByKey("userId", "key"); + }); + + test("EXPECT getRepository to have been called", () => { + expect(AppDataSource.getRepository).toHaveBeenCalledTimes(1); + expect(AppDataSource.getRepository).toHaveBeenCalledWith(UserSetting); + }); + + test("EXPECT repository.findOne to have been called", () => { + expect(findOneMock).toHaveBeenCalledTimes(1); + expect(findOneMock).toHaveBeenCalledWith({ where: { UserId: "userId", Key: "key" }, relations: {}}); + }) + + test("EXPECT single entity returned", () => { + expect(result).toBe(userSetting); + }); }); diff --git a/tests/database/entites/__snapshots__/UserSetting.test.ts.snap b/tests/database/entites/__snapshots__/UserSetting.test.ts.snap new file mode 100644 index 0000000..1542d21 --- /dev/null +++ b/tests/database/entites/__snapshots__/UserSetting.test.ts.snap @@ -0,0 +1,12 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`constructor EXPECT settings to be configured 1`] = ` +{ + "Id": Any, + "Key": "key", + "UserId": "userId", + "Value": "value", + "WhenCreated": Any, + "WhenUpdated": Any, +} +`;