Compare commits

...

4 commits

Author SHA1 Message Date
3d8b5a8347 WIP: Start of config command tests
All checks were successful
Test / build (push) Successful in 16s
2024-05-06 16:34:17 +01:00
3afc28e02b Merge branch 'develop' into feature/258-fix-tests 2024-05-06 16:13:21 +01:00
932d3ac3d5 Update dependency jest-mock-extended to v3.0.7 (#432)
All checks were successful
Deploy To Stage / build (push) Successful in 7s
Deploy To Stage / deploy (push) Successful in 14s
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [jest-mock-extended](https://github.com/marchaos/jest-mock-extended) | dependencies | patch | [`3.0.6` -> `3.0.7`](https://renovatebot.com/diffs/npm/jest-mock-extended/3.0.6/3.0.7) |

---

### Release Notes

<details>
<summary>marchaos/jest-mock-extended (jest-mock-extended)</summary>

### [`v3.0.7`](https://github.com/marchaos/jest-mock-extended/releases/tag/3.0.7)

[Compare Source](77a64ae832...3.0.7)

Bumped ts-essentials

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4wLjAiLCJ1cGRhdGVkSW5WZXIiOiIzNy4wLjAiLCJ0YXJnZXRCcmFuY2giOiJkZXZlbG9wIn0=-->

Reviewed-on: #432
Co-authored-by: Renovate Bot <renovate@vylpes.com>
Co-committed-by: Renovate Bot <renovate@vylpes.com>
2024-05-06 16:11:58 +01:00
6412ad0ad4 Update dependency @types/node to v20.12.8 (#431)
All checks were successful
Deploy To Stage / build (push) Successful in 10s
Deploy To Stage / deploy (push) Successful in 15s
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node) ([source](https://github.com/DefinitelyTyped/DefinitelyTyped)) | devDependencies | patch | [`20.12.7` -> `20.12.8`](https://renovatebot.com/diffs/npm/@types%2fnode/20.12.7/20.12.8) |

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4wLjAiLCJ1cGRhdGVkSW5WZXIiOiIzNy4wLjAiLCJ0YXJnZXRCcmFuY2giOiJkZXZlbG9wIn0=-->

Reviewed-on: #431
Co-authored-by: Renovate Bot <renovate@vylpes.com>
Co-committed-by: Renovate Bot <renovate@vylpes.com>
2024-05-06 16:11:17 +01:00
3 changed files with 129 additions and 32 deletions

22
package-lock.json generated
View file

@ -1347,9 +1347,9 @@
} }
}, },
"node_modules/@types/node": { "node_modules/@types/node": {
"version": "20.12.7", "version": "20.12.8",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.7.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.8.tgz",
"integrity": "sha512-wq0cICSkRLVaf3UGLMGItu/PtdY7oaXaI/RVU+xliKVOtRna3PRY57ZDfztpDL0n11vfymMUnXv8QwYCO7L1wg==", "integrity": "sha512-NU0rJLJnshZWdE/097cdCBbyW1h4hEg0xpovcoAQYHl8dnEyp/NAOiE45pvc+Bd1Dt+2r94v2eGFpQJ4R7g+2w==",
"dependencies": { "dependencies": {
"undici-types": "~5.26.4" "undici-types": "~5.26.4"
} }
@ -4838,11 +4838,11 @@
} }
}, },
"node_modules/jest-mock-extended": { "node_modules/jest-mock-extended": {
"version": "3.0.6", "version": "3.0.7",
"resolved": "https://registry.npmjs.org/jest-mock-extended/-/jest-mock-extended-3.0.6.tgz", "resolved": "https://registry.npmjs.org/jest-mock-extended/-/jest-mock-extended-3.0.7.tgz",
"integrity": "sha512-DJuEoFzio0loqdX8NIwkbE9dgIXNzaj//pefOQxGkoivohpxbSQeNHCAiXkDNA/fmM4EIJDoZnSibP4w3dUJ9g==", "integrity": "sha512-7lsKdLFcW9B9l5NzZ66S/yTQ9k8rFtnwYdCNuRU/81fqDWicNDVhitTSPnrGmNeNm0xyw0JHexEOShrIKRCIRQ==",
"dependencies": { "dependencies": {
"ts-essentials": "^9.4.2" "ts-essentials": "^10.0.0"
}, },
"peerDependencies": { "peerDependencies": {
"jest": "^24.0.0 || ^25.0.0 || ^26.0.0 || ^27.0.0 || ^28.0.0 || ^29.0.0", "jest": "^24.0.0 || ^25.0.0 || ^26.0.0 || ^27.0.0 || ^28.0.0 || ^29.0.0",
@ -8083,11 +8083,11 @@
} }
}, },
"node_modules/ts-essentials": { "node_modules/ts-essentials": {
"version": "9.4.2", "version": "10.0.0",
"resolved": "https://registry.npmjs.org/ts-essentials/-/ts-essentials-9.4.2.tgz", "resolved": "https://registry.npmjs.org/ts-essentials/-/ts-essentials-10.0.0.tgz",
"integrity": "sha512-mB/cDhOvD7pg3YCLk2rOtejHjjdSi9in/IBYE13S+8WA5FBSraYf4V/ws55uvs0IvQ/l0wBOlXy5yBNZ9Bl8ZQ==", "integrity": "sha512-77FHNJEyysF9+1s4G6eejuA1lxw7uMchT3ZPy3CIbh7GIunffpshtM8pTe5G6N5dpOzNevqRHew859ceLWVBfw==",
"peerDependencies": { "peerDependencies": {
"typescript": ">=4.1.0" "typescript": ">=4.5.0"
}, },
"peerDependenciesMeta": { "peerDependenciesMeta": {
"typescript": { "typescript": {

View file

@ -70,7 +70,7 @@ export default class Config extends Command {
await this.SendHelpText(interaction); await this.SendHelpText(interaction);
break; break;
case 'reset': case 'reset':
await this.ResetValue(interaction); await this.ResetValue(interaction, server);
break; break;
case 'get': case 'get':
await this.GetValue(interaction); await this.GetValue(interaction);
@ -95,8 +95,6 @@ export default class Config extends Command {
} }
private async GetValue(interaction: CommandInteraction) { private async GetValue(interaction: CommandInteraction) {
if (!interaction.guildId) return;
const key = interaction.options.get('key'); const key = interaction.options.get('key');
if (!key || !key.value) { if (!key || !key.value) {
@ -104,7 +102,7 @@ export default class Config extends Command {
return; return;
} }
const server = await Server.FetchOneById<Server>(Server, interaction.guildId, [ const server = await Server.FetchOneById<Server>(Server, interaction.guildId!, [
"Settings", "Settings",
]); ]);
@ -128,7 +126,7 @@ export default class Config extends Command {
} }
} }
private async ResetValue(interaction: CommandInteraction) { private async ResetValue(interaction: CommandInteraction, server: Server) {
if (!interaction.guildId) return; if (!interaction.guildId) return;
const key = interaction.options.get('key'); const key = interaction.options.get('key');
@ -138,15 +136,6 @@ export default class Config extends Command {
return; return;
} }
const server = await Server.FetchOneById<Server>(Server, interaction.guildId, [
"Settings",
]);
if (!server) {
await interaction.reply('Server not found.');
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) {

View file

@ -1,6 +1,13 @@
import { ChatInputCommandInteraction, PermissionsBitField, SlashCommandBuilder, SlashCommandStringOption, SlashCommandSubcommandBuilder } from "discord.js"; import { ChatInputCommandInteraction, CommandInteraction, PermissionsBitField, SlashCommandBuilder, SlashCommandStringOption, SlashCommandSubcommandBuilder } from "discord.js";
import Command from "../../src/commands/config"; import Command from "../../src/commands/config";
import Server from "../../src/database/entities/Server"; import Server from "../../src/database/entities/Server";
import fs from "fs";
import EmbedColours from "../../src/constants/EmbedColours";
import Setting from "../../src/database/entities/Setting";
beforeEach(() => {
process.cwd = jest.fn().mockReturnValue("/cwd");
});
describe("constructor", () => { describe("constructor", () => {
test("EXPECT properties to be set", () => { test("EXPECT properties to be set", () => {
@ -155,20 +162,121 @@ describe("execute", () => {
}); });
describe("list", () => { describe("list", () => {
test.todo("EXPECT help text to be sent"); test("EXPECT help text to be sent", async () => {
let repliedWith: any;
// Assert
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
options: {
getSubcommand: jest.fn().mockReturnValue("list"),
},
reply: jest.fn().mockImplementation((options: any) => {
repliedWith = options.embeds[0];
}),
} as unknown as CommandInteraction;
Server.FetchOneById = jest.fn().mockResolvedValue({});
const readFileSyncMock = jest.spyOn(fs, "readFileSync").mockReturnValue("Example config text");
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(readFileSyncMock).toHaveBeenCalledTimes(1);
expect(readFileSyncMock).toHaveBeenCalledWith("/cwd/data/usage/config.txt");
expect(repliedWith).toBeDefined();
expect(repliedWith.data.color).toBe(EmbedColours.Ok);
expect(repliedWith.data.title).toBe("Config");
expect(repliedWith.data.description).toBe("Example config text");
});
}); });
describe("reset", () => { describe("reset", () => {
test.todo("EXPECT setting value to be set to default"); test("EXPECT setting value to be set to default", async () => {
// Assert
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
options: {
getSubcommand: jest.fn().mockReturnValue("reset"),
get: jest.fn().mockReturnValue({
value: "test.key",
}),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
test.todo("GIVEN interaction.guildId is null, EXPECT nothing to happen"); Server.FetchOneById = jest.fn().mockResolvedValue({
Settings: [
{
Key: "test.key",
Value: "12345",
},
],
});
test.todo("GIVEN key is null, EXPECT error"); Setting.Remove = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.options.get).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledWith("key");
expect(Server.FetchOneById).toHaveBeenCalledTimes(1);
expect(Server.FetchOneById).toHaveBeenCalledWith(Server, "guildId", [ "Settings" ]);
expect(Setting.Remove).toHaveBeenCalledTimes(1);
expect(Setting.Remove).toHaveBeenCalledWith(Setting, {
Key: "test.key",
Value: "12345",
});
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("The setting has been reset to the default.");
});
test("GIVEN key is null, EXPECT error", async () => {
// Assert
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
options: {
getSubcommand: jest.fn().mockReturnValue("reset"),
get: jest.fn().mockReturnValue(null),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Server.FetchOneById = jest.fn().mockResolvedValue({
Settings: [
{
Key: "test.key",
Value: "12345",
},
],
});
Setting.Remove = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
});
test.todo("GIVEN key.value is undefined, EXPECT error"); test.todo("GIVEN key.value is undefined, EXPECT error");
test.todo("GIVEN server is not found in database, EXPECT error");
test.todo("GIVEN setting is not found, EXPECT error"); test.todo("GIVEN setting is not found, EXPECT error");
}); });