node tests: Use override/with_field in pm_list.

This commit is contained in:
Steve Howell 2021-02-20 12:14:49 +00:00 committed by Steve Howell
parent 94d6dd5ee5
commit 7e4d784888
1 changed files with 66 additions and 51 deletions

View File

@ -2,7 +2,7 @@
const {strict: assert} = require("assert");
const {set_global, zrequire} = require("../zjsunit/namespace");
const {set_global, with_field, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const {make_zjquery} = require("../zjsunit/zjquery");
@ -59,28 +59,27 @@ people.initialize_current_user(me.user_id);
run_test("close", () => {
let collapsed;
$("#private-container").empty = function () {
$("#private-container").empty = () => {
collapsed = true;
};
pm_list.close();
assert(collapsed);
});
run_test("build_private_messages_list", () => {
run_test("build_private_messages_list", (override) => {
const timestamp = 0;
pm_conversations.recent.insert([101, 102], timestamp);
unread.num_unread_for_person = function () {
return 1;
};
let num_unread_for_person = 1;
override(unread, "num_unread_for_person", () => num_unread_for_person);
let pm_data;
pm_list_dom.pm_ul = (data) => {
override(pm_list_dom, "pm_ul", (data) => {
pm_data = data;
};
});
narrow_state.filter = () => {};
override(narrow_state, "filter", () => {});
pm_list._build_private_messages_list();
const expected_data = [
@ -99,9 +98,7 @@ run_test("build_private_messages_list", () => {
assert.deepEqual(pm_data, expected_data);
unread.num_unread_for_person = function () {
return 0;
};
num_unread_for_person = 0;
pm_list._build_private_messages_list();
expected_data[0].unread = 0;
expected_data[0].is_zero = true;
@ -112,20 +109,18 @@ run_test("build_private_messages_list", () => {
assert.deepEqual(pm_data, expected_data);
});
run_test("build_private_messages_list_bot", () => {
run_test("build_private_messages_list_bot", (override) => {
const timestamp = 0;
pm_conversations.recent.insert([314], timestamp);
unread.num_unread_for_person = function () {
return 1;
};
override(unread, "num_unread_for_person", () => 1);
let pm_data;
pm_list_dom.pm_ul = (data) => {
override(pm_list_dom, "pm_ul", (data) => {
pm_data = data;
};
});
narrow_state.active = () => true;
override(narrow_state, "filter", () => {});
pm_list._build_private_messages_list();
const expected_data = [
@ -156,9 +151,17 @@ run_test("build_private_messages_list_bot", () => {
assert.deepEqual(pm_data, expected_data);
});
run_test("update_dom_with_unread_counts", () => {
run_test("update_dom_with_unread_counts", (override) => {
let counts;
let toggle_button_set;
let expected_unread_count;
override(narrow_state, "active", () => true);
override(unread_ui, "set_count_toggle_button", (elt, count) => {
toggle_button_set = true;
assert.equal(count, expected_unread_count);
});
const total_value = $.create("total-value-stub");
const total_count = $.create("total-count-stub");
@ -170,10 +173,7 @@ run_test("update_dom_with_unread_counts", () => {
private_message_count: 10,
};
unread_ui.set_count_toggle_button = function (elt, count) {
toggle_button_set = true;
assert.equal(count, 10);
};
expected_unread_count = 10;
toggle_button_set = false;
pm_list.update_dom_with_unread_counts(counts);
@ -183,28 +183,27 @@ run_test("update_dom_with_unread_counts", () => {
private_message_count: 0,
};
unread_ui.set_count_toggle_button = function (elt, count) {
toggle_button_set = true;
assert.equal(count, 0);
};
expected_unread_count = 0;
toggle_button_set = false;
pm_list.update_dom_with_unread_counts(counts);
assert(toggle_button_set);
});
run_test("get_active_user_ids_string", () => {
narrow_state.filter = () => {};
run_test("get_active_user_ids_string", (override) => {
let active_filter;
override(narrow_state, "filter", () => active_filter);
assert.equal(pm_list.get_active_user_ids_string(), undefined);
function set_filter_result(emails) {
narrow_state.filter = () => ({
active_filter = {
operands: (operand) => {
assert.equal(operand, "pm-with");
return emails;
},
});
};
}
set_filter_result([]);
@ -214,59 +213,75 @@ run_test("get_active_user_ids_string", () => {
assert.equal(pm_list.get_active_user_ids_string(), "101,102");
});
run_test("is_all_privates", () => {
narrow_state.filter = () => {};
assert.equal(pm_list.is_all_privates(), false);
narrow_state.filter = () => ({
function private_filter() {
return {
operands: (operand) => {
assert.equal(operand, "is");
return ["private", "starred"];
},
});
};
}
run_test("is_all_privates", (override) => {
let filter;
override(narrow_state, "filter", () => filter);
filter = undefined;
assert.equal(pm_list.is_all_privates(), false);
filter = private_filter();
assert.equal(pm_list.is_all_privates(), true);
});
run_test("expand", (override) => {
override(narrow_state, "filter", private_filter);
override(narrow_state, "active", () => true);
override(pm_list, "_build_private_messages_list", () => "PM_LIST_CONTENTS");
let html_updated;
vdom.update = () => {
override(vdom, "update", () => {
html_updated = true;
};
});
pm_list.expand();
assert(html_updated);
assert($(".top_left_private_messages").hasClass("active-filter"));
});
run_test("update_private_messages", (override) => {
narrow_state.active = () => true;
override(narrow_state, "active", () => true);
$("#private-container").find = (sel) => {
assert.equal(sel, "ul");
};
override(pm_list, "_build_private_messages_list", () => "PM_LIST_CONTENTS");
let html_updated;
vdom.update = (replace_content, find) => {
override(vdom, "update", (replace_content, find) => {
html_updated = true;
// get line coverage for simple one-liners
replace_content();
find();
};
});
pm_list.update_private_messages();
assert(html_updated);
assert($(".top_left_private_messages").hasClass("active-filter"));
});
run_test("ensure coverage", () => {
run_test("ensure coverage", (override) => {
// These aren't rigorous; they just cover cases
// where functions early exit.
narrow_state.active = () => false;
pm_list.rebuild_recent = () => {
throw new Error("we should not call rebuild_recent");
};
pm_list.update_private_messages();
override(narrow_state, "active", () => false);
with_field(
pm_list,
"rebuild_recent",
() => {
throw new Error("we should not call rebuild_recent");
},
() => {
pm_list.update_private_messages();
},
);
});