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-20 18:45:09 +02:00
|
|
|
var clean_up;
|
2013-07-28 23:05:01 +02:00
|
|
|
|
|
|
|
function set_up_dependencies() {
|
2013-08-01 17:53:10 +02:00
|
|
|
var _ = global._ = require('third/underscore/underscore.js');
|
2013-07-28 23:05:01 +02:00
|
|
|
global.Handlebars = require('handlebars');
|
|
|
|
|
2013-08-01 17:53:10 +02:00
|
|
|
var actual_narrow = require('js/narrow.js');
|
2013-07-30 23:02:10 +02:00
|
|
|
var search = require('js/search_suggestion.js');
|
2013-08-10 01:31:31 +02:00
|
|
|
global.narrow = require('js/narrow.js');
|
2013-07-28 23:05:01 +02:00
|
|
|
|
|
|
|
global.page_params = {
|
|
|
|
email: 'bob@zulip.com'
|
|
|
|
};
|
|
|
|
|
2013-08-01 17:53:10 +02:00
|
|
|
global.typeahead_helper = require('js/typeahead_helper.js');
|
2013-07-28 23:05:01 +02:00
|
|
|
|
2013-08-06 19:08:39 +02:00
|
|
|
global.util = require('js/util.js');
|
2013-08-07 23:56:51 +02:00
|
|
|
global.Dict = require('js/dict.js');
|
|
|
|
global.recent_subjects = new global.Dict();
|
2013-08-06 19:08:39 +02:00
|
|
|
|
2013-08-10 01:31:31 +02:00
|
|
|
global.Filter = require('js/filter.js');
|
2013-08-15 21:11:07 +02:00
|
|
|
global.stream_data = require('js/stream_data.js');
|
2013-08-10 01:31:31 +02:00
|
|
|
|
2013-08-20 18:45:09 +02:00
|
|
|
|
|
|
|
var narrow_stream = global.narrow.stream;
|
|
|
|
var stream_data_subscribed_streams = global.stream_data.subscribed_streams;
|
|
|
|
clean_up = function () {
|
|
|
|
global.narrow.stream = narrow_stream;
|
|
|
|
global.stream_data.subscribed_streams = stream_data_subscribed_streams;
|
|
|
|
};
|
|
|
|
|
2013-07-28 23:05:01 +02:00
|
|
|
return search;
|
|
|
|
}
|
|
|
|
|
|
|
|
var search = set_up_dependencies();
|
|
|
|
|
|
|
|
(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
|
|
|
}());
|
|
|
|
|
|
|
|
(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-07 23:56:51 +02:00
|
|
|
global.recent_subjects = new global.Dict({
|
2013-07-28 23:05:01 +02:00
|
|
|
office: [
|
|
|
|
{subject: 'team'},
|
|
|
|
{subject: 'ignore'},
|
|
|
|
{subject: 'test'}
|
|
|
|
]
|
2013-08-07 23:56:51 +02:00
|
|
|
});
|
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-07 23:56:51 +02:00
|
|
|
global.recent_subjects = new global.Dict();
|
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-07 23:56:51 +02:00
|
|
|
global.recent_subjects = new global.Dict({
|
2013-07-28 23:05:01 +02:00
|
|
|
office: [
|
|
|
|
{subject: 'team'},
|
|
|
|
{subject: 'ignore'},
|
|
|
|
{subject: 'test'}
|
|
|
|
]
|
2013-08-07 23:56:51 +02:00
|
|
|
});
|
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
|
|
|
}());
|
2013-08-20 18:45:09 +02:00
|
|
|
|
|
|
|
clean_up();
|
|
|
|
|