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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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