nodeclub/test/common/at.test.js
2014-09-06 18:04:34 +08:00

102 lines
3.1 KiB
JavaScript

/*!
* nodeclub - mention controller test
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var rewire = require("rewire");
var should = require('should');
var at = require('../../common/at');
var message = require('../../common/message');
var createUsers = require('../support/create_test_users').createUsers;
var mm = require('mm');
describe('test/common/at.test.js', function () {
before(function (done) {
createUsers(done);
});
afterEach(function () {
mm.restore();
});
var text = '@testuser1 哈哈, hellowprd testuser1 testuser2 \
testuser3 @testuser2你好 \
@testuser1@testuser3\
@testuser2@testuser123 oh my god';
var linkedText = '[@testuser1](/user/testuser1) 哈哈, hellowprd testuser1 testuser2 \
testuser3 [@testuser2](/user/testuser2)你好 \
[@testuser1](/user/testuser1)[@testuser3](/user/testuser3)\
[@testuser2](/user/testuser2)[@testuser1](/user/testuser1)23 oh my god';
describe('fetchUsers()', function () {
var mentionUser = rewire('../../common/at');
var fetchUsers = mentionUser.__get__('fetchUsers');
it('should found 6 users', function () {
var users = fetchUsers(text);
should.exist(users);
users.should.length(6);
for (var i = 0; i < users.length; i++) {
var user = users[i];
user.should.match(/^testuser\d+$/);
}
});
it('should found 0 user in text', function () {
var users = fetchUsers('no users match in text @ @@@@ @ @@@ @哈哈 @ testuser1');
users.should.length(0);
});
});
describe('linkUsers()', function () {
it('should link all mention users', function (done) {
at.linkUsers(text, function (err, text2) {
should.not.exist(err);
text2.should.equal(linkedText);
done();
});
});
});
describe('sendMessageToMentionUsers()', function () {
it('should send message to all mention users', function (done) {
at.sendMessageToMentionUsers(text, '4fb9db9c1dc2160000000005', '4fcae41e1eb86c0000000003',
function (err) {
should.not.exist(err);
done();
});
});
it('should not send message to no mention users', function (done) {
at.sendMessageToMentionUsers('abc no mentions', '4fb9db9c1dc2160000000005', '4fcae41e1eb86c0000000003',
function (err) {
should.not.exist(err);
done();
});
});
describe('mock message.sendAtMessage() error', function () {
before(function () {
mm(message, 'sendAtMessage', function () {
var callback = arguments[arguments.length - 1];
process.nextTick(function () {
callback(new Error('mock sendAtMessage() error'));
});
});
});
it('should return error', function (done) {
at.sendMessageToMentionUsers(text, '4fb9db9c1dc2160000000005', '4fcae41e1eb86c0000000003',
function (err) {
should.exist(err);
err.message.should.equal('mock sendAtMessage() error');
done();
});
});
});
});
});