Refactored tests

This commit is contained in:
chroventer
2020-09-29 12:21:08 -07:00
parent 8d8cb7d718
commit ad7bd30468
+47 -59
View File
@@ -1,27 +1,27 @@
import { assert } from "https://deno.land/std@0.70.0/testing/asserts.ts";
import { assertEquals, delay } from "../deps.ts";
import {
botID,
cache,
createClient,
createGuildRole,
deleteRole,
deleteServer,
editRole,
Intents,
Role,
} from "../mod.ts";
import { createServer } from "../src/handlers/guild.ts";
import { Guild } from "../src/structures/guild.ts";
import { Role } from "../src/structures/role.ts";
const token = "NzUxNzkyMDE3MzUyMjk0NDQx.X1OO4A.GFAh2FCeDNChFQfd3ZNiu6dEAmQ";
//const token = Deno.env.get("DISCORD_TOKEN");
if (!token) throw "No Token Provided!";
const token = Deno.env.get("DISCORD_TOKEN");
if (!token) throw "Token is not provided";
createClient({
token,
intents: [Intents.GUILD_MESSAGES, Intents.GUILDS],
});
// Default options for all test cases
const testOptions = {
sanitizeOps: false,
sanitizeResources: false,
@@ -30,101 +30,89 @@ const testOptions = {
Deno.test({
name: "Connecting to the gateway",
fn: async () => {
// Delay the execution by 15 seconds
await delay(15000);
assertEquals(botID, "675412054529540107");
// Check whether botID is nil or not
assert(botID);
},
...testOptions,
});
let createdGuild: Guild;
let guildID: string;
Deno.test({
name: "Create a guild with and without options",
name: "Create a guild (without options)",
async fn() {
// Create a test guild
const guild = (await createServer({
// Create a test guild with the name "Discordeno Test"
const createdGuild = (await createServer({
name: "Discordeno Test",
})) as Guild;
assertEquals(typeof guild.id, "string");
createdGuild = guild;
// Check whether createdGuild is nil or not
assert(createdGuild);
guildID = createdGuild.id;
},
...testOptions,
});
// Roles
let createdRole: Role;
let roleID: string;
Deno.test({
name: "Create a role with and without options",
name: "Create a role in a guild (~with~ and without options)",
fn: async () => {
if (!createdGuild.id) throw "The test guild does not exist.";
const role1 = await createGuildRole(createdGuild.id, {
// Create a role "Role 1" in the test guild
const createdRole = await createGuildRole(guildID, {
name: "Role 1",
});
assertEquals(role1.id, "string");
// with options
const role2 = await createGuildRole(createdGuild.id, {
name: "Role 2",
color: 15277667,
hoist: true,
permissions: ["ADMINISTRATOR"],
mentionable: true,
});
assertEquals(typeof role2.id, "string");
assertEquals(role2.name, "Role 2");
assertEquals(role2.color, 15277667);
assertEquals(role2.hoist, true);
assertEquals(role2.mentionable, true);
assertEquals(role2.permissions, 0x00000008);
createdRole = role2;
// Check whether the created role is nil or not
assert(createdRole.id);
roleID = createdRole.id;
},
...testOptions,
});
Deno.test({
name: "Edit a role",
name: "Edit a role in a guild",
fn: async () => {
if (!createdGuild.id) throw "The guild id was not present";
await editRole(createdGuild.id, createdRole.id, {
const updatedRole = (await editRole(guildID, roleID, {
name: "Edited Role",
color: 4320244,
hoist: false,
permissions: ["READ_MESSAGE_HISTORY"],
mentionable: false,
});
})) as Role;
const role = cache.guilds.get(createdRole.id)?.roles.get(createdRole.id);
if (!role) throw "Role not found on edit.";
assertEquals(typeof role.id, "string");
assertEquals(role.name, "Discordeno Edited");
assertEquals(role.color, 4320244);
assertEquals(role.hoist, false);
assertEquals(role.mentionable, false);
createdRole = role;
// Assertions
assert(updatedRole.id);
assertEquals(updatedRole.name, "Edited Role");
assertEquals(updatedRole.color, 4320244);
assertEquals(updatedRole.hoist, false);
assertEquals(updatedRole.mentionable, false);
roleID = updatedRole.id;
},
...testOptions,
});
Deno.test({
name: "Delete Role",
fn: async () => {
await deleteRole(createdGuild.id, createdRole.id);
createdRole.id = "";
assertEquals(createdRole.id, "");
name: "Delete a role from the guild",
async fn() {
await deleteRole(guildID, roleID);
roleID = "";
assertEquals(roleID, "");
},
});
Deno.test({
name: "Deleting guild(Bot is owner)",
fn: async () => {
if (!createdGuild.id) throw "The guild id was not present.";
await deleteServer(createdGuild.id);
createdGuild.id = "";
assertEquals(createdGuild, "");
name: "Delete a guild",
async fn() {
await deleteServer(guildID);
guildID = "";
assertEquals(guildID, "");
},
...testOptions,
});