Skip to content

Commit

Permalink
add tests and make modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Starman3787 committed Aug 23, 2024
1 parent c1c1604 commit 9c949cc
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/managers/GuildCacheOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class GuildCacheOptions {
*/
setMessageCaching(option) {
if (typeof option !== "boolean")
throw new TypeError("GLUON: Setting must be a boolean");
throw new TypeError("GLUON: Message caching must be a boolean");

if (option === true)
this.#_cache_options |= GLUON_GUILD_CACHING_OPTIONS.MESSAGES;
Expand All @@ -42,7 +42,7 @@ class GuildCacheOptions {
*/
setFileCaching(option) {
if (typeof option !== "boolean")
throw new TypeError("GLUON: Setting must be a boolean");
throw new TypeError("GLUON: File caching must be a boolean");

if (option === true)
this.#_cache_options |= GLUON_GUILD_CACHING_OPTIONS.FILES;
Expand All @@ -59,7 +59,7 @@ class GuildCacheOptions {
*/
setVoiceStateCaching(option) {
if (typeof option !== "boolean")
throw new TypeError("GLUON: Setting must be a boolean");
throw new TypeError("GLUON: Voice state caching must be a boolean");

if (option === true)
this.#_cache_options |= GLUON_GUILD_CACHING_OPTIONS.VOICE_STATES;
Expand All @@ -76,7 +76,7 @@ class GuildCacheOptions {
*/
setMemberCaching(option) {
if (typeof option !== "boolean")
throw new TypeError("GLUON: Setting must be a boolean");
throw new TypeError("GLUON: Member caching must be a boolean");

if (option === true)
this.#_cache_options |= GLUON_GUILD_CACHING_OPTIONS.MEMBERS;
Expand All @@ -93,7 +93,7 @@ class GuildCacheOptions {
*/
setRoleCaching(option) {
if (typeof option !== "boolean")
throw new TypeError("GLUON: Setting must be a boolean");
throw new TypeError("GLUON: Role caching must be a boolean");

if (option === true)
this.#_cache_options |= GLUON_GUILD_CACHING_OPTIONS.ROLES;
Expand All @@ -110,7 +110,7 @@ class GuildCacheOptions {
*/
setChannelCaching(option) {
if (typeof option !== "boolean")
throw new TypeError("GLUON: Setting must be a boolean");
throw new TypeError("GLUON: Channel caching must be a boolean");

if (option === true)
this.#_cache_options |= GLUON_GUILD_CACHING_OPTIONS.CHANNELS;
Expand All @@ -127,7 +127,7 @@ class GuildCacheOptions {
*/
setEmojiCaching(option) {
if (typeof option !== "boolean")
throw new TypeError("GLUON: Setting must be a boolean");
throw new TypeError("GLUON: Emoji caching must be a boolean");

if (option === true)
this.#_cache_options |= GLUON_GUILD_CACHING_OPTIONS.EMOJIS;
Expand All @@ -144,7 +144,7 @@ class GuildCacheOptions {
*/
setThreadCaching(option) {
if (typeof option !== "boolean")
throw new TypeError("GLUON: Setting must be a boolean");
throw new TypeError("GLUON: Thread caching must be a boolean");

if (option === true)
this.#_cache_options |= GLUON_GUILD_CACHING_OPTIONS.THREADS;
Expand All @@ -161,7 +161,7 @@ class GuildCacheOptions {
*/
setInviteCaching(option) {
if (typeof option !== "boolean")
throw new TypeError("GLUON: Setting must be a boolean");
throw new TypeError("GLUON: Invite caching must be a boolean");

if (option === true)
this.#_cache_options |= GLUON_GUILD_CACHING_OPTIONS.INVITES;
Expand All @@ -178,7 +178,7 @@ class GuildCacheOptions {
*/
setScheduledEventCaching(option) {
if (typeof option !== "boolean")
throw new TypeError("GLUON: Setting must be a boolean");
throw new TypeError("GLUON: Scheduled event caching must be a boolean");

if (option === true)
this.#_cache_options |= GLUON_GUILD_CACHING_OPTIONS.SCHEDULED_EVENTS;
Expand Down Expand Up @@ -334,7 +334,11 @@ class GuildCacheOptions {
* @public
*/
toString() {
return `GuildCacheOptions { fileCaching: ${this.fileCaching}, messageCaching: ${this.messageCaching}, voiceStateCaching: ${this.voiceStateCaching}, memberCaching: ${this.memberCaching}, roleCaching: ${this.roleCaching}, channelCaching: ${this.channelCaching}, emojiCaching: ${this.emojiCaching}, threadCaching: ${this.threadCaching}, inviteCaching: ${this.inviteCaching} }`;
return `GuildCacheOptions { ${Object.entries(GLUON_GUILD_CACHING_OPTIONS)
.map(
([key, value]) => `${key}: ${(this.#_cache_options & value) === value}`,
)
.join(", ")} }`;
}

/**
Expand Down
157 changes: 157 additions & 0 deletions test/managers/GuildCacheOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { expect } from "chai";
import GuildCacheOptions from "../../src/managers/GuildCacheOptions.js";
import { GLUON_GUILD_CACHING_OPTIONS } from "../../src/constants.js";

describe("GuildCacheOptions", function () {
context("check import", function () {
it("should be a function", function () {
expect(GuildCacheOptions).to.be.a("function");
});
});

context("check structure", function () {
it("should create a new instance of GuildCacheOptions with the correct options", function () {
const cacheOptions = new GuildCacheOptions();
expect(cacheOptions).to.be.an.instanceOf(GuildCacheOptions);
expect(cacheOptions).to.have.property("setMessageCaching");
expect(cacheOptions).to.have.property("setFileCaching");
expect(cacheOptions).to.have.property("setMemberCaching");
expect(cacheOptions).to.have.property("setChannelCaching");
expect(cacheOptions).to.have.property("setRoleCaching");
expect(cacheOptions).to.have.property("setVoiceStateCaching");
expect(cacheOptions).to.have.property("setThreadCaching");
expect(cacheOptions).to.have.property("setEmojiCaching");
expect(cacheOptions).to.have.property("setInviteCaching");
expect(cacheOptions).to.have.property("setScheduledEventCaching");
expect(cacheOptions).to.have.property("messageCaching");
expect(cacheOptions).to.have.property("fileCaching");
expect(cacheOptions).to.have.property("memberCaching");
expect(cacheOptions).to.have.property("channelCaching");
expect(cacheOptions).to.have.property("roleCaching");
expect(cacheOptions).to.have.property("voiceStateCaching");
expect(cacheOptions).to.have.property("threadCaching");
expect(cacheOptions).to.have.property("emojiCaching");
expect(cacheOptions).to.have.property("inviteCaching");
expect(cacheOptions).to.have.property("scheduledEventCaching");
expect(cacheOptions).to.have.property("toString");
expect(cacheOptions).to.have.property("toJSON");
});
it("should have the correct values when added in the constructor", function () {
const cacheOptions = new GuildCacheOptions(
GLUON_GUILD_CACHING_OPTIONS.CHANNELS |
GLUON_GUILD_CACHING_OPTIONS.ROLES,
);
expect(cacheOptions.channelCaching).to.be.true;
expect(cacheOptions.roleCaching).to.be.true;
expect(cacheOptions.messageCaching).to.be.false;
});
});

context("check set methods", function () {
it("should set the correct values", function () {
const cacheOptions = new GuildCacheOptions();
cacheOptions.setMessageCaching(true);
cacheOptions.setFileCaching(true);
cacheOptions.setMemberCaching(true);
cacheOptions.setChannelCaching(true);
cacheOptions.setRoleCaching(true);
cacheOptions.setVoiceStateCaching(true);
cacheOptions.setThreadCaching(true);
cacheOptions.setEmojiCaching(true);
cacheOptions.setInviteCaching(true);
cacheOptions.setScheduledEventCaching(true);
expect(cacheOptions.messageCaching).to.be.true;
expect(cacheOptions.fileCaching).to.be.true;
expect(cacheOptions.memberCaching).to.be.true;
expect(cacheOptions.channelCaching).to.be.true;
expect(cacheOptions.roleCaching).to.be.true;
expect(cacheOptions.voiceStateCaching).to.be.true;
expect(cacheOptions.threadCaching).to.be.true;
expect(cacheOptions.emojiCaching).to.be.true;
expect(cacheOptions.inviteCaching).to.be.true;
expect(cacheOptions.scheduledEventCaching).to.be.true;
});
});

context("check invalid input", function () {
it("should throw a TypeError", function () {
const cacheOptions = new GuildCacheOptions();
expect(() => cacheOptions.setMessageCaching("true")).to.throw(
TypeError,
"GLUON: Message caching must be a boolean",
);
expect(() => cacheOptions.setFileCaching("true")).to.throw(
TypeError,
"GLUON: File caching must be a boolean",
);
expect(() => cacheOptions.setMemberCaching("true")).to.throw(
TypeError,
"GLUON: Member caching must be a boolean",
);
expect(() => cacheOptions.setChannelCaching("true")).to.throw(
TypeError,
"GLUON: Channel caching must be a boolean",
);
expect(() => cacheOptions.setRoleCaching("true")).to.throw(
TypeError,
"GLUON: Role caching must be a boolean",
);
expect(() => cacheOptions.setVoiceStateCaching("true")).to.throw(
TypeError,
"GLUON: Voice state caching must be a boolean",
);
expect(() => cacheOptions.setThreadCaching("true")).to.throw(
TypeError,
"GLUON: Thread caching must be a boolean",
);
expect(() => cacheOptions.setEmojiCaching("true")).to.throw(
TypeError,
"GLUON: Emoji caching must be a boolean",
);
expect(() => cacheOptions.setInviteCaching("true")).to.throw(
TypeError,
"GLUON: Invite caching must be a boolean",
);
expect(() => cacheOptions.setScheduledEventCaching("true")).to.throw(
TypeError,
"GLUON: Scheduled event caching must be a boolean",
);
});
});

context("check toString", function () {
it("should return the correct string", function () {
const cacheOptions = new GuildCacheOptions();
cacheOptions.setMessageCaching(true);
cacheOptions.setFileCaching(true);
cacheOptions.setMemberCaching(true);
cacheOptions.setChannelCaching(true);
cacheOptions.setRoleCaching(true);
cacheOptions.setVoiceStateCaching(true);
cacheOptions.setThreadCaching(true);
cacheOptions.setEmojiCaching(false);
cacheOptions.setInviteCaching(true);
cacheOptions.setScheduledEventCaching(true);
expect(cacheOptions.toString()).to.equal(
"GuildCacheOptions { MESSAGES: true, FILES: true, VOICE_STATES: true, ROLES: true, EMOJIS: false, INVITES: true, CHANNELS: true, MEMBERS: true, THREADS: true, SCHEDULED_EVENTS: true }",
);
});
});

context("check toJSON", function () {
it("should return the correct JSON object", function () {
const cacheOptions = new GuildCacheOptions();
cacheOptions.setMessageCaching(true);
cacheOptions.setFileCaching(true);
cacheOptions.setMemberCaching(true);
cacheOptions.setChannelCaching(true);
cacheOptions.setRoleCaching(true);
cacheOptions.setVoiceStateCaching(true);
cacheOptions.setThreadCaching(true);
cacheOptions.setEmojiCaching(false);
cacheOptions.setInviteCaching(true);
cacheOptions.setScheduledEventCaching(true);
expect(cacheOptions.toJSON()).to.equal(1007);
});
});
});

0 comments on commit 9c949cc

Please sign in to comment.