From 329f38975eedce3da588d1be376a8f35dff186fd Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Mon, 27 Jul 2020 14:06:46 +0000 Subject: [PATCH] zjsunit: Add with_field helper. This just lets us temporarily assign a value to a field. Differences with the "override" scheme: * override only works on globals * override (when passed in via run_test) will just automatically clean up at the end of the function --- .eslintrc.json | 1 + frontend_tests/node_tests/markdown.js | 9 +++------ frontend_tests/zjsunit/index.js | 1 + frontend_tests/zjsunit/namespace.js | 7 +++++++ 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 96be4766ab..fde02677a9 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -348,6 +348,7 @@ "document": false, "set_global": false, "window": false, + "with_field": false, "zrequire": false }, "rules": { diff --git a/frontend_tests/node_tests/markdown.js b/frontend_tests/node_tests/markdown.js index 5d0dc97e6a..e1daff6765 100644 --- a/frontend_tests/node_tests/markdown.js +++ b/frontend_tests/node_tests/markdown.js @@ -758,12 +758,9 @@ run_test("missing unicode emojis", () => { // return undefined } - const actual_get_emoji_name = emoji.get_emoji_name; - emoji.get_emoji_name = fake_get_emoji_name; + with_field(emoji, "get_emoji_name", fake_get_emoji_name, () => { + markdown.apply_markdown(message); + }); - markdown.apply_markdown(message); assert.equal(message.content, "

\u{1f6b2}

"); - - // restore get_emoji_name for any future tests that follow - emoji.get_emoji_name = actual_get_emoji_name; }); diff --git a/frontend_tests/zjsunit/index.js b/frontend_tests/zjsunit/index.js index 1aa5193ab8..1fa70d006a 100644 --- a/frontend_tests/zjsunit/index.js +++ b/frontend_tests/zjsunit/index.js @@ -37,6 +37,7 @@ if (files.length === 0) { } // Set up our namespace helpers. +global.with_field = namespace.with_field; global.set_global = namespace.set_global; global.patch_builtin = namespace.set_global; global.zrequire = namespace.zrequire; diff --git a/frontend_tests/zjsunit/namespace.js b/frontend_tests/zjsunit/namespace.js index be767e1048..effe06e6c3 100644 --- a/frontend_tests/zjsunit/namespace.js +++ b/frontend_tests/zjsunit/namespace.js @@ -69,6 +69,13 @@ exports.stub_out_jquery = function () { $.now = function () {}; }; +exports.with_field = function (obj, field, val, f) { + const old_val = obj[field]; + obj[field] = val; + f(); + obj[field] = old_val; +}; + exports.with_overrides = function (test_function) { // This function calls test_function() and passes in // a way to override the namespace temporarily.