mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			300 lines
		
	
	
		
			7.8 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			300 lines
		
	
	
		
			7.8 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
"use strict";
 | 
						|
 | 
						|
const {strict: assert} = require("assert");
 | 
						|
 | 
						|
const {mock_cjs, mock_esm, with_field, zrequire} = require("../zjsunit/namespace");
 | 
						|
const {run_test} = require("../zjsunit/test");
 | 
						|
const $ = require("../zjsunit/zjquery");
 | 
						|
 | 
						|
mock_cjs("jquery", $);
 | 
						|
const narrow_state = mock_esm("../../static/js/narrow_state");
 | 
						|
const pm_list_dom = mock_esm("../../static/js/pm_list_dom");
 | 
						|
const unread = mock_esm("../../static/js/unread");
 | 
						|
const unread_ui = mock_esm("../../static/js/unread_ui");
 | 
						|
const vdom = mock_esm("../../static/js/vdom", {
 | 
						|
    render: () => "fake-dom-for-pm-list",
 | 
						|
});
 | 
						|
 | 
						|
mock_esm("../../static/js/stream_popover", {
 | 
						|
    hide_topic_popover() {},
 | 
						|
});
 | 
						|
mock_esm("../../static/js/ui", {
 | 
						|
    get_content_element: (element) => element,
 | 
						|
});
 | 
						|
 | 
						|
const people = zrequire("people");
 | 
						|
const pm_conversations = zrequire("pm_conversations");
 | 
						|
const pm_list = zrequire("pm_list");
 | 
						|
 | 
						|
const alice = {
 | 
						|
    email: "alice@zulip.com",
 | 
						|
    user_id: 101,
 | 
						|
    full_name: "Alice",
 | 
						|
};
 | 
						|
const bob = {
 | 
						|
    email: "bob@zulip.com",
 | 
						|
    user_id: 102,
 | 
						|
    full_name: "Bob",
 | 
						|
};
 | 
						|
const me = {
 | 
						|
    email: "me@zulip.com",
 | 
						|
    user_id: 103,
 | 
						|
    full_name: "Me Myself",
 | 
						|
};
 | 
						|
const bot_test = {
 | 
						|
    email: "outgoingwebhook@zulip.com",
 | 
						|
    user_id: 314,
 | 
						|
    full_name: "Outgoing webhook",
 | 
						|
    is_admin: false,
 | 
						|
    is_bot: true,
 | 
						|
};
 | 
						|
people.add_active_user(alice);
 | 
						|
people.add_active_user(bob);
 | 
						|
people.add_active_user(me);
 | 
						|
people.add_active_user(bot_test);
 | 
						|
people.initialize_current_user(me.user_id);
 | 
						|
 | 
						|
function test(label, f) {
 | 
						|
    run_test(label, (override) => {
 | 
						|
        pm_conversations.clear_for_testing();
 | 
						|
        pm_list.clear_for_testing();
 | 
						|
        f(override);
 | 
						|
    });
 | 
						|
}
 | 
						|
 | 
						|
test("close", () => {
 | 
						|
    let collapsed;
 | 
						|
    $("#private-container").empty = () => {
 | 
						|
        collapsed = true;
 | 
						|
    };
 | 
						|
    pm_list.close();
 | 
						|
    assert(collapsed);
 | 
						|
});
 | 
						|
 | 
						|
test("build_private_messages_list", (override) => {
 | 
						|
    const timestamp = 0;
 | 
						|
    pm_conversations.recent.insert([101, 102], timestamp);
 | 
						|
 | 
						|
    let num_unread_for_person = 1;
 | 
						|
    override(unread, "num_unread_for_person", () => num_unread_for_person);
 | 
						|
 | 
						|
    let pm_data;
 | 
						|
 | 
						|
    override(pm_list_dom, "pm_ul", (data) => {
 | 
						|
        pm_data = data;
 | 
						|
    });
 | 
						|
 | 
						|
    override(narrow_state, "filter", () => {});
 | 
						|
    pm_list._build_private_messages_list();
 | 
						|
 | 
						|
    const expected_data = [
 | 
						|
        {
 | 
						|
            recipients: "Alice, Bob",
 | 
						|
            user_ids_string: "101,102",
 | 
						|
            unread: 1,
 | 
						|
            is_zero: false,
 | 
						|
            is_active: false,
 | 
						|
            url: "#narrow/pm-with/101,102-group",
 | 
						|
            user_circle_class: "user_circle_fraction",
 | 
						|
            fraction_present: undefined,
 | 
						|
            is_group: true,
 | 
						|
        },
 | 
						|
    ];
 | 
						|
 | 
						|
    assert.deepEqual(pm_data, expected_data);
 | 
						|
 | 
						|
    num_unread_for_person = 0;
 | 
						|
    pm_list._build_private_messages_list();
 | 
						|
    expected_data[0].unread = 0;
 | 
						|
    expected_data[0].is_zero = true;
 | 
						|
    assert.deepEqual(pm_data, expected_data);
 | 
						|
 | 
						|
    pm_list._build_private_messages_list();
 | 
						|
    assert.deepEqual(pm_data, expected_data);
 | 
						|
});
 | 
						|
 | 
						|
test("build_private_messages_list_bot", (override) => {
 | 
						|
    const timestamp = 0;
 | 
						|
    pm_conversations.recent.insert([101, 102], timestamp);
 | 
						|
    pm_conversations.recent.insert([314], timestamp);
 | 
						|
 | 
						|
    override(unread, "num_unread_for_person", () => 1);
 | 
						|
 | 
						|
    let pm_data;
 | 
						|
    override(pm_list_dom, "pm_ul", (data) => {
 | 
						|
        pm_data = data;
 | 
						|
    });
 | 
						|
 | 
						|
    override(narrow_state, "filter", () => {});
 | 
						|
 | 
						|
    pm_list._build_private_messages_list();
 | 
						|
    const expected_data = [
 | 
						|
        {
 | 
						|
            recipients: "Outgoing webhook",
 | 
						|
            user_ids_string: "314",
 | 
						|
            unread: 1,
 | 
						|
            is_zero: false,
 | 
						|
            is_active: false,
 | 
						|
            url: "#narrow/pm-with/314-outgoingwebhook",
 | 
						|
            user_circle_class: "user_circle_green",
 | 
						|
            fraction_present: undefined,
 | 
						|
            is_group: false,
 | 
						|
        },
 | 
						|
        {
 | 
						|
            recipients: "Alice, Bob",
 | 
						|
            user_ids_string: "101,102",
 | 
						|
            unread: 1,
 | 
						|
            is_zero: false,
 | 
						|
            is_active: false,
 | 
						|
            url: "#narrow/pm-with/101,102-group",
 | 
						|
            user_circle_class: "user_circle_fraction",
 | 
						|
            fraction_present: undefined,
 | 
						|
            is_group: true,
 | 
						|
        },
 | 
						|
    ];
 | 
						|
 | 
						|
    assert.deepEqual(pm_data, expected_data);
 | 
						|
});
 | 
						|
 | 
						|
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");
 | 
						|
    const private_li = $(".top_left_private_messages");
 | 
						|
    private_li.set_find_results(".count", total_count);
 | 
						|
    total_count.set_find_results(".value", total_value);
 | 
						|
 | 
						|
    counts = {
 | 
						|
        private_message_count: 10,
 | 
						|
    };
 | 
						|
 | 
						|
    expected_unread_count = 10;
 | 
						|
 | 
						|
    toggle_button_set = false;
 | 
						|
    pm_list.update_dom_with_unread_counts(counts);
 | 
						|
    assert(toggle_button_set);
 | 
						|
 | 
						|
    counts = {
 | 
						|
        private_message_count: 0,
 | 
						|
    };
 | 
						|
 | 
						|
    expected_unread_count = 0;
 | 
						|
 | 
						|
    toggle_button_set = false;
 | 
						|
    pm_list.update_dom_with_unread_counts(counts);
 | 
						|
    assert(toggle_button_set);
 | 
						|
});
 | 
						|
 | 
						|
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) {
 | 
						|
        active_filter = {
 | 
						|
            operands: (operand) => {
 | 
						|
                assert.equal(operand, "pm-with");
 | 
						|
                return emails;
 | 
						|
            },
 | 
						|
        };
 | 
						|
    }
 | 
						|
 | 
						|
    set_filter_result([]);
 | 
						|
    assert.equal(pm_list.get_active_user_ids_string(), undefined);
 | 
						|
 | 
						|
    set_filter_result(["bob@zulip.com,alice@zulip.com"]);
 | 
						|
    assert.equal(pm_list.get_active_user_ids_string(), "101,102");
 | 
						|
});
 | 
						|
 | 
						|
function private_filter() {
 | 
						|
    return {
 | 
						|
        operands: (operand) => {
 | 
						|
            assert.equal(operand, "is");
 | 
						|
            return ["private", "starred"];
 | 
						|
        },
 | 
						|
    };
 | 
						|
}
 | 
						|
 | 
						|
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);
 | 
						|
});
 | 
						|
 | 
						|
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;
 | 
						|
    override(vdom, "update", () => {
 | 
						|
        html_updated = true;
 | 
						|
    });
 | 
						|
 | 
						|
    assert(!$(".top_left_private_messages").hasClass("active-filter"));
 | 
						|
 | 
						|
    pm_list.expand();
 | 
						|
    assert(html_updated);
 | 
						|
    assert($(".top_left_private_messages").hasClass("active-filter"));
 | 
						|
});
 | 
						|
 | 
						|
test("update_private_messages", (override) => {
 | 
						|
    let html_updated;
 | 
						|
    let container_found;
 | 
						|
 | 
						|
    override(narrow_state, "filter", private_filter);
 | 
						|
    override(narrow_state, "active", () => true);
 | 
						|
    override(pm_list, "_build_private_messages_list", () => "PM_LIST_CONTENTS");
 | 
						|
 | 
						|
    $("#private-container").find = (sel) => {
 | 
						|
        assert.equal(sel, "ul");
 | 
						|
        container_found = true;
 | 
						|
    };
 | 
						|
 | 
						|
    override(vdom, "update", (replace_content, find) => {
 | 
						|
        html_updated = true;
 | 
						|
 | 
						|
        // get line coverage for simple one-liners
 | 
						|
        replace_content();
 | 
						|
        find();
 | 
						|
    });
 | 
						|
 | 
						|
    pm_list.expand();
 | 
						|
    pm_list.update_private_messages();
 | 
						|
    assert(html_updated);
 | 
						|
    assert(container_found);
 | 
						|
});
 | 
						|
 | 
						|
test("ensure coverage", (override) => {
 | 
						|
    // These aren't rigorous; they just cover cases
 | 
						|
    // where functions early exit.
 | 
						|
    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();
 | 
						|
        },
 | 
						|
    );
 | 
						|
});
 |