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
This commit is contained in:
Steve Howell 2020-07-27 14:06:46 +00:00 committed by Steve Howell
parent 2e864d487e
commit 329f38975e
4 changed files with 12 additions and 6 deletions

View File

@ -348,6 +348,7 @@
"document": false,
"set_global": false,
"window": false,
"with_field": false,
"zrequire": false
},
"rules": {

View File

@ -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);
assert.equal(message.content, "<p>\u{1f6b2}</p>");
});
// restore get_emoji_name for any future tests that follow
emoji.get_emoji_name = actual_get_emoji_name;
assert.equal(message.content, "<p>\u{1f6b2}</p>");
});

View File

@ -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;

View File

@ -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.