WIP: Split up moon counter from the database #489

Draft
Vylpes wants to merge 16 commits from feature/300-moon-set into develop
5 changed files with 226 additions and 32 deletions
Showing only changes of commit 056783bc44 - Show all commits

View file

@ -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", () => { 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);
});
}); });

View file

@ -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,
},
]
`;

View file

@ -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 List from "../../../../src/buttonEvents/304276391837302787/moons/list";
import UserSetting from "../../../../src/database/entities/UserSetting"; import UserSetting from "../../../../src/database/entities/UserSetting";
import Moon from "../../../../src/database/entities/304276391837302787/Moon"; import Moon from "../../../../src/database/entities/304276391837302787/Moon";
describe("GIVEN happy flow", () => {
let updatedWithEmbeds: EmbedBuilder[] | undefined;
let updatedWithRows: ActionRowBuilder<ButtonBuilder>[] | 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", () => { describe("GIVEN interaction.guild is null", () => {
const interaction = { const interaction = {
guild: null, guild: null,
@ -209,27 +266,3 @@ describe("GIVEN no moons on current page", () => {
expect(updatedWith![0].data.description).toBe("*none*"); 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");
});
});

View file

@ -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<String>,
"MoonNumber": 2,
"UserId": "userId",
"WhenCreated": Any<Date>,
"WhenUpdated": Any<Date>,
}
`;

View file

@ -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", () => { describe("GIVEN description is null", () => {
test.todo("EXPECT error replied"); test.todo("EXPECT error replied");
}); });
@ -13,7 +94,3 @@ describe("GIVEN moon count setting exists", () => {
describe("GIVEN moon count setting does not exist", () => { describe("GIVEN moon count setting does not exist", () => {
test.todo("EXPECT new entity to be created"); test.todo("EXPECT new entity to be created");
}); });
test.todo("EXPECT setting to be saved");
test.todo("EXPECT embed to be replied");