From e879f3814e5d6d8c60104a5247e4eeadef42e37a Mon Sep 17 00:00:00 2001 From: Sheogorath Date: Wed, 17 Jul 2019 20:45:34 +0200 Subject: [PATCH 1/2] Add real unit tests for channel Before we tested channel implicitly by running it through the tests on the server.js. After some learning today, it's time to add an own set of unit tests to channel. --- test/channel.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 test/channel.js diff --git a/test/channel.js b/test/channel.js new file mode 100644 index 0000000..2c32c5e --- /dev/null +++ b/test/channel.js @@ -0,0 +1,23 @@ +const assert = require('assert'); +const Channel = require("../src/channel.js"); + +describe('Channel', function () { + describe('#contructor()', function () { + it("should create a channel", function () { + const channel = new Channel("#testchan") + assert.equal(typeof channel, 'object') + }) + }) + + describe('#join(user)', function () { + it("should add the user to a channel and send a JOIN command to the user", function (done) { + const channel = new Channel("#testchan") + let mockedUser = {nickname: "some_nick"} + mockedUser.sendRaw = function(data) { + assert.equal(data, ":some_nick JOIN #testchan") + done() + } + channel.join(mockedUser) + }) + }) +}) From fd860b54eea4adade9eb1d46b66c507134151d34 Mon Sep 17 00:00:00 2001 From: Sheogorath Date: Wed, 17 Jul 2019 20:59:32 +0200 Subject: [PATCH 2/2] Implement channel parting on the channel object This patch allows the channel to part users from it. This provides the basics for the future implementation of the PART command. --- src/channel.js | 11 +++++++++++ test/channel.js | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/channel.js b/src/channel.js index f4d82f3..cf0eddf 100644 --- a/src/channel.js +++ b/src/channel.js @@ -9,6 +9,17 @@ function Channel(name) { item.sendRaw(`:${user.nickname} JOIN ${channel.name}`) }) } + + this.part = function(user) { + let channel = this + let userIndex = this.userlist.indexOf(user) + if (userIndex >= 0) { + this.userlist.forEach(function(item) { + item.sendRaw(`:${user.nickname} PART ${channel.name}`) + }) + this.userlist.splice(index, 1) + } + } } module.exports = Channel diff --git a/test/channel.js b/test/channel.js index 2c32c5e..68accce 100644 --- a/test/channel.js +++ b/test/channel.js @@ -20,4 +20,22 @@ describe('Channel', function () { channel.join(mockedUser) }) }) + + describe('#part(user)', function () { + it("should remove a user from a channel and send a PART command to the user", function (done) { + const channel = new Channel("#testchan") + let mockedUser = {nickname: "some_nick"} + mockedUser.sendRaw = function(data) { + // we expect a PART message from our user to the channel (including us) + if (data.indexOf("PART") >= 0) { + assert.equal(data, ":some_nick PART #testchan") + done() + } + } + // we can't part a channel without joining it first + channel.join(mockedUser) + + channel.part(mockedUser) + }) + }) })