From ad7bd30468f71310ea220311072c0de0e36dfd23 Mon Sep 17 00:00:00 2001 From: chroventer Date: Tue, 29 Sep 2020 12:21:08 -0700 Subject: [PATCH] Refactored tests --- tests/mod.test.ts | 106 ++++++++++++++++++++-------------------------- 1 file changed, 47 insertions(+), 59 deletions(-) diff --git a/tests/mod.test.ts b/tests/mod.test.ts index 92fc30fe9..21281dff9 100644 --- a/tests/mod.test.ts +++ b/tests/mod.test.ts @@ -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, });