2021-07-19 19:56:13 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2024-08-02 19:50:39 +02:00
|
|
|
const {make_user} = require("./lib/example_user");
|
2023-12-05 16:06:43 +01:00
|
|
|
const {mock_esm, zrequire} = require("./lib/namespace");
|
2023-02-22 23:04:10 +01:00
|
|
|
const {run_test} = require("./lib/test");
|
|
|
|
const $ = require("./lib/zjquery");
|
2024-02-13 02:08:16 +01:00
|
|
|
const {current_user} = require("./lib/zpage_params");
|
2021-07-19 19:56:13 +02:00
|
|
|
|
2023-12-05 16:06:43 +01:00
|
|
|
mock_esm("../src/settings_data", {
|
|
|
|
user_can_access_all_other_users: () => true,
|
|
|
|
});
|
|
|
|
|
2021-07-19 19:56:13 +02:00
|
|
|
/*
|
2021-07-28 22:00:31 +02:00
|
|
|
Until now, we had seen various testing techniques, learned
|
2021-07-19 19:56:13 +02:00
|
|
|
how to use helper functions like `mock_esm`, `override` of
|
|
|
|
`run_test` etc., but we didn't see how to deal with
|
2022-06-27 22:35:01 +02:00
|
|
|
render calls to Handlebars templates. We'll learn that
|
2021-07-19 19:56:13 +02:00
|
|
|
in this test.
|
|
|
|
|
2021-07-28 22:00:31 +02:00
|
|
|
The below code tests the rendering of typing notifications which
|
|
|
|
is handled by the function `typing_events.render_notifications_for_narrow`.
|
|
|
|
The function relies on the `typing_notifications.hbs` template for
|
|
|
|
rendering html.
|
|
|
|
It is worthwhile to read those (they're short and simple) before proceeding
|
|
|
|
as that would help better understand the below test.
|
2021-07-19 19:56:13 +02:00
|
|
|
*/
|
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
const {Filter} = zrequire("filter");
|
2024-02-13 03:44:04 +01:00
|
|
|
const message_lists = zrequire("message_lists");
|
2021-07-28 22:00:31 +02:00
|
|
|
const people = zrequire("people");
|
2022-07-10 01:06:33 +02:00
|
|
|
const typing_data = zrequire("typing_data");
|
|
|
|
const typing_events = zrequire("typing_events");
|
2021-07-19 19:56:13 +02:00
|
|
|
|
2021-07-28 22:00:31 +02:00
|
|
|
// Let us add a few users to use as typists.
|
2024-08-02 19:50:39 +02:00
|
|
|
const anna = make_user({
|
2021-07-28 22:00:31 +02:00
|
|
|
email: "anna@example.com",
|
|
|
|
full_name: "Anna Karenina",
|
|
|
|
user_id: 8,
|
2024-08-02 19:50:39 +02:00
|
|
|
});
|
2021-07-28 22:00:31 +02:00
|
|
|
|
2024-08-02 19:50:39 +02:00
|
|
|
const vronsky = make_user({
|
2021-07-28 22:00:31 +02:00
|
|
|
email: "vronsky@example.com",
|
|
|
|
full_name: "Alexei Vronsky",
|
|
|
|
user_id: 9,
|
2024-08-02 19:50:39 +02:00
|
|
|
});
|
2021-07-28 22:00:31 +02:00
|
|
|
|
2024-08-02 19:50:39 +02:00
|
|
|
const levin = make_user({
|
2021-07-28 22:00:31 +02:00
|
|
|
email: "levin@example.com",
|
|
|
|
full_name: "Konstantin Levin",
|
|
|
|
user_id: 10,
|
2024-08-02 19:50:39 +02:00
|
|
|
});
|
2021-07-28 22:00:31 +02:00
|
|
|
|
2024-08-02 19:50:39 +02:00
|
|
|
const kitty = make_user({
|
2021-07-28 22:00:31 +02:00
|
|
|
email: "kitty@example.com",
|
|
|
|
full_name: "Kitty S",
|
|
|
|
user_id: 11,
|
2024-08-02 19:50:39 +02:00
|
|
|
});
|
2021-07-28 22:00:31 +02:00
|
|
|
|
|
|
|
people.add_active_user(anna);
|
|
|
|
people.add_active_user(vronsky);
|
|
|
|
people.add_active_user(levin);
|
|
|
|
people.add_active_user(kitty);
|
2021-07-19 19:56:13 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Notice the `mock_template` in the object passed to `run_test` wrapper below.
|
|
|
|
It is pretty similar to `override` we've seen in previous examples but
|
|
|
|
mocks a template instead of a js function.
|
|
|
|
|
|
|
|
Just like `override`, `mock_template` lets us run a function taking in
|
|
|
|
the arguments passed to the template. Additionally, we can also have
|
|
|
|
the rendered html passed as an argument.
|
|
|
|
|
|
|
|
It's usage below will make it more clear to you.
|
|
|
|
*/
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test("typing_events.render_notifications_for_narrow", ({override, mock_template}) => {
|
2021-07-28 22:00:31 +02:00
|
|
|
// All typists are rendered in `#typing_notifications`.
|
2022-01-25 11:36:19 +01:00
|
|
|
const $typing_notifications = $("#typing_notifications");
|
2021-07-28 22:00:31 +02:00
|
|
|
|
2023-06-16 17:37:19 +02:00
|
|
|
// Narrow to a group direct message with four users.
|
2024-02-13 02:08:16 +01:00
|
|
|
override(current_user, "user_id", anna.user_id);
|
2022-07-10 01:06:33 +02:00
|
|
|
const group = [anna.user_id, vronsky.user_id, levin.user_id, kitty.user_id];
|
2021-04-05 21:10:11 +02:00
|
|
|
const conversation_key = typing_data.get_direct_message_conversation_key(group);
|
2022-07-10 01:06:33 +02:00
|
|
|
const group_emails = `${anna.email},${vronsky.email},${levin.email},${kitty.email}`;
|
2024-02-13 03:44:04 +01:00
|
|
|
message_lists.set_current({
|
|
|
|
data: {
|
|
|
|
filter: new Filter([{operator: "dm", operand: group_emails}]),
|
|
|
|
},
|
|
|
|
});
|
2021-07-28 22:00:31 +02:00
|
|
|
|
|
|
|
// Based on typing_events.MAX_USERS_TO_DISPLAY_NAME (which is currently 3),
|
|
|
|
// we display either the list of all users typing (if they do not exceed
|
|
|
|
// MAX_USERS_TO_DISPLAY_NAME) or 'Several people are typing…'
|
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
// For now, set two of the users as being typists.
|
2021-04-05 21:10:11 +02:00
|
|
|
typing_data.add_typist(conversation_key, anna.user_id);
|
|
|
|
typing_data.add_typist(conversation_key, vronsky.user_id);
|
2021-07-28 22:00:31 +02:00
|
|
|
|
|
|
|
const two_typing_users_rendered_html = "Two typing users rendered html stub";
|
|
|
|
|
|
|
|
// As you can see below, the first argument of mock_template takes
|
2023-02-22 23:03:47 +01:00
|
|
|
// the relative path of the template we want to mock w.r.t web/templates/
|
2021-07-19 19:56:13 +02:00
|
|
|
//
|
2021-10-18 16:30:46 +02:00
|
|
|
// The second argument takes a boolean determining whether to render html.
|
2021-07-19 19:56:13 +02:00
|
|
|
// We mostly set this to `false` and recommend you avoid setting this to `true`
|
|
|
|
// unless necessary in situations where you want to test conditionals
|
2021-07-28 22:00:31 +02:00
|
|
|
// or something similar. The latter examples below would make that more clear.
|
2021-07-19 19:56:13 +02:00
|
|
|
//
|
|
|
|
// The third takes a function to run on calling this template. The function
|
|
|
|
// gets passed an object(`args` below) containing arguments passed to the template.
|
|
|
|
// Additionally, it can also have rendered html passed to it if second argument of
|
2021-07-28 22:00:31 +02:00
|
|
|
// mock_template was set to `true`. Any render calls to this template
|
2021-07-19 19:56:13 +02:00
|
|
|
// will run the function and return the function's return value.
|
2021-07-28 22:00:31 +02:00
|
|
|
//
|
|
|
|
// We often use the function in third argument, like below, to make sure
|
|
|
|
// the arguments passed to the template are what we expect.
|
|
|
|
mock_template("typing_notifications.hbs", false, (args) => {
|
2022-07-10 01:06:33 +02:00
|
|
|
assert.deepEqual(args.users, [anna, vronsky]);
|
2021-07-28 22:00:31 +02:00
|
|
|
assert.ok(!args.several_users); // Whether to show 'Several people are typing…'
|
|
|
|
return two_typing_users_rendered_html;
|
|
|
|
});
|
|
|
|
|
|
|
|
typing_events.render_notifications_for_narrow();
|
|
|
|
// Make sure #typing_notifications's html content is set to the rendered template
|
|
|
|
// which we mocked and gave a custom return value.
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.equal($typing_notifications.html(), two_typing_users_rendered_html);
|
2021-07-28 22:00:31 +02:00
|
|
|
|
|
|
|
// Now we'll see how setting the second argument to `true`
|
|
|
|
// can be helpful in testing conditionals inside the template.
|
|
|
|
|
|
|
|
// Let's set the mock to just return the rendered html.
|
2023-06-29 21:59:08 +02:00
|
|
|
mock_template("typing_notifications.hbs", true, (_args, rendered_html) => rendered_html);
|
2021-07-28 22:00:31 +02:00
|
|
|
|
|
|
|
// Since we only have two(<MAX_USERS_TO_DISPLAY_NAME) typists, both of them
|
|
|
|
// should be rendered but not 'Several people are typing…'
|
|
|
|
typing_events.render_notifications_for_narrow();
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($typing_notifications.html().includes(`${anna.full_name} is typing…`));
|
|
|
|
assert.ok($typing_notifications.html().includes(`${vronsky.full_name} is typing…`));
|
|
|
|
assert.ok(!$typing_notifications.html().includes("Several people are typing…"));
|
2021-07-28 22:00:31 +02:00
|
|
|
|
|
|
|
// Change to having four typists and verify the rendered html has
|
|
|
|
// 'Several people are typing…' but not the list of users.
|
2021-04-05 21:10:11 +02:00
|
|
|
typing_data.add_typist(conversation_key, levin.user_id);
|
|
|
|
typing_data.add_typist(conversation_key, kitty.user_id);
|
2021-07-28 22:00:31 +02:00
|
|
|
|
|
|
|
typing_events.render_notifications_for_narrow();
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($typing_notifications.html().includes("Several people are typing…"));
|
|
|
|
assert.ok(!$typing_notifications.html().includes(`${anna.full_name} is typing…`));
|
|
|
|
assert.ok(!$typing_notifications.html().includes(`${vronsky.full_name} is typing…`));
|
|
|
|
assert.ok(!$typing_notifications.html().includes(`${levin.full_name} is typing…`));
|
|
|
|
assert.ok(!$typing_notifications.html().includes(`${kitty.full_name} is typing…`));
|
2021-07-19 19:56:13 +02:00
|
|
|
});
|