Skip to content

Commit

Permalink
Create MessagePollManager.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Starman3787 committed Aug 26, 2024
1 parent 2af1ded commit a217f9a
Showing 1 changed file with 151 additions and 0 deletions.
151 changes: 151 additions & 0 deletions test/managers/MessagePollManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { expect } from "chai";
import MessagePollManager from "../../src/managers/MessagePollManager.js";
import { TEST_CLIENTS } from "../../src/testData.js";
import { TO_JSON_TYPES_ENUM } from "../../src/constants.js";

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

context("check structure", function () {
it("should create a new instance of MessagePollManager with the correct options", function () {
const client = TEST_CLIENTS.ALL_CACHES_ENABLED();
const messagePollManager = new MessagePollManager(client, {});
expect(messagePollManager).to.be.an.instanceOf(MessagePollManager);
expect(messagePollManager).to.have.property("_addVote");
});
});

context("check input of constructor", function () {
it("should throw an error if client is not a Client instance", function () {
expect(() => new MessagePollManager()).to.throw(
TypeError,
"GLUON: Client must be an instance of Client.",
);
});
it("should throw an error if existingResponses is not an object", function () {
const client = TEST_CLIENTS.ALL_CACHES_ENABLED();
expect(() => new MessagePollManager(client, 123456)).to.throw(
TypeError,
"GLUON: Existing responses must be an object.",
);
});
it("should input the correct data into the cache", function () {
const client = TEST_CLIENTS.ALL_CACHES_ENABLED();
const messagePollManager = new MessagePollManager(client, {
1: [123456],
});
messagePollManager._addVote("123457", 1);
expect(
messagePollManager.toJSON(TO_JSON_TYPES_ENUM.CACHE_FORMAT),
).to.deep.equal({
1: [String(123456), String(123457)],
});
});
});

context("check addVote method", function () {
it("should throw an error if user_id is not a string", function () {
const client = TEST_CLIENTS.ALL_CACHES_ENABLED();
const messagePollManager = new MessagePollManager(client);
expect(() => messagePollManager._addVote(123456, 1)).to.throw(
TypeError,
"GLUON: User ID must be a string.",
);
});
it("should throw an error if answer_id is not a number", function () {
const client = TEST_CLIENTS.ALL_CACHES_ENABLED();
const messagePollManager = new MessagePollManager(client);
expect(() => messagePollManager._addVote("123456", "1")).to.throw(
TypeError,
"GLUON: Answer ID must be a number.",
);
});
it("should add the vote to the cache", function () {
const client = TEST_CLIENTS.ALL_CACHES_ENABLED();
const messagePollManager = new MessagePollManager(client);
messagePollManager._addVote("123456", 1);
expect(
messagePollManager.toJSON(TO_JSON_TYPES_ENUM.CACHE_FORMAT),
).to.deep.equal({
1: [String(123456)],
});
});
});

context("check removeVote method", function () {
it("should throw an error if user_id is not a string", function () {
const client = TEST_CLIENTS.ALL_CACHES_ENABLED();
const messagePollManager = new MessagePollManager(client);
expect(() => messagePollManager._removeVote(123456, 1)).to.throw(
TypeError,
"GLUON: User ID must be a string.",
);
});
it("should throw an error if answer_id is not a number", function () {
const client = TEST_CLIENTS.ALL_CACHES_ENABLED();
const messagePollManager = new MessagePollManager(client);
expect(() => messagePollManager._removeVote("123456", "1")).to.throw(
TypeError,
"GLUON: Answer ID must be a number.",
);
});
it("should remove the vote from the cache", function () {
const client = TEST_CLIENTS.ALL_CACHES_ENABLED();
const messagePollManager = new MessagePollManager(client, {
1: [123456],
});
messagePollManager._removeVote("123456", 1);
expect(
messagePollManager.toJSON(TO_JSON_TYPES_ENUM.CACHE_FORMAT),
).to.deep.equal({
1: [],
});
});
});

context("check toJSON method", function () {
it("should return the correct cache format", function () {
const client = TEST_CLIENTS.ALL_CACHES_ENABLED();
const messagePollManager = new MessagePollManager(client, {
1: [123456],
});
expect(
messagePollManager.toJSON(TO_JSON_TYPES_ENUM.CACHE_FORMAT),
).to.deep.equal({
1: [String(123456)],
});
});
it("should return the correct storage format", function () {
const client = TEST_CLIENTS.ALL_CACHES_ENABLED();
const messagePollManager = new MessagePollManager(client, {
1: [123456],
});
expect(
messagePollManager.toJSON(TO_JSON_TYPES_ENUM.STORAGE_FORMAT),
).to.deep.equal({
1: [String(123456)],
});
});
it("should return the correct discord format", function () {
const client = TEST_CLIENTS.ALL_CACHES_ENABLED();
const messagePollManager = new MessagePollManager(client, {
1: [123456],
});
expect(
messagePollManager.toJSON(TO_JSON_TYPES_ENUM.DISCORD_FORMAT),
).to.deep.equal({
answer_counts: [
{
count: 1,
id: 1,
me_voted: false,
},
],
});
});
});
});

0 comments on commit a217f9a

Please sign in to comment.