Create config tests
This commit is contained in:
parent
00f6a60a43
commit
d5a5282371
1 changed files with 215 additions and 9 deletions
|
@ -1,21 +1,227 @@
|
||||||
|
import { CommandInteraction, PermissionsBitField, SlashCommandBuilder, SlashCommandRoleOption } from "discord.js";
|
||||||
|
import Config from "../../../src/commands/Role/config";
|
||||||
|
import Role from "../../../src/database/entities/Role";
|
||||||
|
import Server from "../../../src/database/entities/Server";
|
||||||
|
|
||||||
describe('constructor', () => {
|
describe('constructor', () => {
|
||||||
test.todo('EXPECT properties to be set');
|
test('EXPECT properties to be set', () => {
|
||||||
|
const config = new Config();
|
||||||
|
|
||||||
|
expect(config.CommandBuilder).toBeDefined();
|
||||||
|
|
||||||
|
const commandBuilder = config.CommandBuilder as SlashCommandBuilder;
|
||||||
|
|
||||||
|
expect(commandBuilder.name).toBe("configrole");
|
||||||
|
expect(commandBuilder.description).toBe("Toggle your roles");
|
||||||
|
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.ManageRoles.toString());
|
||||||
|
expect(commandBuilder.options.length).toBe(1);
|
||||||
|
|
||||||
|
const roleOption = commandBuilder.options[0] as SlashCommandRoleOption;
|
||||||
|
|
||||||
|
expect(roleOption.name).toBe("role");
|
||||||
|
expect(roleOption.description).toBe("The role name");
|
||||||
|
expect(roleOption.required).toBe(true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("execute", () => {
|
describe("execute", () => {
|
||||||
test.todo("GIVEN role is marked as assignable, EXPECT role to be removed");
|
test("GIVEN role is marked as assignable, EXPECT role to be removed", async () => {
|
||||||
|
const role = {
|
||||||
|
role: {
|
||||||
|
id: "roleId",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
test.todo("GIVEN role is not marked as assignable, EXPECT role to be added");
|
const interaction = {
|
||||||
|
guildId: "guildId",
|
||||||
|
guild: {},
|
||||||
|
member: {},
|
||||||
|
options: {
|
||||||
|
get: jest.fn().mockReturnValue(role),
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
test.todo("GIVEN interaction.guildId is null, EXPECT error");
|
Role.FetchOneByRoleId = jest.fn().mockResolvedValue({});
|
||||||
|
Role.Remove = jest.fn();
|
||||||
|
|
||||||
test.todo("GIVEN interaction.guild is null, EXPECT error");
|
const config = new Config();
|
||||||
|
await config.execute(interaction);
|
||||||
|
|
||||||
test.todo("GIVEN interaction.member is null, EXPECT error");
|
expect(Role.FetchOneByRoleId).toHaveBeenCalledTimes(1);
|
||||||
|
expect(Role.FetchOneByRoleId).toHaveBeenCalledWith("roleId");
|
||||||
|
|
||||||
test.todo("GIVEN role is null, EXPECT error");
|
expect(Role.Remove).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
test.todo("GIVEN role.role is undefined, EXPECT error");
|
expect(interaction.options.get).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.options.get).toHaveBeenCalledWith("role");
|
||||||
|
|
||||||
test.todo("GIVEN server is not configured in the database, EXPECT error");
|
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.reply).toHaveBeenCalledWith("Removed role from configuration.");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("GIVEN role is not marked as assignable, EXPECT role to be added", async () => {
|
||||||
|
let newRole: Role | undefined;
|
||||||
|
|
||||||
|
const role = {
|
||||||
|
role: {
|
||||||
|
id: "roleId",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const interaction = {
|
||||||
|
guildId: "guildId",
|
||||||
|
guild: {},
|
||||||
|
member: {},
|
||||||
|
options: {
|
||||||
|
get: jest.fn().mockReturnValue(role),
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
|
Role.FetchOneByRoleId = jest.fn().mockResolvedValue(null);
|
||||||
|
Role.prototype.SetServer = jest.fn();
|
||||||
|
Role.prototype.Save = jest.fn().mockImplementation((_, role) => {
|
||||||
|
newRole = role;
|
||||||
|
});
|
||||||
|
|
||||||
|
Server.FetchOneById = jest.fn().mockResolvedValue({});
|
||||||
|
|
||||||
|
const config = new Config();
|
||||||
|
await config.execute(interaction);
|
||||||
|
|
||||||
|
expect(Role.FetchOneByRoleId).toHaveBeenCalledTimes(1);
|
||||||
|
expect(Role.FetchOneByRoleId).toHaveBeenCalledWith("roleId");
|
||||||
|
|
||||||
|
expect(Role.prototype.SetServer).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
|
expect(Role.prototype.Save).toHaveBeenCalledTimes(1);
|
||||||
|
expect(newRole).toBeDefined();
|
||||||
|
expect(newRole!.RoleId).toBe("roleId");
|
||||||
|
|
||||||
|
expect(interaction.options.get).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.options.get).toHaveBeenCalledWith("role");
|
||||||
|
|
||||||
|
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.reply).toHaveBeenCalledWith("Added role to configuration.");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("GIVEN interaction.guildId is null, EXPECT error", async () => {
|
||||||
|
const interaction = {
|
||||||
|
guildId: null,
|
||||||
|
options: {
|
||||||
|
get: jest.fn(),
|
||||||
|
}
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
|
const config = new Config();
|
||||||
|
await config.execute(interaction);
|
||||||
|
|
||||||
|
expect(interaction.options.get).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("GIVEN interaction.guild is null, EXPECT error", async () => {
|
||||||
|
const interaction = {
|
||||||
|
guildId: "guildId",
|
||||||
|
guild: null,
|
||||||
|
options: {
|
||||||
|
get: jest.fn(),
|
||||||
|
}
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
|
const config = new Config();
|
||||||
|
await config.execute(interaction);
|
||||||
|
|
||||||
|
expect(interaction.options.get).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("GIVEN interaction.member is null, EXPECT error", async () => {
|
||||||
|
const interaction = {
|
||||||
|
guildId: "guildId",
|
||||||
|
guild: {},
|
||||||
|
member: null,
|
||||||
|
options: {
|
||||||
|
get: jest.fn(),
|
||||||
|
}
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
|
const config = new Config();
|
||||||
|
await config.execute(interaction);
|
||||||
|
|
||||||
|
expect(interaction.options.get).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("GIVEN role is null, EXPECT error", async () => {
|
||||||
|
const interaction = {
|
||||||
|
guildId: "guildId",
|
||||||
|
guild: {},
|
||||||
|
member: {},
|
||||||
|
options: {
|
||||||
|
get: jest.fn().mockReturnValue(null),
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
|
const config = new Config();
|
||||||
|
await config.execute(interaction);
|
||||||
|
|
||||||
|
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("GIVEN role.role is undefined, EXPECT error", async () => {
|
||||||
|
const role = {
|
||||||
|
role: null,
|
||||||
|
}
|
||||||
|
|
||||||
|
const interaction = {
|
||||||
|
guildId: "guildId",
|
||||||
|
guild: {},
|
||||||
|
member: {},
|
||||||
|
options: {
|
||||||
|
get: jest.fn().mockReturnValue(role),
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
|
const config = new Config();
|
||||||
|
await config.execute(interaction);
|
||||||
|
|
||||||
|
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("GIVEN server is not configured in the database, EXPECT error", async () => {
|
||||||
|
let newRole: Role | undefined;
|
||||||
|
|
||||||
|
const role = {
|
||||||
|
role: {
|
||||||
|
id: "roleId",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const interaction = {
|
||||||
|
guildId: "guildId",
|
||||||
|
guild: {},
|
||||||
|
member: {},
|
||||||
|
options: {
|
||||||
|
get: jest.fn().mockReturnValue(role),
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
|
Role.FetchOneByRoleId = jest.fn().mockResolvedValue(null);
|
||||||
|
Role.prototype.SetServer = jest.fn();
|
||||||
|
Role.prototype.Save = jest.fn().mockImplementation((_, role) => {
|
||||||
|
newRole = role;
|
||||||
|
});
|
||||||
|
|
||||||
|
Server.FetchOneById = jest.fn().mockResolvedValue(null);
|
||||||
|
|
||||||
|
const config = new Config();
|
||||||
|
await config.execute(interaction);
|
||||||
|
|
||||||
|
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.reply).toHaveBeenCalledWith("This server has not been setup.");
|
||||||
|
});
|
||||||
});
|
});
|
Loading…
Reference in a new issue