zjsunit: Make window a Proxy for global.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
Anders Kaseorg
2019-07-25 00:13:22 -07:00
parent 0ce4de0421
commit 99563eb150
13 changed files with 52 additions and 80 deletions

View File

@@ -1,6 +1,4 @@
global.patch_builtin('window', {
bridge: false,
});
set_global('bridge', false);
set_global('blueslip', global.make_zblueslip({
error: false, // Ignore errors. We only check for warnings in this module.
@@ -873,10 +871,7 @@ run_test('initialize', () => {
assert.equal('#compose-textarea', elem);
resize_watch_manual_resize_checked = true;
};
global.window = {
XMLHttpRequest: true,
bridge: true,
};
set_global('bridge', true);
let xmlhttprequest_checked = false;
set_global('XMLHttpRequest', function () {
this.upload = true;

View File

@@ -8,8 +8,8 @@ set_global('compose_ui', {});
const { JSDOM } = require("jsdom");
const { window } = new JSDOM('<!DOCTYPE html><p>Hello world</p>');
const { document } = window;
global.document = document;
global.$ = require('jquery')(window);
set_global('$', require('jquery')(window));
set_global('document', document);
const copy_and_paste = zrequire('copy_and_paste');

View File

@@ -3,7 +3,6 @@ const noop = function () {};
set_global('document', 'document-stub');
set_global('$', global.make_zjquery());
global.patch_builtin('window', {});
global.patch_builtin('setTimeout', func => func());
// These dependencies are closer to the dispatcher, and they
@@ -913,13 +912,13 @@ with_overrides(function (override) {
assert_same(page_params.realm_name, 'new_realm_name');
let called = false;
window.electron_bridge = {
set_global('electron_bridge', {
send_event: (key, val) => {
assert_same(key, 'realm_name');
assert_same(val, 'new_realm_name');
called = true;
},
};
});
dispatch(event);
assert_same(called, true);
@@ -954,13 +953,13 @@ with_overrides(function (override) {
override('realm_icon.rerender', noop);
called = false;
window.electron_bridge = {
set_global('electron_bridge', {
send_event: (key, val) => {
assert_same(key, 'realm_icon_url');
assert_same(val, 'icon.png');
called = true;
},
};
});
dispatch(event);
@@ -981,7 +980,7 @@ with_overrides(function (override) {
assert_same(page_params.realm_night_logo_source, 'U');
event = event_fixtures.realm__deactivated;
window.location = {};
set_global('location', {});
dispatch(event);
assert_same(window.location.href, "/accounts/deactivated/");
});

View File

@@ -3,15 +3,11 @@ zrequire('stream_data');
zrequire('people');
zrequire('util');
const _window = {
location: {
set_global('location', {
protocol: "https:",
host: "example.com",
pathname: "/",
},
};
global.patch_builtin('window', _window);
});
const hamlet = {
user_id: 1,

View File

@@ -1,12 +1,10 @@
set_global('$', global.make_zjquery());
const window_stub = $.create('window-stub');
global.patch_builtin('window', {
location: {
set_global('location', {
protocol: 'http:',
host: 'example.com',
},
to_$: () => window_stub,
});
set_global('to_$', () => window_stub);
zrequire('people');
zrequire('hash_util');
zrequire('hashchange');

View File

@@ -15,12 +15,10 @@ set_global('ui_util', {
place_caret_at_end: noop,
});
global.patch_builtin('window', {
getSelection: () => {
set_global('getSelection', () => {
return {
anchorOffset: 0,
};
},
});
let id_seq = 0;

View File

@@ -11,10 +11,8 @@ zrequire('emoji');
zrequire('message_store');
zrequire('markdown');
global.patch_builtin('window', {
location: {
set_global('location', {
origin: 'http://zulip.zulipdev.com',
},
});
set_global('page_params', {

View File

@@ -1,9 +1,7 @@
const noop = function () {};
set_global('document', {});
global.patch_builtin('window', {
addEventListener: noop,
});
set_global('addEventListener', noop);
global.stub_out_jquery();
zrequire('message_store');

View File

@@ -9,10 +9,8 @@ zrequire('stream_data');
zrequire('search_util');
set_global('page_params', {});
global.patch_builtin('window', {
location: {
set_global('location', {
hash: "#streams/1/announce",
},
});
zrequire('subs');

View File

@@ -25,8 +25,6 @@ set_global('document', {
},
});
global.patch_builtin('window', {});
set_global('csrf_token', 'whatever');
set_global('$', () => {});
@@ -130,7 +128,7 @@ document_stub.on = () => {};
document_stub.idle = () => {};
const window_stub = $.create('window-stub');
window.to_$ = () => window_stub;
set_global('to_$', () => window_stub);
window_stub.idle = () => {};
ui_init.initialize_kitchen_sink_stuff = () => {};

View File

@@ -10,9 +10,7 @@ set_global('page_params', {
max_file_upload_size: 25,
});
set_global('csrf_token', { });
global.patch_builtin('window', {
bridge: false,
});
set_global('bridge', false);
// Setting these up so that we can test that links to uploads within messages are
// automatically converted to server relative links.

View File

@@ -14,16 +14,6 @@ require("@babel/register")({
global.assert = require('assert');
global._ = require('underscore/underscore.js');
const _ = global._;
const windowObj = {
location: {
hash: '#',
},
};
global.window = _.extend({}, windowObj, {
to_$: () => {
return windowObj;
},
});
global.Dict = require('../../static/js/dict').Dict;
@@ -44,11 +34,16 @@ if (_.isEmpty(files)) {
// Set up our namespace helpers.
const namespace = require('./namespace.js');
global.set_global = namespace.set_global;
global.patch_builtin = namespace.patch_builtin;
global.patch_builtin = namespace.set_global;
global.zrequire = namespace.zrequire;
global.stub_out_jquery = namespace.stub_out_jquery;
global.with_overrides = namespace.with_overrides;
global.window = new Proxy(global, {
set: (obj, prop, value) => namespace.set_global(prop, value),
});
global.to_$ = () => window;
// Set up stub helpers.
const stub = require('./stub.js');
global.with_stub = stub.with_stub;
@@ -114,6 +109,9 @@ global.run_test = (label, f) => {
try {
files.forEach(function (file) {
set_global('location', {
hash: '#',
});
global.patch_builtin('setTimeout', noop);
global.patch_builtin('setInterval', noop);
_.throttle = immediate;

View File

@@ -1,17 +1,16 @@
const _ = require('underscore/underscore.js');
let dependencies = [];
const requires = [];
let old_builtins = {};
const new_globals = new Set();
let old_globals = {};
exports.set_global = function (name, val) {
global[name] = val;
dependencies.push(name);
return val;
};
exports.patch_builtin = function (name, val) {
old_builtins[name] = global[name];
if (!(name in old_globals)) {
if (!(name in global)) {
new_globals.add(name);
}
old_globals[name] = global[name];
}
global[name] = val;
return val;
};
@@ -31,16 +30,15 @@ exports.zrequire = function (name, fn) {
};
exports.restore = function () {
dependencies.forEach(function (name) {
delete global[name];
});
requires.forEach(function (fn) {
delete require.cache[require.resolve(fn)];
});
dependencies = [];
delete global.window.electron_bridge;
_.extend(global, old_builtins);
old_builtins = {};
_.extend(global, old_globals);
old_globals = {};
for (const name of new_globals) {
delete global[name];
}
new_globals.clear();
};
exports.stub_out_jquery = function () {