Skip to content
This repository has been archived by the owner on Dec 3, 2021. It is now read-only.

Commit

Permalink
Added unit tests for the DiscordChannel class
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackhawk-TA committed Jul 13, 2019
1 parent 97cb4d1 commit df299d4
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "./build/src/DiscordBot.js",
"scripts": {
"build": "typedoc --excludeExternals --out doc src/wrappers/interfaces/ && tsc",
"test": "tsc && mocha -r ts-node/register **/*Test.ts --exit",
"test": "tsc && nyc mocha -r ts-node/register **/*Test.ts --exit",
"coverage": "nyc npm run test | coveralls",
"validate": "eslint --ext .ts src tests",
"validation-build": "npm run-script validate && npm run-script coverage && npm run-script build"
Expand Down
9 changes: 4 additions & 5 deletions src/wrappers/discord/DiscordChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,15 @@ export class DiscordChannel implements IChannel {
});
}

deleteMessages(messages: IMessage[]): Promise<IMessage[]> {
deleteMessages(MessagesToDelete: IMessage[]): Promise<IMessage[]> {
return new Promise<IMessage[]>((resolve, reject) => {
let Messages: IMessage[] = [];
for (let i: number = 0; i < messages.length; i++) {
let Message: IMessage = new DiscordMessage(messages[i]);
Message.delete().then((result) => {
for (let i: number = 0; i < MessagesToDelete.length; i++) {
MessagesToDelete[i].delete().then((result) => {
if (result instanceof DiscordMessage) {
Messages.push(result);
}
if (i === messages.length - 1) {
if (i === MessagesToDelete.length - 1) {
resolve(Messages);
}
}).catch(error => {
Expand Down
4 changes: 2 additions & 2 deletions src/wrappers/interfaces/IChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export interface IChannel {

/**
* Delete every message in an array of messages.
* @param messages - The messages which shall be deleted
* @param MessagesToDelete - The messages which shall be deleted
*/
deleteMessages(messages: IMessage[]): Promise<IMessage[]>;
deleteMessages(MessagesToDelete: IMessage[]): Promise<IMessage[]>;

/**
* Resolves with a collection of messages that pass the specified filter.
Expand Down
178 changes: 178 additions & 0 deletions tests/discord/DiscordChannelTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import {DiscordChannel} from "../../src/wrappers/discord/DiscordChannel";
import {DiscordServer} from "../../src/wrappers/discord/DiscordServer";
import {DiscordMessage} from "../../src/wrappers/discord/DiscordMessage";

const assert = require("assert");
const sinon = require("sinon");

describe("The class DiscordChannel", function() {
beforeEach(function() {
this.channel = {
id: 123,
name: "channelName",
guild: {},
send: function() {},
fetchMessages: function() {},
awaitMessages: function() {}
};
this.Channel = new DiscordChannel(this.channel);
});

afterEach(function() {
this.channel = {};
this.Channel = null;
});

describe("The method send", function() {
it("Calls the send message of the api", function() {
//Arrange
let options: object = {
"some": "options"
};
let sendStub = sinon.stub(this.channel, "send");

//Act
this.Channel.send("test", options);

//Assert
assert.strictEqual(sendStub.callCount, 1, "The send method of the api was called.");
assert.strictEqual(sendStub.getCall(0).args[0], "test", "The first parameter of the send function is correct.");
assert.strictEqual(sendStub.getCall(0).args[1], options, "The second parameter of the send function is correct.");

//Cleanup
sendStub.restore();
});
});

describe("The method getId", function() {
it("Returns the channel id", function() {
//Act
let channelId: number = this.Channel.getId();

//Assert
assert.strictEqual(channelId, 123, "The correct channel id was returned.");
});
});

describe("The method getName", function() {
it("Returns the channel name", function() {
//Act
let channelName: string = this.Channel.getName();

//Assert
assert.strictEqual(channelName, "channelName", "The correct channel name was returned.");
});
});

describe("The method getServer", function() {
it("Returns the wrapped server object", function() {
//Act
let Server = this.Channel.getServer();

//Assert
assert.strictEqual(Server instanceof DiscordServer, true, "The server object was wrapped correctly.");
});
});

describe("The method getMessages", function() {
it("Returns the requested messages", function() {
//Arrange
let messagesReturned = [{}, {}];
let fetchMessagesStub = sinon.stub(this.channel, "fetchMessages").returns(Promise.resolve(messagesReturned));

//Act
return this.Channel.getMessages(2).then(messages => {
//Assert
assert.strictEqual(fetchMessagesStub.callCount, 1, "The method fetchMessages was called.");
assert.deepStrictEqual(fetchMessagesStub.getCall(0).args[0], {limit: 2}, "The method fetchMessages was called with the correct parameters.");
assert.strictEqual(messages.length, 2, "The amount of returned messages is correct.");
assert.strictEqual(messages[0] instanceof DiscordMessage, true, "The first message object was wrapped correctly.");
assert.strictEqual(messages[1] instanceof DiscordMessage, true, "The second message object was wrapped correctly.");
});
});

it("Returns an error when fetchMessages was rejected", function() {
//Arrange
let fetchMessagesStub = sinon.stub(this.channel, "fetchMessages").returns(Promise.reject(new Error("Some error")));

//Act
return this.Channel.getMessages(2).catch(error => {
//Assert
assert.strictEqual(fetchMessagesStub.callCount, 1, "The message fetchMessages was called.");
assert.deepStrictEqual(fetchMessagesStub.getCall(0).args[0], {limit: 2}, "The method fetchMessages was called with the correct parameters.");
assert.strictEqual(error instanceof Error, true, "An error was returned.");
});
});
});

describe("The method deleteMessages", function() {
it("Returns the deleted messages", function() {
//Arrange
let FirstMessage = new DiscordMessage({});
let SecondMessage = new DiscordMessage({});
sinon.stub(FirstMessage, "delete").returns(Promise.resolve(FirstMessage));
sinon.stub(SecondMessage,"delete").returns(Promise.resolve(SecondMessage));

//Act
return this.Channel.deleteMessages([FirstMessage, SecondMessage]).then(messages => {
//Assert
assert.strictEqual(messages.length, 2, "The amount of returned messages is correct.");
assert.strictEqual(messages[0], FirstMessage, "The correct message was deleted and returned.");
assert.strictEqual(messages[1], SecondMessage, "The correct message was deleted and returned.");
});
});

it("Returns an error because the delete promise of a message was rejected", function() {
//Arrange
let Message = new DiscordMessage({});
sinon.stub(Message, "delete").returns(Promise.reject(new Error("Some error")));

//Act
return this.Channel.deleteMessages([Message]).catch(error => {
assert.strictEqual(error instanceof Error, true, "An error object was returned.");
assert.strictEqual(error.toString(), "Error: Some error", "The correct error was returned.")
});
});
});

describe("The method awaitMessages", function() {
it("Returns the requested messages", function() {
//Arrange
let options = {
"some": "option"
};
let messagesReturned = {
array: function() {
return [{}, {}];
}
};
let awaitMessagesStub = sinon.stub(this.channel, "awaitMessages").returns(Promise.resolve(messagesReturned));

//Act
return this.Channel.awaitMessages(options).then(messages => {
//Assert
assert.strictEqual(awaitMessagesStub.callCount, 1, "The method awaitMessages was called.");
assert.deepStrictEqual(awaitMessagesStub.getCall(0).args[1], options, "The method awaitMessages was called with the correct parameters.");
assert.strictEqual(messages.length, 2, "The amount of returned messages is correct.");
assert.strictEqual(messages[0] instanceof DiscordMessage, true, "The first message object was wrapped correctly.");
assert.strictEqual(messages[1] instanceof DiscordMessage, true, "The second message object was wrapped correctly.");
});
});

it("Returns an error when awaitMessages was rejected", function() {
//Arrange
let options = {
"some": "options"
};
let awaitMessagesStub = sinon.stub(this.channel, "awaitMessages").returns(Promise.reject(new Error("Some error")));

//Act
return this.Channel.awaitMessages(options).catch(error => {
//Assert
assert.strictEqual(awaitMessagesStub.callCount, 1, "The message awaitMessages was called.");
assert.deepStrictEqual(awaitMessagesStub.getCall(0).args[1], options, "The method awaitMessages was called with the correct parameters.");
assert.strictEqual(error instanceof Error, true, "An error was returned.");
});
});
});
});

0 comments on commit df299d4

Please sign in to comment.