Create config command tests
This commit is contained in:
parent
2377ec165f
commit
bd6858daa3
2 changed files with 51 additions and 14 deletions
|
@ -140,22 +140,17 @@ export default class Config extends Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
private async SetValue(interaction: CommandInteraction, server: Server) {
|
private async SetValue(interaction: CommandInteraction, server: Server) {
|
||||||
const key = interaction.options.get('key');
|
const key = interaction.options.get('key', true);
|
||||||
const value = interaction.options.get('value');
|
const value = interaction.options.get('value', true);
|
||||||
|
|
||||||
if (!key || !key.value || !value || !value.value) {
|
|
||||||
await interaction.reply('Fields are required.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const setting = server.Settings.filter(x => x.Key == key.value)[0];
|
const setting = server.Settings.filter(x => x.Key == key.value)[0];
|
||||||
|
|
||||||
if (setting) {
|
if (setting) {
|
||||||
setting.UpdateBasicDetails(key.value.toString(), value.value.toString());
|
setting.UpdateBasicDetails(key.value!.toString(), value.value!.toString());
|
||||||
|
|
||||||
await setting.Save(Setting, setting);
|
await setting.Save(Setting, setting);
|
||||||
} else {
|
} else {
|
||||||
const newSetting = new Setting(key.value.toString(), value.value.toString());
|
const newSetting = new Setting(key.value!.toString(), value.value!.toString());
|
||||||
|
|
||||||
await newSetting.Save(Setting, newSetting);
|
await newSetting.Save(Setting, newSetting);
|
||||||
|
|
||||||
|
|
|
@ -558,13 +558,55 @@ describe("set", () => {
|
||||||
expect(server.Settings[0].Save).toHaveBeenCalledWith(Setting, server.Settings[0]);
|
expect(server.Settings[0].Save).toHaveBeenCalledWith(Setting, server.Settings[0]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test.todo("GIVEN setting is not set, EXPECT setting to be added");
|
test("GIVEN setting is not set, EXPECT setting to be added", async () => {
|
||||||
|
let savedSetting: Setting | undefined;
|
||||||
|
|
||||||
test.todo("GIVEN key is null, EXPECT error");
|
// Assert
|
||||||
|
const interaction = {
|
||||||
|
isChatInputCommand: jest.fn().mockReturnValue(true),
|
||||||
|
guildId: "guildId",
|
||||||
|
options: {
|
||||||
|
getSubcommand: jest.fn().mockReturnValue("set"),
|
||||||
|
get: jest.fn().mockReturnValueOnce({
|
||||||
|
value: "test.key",
|
||||||
|
}).mockReturnValue({
|
||||||
|
value: "54321",
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
test.todo("GIVEN key.value is undefined, EXPECT error");
|
const server = {
|
||||||
|
Settings: [],
|
||||||
|
AddSettingToServer: jest.fn(),
|
||||||
|
Save: jest.fn(),
|
||||||
|
} as unknown as Server;
|
||||||
|
|
||||||
test.todo("GIVEN value is null, EXPECT error");
|
Server.FetchOneById = jest.fn().mockResolvedValue(server);
|
||||||
|
|
||||||
test.todo("GIVEN value.value is undefined, EXPECT error");
|
Setting.prototype.Save = jest.fn().mockImplementation((_, setting: Setting) => {
|
||||||
|
savedSetting = setting;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Act
|
||||||
|
const command = new Command();
|
||||||
|
await command.execute(interaction);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(Setting.prototype.Save).toHaveBeenCalledTimes(1);
|
||||||
|
expect(Setting.prototype.Save).toHaveBeenCalledWith(Setting, expect.any(Setting));
|
||||||
|
|
||||||
|
expect(server.AddSettingToServer).toHaveBeenCalledTimes(1);
|
||||||
|
expect(server.AddSettingToServer).toHaveBeenCalledWith(expect.any(Setting));
|
||||||
|
|
||||||
|
expect(server.Save).toHaveBeenCalledTimes(1);
|
||||||
|
expect(server.Save).toHaveBeenCalledWith(Server, server);
|
||||||
|
|
||||||
|
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.reply).toHaveBeenCalledWith("Setting has been set.");
|
||||||
|
|
||||||
|
expect(savedSetting).toBeDefined();
|
||||||
|
expect(savedSetting!.Key).toBe("test.key");
|
||||||
|
expect(savedSetting!.Value).toBe("54321");
|
||||||
|
});
|
||||||
});
|
});
|
Loading…
Reference in a new issue