From fe1c7612f56e9fc73872086c217cad0a48a8fdbd Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Thu, 30 Nov 2017 23:46:55 +0200 Subject: [PATCH] Add tests for getFilteredClone methods --- test/models/chan.js | 87 ++++++++++++++++++++++++++++++++++++++++++ test/models/network.js | 44 ++++++++++++++++++--- 2 files changed, 126 insertions(+), 5 deletions(-) diff --git a/test/models/chan.js b/test/models/chan.js index 77dc1752..02dc039a 100644 --- a/test/models/chan.js +++ b/test/models/chan.js @@ -145,4 +145,91 @@ describe("Chan", function() { ]); }); }); + + describe("#getFilteredClone(lastActiveChannel, lastMessage)", function() { + it("should send empty user list", function() { + const chan = new Chan(); + chan.setUser(new User({nick: "test"})); + + expect(chan.getFilteredClone().users).to.be.empty; + }); + + it("should keep necessary properties", function() { + const chan = new Chan(); + + expect(chan.getFilteredClone()).to.be.an("object").that.has.all.keys( + "firstUnread", + "highlight", + "id", + "key", + "messages", + "name", + "topic", + "type", + "unread", + "users" + ); + }); + + it("should send only last message for non active channel", function() { + const chan = new Chan({ + id: 1337, + messages: [ + new Msg({id: 10}), + new Msg({id: 11}), + new Msg({id: 12}), + new Msg({id: 13}), + ], + }); + + expect(chan.id).to.equal(1337); + + const messages = chan.getFilteredClone(999).messages; + + expect(messages).to.have.lengthOf(1); + expect(messages[0].id).to.equal(13); + }); + + it("should send more messages for active channel", function() { + const chan = new Chan({ + id: 1337, + messages: [ + new Msg({id: 10}), + new Msg({id: 11}), + new Msg({id: 12}), + new Msg({id: 13}), + ], + }); + + expect(chan.id).to.equal(1337); + + const messages = chan.getFilteredClone(1337).messages; + + expect(messages).to.have.lengthOf(4); + expect(messages[0].id).to.equal(10); + expect(messages[3].id).to.equal(13); + }); + + it("should only send new messages", function() { + const chan = new Chan({ + id: 1337, + messages: [ + new Msg({id: 10}), + new Msg({id: 11}), + new Msg({id: 12}), + new Msg({id: 13}), + new Msg({id: 14}), + new Msg({id: 15}), + ], + }); + + expect(chan.id).to.equal(1337); + + const messages = chan.getFilteredClone(1337, 12).messages; + + expect(messages).to.have.lengthOf(3); + expect(messages[0].id).to.equal(13); + expect(messages[2].id).to.equal(15); + }); + }); }); diff --git a/test/models/network.js b/test/models/network.js index a8a97895..97354295 100644 --- a/test/models/network.js +++ b/test/models/network.js @@ -1,10 +1,10 @@ "use strict"; -var expect = require("chai").expect; - -var Chan = require("../../src/models/chan"); -var Msg = require("../../src/models/msg"); -var Network = require("../../src/models/network"); +const expect = require("chai").expect; +const Chan = require("../../src/models/chan"); +const Msg = require("../../src/models/msg"); +const User = require("../../src/models/user"); +const Network = require("../../src/models/network"); describe("Network", function() { describe("#export()", function() { @@ -91,4 +91,38 @@ describe("Network", function() { expect(network.channels[1].messages[2].text).to.equal("message after network creation"); }); }); + + describe("#getFilteredClone(lastActiveChannel, lastMessage)", function() { + it("should filter channels", function() { + const chan = new Chan(); + chan.setUser(new User({nick: "test"})); + + const network = new Network({ + channels: [ + chan, + ], + }); + + expect(network.channels[0].users).to.be.empty; + }); + + it("should keep necessary properties", function() { + const network = new Network(); + + expect(network.getFilteredClone()).to.be.an("object").that.has.all.keys( + "channels", + "commands", + "host", + "hostname", + "id", + "ip", + "name", + "port", + "realname", + "serverOptions", + "tls", + "username" + ); + }); + }); });