zulip/frontend_tests/node_tests/subs.js

200 lines
6.3 KiB
JavaScript
Raw Normal View History

2016-12-26 10:13:31 +01:00
global.stub_out_jquery();
add_dependencies({
stream_data: 'js/stream_data',
Handlebars: 'handlebars',
templates: 'js/templates',
i18n: 'i18next',
});
2017-03-04 17:55:16 +01:00
set_global('channel', {});
2016-12-26 10:13:31 +01:00
var subs = require('js/subs.js');
var jsdom = require("jsdom");
var window = jsdom.jsdom().defaultView;
global.$ = require('jquery')(window);
var i18n = global.i18n;
i18n.init({
nsSeparator: false,
keySeparator: false,
interpolation: {
prefix: "__",
suffix: "__",
},
lng: 'en',
});
subs.stream_name_match_stream_ids = [];
subs.stream_description_match_stream_ids = [];
2016-12-26 10:13:31 +01:00
(function test_filter_table() {
var denmark = {
subscribed: false,
name: 'Denmark',
stream_id: 1,
description: 'Copenhagen',
2016-12-26 10:13:31 +01:00
};
var poland = {
subscribed: true,
name: 'Poland',
stream_id: 2,
description: 'monday',
2016-12-26 10:13:31 +01:00
};
var pomona = {
subscribed: true,
name: 'Pomona',
stream_id: 3,
description: 'college',
2016-12-26 10:13:31 +01:00
};
var cpp = {
subscribed: true,
name: 'C++',
stream_id: 4,
};
2016-12-26 10:13:31 +01:00
var elem_1 = $(global.render_template("subscription", denmark));
var elem_2 = $(global.render_template("subscription", poland));
var elem_3 = $(global.render_template("subscription", pomona));
var elem_4 = $(global.render_template("subscription", cpp));
2016-12-26 10:13:31 +01:00
$("body").empty();
$("body").append('<div id="subscriptions_table"></div>');
var streams_list = $('<div class="streams-list"></div>');
$("#subscriptions_table").append(streams_list);
stream_data.add_sub("Denmark", denmark);
stream_data.add_sub("Poland", poland);
stream_data.add_sub("Pomona", pomona);
stream_data.add_sub("C++", cpp);
2016-12-26 10:13:31 +01:00
streams_list.append(elem_1);
streams_list.append(elem_2);
streams_list.append(elem_3);
// Search with single keyword
subs.filter_table({input: "Po", subscribed_only: false});
assert(elem_1.hasClass("notdisplayed"));
assert(!elem_2.hasClass("notdisplayed"));
assert(!elem_3.hasClass("notdisplayed"));
// Search with multiple keywords
subs.filter_table({input: "Denmark, Pol", subscribed_only: false});
assert(!elem_1.hasClass("notdisplayed"));
assert(!elem_2.hasClass("notdisplayed"));
assert(elem_3.hasClass("notdisplayed"));
subs.filter_table({input: "Den, Pol", subscribed_only: false});
assert(!elem_1.hasClass("notdisplayed"));
2016-12-26 10:13:31 +01:00
assert(!elem_2.hasClass("notdisplayed"));
assert(elem_3.hasClass("notdisplayed"));
// Search is case-insensitive
subs.filter_table({input: "po", subscribed_only: false});
assert(elem_1.hasClass("notdisplayed"));
assert(!elem_2.hasClass("notdisplayed"));
assert(!elem_3.hasClass("notdisplayed"));
// Search handles unusual characters like C++
subs.filter_table({input: "c++", subscribed_only: false});
assert(elem_1.hasClass("notdisplayed"));
assert(elem_2.hasClass("notdisplayed"));
assert(elem_3.hasClass("notdisplayed"));
assert(!elem_4.hasClass("notdisplayed"));
2016-12-26 10:13:31 +01:00
// Search subscribed streams only
subs.filter_table({input: "d", subscribed_only: true});
assert(elem_1.hasClass("notdisplayed"));
assert(!elem_2.hasClass("notdisplayed"));
assert(elem_3.hasClass("notdisplayed"));
// data-temp-view condition
elem_1.attr("data-temp-view", "true");
subs.filter_table({input: "d", subscribed_only: true});
assert(!elem_1.hasClass("notdisplayed"));
assert(!elem_2.hasClass("notdisplayed"));
assert(elem_3.hasClass("notdisplayed"));
elem_1.attr("data-temp-view", "false");
subs.filter_table({input: "d", subscribed_only: true});
assert(elem_1.hasClass("notdisplayed"));
assert(!elem_2.hasClass("notdisplayed"));
assert(elem_3.hasClass("notdisplayed"));
elem_1.removeAttr("data-temp-view");
// active stream-row is not included in results
elem_1.addClass("active");
$("#subscriptions_table").append($('<div class="right"></div>'));
$(".right").append($('<div class="settings"></div>'));
$(".right").append($('<div class="nothing-selected"></div>').hide());
subs.filter_table({input: "d", subscribed_only: true});
assert(!elem_1.hasClass("active"));
assert.equal($(".right .settings").css("display"), "none");
assert.notEqual($(".right .nothing-selected").css("display"), "none");
// Search terms match stream description
subs.filter_table({input: "Co", subscribed_only: false});
assert(!elem_1.hasClass("notdisplayed"));
assert(elem_2.hasClass("notdisplayed"));
assert(!elem_3.hasClass("notdisplayed"));
subs.filter_table({input: "Mon", subscribed_only: false});
assert(elem_1.hasClass("notdisplayed"));
assert(!elem_2.hasClass("notdisplayed"));
assert(!elem_3.hasClass("notdisplayed"));
subs.filter_table({input: "p", subscribed_only: false});
assert.equal(subs.stream_name_match_stream_ids.length, 2);
assert.equal(subs.stream_description_match_stream_ids, 1);
assert.equal(subs.stream_name_match_stream_ids[0], 2);
assert.equal(subs.stream_name_match_stream_ids[1], 3);
assert.equal(subs.stream_description_match_stream_ids[0], 1);
subs.filter_table({input: "d", subscribed_only: false});
assert.equal(subs.stream_name_match_stream_ids.length, 2);
assert.equal(subs.stream_description_match_stream_ids, 0);
assert.equal(subs.stream_name_match_stream_ids[0], 1);
assert.equal(subs.stream_name_match_stream_ids[1], 2);
2016-12-26 10:13:31 +01:00
}());
2017-03-04 17:55:16 +01:00
(function test_sub_or_unsub() {
var denmark = {
subscribed: false,
name: 'Denmark',
stream_id: 1,
description: 'Copenhagen',
};
stream_data.clear_subscriptions();
stream_data.add_sub("Denmark", denmark);
var post_params;
global.channel.post = function (params) {
post_params = params;
};
subs.sub_or_unsub(denmark.name);
assert.equal(post_params.url, '/json/users/me/subscriptions');
assert.deepEqual(post_params.data,
{subscriptions: '[{"name":"Denmark"}]'});
global.channel.post = undefined;
global.channel.del = function (params) {
post_params = params;
};
stream_data.get_sub_by_id(denmark.stream_id).subscribed = true;
subs.sub_or_unsub(denmark.name);
assert.equal(post_params.url, '/json/users/me/subscriptions');
assert.deepEqual(post_params.data,
{subscriptions: '["Denmark"]'});
}());