mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	node tests: Prefer override over with_field.
This commit replaces `with_field` calls to use the override style instead. `override` is preferred since it makes sure the stubbed function is actually called, while `with_field` doesn't, which makes it hard to spot dead code.
This commit is contained in:
		
				
					committed by
					
						
						Steve Howell
					
				
			
			
				
	
			
			
			
						parent
						
							57f2b8760a
						
					
				
				
					commit
					6c6def2dcc
				
			@@ -4,7 +4,7 @@ const {strict: assert} = require("assert");
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const autosize = require("autosize");
 | 
					const autosize = require("autosize");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const {set_global, with_field, zrequire} = require("../zjsunit/namespace");
 | 
					const {set_global, zrequire} = require("../zjsunit/namespace");
 | 
				
			||||||
const {run_test} = require("../zjsunit/test");
 | 
					const {run_test} = require("../zjsunit/test");
 | 
				
			||||||
const {make_zjquery} = require("../zjsunit/zjquery");
 | 
					const {make_zjquery} = require("../zjsunit/zjquery");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -80,21 +80,19 @@ function make_textbox(s) {
 | 
				
			|||||||
    return widget;
 | 
					    return widget;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
run_test("autosize_textarea", () => {
 | 
					run_test("autosize_textarea", (override) => {
 | 
				
			||||||
    const textarea_autosized = {};
 | 
					    const textarea_autosized = {};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    function fake_autosize_update(textarea) {
 | 
					    override(autosize, "update", (textarea) => {
 | 
				
			||||||
        textarea_autosized.textarea = textarea;
 | 
					        textarea_autosized.textarea = textarea;
 | 
				
			||||||
        textarea_autosized.autosized = true;
 | 
					        textarea_autosized.autosized = true;
 | 
				
			||||||
    }
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    with_field(autosize, "update", fake_autosize_update, () => {
 | 
					 | 
				
			||||||
    // Call autosize_textarea with an argument
 | 
					    // Call autosize_textarea with an argument
 | 
				
			||||||
    const container = "container-stub";
 | 
					    const container = "container-stub";
 | 
				
			||||||
    compose_ui.autosize_textarea(container);
 | 
					    compose_ui.autosize_textarea(container);
 | 
				
			||||||
    assert.equal(textarea_autosized.textarea, container);
 | 
					    assert.equal(textarea_autosized.textarea, container);
 | 
				
			||||||
    assert(textarea_autosized.autosized);
 | 
					    assert(textarea_autosized.autosized);
 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
run_test("insert_syntax_and_focus", () => {
 | 
					run_test("insert_syntax_and_focus", () => {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,7 +4,7 @@ const {strict: assert} = require("assert");
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const markdown_test_cases = require("../../zerver/tests/fixtures/markdown_test_cases.json");
 | 
					const markdown_test_cases = require("../../zerver/tests/fixtures/markdown_test_cases.json");
 | 
				
			||||||
const markdown_assert = require("../zjsunit/markdown_assert");
 | 
					const markdown_assert = require("../zjsunit/markdown_assert");
 | 
				
			||||||
const {set_global, with_field, zrequire} = require("../zjsunit/namespace");
 | 
					const {set_global, zrequire} = require("../zjsunit/namespace");
 | 
				
			||||||
const {run_test} = require("../zjsunit/test");
 | 
					const {run_test} = require("../zjsunit/test");
 | 
				
			||||||
const {make_zjquery} = require("../zjsunit/zjquery");
 | 
					const {make_zjquery} = require("../zjsunit/zjquery");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -750,7 +750,7 @@ run_test("translate_emoticons_to_names", () => {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
run_test("missing unicode emojis", () => {
 | 
					run_test("missing unicode emojis", (override) => {
 | 
				
			||||||
    const message = {raw_content: "\u{1F6B2}"};
 | 
					    const message = {raw_content: "\u{1F6B2}"};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    markdown.apply_markdown(message);
 | 
					    markdown.apply_markdown(message);
 | 
				
			||||||
@@ -759,15 +759,11 @@ run_test("missing unicode emojis", () => {
 | 
				
			|||||||
        '<p><span aria-label="bike" class="emoji emoji-1f6b2" role="img" title="bike">:bike:</span></p>',
 | 
					        '<p><span aria-label="bike" class="emoji emoji-1f6b2" role="img" title="bike">:bike:</span></p>',
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    override(emoji, "get_emoji_name", (codepoint) => {
 | 
				
			||||||
        // Now simulate that we don't know any emoji names.
 | 
					        // Now simulate that we don't know any emoji names.
 | 
				
			||||||
    function fake_get_emoji_name(codepoint) {
 | 
					 | 
				
			||||||
        assert.equal(codepoint, "1f6b2");
 | 
					        assert.equal(codepoint, "1f6b2");
 | 
				
			||||||
        // return undefined
 | 
					        // return undefined
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    with_field(emoji, "get_emoji_name", fake_get_emoji_name, () => {
 | 
					 | 
				
			||||||
        markdown.apply_markdown(message);
 | 
					 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					    markdown.apply_markdown(message);
 | 
				
			||||||
    assert.equal(message.content, "<p>\u{1F6B2}</p>");
 | 
					    assert.equal(message.content, "<p>\u{1F6B2}</p>");
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -229,34 +229,25 @@ run_test("is_all_privates", () => {
 | 
				
			|||||||
    assert.equal(pm_list.is_all_privates(), true);
 | 
					    assert.equal(pm_list.is_all_privates(), true);
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function with_fake_list(f) {
 | 
					run_test("expand", (override) => {
 | 
				
			||||||
    with_field(pm_list, "_build_private_messages_list", () => "PM_LIST_CONTENTS", f);
 | 
					    override(pm_list, "_build_private_messages_list", () => "PM_LIST_CONTENTS");
 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
run_test("expand", () => {
 | 
					 | 
				
			||||||
    with_fake_list(() => {
 | 
					 | 
				
			||||||
    let html_updated;
 | 
					    let html_updated;
 | 
				
			||||||
 | 
					 | 
				
			||||||
    vdom.update = () => {
 | 
					    vdom.update = () => {
 | 
				
			||||||
        html_updated = true;
 | 
					        html_updated = true;
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pm_list.expand();
 | 
					    pm_list.expand();
 | 
				
			||||||
 | 
					 | 
				
			||||||
    assert(html_updated);
 | 
					    assert(html_updated);
 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
run_test("update_private_messages", () => {
 | 
					run_test("update_private_messages", (override) => {
 | 
				
			||||||
    narrow_state.active = () => true;
 | 
					    narrow_state.active = () => true;
 | 
				
			||||||
 | 
					 | 
				
			||||||
    $("#private-container").find = (sel) => {
 | 
					    $("#private-container").find = (sel) => {
 | 
				
			||||||
        assert.equal(sel, "ul");
 | 
					        assert.equal(sel, "ul");
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					    override(pm_list, "_build_private_messages_list", () => "PM_LIST_CONTENTS");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    with_fake_list(() => {
 | 
					 | 
				
			||||||
    let html_updated;
 | 
					    let html_updated;
 | 
				
			||||||
 | 
					 | 
				
			||||||
    vdom.update = (replace_content, find) => {
 | 
					    vdom.update = (replace_content, find) => {
 | 
				
			||||||
        html_updated = true;
 | 
					        html_updated = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -276,7 +267,6 @@ run_test("update_private_messages", () => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    assert(html_updated);
 | 
					    assert(html_updated);
 | 
				
			||||||
    assert($(".top_left_private_messages").hasClass("active-filter"));
 | 
					    assert($(".top_left_private_messages").hasClass("active-filter"));
 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
run_test("ensure coverage", () => {
 | 
					run_test("ensure coverage", () => {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,7 +3,7 @@
 | 
				
			|||||||
const {strict: assert} = require("assert");
 | 
					const {strict: assert} = require("assert");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const {stub_templates} = require("../zjsunit/handlebars");
 | 
					const {stub_templates} = require("../zjsunit/handlebars");
 | 
				
			||||||
const {set_global, with_field, zrequire} = require("../zjsunit/namespace");
 | 
					const {set_global, zrequire} = require("../zjsunit/namespace");
 | 
				
			||||||
const {make_stub} = require("../zjsunit/stub");
 | 
					const {make_stub} = require("../zjsunit/stub");
 | 
				
			||||||
const {run_test} = require("../zjsunit/test");
 | 
					const {run_test} = require("../zjsunit/test");
 | 
				
			||||||
const {make_zjquery} = require("../zjsunit/zjquery");
 | 
					const {make_zjquery} = require("../zjsunit/zjquery");
 | 
				
			||||||
@@ -686,7 +686,7 @@ run_test("with_view_stubs", () => {
 | 
				
			|||||||
    });
 | 
					    });
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
run_test("error_handling", () => {
 | 
					run_test("error_handling", (override) => {
 | 
				
			||||||
    message_store.get = function () {
 | 
					    message_store.get = function () {
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
@@ -700,15 +700,8 @@ run_test("error_handling", () => {
 | 
				
			|||||||
        emoji_code: "991",
 | 
					        emoji_code: "991",
 | 
				
			||||||
        user_id: 99,
 | 
					        user_id: 99,
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					    override(reactions, "current_user_has_reacted_to_emoji", () => true);
 | 
				
			||||||
    with_field(
 | 
					 | 
				
			||||||
        reactions,
 | 
					 | 
				
			||||||
        "current_user_has_reacted_to_emoji",
 | 
					 | 
				
			||||||
        () => true,
 | 
					 | 
				
			||||||
        () => {
 | 
					 | 
				
			||||||
    reactions.toggle_emoji_reaction(55, bogus_event.emoji_name);
 | 
					    reactions.toggle_emoji_reaction(55, bogus_event.emoji_name);
 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
    );
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    reactions.add_reaction(bogus_event);
 | 
					    reactions.add_reaction(bogus_event);
 | 
				
			||||||
    reactions.remove_reaction(bogus_event);
 | 
					    reactions.remove_reaction(bogus_event);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user