diff --git a/package.json b/package.json index 7fa176ee..345ad299 100644 --- a/package.json +++ b/package.json @@ -41,13 +41,14 @@ "socket.io": "1.4.5" }, "devDependencies": { - "stylelint": "4.3.3", + "chai": "3.5.0", "eslint": "2.3.0", "grunt": "0.4.5", "grunt-cli": "0.1.13", "grunt-contrib-uglify": "0.11.1", "grunt-contrib-watch": "0.6.1", "handlebars": "4.0.5", - "mocha": "2.4.5" + "mocha": "2.4.5", + "stylelint": "4.3.3" } } diff --git a/test/models/chan.js b/test/models/chan.js new file mode 100644 index 00000000..d6e42714 --- /dev/null +++ b/test/models/chan.js @@ -0,0 +1,80 @@ +"use strict"; + +var expect = require("chai").expect; + +var Chan = require("../../src/models/chan"); +var User = require("../../src/models/user"); + +function makeUser(name, mode) { + return new User({name: name, mode: mode}); +} + +function getUserNames(chan) { + return chan.users.map(function(u) { return u.name; }); +} + +describe("Chan", function() { + describe("#sortUsers()", function() { + + it("should sort a simple user list", function() { + var chan = new Chan({users: [ + "JocelynD", "YaManicKill", "astorije", "xPaw", "Max-P" + ].map(makeUser)}); + chan.sortUsers(); + + expect(getUserNames(chan)).to.deep.equal([ + "astorije", "JocelynD", "Max-P", "xPaw", "YaManicKill" + ]); + }); + + it("should group users by modes", function() { + var chan = new Chan({users: [ + new User({name: "JocelynD", mode: "&"}), + new User({name: "YaManicKill", mode: "+"}), + new User({name: "astorije", mode: "%"}), + new User({name: "xPaw", mode: "~"}), + new User({name: "Max-P", mode: "@"}), + ]}); + chan.sortUsers(); + + expect(getUserNames(chan)).to.deep.equal([ + "xPaw", "JocelynD", "Max-P", "astorije", "YaManicKill" + ]); + }); + + it("should sort a mix of users and modes", function() { + var chan = new Chan({users: [ + new User({name: "JocelynD"}), + new User({name: "YaManicKill", mode: "@"}), + new User({name: "astorije"}), + new User({name: "xPaw"}), + new User({name: "Max-P", mode: "@"}), + ]}); + chan.sortUsers(); + + expect(getUserNames(chan)).to.deep.equal( + ["Max-P", "YaManicKill", "astorije", "JocelynD", "xPaw"] + ); + }); + + it("should be case-insensitive", function() { + var chan = new Chan({users: ["aB", "Ad", "AA", "ac"].map(makeUser)}); + chan.sortUsers(); + + expect(getUserNames(chan)).to.deep.equal(["AA", "aB", "ac", "Ad"]); + }); + + it("should parse special characters successfully", function() { + var chan = new Chan({users: [ + "[foo", "]foo", "(foo)", "{foo}", "", "_foo", "@foo", "^foo", + "&foo", "!foo", "+foo", "Foo" + ].map(makeUser)}); + chan.sortUsers(); + + expect(getUserNames(chan)).to.deep.equal([ + "!foo", "&foo", "(foo)", "+foo", "", "@foo", "[foo", "]foo", + "^foo", "_foo", "Foo", "{foo}" + ]); + }); + }); +});