zulip/frontend_tests/node_tests/people_errors.js
Anders Kaseorg c520890f54 node_tests: Remove low-hanging uses of __Rewire__.
When we were preparing the conversion to ES modules in 2019, the
primary obstacle was that the Node tests extensively relied on the
ability to reach into modules and mutate their CommonJS exports in
order to mock things.  ES module bindings are not mutable, so in
commit 173c9cee42 we added
babel-plugin-rewire-ts as a kludgy transpilation-based workaround for
this to unblock the conversion.

However, babel-plugin-rewire-ts is slow, buggy, nonstandard,
confusing, and unmaintained.  It’s incompatible with running our ES
modules as native ES modules, and prevents us from taking advantage of
modern tools for ES modules.  So we want to excise all use of
__Rewire__ (and the disallow_rewire, override_rewire helper functions
that rely on it) from the tests and remove babel-plugin-rewire-ts.

Commits 64abdc199e and
e17ba5260a (#20730) prepared for this by
letting us see where __Rewire__ is being used.  Now we go through and
remove most of the uses that are easy to remove without modifying the
production code at all.

Signed-off-by: Anders Kaseorg <[email protected]>
2022-07-13 16:27:30 -07:00

118 lines
3.8 KiB
JavaScript

"use strict";
const {strict: assert} = require("assert");
const {mock_esm, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const blueslip = require("../zjsunit/zblueslip");
const reload_state = mock_esm("../../static/js/reload_state", {
is_in_progress: () => false,
});
const people = zrequire("people");
const me = {
email: "[email protected]",
user_id: 30,
full_name: "Me Myself",
timezone: "America/Los_Angeles",
};
people.init();
people.add_active_user(me);
people.initialize_current_user(me.user_id);
run_test("report_late_add", ({override}) => {
blueslip.expect("error", "Added user late: user_id=55 [email protected]");
people.report_late_add(55, "[email protected]");
blueslip.expect("log", "Added user late: user_id=55 [email protected]");
override(reload_state, "is_in_progress", () => true);
people.report_late_add(55, "[email protected]");
});
run_test("is_my_user_id", () => {
blueslip.expect("error", "user_id is a string in my_user_id: 999");
assert.equal(people.is_my_user_id("999"), false);
blueslip.expect("error", "user_id is a string in my_user_id: 30");
assert.equal(people.is_my_user_id(me.user_id.toString()), true);
});
run_test("blueslip", () => {
const unknown_email = "[email protected]";
blueslip.expect("debug", "User email operand unknown: " + unknown_email);
people.id_matches_email_operand(42, unknown_email);
blueslip.expect("error", "Unknown user_id: 9999");
people.get_actual_name_from_user_id(9999);
blueslip.expect("error", "Unknown email for get_user_id: " + unknown_email);
people.get_user_id(unknown_email);
blueslip.expect("warn", "No user_id provided for [email protected]");
const person = {
email: "[email protected]",
user_id: undefined,
full_name: "Person Person",
};
people.add_active_user(person);
blueslip.expect("error", "No user_id found for [email protected]");
const user_id = people.get_user_id("[email protected]");
assert.equal(user_id, undefined);
blueslip.expect("warn", "Unknown user ids: 1,2");
people.user_ids_string_to_emails_string("1,2");
blueslip.expect("warn", "Unknown emails: " + unknown_email);
people.email_list_to_user_ids_string([unknown_email]);
let message = {
type: "private",
display_recipient: [],
sender_id: me.user_id,
};
blueslip.expect("error", "Empty recipient list in message", 4);
people.pm_with_user_ids(message);
people.group_pm_with_user_ids(message);
people.all_user_ids_in_pm(message);
assert.equal(people.pm_perma_link(message), undefined);
const charles = {
email: "[email protected]",
user_id: 451,
full_name: "Charles Dickens",
avatar_url: "charles.com/foo.png",
};
const maria = {
email: "[email protected]",
user_id: 452,
full_name: "Maria Athens",
};
people.add_active_user(charles);
people.add_active_user(maria);
message = {
type: "private",
display_recipient: [{id: maria.user_id}, {id: 42}, {id: charles.user_id}],
sender_id: charles.user_id,
};
blueslip.expect("error", "Unknown user id in message: 42");
const reply_to = people.pm_reply_to(message);
assert.ok(reply_to.includes("?"));
blueslip.expect("error", "Unknown user_id in get_by_user_id: 42");
blueslip.expect("error", "Unknown people in message");
const uri = people.pm_with_url({type: "private", display_recipient: [{id: 42}]});
assert.equal(uri.indexOf("unk"), uri.length - 3);
blueslip.expect("error", "Undefined field id");
assert.equal(people.my_custom_profile_data(undefined), undefined);
blueslip.expect("error", "Trying to set undefined field id");
people.set_custom_profile_field_data(maria.user_id, {});
});