Skip to content

Node.js based forum software built for the modern web

group search tests

Barış Soner Uşaklı376c71adda42
diff --git a/src/groups/search.js b/src/groups/search.js
index 62df79696c..5552eb932c 100644
--- a/src/groups/search.js
+++ b/src/groups/search.js
@@ -88,10 +88,7 @@ module.exports = function (Groups) {
if (!data.query) {
Groups.getOwnersAndMembers(data.groupName, data.uid, 0, 19, function (err, users) {
- if (err) {
- return callback(err);
- }
- callback(null, { users: users });
+ callback(err, { users: users });
});
return;
}
diff --git a/test/groups.js b/test/groups.js
index 6b52787efa..abc858e9dc 100644
--- a/test/groups.js
+++ b/test/groups.js
@@ -57,6 +57,10 @@ describe('Groups', function () {
// Also create a hidden group
Groups.join('Hidden', 'Test', next);
},
+ function (next) {
+ // create another group that starts with test for search/sort
+ Groups.create({ name: 'Test2', description: 'Foobar!' }, next);
+ },
], function (err, results) {
assert.ifError(err);
testUid = results[3];
@@ -69,7 +73,7 @@ describe('Groups', function () {
it('should list the groups present', function (done) {
Groups.getGroupsFromSet('groups:createtime', 0, 0, -1, function (err, groups) {
assert.ifError(err);
- assert.equal(groups.length, 6);
+ assert.equal(groups.length, 7);
done();
});
});
@@ -98,10 +102,18 @@ describe('Groups', function () {
describe('.search()', function () {
var socketGroups = require('../src/socket.io/groups');
+ it('should return empty array if query is falsy', function (done) {
+ Groups.search(null, {}, function (err, groups) {
+ assert.ifError(err);
+ assert.equal(0, groups.length);
+ done();
+ });
+ });
+
it('should return the groups when search query is empty', function (done) {
socketGroups.search({ uid: adminUid }, { query: '' }, function (err, groups) {
assert.ifError(err);
- assert.equal(3, groups.length);
+ assert.equal(4, groups.length);
done();
});
});
@@ -109,7 +121,7 @@ describe('Groups', function () {
it('should return the "Test" group when searched for', function (done) {
socketGroups.search({ uid: adminUid }, { query: 'test' }, function (err, groups) {
assert.ifError(err);
- assert.equal(1, groups.length);
+ assert.equal(2, groups.length);
assert.strictEqual('Test', groups[0].name);
done();
});
@@ -118,7 +130,7 @@ describe('Groups', function () {
it('should return the "Test" group when searched for and sort by member count', function (done) {
Groups.search('test', { filterHidden: true, sort: 'count' }, function (err, groups) {
assert.ifError(err);
- assert.equal(1, groups.length);
+ assert.equal(2, groups.length);
assert.strictEqual('Test', groups[0].name);
done();
});
@@ -127,25 +139,37 @@ describe('Groups', function () {
it('should return the "Test" group when searched for and sort by creation time', function (done) {
Groups.search('test', { filterHidden: true, sort: 'date' }, function (err, groups) {
assert.ifError(err);
- assert.equal(1, groups.length);
- assert.strictEqual('Test', groups[0].name);
+ assert.equal(2, groups.length);
+ assert.strictEqual('Test', groups[1].name);
done();
});
});
it('should return all users if no query', function (done) {
- User.create({
- username: 'newuser',
- email: '[email protected]',
- }, function (err, uid) {
+ function createAndJoinGroup(username, email, callback) {
+ async.waterfall([
+ function (next) {
+ User.create({ username: username, email: email }, next);
+ },
+ function (uid, next) {
+ Groups.join('Test', uid, next);
+ },
+ ], callback);
+ }
+ async.series([
+ function (next) {
+ createAndJoinGroup('newuser', '[email protected]', next);
+ },
+ function (next) {
+ createAndJoinGroup('bob', '[email protected]', next);
+ },
+ ], function (err) {
assert.ifError(err);
- Groups.join('Test', uid, function (err) {
+
+ socketGroups.searchMembers({ uid: adminUid }, { groupName: 'Test', query: '' }, function (err, data) {
assert.ifError(err);
- socketGroups.searchMembers({ uid: adminUid }, { groupName: 'Test', query: '' }, function (err, data) {
- assert.ifError(err);
- assert.equal(data.users.length, 2);
- done();
- });
+ assert.equal(data.users.length, 3);
+ done();
});
});
});