From 4735384d6874c2fcb151f1a7d3694fea88fc95f1 Mon Sep 17 00:00:00 2001 From: Starman <30315137+Starman3787@users.noreply.github.com> Date: Wed, 17 Jul 2024 17:38:17 +0100 Subject: [PATCH] more unit tests --- test/util/builder/buttonBuilder.js | 139 +++++++++++++++++++++++++++++ test/util/builder/embedBuilder.js | 38 ++++---- 2 files changed, 158 insertions(+), 19 deletions(-) create mode 100644 test/util/builder/buttonBuilder.js diff --git a/test/util/builder/buttonBuilder.js b/test/util/builder/buttonBuilder.js new file mode 100644 index 000000000..1e6cbe07e --- /dev/null +++ b/test/util/builder/buttonBuilder.js @@ -0,0 +1,139 @@ +let expect; +before(async () => { + expect = (await import("chai")).expect; +}); +const { COMPONENT_TYPES } = require("../../../src/constants"); +const Button = require("../../../src/util/builder/buttonBuilder"); + +describe("ButtonBuilder", () => { + context("check import", function () { + it("should be an object", function () { + const button = new Button(); + expect(button).to.be.an("object"); + }); + }); + + context("check setLabel", function () { + it("should have method setLabel", function () { + const button = new Button(); + expect(button).to.respondTo("setLabel"); + }); + it("should set the label of the button", function () { + const button = new Button(); + button.setLabel("test"); + expect(button.label).to.equal("test"); + }); + it("should throw an error if no label is provided", function () { + const button = new Button(); + expect(() => button.setLabel()).to.throw( + TypeError, + "GLUON: Button label must be provided." + ); + }); + }); + + context("check setEmoji", function () { + it("should have method setEmoji", function () { + const button = new Button(); + expect(button).to.respondTo("setEmoji"); + }); + it("should set the emoji of the button", function () { + const button = new Button(); + button.setEmoji("👍"); + expect(button.emoji.name).to.equal("👍"); + }); + it("should set the emoji id of the button", function () { + const button = new Button(); + button.setEmoji("<:bitcoin:844240546246950922>"); + expect(button.emoji.id).to.equal("844240546246950922"); + expect(button.emoji.name).to.equal("bitcoin"); + expect(button.emoji.animated).to.equal(false); + }); + it("should throw an error if no emoji is provided", function () { + const button = new Button(); + expect(() => button.setEmoji()).to.throw( + TypeError, + "GLUON: The emoji must be a string." + ); + }); + it("should throw an error if string is not an emoji", function () { + const button = new Button(); + expect(() => button.setEmoji("test")).to.throw( + TypeError, + "GLUON: Button emoji must be provided." + ); + }); + }); + + context("check setStyle", function () { + it("should have method setStyle", function () { + const button = new Button(); + expect(button).to.respondTo("setStyle"); + }); + it("should set the style of the button", function () { + const button = new Button(); + button.setStyle(1); + expect(button.style).to.equal(1); + }); + }); + + context("check setCustomID", function () { + it("should have method setCustomID", function () { + const button = new Button(); + expect(button).to.respondTo("setCustomID"); + }); + it("should set the custom id of the button", function () { + const button = new Button(); + button.setCustomID("custom_id"); + expect(button.custom_id).to.equal("custom_id"); + }); + }); + + context("check setURL", function () { + it("should have method setURL", function () { + const button = new Button(); + expect(button).to.respondTo("setURL"); + }); + it("should set the url of the button", function () { + const button = new Button(); + button.setURL("https://example.com"); + expect(button.url).to.equal("https://example.com"); + }); + }); + + context("check setDisabled", function () { + it("should have method setDisabled", function () { + const button = new Button(); + expect(button).to.respondTo("setDisabled"); + }); + it("should set the disabled status of the button", function () { + const button = new Button(); + button.setDisabled(true); + expect(button.disabled).to.equal(true); + }); + }); + + context("check toJSON", function () { + it("should have method toJSON", function () { + const button = new Button(); + expect(button).to.respondTo("toJSON"); + }); + it("should return the button as an object", function () { + const button = new Button() + .setCustomID("custom_id") + .setLabel("test") + .setDisabled(true) + .setEmoji("👍") + .setStyle(1); + expect(button.toJSON()).to.deep.equal({ + type: COMPONENT_TYPES.BUTTON, + disabled: true, + label: "test", + style: 1, + emoji: { name: "👍", id: null }, + url: undefined, + custom_id: "custom_id", + }); + }); + }); +}); diff --git a/test/util/builder/embedBuilder.js b/test/util/builder/embedBuilder.js index 7d5b67389..f305fa949 100644 --- a/test/util/builder/embedBuilder.js +++ b/test/util/builder/embedBuilder.js @@ -67,8 +67,8 @@ describe("Embed", function () { }); it("should set the url of the embed", function () { const embed = new Embed(); - embed.setURL("url"); - expect(embed.url).to.equal("url"); + embed.setURL("https://example.com"); + expect(embed.url).to.equal("https://example.com"); }); it("should throw an error if the url is empty", function () { const embed = new Embed(); @@ -115,14 +115,14 @@ describe("Embed", function () { }); it("should set the footer of the embed", function () { const embed = new Embed(); - embed.setFooter("footer", "icon"); + embed.setFooter("footer", "https://example.com"); expect(embed.footer.text).to.equal("footer"); - expect(embed.footer.icon_url).to.equal("icon"); + expect(embed.footer.icon_url).to.equal("https://example.com"); }); it("should throw an error if the text is empty", function () { const embed = new Embed(); expect(() => { - embed.setFooter(undefined, "icon"); + embed.setFooter(undefined, "https://example.com"); }).to.throw(TypeError, "GLUON: Embed footer text must be provided."); }); it("should not allow the character limit to be exceeded", function () { @@ -139,8 +139,8 @@ describe("Embed", function () { }); it("should set the image of the embed", function () { const embed = new Embed(); - embed.setImage("image"); - expect(embed.image.url).to.equal("image"); + embed.setImage("https://example.com"); + expect(embed.image.url).to.equal("https://example.com"); }); }); @@ -151,8 +151,8 @@ describe("Embed", function () { }); it("should set the thumbnail of the embed", function () { const embed = new Embed(); - embed.setThumbnail("thumbnail"); - expect(embed.thumbnail.url).to.equal("thumbnail"); + embed.setThumbnail("https://example.com"); + expect(embed.thumbnail.url).to.equal("https://example.com"); }); it("should throw an error if the url is empty", function () { const embed = new Embed(); @@ -180,8 +180,8 @@ describe("Embed", function () { }); it("should set the author url of the embed", function () { const embed = new Embed(); - embed.setAuthor("author", "url"); - expect(embed.author.url).to.equal("url"); + embed.setAuthor("author", "https://example.com"); + expect(embed.author.url).to.equal("https://example.com"); }); it("should not allow the character limit to be exceeded", function () { const embed = new Embed(); @@ -263,7 +263,7 @@ describe("Embed", function () { const embed = new Embed(); embed.setTitle("title"); embed.setDescription("description"); - embed.setURL("url"); + embed.setURL("https://example.com"); embed.setColor("color"); embed.setTimestamp(123456); embed.setFooter("footer"); @@ -282,7 +282,7 @@ describe("Embed", function () { const embed = new Embed(); embed.setTitle("title"); embed.setDescription("description"); - embed.setURL("url"); + embed.setURL("https://example.com"); embed.setColor("color"); embed.setTimestamp(123456); embed.setFooter("footer"); @@ -303,29 +303,29 @@ describe("Embed", function () { const embed = new Embed(); embed.setTitle("title"); embed.setDescription("description"); - embed.setURL("url"); + embed.setURL("https://example.com"); embed.setColor("color"); embed.setTimestamp(123456); embed.setFooter("footer"); - embed.setImage("image"); - embed.setThumbnail("thumbnail"); + embed.setImage("https://example.com"); + embed.setThumbnail("https://example.com"); embed.setAuthor("author"); embed.addField("field", "fieldValue"); expect(embed.toJSON()).to.deep.equal({ type: "rich", title: "title", description: "description", - url: "url", + url: "https://example.com", color: hexToInt("color"), timestamp: new Date(123456 * 1000).toISOString(), footer: { text: "footer", }, image: { - url: "image", + url: "https://example.com", }, thumbnail: { - url: "thumbnail", + url: "https://example.com", }, author: { name: "author",