2013-07-30 23:02:10 +02:00
|
|
|
// Unit test the search_suggestion.js module.
|
2013-07-28 23:05:01 +02:00
|
|
|
//
|
|
|
|
// These tests are framework-free and run sequentially; they are invoked
|
|
|
|
// immediately after being defined. The contract here is that tests should
|
|
|
|
// clean up after themselves, and they should explicitly stub all
|
|
|
|
// dependencies.
|
|
|
|
|
|
|
|
var assert = require('assert');
|
|
|
|
|
2013-08-21 20:27:14 +02:00
|
|
|
add_dependencies({
|
|
|
|
_: 'third/underscore/underscore.js',
|
|
|
|
util: 'js/util.js',
|
|
|
|
Dict: 'js/dict.js',
|
|
|
|
Handlebars: 'handlebars',
|
|
|
|
Filter: 'js/filter.js',
|
|
|
|
typeahead_helper: 'js/typeahead_helper.js',
|
|
|
|
stream_data: 'js/stream_data.js',
|
|
|
|
narrow: 'js/narrow.js'
|
|
|
|
});
|
2013-07-28 23:05:01 +02:00
|
|
|
|
2013-08-22 00:01:14 +02:00
|
|
|
var search = require('js/search_suggestion.js');
|
2013-07-28 23:05:01 +02:00
|
|
|
|
2013-08-22 00:01:14 +02:00
|
|
|
set_global('page_params', {
|
|
|
|
email: 'bob@zulip.com'
|
|
|
|
});
|
|
|
|
set_global('recent_subjects', new global.Dict({fold_case: true}));
|
2013-08-20 18:45:09 +02:00
|
|
|
|
2013-08-22 00:01:14 +02:00
|
|
|
var stream_data = require('js/stream_data.js');
|
|
|
|
set_global('stream_data', {
|
|
|
|
get_name: stream_data.get_name
|
|
|
|
});
|
2013-07-28 23:05:01 +02:00
|
|
|
|
2013-08-22 01:29:28 +02:00
|
|
|
set_global('narrow', {});
|
2013-07-28 23:05:01 +02:00
|
|
|
|
|
|
|
(function test_basic_get_suggestions() {
|
|
|
|
var query = 'fred';
|
|
|
|
|
2013-08-15 21:11:07 +02:00
|
|
|
global.stream_data.subscribed_streams = function () {
|
2013-07-28 23:05:01 +02:00
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
|
|
|
global.narrow.stream = function () {
|
|
|
|
return 'office';
|
|
|
|
};
|
|
|
|
|
|
|
|
var suggestions = search.get_suggestions(query);
|
|
|
|
|
|
|
|
var expected = [
|
2013-08-05 18:35:46 +02:00
|
|
|
'fred'
|
2013-07-28 23:05:01 +02:00
|
|
|
];
|
2013-07-30 19:41:23 +02:00
|
|
|
assert.deepEqual(suggestions.strings, expected);
|
2013-07-28 23:05:01 +02:00
|
|
|
}());
|
|
|
|
|
2013-08-28 01:23:42 +02:00
|
|
|
(function test_subset_suggestions() {
|
|
|
|
var query = 'stream:Denmark topic:Hamlet shakespeare';
|
|
|
|
|
|
|
|
global.stream_data.subscribed_streams = function () {
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
|
|
|
global.narrow.stream = function () {
|
|
|
|
return undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
var suggestions = search.get_suggestions(query);
|
|
|
|
|
|
|
|
var expected = [
|
|
|
|
"stream:Denmark topic:Hamlet shakespeare",
|
|
|
|
"stream:Denmark topic:Hamlet",
|
|
|
|
"stream:Denmark"
|
|
|
|
];
|
|
|
|
|
|
|
|
assert.deepEqual(suggestions.strings, expected);
|
|
|
|
}());
|
|
|
|
|
2013-07-28 23:05:01 +02:00
|
|
|
(function test_empty_query_suggestions() {
|
|
|
|
var query = '';
|
|
|
|
|
2013-08-15 21:11:07 +02:00
|
|
|
global.stream_data.subscribed_streams = function () {
|
2013-07-28 23:05:01 +02:00
|
|
|
return ['devel', 'office'];
|
|
|
|
};
|
|
|
|
|
|
|
|
global.narrow.stream = function () {
|
|
|
|
return undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
var suggestions = search.get_suggestions(query);
|
|
|
|
|
|
|
|
var expected = [
|
|
|
|
"",
|
|
|
|
"in:all",
|
|
|
|
"is:private",
|
|
|
|
"is:starred",
|
|
|
|
"is:mentioned",
|
|
|
|
"sender:bob@zulip.com",
|
|
|
|
"stream:devel",
|
|
|
|
"stream:office"
|
|
|
|
];
|
|
|
|
|
2013-07-30 19:41:23 +02:00
|
|
|
assert.deepEqual(suggestions.strings, expected);
|
2013-07-30 22:39:32 +02:00
|
|
|
|
|
|
|
function describe(q) {
|
|
|
|
return suggestions.lookup_table[q].description;
|
|
|
|
}
|
|
|
|
assert.equal(describe('in:all'), 'All messages');
|
|
|
|
assert.equal(describe('is:private'), 'Private messages');
|
|
|
|
assert.equal(describe('is:starred'), 'Starred messages');
|
|
|
|
assert.equal(describe('is:mentioned'), '@-mentions');
|
|
|
|
assert.equal(describe('sender:bob@zulip.com'), 'Sent by me');
|
|
|
|
assert.equal(describe('stream:devel'), 'Narrow to stream <strong>devel</strong>');
|
2013-07-28 23:05:01 +02:00
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_topic_suggestions() {
|
|
|
|
var query = 'te';
|
|
|
|
|
2013-08-15 21:11:07 +02:00
|
|
|
global.stream_data.subscribed_streams = function () {
|
2013-07-28 23:05:01 +02:00
|
|
|
return ['office'];
|
|
|
|
};
|
|
|
|
|
|
|
|
global.narrow.stream = function () {
|
|
|
|
return 'office';
|
|
|
|
};
|
|
|
|
|
2013-08-19 20:46:27 +02:00
|
|
|
global.recent_subjects = new global.Dict.from({
|
2013-07-28 23:05:01 +02:00
|
|
|
office: [
|
|
|
|
{subject: 'team'},
|
|
|
|
{subject: 'ignore'},
|
|
|
|
{subject: 'test'}
|
|
|
|
]
|
2013-08-19 21:17:10 +02:00
|
|
|
}, {fold_case: true});
|
2013-07-28 23:05:01 +02:00
|
|
|
|
|
|
|
var suggestions = search.get_suggestions(query);
|
|
|
|
|
|
|
|
var expected = [
|
|
|
|
"te",
|
|
|
|
"stream:office topic:team",
|
2013-08-05 18:35:46 +02:00
|
|
|
"stream:office topic:test"
|
2013-07-28 23:05:01 +02:00
|
|
|
];
|
|
|
|
|
2013-07-30 19:41:23 +02:00
|
|
|
assert.deepEqual(suggestions.strings, expected);
|
2013-07-30 22:39:32 +02:00
|
|
|
|
|
|
|
function describe(q) {
|
|
|
|
return suggestions.lookup_table[q].description;
|
|
|
|
}
|
|
|
|
assert.equal(describe('te'), "Search for te");
|
|
|
|
assert.equal(describe('stream:office topic:team'), "Narrow to office > team");
|
|
|
|
|
2013-07-28 23:05:01 +02:00
|
|
|
}());
|
|
|
|
|
2013-08-06 19:08:39 +02:00
|
|
|
(function test_whitespace_glitch() {
|
|
|
|
var query = 'stream:office '; // note trailing space
|
|
|
|
|
2013-08-15 21:11:07 +02:00
|
|
|
global.stream_data.subscribed_streams = function () {
|
2013-08-06 19:08:39 +02:00
|
|
|
return ['office'];
|
|
|
|
};
|
|
|
|
|
|
|
|
global.narrow.stream = function () {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2013-08-19 21:17:10 +02:00
|
|
|
global.recent_subjects = new global.Dict({fold_case: true});
|
2013-08-06 19:08:39 +02:00
|
|
|
|
|
|
|
var suggestions = search.get_suggestions(query);
|
|
|
|
|
|
|
|
var expected = [
|
|
|
|
"stream:office"
|
|
|
|
];
|
|
|
|
|
|
|
|
assert.deepEqual(suggestions.strings, expected);
|
|
|
|
}());
|
2013-07-28 23:05:01 +02:00
|
|
|
|
|
|
|
(function test_people_suggestions() {
|
|
|
|
var query = 'te';
|
|
|
|
|
2013-08-15 21:11:07 +02:00
|
|
|
global.stream_data.subscribed_streams = function () {
|
2013-07-28 23:05:01 +02:00
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
|
|
|
global.narrow.stream = function () {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
global.page_params.people_list = [
|
|
|
|
{
|
|
|
|
email: 'ted@zulip.com',
|
|
|
|
full_name: 'Ted Smith'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
email: 'alice@zulip.com',
|
|
|
|
full_name: 'Alice Ignore'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2013-08-19 20:46:27 +02:00
|
|
|
global.recent_subjects = new global.Dict.from({
|
2013-07-28 23:05:01 +02:00
|
|
|
office: [
|
|
|
|
{subject: 'team'},
|
|
|
|
{subject: 'ignore'},
|
|
|
|
{subject: 'test'}
|
|
|
|
]
|
2013-08-19 21:17:10 +02:00
|
|
|
}, {fold_case: true});
|
2013-07-28 23:05:01 +02:00
|
|
|
|
|
|
|
var suggestions = search.get_suggestions(query);
|
|
|
|
|
|
|
|
var expected = [
|
|
|
|
"te",
|
|
|
|
"pm-with:ted@zulip.com",
|
2013-08-05 18:35:46 +02:00
|
|
|
"sender:ted@zulip.com"
|
2013-07-28 23:05:01 +02:00
|
|
|
];
|
|
|
|
|
2013-07-30 19:41:23 +02:00
|
|
|
assert.deepEqual(suggestions.strings, expected);
|
2013-07-30 22:39:32 +02:00
|
|
|
function describe(q) {
|
|
|
|
return suggestions.lookup_table[q].description;
|
|
|
|
}
|
|
|
|
assert.equal(describe('pm-with:ted@zulip.com'),
|
|
|
|
"Narrow to private messages with <strong>Te</strong>d Smith <<strong>te</strong>d@zulip.com>");
|
|
|
|
assert.equal(describe('sender:ted@zulip.com'),
|
|
|
|
"Narrow to messages sent by <strong>Te</strong>d Smith <<strong>te</strong>d@zulip.com>");
|
|
|
|
|
2013-08-07 15:40:47 +02:00
|
|
|
suggestions = search.get_suggestions('Ted '); // note space
|
|
|
|
|
|
|
|
expected = [
|
|
|
|
"Ted",
|
|
|
|
"pm-with:ted@zulip.com",
|
|
|
|
"sender:ted@zulip.com"
|
|
|
|
];
|
|
|
|
|
|
|
|
assert.deepEqual(suggestions.strings, expected);
|
2013-07-28 23:05:01 +02:00
|
|
|
}());
|