Merge pull request #197 from thelounge/astorije/test-sort-users

Add tests for the sortUsers method
This commit is contained in:
Pavel Djundik 2016-03-16 10:58:10 +02:00
commit 9ef348e614
2 changed files with 83 additions and 2 deletions

View File

@ -41,13 +41,14 @@
"socket.io": "1.4.5" "socket.io": "1.4.5"
}, },
"devDependencies": { "devDependencies": {
"stylelint": "4.3.3", "chai": "3.5.0",
"eslint": "2.3.0", "eslint": "2.3.0",
"grunt": "0.4.5", "grunt": "0.4.5",
"grunt-cli": "0.1.13", "grunt-cli": "0.1.13",
"grunt-contrib-uglify": "0.11.1", "grunt-contrib-uglify": "0.11.1",
"grunt-contrib-watch": "0.6.1", "grunt-contrib-watch": "0.6.1",
"handlebars": "4.0.5", "handlebars": "4.0.5",
"mocha": "2.4.5" "mocha": "2.4.5",
"stylelint": "4.3.3"
} }
} }

80
test/models/chan.js Normal file
View File

@ -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", "Foo"
].map(makeUser)});
chan.sortUsers();
expect(getUserNames(chan)).to.deep.equal([
"!foo", "&foo", "(foo)", "+foo", "<foo>", "@foo", "[foo", "]foo",
"^foo", "_foo", "Foo", "{foo}"
]);
});
});
});