node tests: Avoid narrow_state mocking in pm_list* tests.

We now use narrow_state directly in pm_list and pm_list_data
tests, rather than mocking it with our `override*` helpers.

In some places I use an actual Filter() object, but
in places where the only testing concern is that the
active is narrow, I use a stub value.

We will continue to mock narrow_state in most places.
In addition to avoiding test-setup complications, we want
to avoid incidental line coverage on narrow_state that
only indirectly validates its behavior. Part of the
trickiness in avoiding narrow_state mocking is that
you often would have to introduce "real" Filter objects,
and the API for Filter objects is somewhat less than
ideal, and its wordiness can distract from the main
point of the tests.

Hopefully the changes here reflect the correct tradeoffs.
This commit is contained in:
Steve Howell 2022-03-16 12:53:34 +00:00 committed by Steve Howell
parent bcbba0b20d
commit dfab993e7d
2 changed files with 16 additions and 20 deletions

View File

@ -2,18 +2,19 @@
const {strict: assert} = require("assert"); const {strict: assert} = require("assert");
const {mock_esm, zrequire} = require("../zjsunit/namespace"); const {zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test"); const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery"); const $ = require("../zjsunit/zjquery");
const narrow_state = mock_esm("../../static/js/narrow_state"); const narrow_state = zrequire("narrow_state");
const pm_list = zrequire("pm_list"); const pm_list = zrequire("pm_list");
run_test("update_dom_with_unread_counts", ({override}) => { run_test("update_dom_with_unread_counts", () => {
let counts; let counts;
override(narrow_state, "active", () => true); // simulate an active narrow
narrow_state.set_current_filter("stub");
assert.equal(narrow_state.active(), true);
const total_count = $.create("total-count-stub"); const total_count = $.create("total-count-stub");
const private_li = $(".top_left_private_messages .private_messages_header"); const private_li = $(".top_left_private_messages .private_messages_header");

View File

@ -5,7 +5,6 @@ const {strict: assert} = require("assert");
const {mock_esm, zrequire} = require("../zjsunit/namespace"); const {mock_esm, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test"); const {run_test} = require("../zjsunit/test");
const narrow_state = mock_esm("../../static/js/narrow_state");
const unread = mock_esm("../../static/js/unread"); const unread = mock_esm("../../static/js/unread");
mock_esm("../../static/js/user_status", { mock_esm("../../static/js/user_status", {
@ -15,6 +14,7 @@ mock_esm("../../static/js/user_status", {
}), }),
}); });
const narrow_state = zrequire("narrow_state");
const people = zrequire("people"); const people = zrequire("people");
const pm_conversations = zrequire("pm_conversations"); const pm_conversations = zrequire("pm_conversations");
const pm_list_data = zrequire("pm_list_data"); const pm_list_data = zrequire("pm_list_data");
@ -49,6 +49,7 @@ people.initialize_current_user(me.user_id);
function test(label, f) { function test(label, f) {
run_test(label, ({override, override_rewire}) => { run_test(label, ({override, override_rewire}) => {
narrow_state.reset_current_filter();
pm_conversations.clear_for_testing(); pm_conversations.clear_for_testing();
f({override, override_rewire}); f({override, override_rewire});
}); });
@ -60,7 +61,7 @@ test("get_convos", ({override}) => {
let num_unread_for_person = 1; let num_unread_for_person = 1;
override(unread, "num_unread_for_person", () => num_unread_for_person); override(unread, "num_unread_for_person", () => num_unread_for_person);
override(narrow_state, "filter", () => {}); assert.equal(narrow_state.filter(), undefined);
const expected_data = [ const expected_data = [
{ {
@ -111,7 +112,7 @@ test("get_convos bot", ({override}) => {
override(unread, "num_unread_for_person", () => 1); override(unread, "num_unread_for_person", () => 1);
override(narrow_state, "filter", () => {}); assert.equal(narrow_state.filter(), undefined);
const expected_data = [ const expected_data = [
{ {
@ -142,20 +143,17 @@ test("get_convos bot", ({override}) => {
assert.deepEqual(pm_data, expected_data); assert.deepEqual(pm_data, expected_data);
}); });
test("get_active_user_ids_string", ({override}) => { test("get_active_user_ids_string", () => {
let active_filter;
override(narrow_state, "filter", () => active_filter);
assert.equal(pm_list_data.get_active_user_ids_string(), undefined); assert.equal(pm_list_data.get_active_user_ids_string(), undefined);
function set_filter_result(emails) { function set_filter_result(emails) {
active_filter = { const active_filter = {
operands: (operand) => { operands: (operand) => {
assert.equal(operand, "pm-with"); assert.equal(operand, "pm-with");
return emails; return emails;
}, },
}; };
narrow_state.set_current_filter(active_filter);
} }
set_filter_result([]); set_filter_result([]);
@ -174,13 +172,10 @@ function private_filter() {
}; };
} }
test("is_all_privates", ({override}) => { test("is_all_privates", () => {
let filter; assert.equal(narrow_state.filter(), undefined);
override(narrow_state, "filter", () => filter);
filter = undefined;
assert.equal(pm_list_data.is_all_privates(), false); assert.equal(pm_list_data.is_all_privates(), false);
filter = private_filter(); narrow_state.set_current_filter(private_filter());
assert.equal(pm_list_data.is_all_privates(), true); assert.equal(pm_list_data.is_all_privates(), true);
}); });