From c8db7b7dd7aa9afcb5cea3d57e8bc31791d0b525 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Thu, 31 May 2018 14:55:28 -0700 Subject: [PATCH] node: Provide a default window object for the node tests. This is preparation for our migration of our JS pipeline to webpack, which includes as part of the process a hack of exporting globals via the window object. --- frontend_tests/node_tests/activity.js | 1 - frontend_tests/node_tests/compose.js | 2 +- frontend_tests/node_tests/dispatch.js | 1 - frontend_tests/node_tests/drafts.js | 1 - frontend_tests/node_tests/hashchange.js | 13 ++++++------- frontend_tests/node_tests/markdown.js | 2 +- frontend_tests/node_tests/notifications.js | 1 - frontend_tests/node_tests/server_events.js | 2 +- frontend_tests/node_tests/subs.js | 3 +-- frontend_tests/node_tests/upload.js | 2 +- frontend_tests/node_tests/util.js | 1 - frontend_tests/zjsunit/index.js | 13 ++++++++++++- 12 files changed, 23 insertions(+), 19 deletions(-) diff --git a/frontend_tests/node_tests/activity.js b/frontend_tests/node_tests/activity.js index 30c99a5c73..c590e8e7fd 100644 --- a/frontend_tests/node_tests/activity.js +++ b/frontend_tests/node_tests/activity.js @@ -75,7 +75,6 @@ set_global('reload', { set_global('resize', { resize_page_components: () => {}, }); -set_global('window', 'window-stub'); const me = { email: 'me@zulip.com', diff --git a/frontend_tests/node_tests/compose.js b/frontend_tests/node_tests/compose.js index 0db4594673..f4edc87e37 100644 --- a/frontend_tests/node_tests/compose.js +++ b/frontend_tests/node_tests/compose.js @@ -13,7 +13,7 @@ set_global('document', { location: {}, }); set_global('transmit', {}); -set_global('window', { +global.patch_builtin('window', { bridge: false, }); set_global('channel', {}); diff --git a/frontend_tests/node_tests/dispatch.js b/frontend_tests/node_tests/dispatch.js index e8d622e61e..b904702e6c 100644 --- a/frontend_tests/node_tests/dispatch.js +++ b/frontend_tests/node_tests/dispatch.js @@ -1,7 +1,6 @@ var noop = function () {}; set_global('document', 'document-stub'); -set_global('window', {}); set_global('$', function () { return { trigger: noop, diff --git a/frontend_tests/node_tests/drafts.js b/frontend_tests/node_tests/drafts.js index adeea69278..69409df588 100644 --- a/frontend_tests/node_tests/drafts.js +++ b/frontend_tests/node_tests/drafts.js @@ -1,6 +1,5 @@ set_global('$', global.make_zjquery()); set_global('i18n', global.stub_i18n); -set_global('window', {}); zrequire('localstorage'); zrequire('drafts'); diff --git a/frontend_tests/node_tests/hashchange.js b/frontend_tests/node_tests/hashchange.js index 7c1c8db9ab..88c311b28d 100644 --- a/frontend_tests/node_tests/hashchange.js +++ b/frontend_tests/node_tests/hashchange.js @@ -1,3 +1,9 @@ +global.patch_builtin('window', { + location: { + protocol: 'http:', + host: 'example.com', + }, +}); zrequire('people'); zrequire('hash_util'); zrequire('hashchange'); @@ -5,12 +11,6 @@ zrequire('stream_data'); set_global('document', 'document-stub'); set_global('history', {}); -set_global('window', { - location: { - protocol: 'http:', - host: 'example.com', - }, -}); set_global('admin', {}); set_global('drafts', {}); @@ -346,4 +346,3 @@ run_test('save_narrow', () => { ]); assert.equal(url_pushed, 'http://example.com/#narrow/is/starred'); }); - diff --git a/frontend_tests/node_tests/markdown.js b/frontend_tests/node_tests/markdown.js index 36b44ec150..08c4e8b5a0 100644 --- a/frontend_tests/node_tests/markdown.js +++ b/frontend_tests/node_tests/markdown.js @@ -12,7 +12,7 @@ zrequire('emoji'); zrequire('message_store'); zrequire('markdown'); -set_global('window', { +global.patch_builtin('window', { location: { origin: 'http://zulip.zulipdev.com', }, diff --git a/frontend_tests/node_tests/notifications.js b/frontend_tests/node_tests/notifications.js index 6dd7efb4a3..064d4f1dd7 100644 --- a/frontend_tests/node_tests/notifications.js +++ b/frontend_tests/node_tests/notifications.js @@ -8,7 +8,6 @@ set_global('document', { return true; }, }); -set_global('window', {}); set_global('page_params', { is_admin: false, realm_users: [], diff --git a/frontend_tests/node_tests/server_events.js b/frontend_tests/node_tests/server_events.js index c0a8811869..3ac4c10c95 100644 --- a/frontend_tests/node_tests/server_events.js +++ b/frontend_tests/node_tests/server_events.js @@ -1,7 +1,7 @@ var noop = function () {}; set_global('document', {}); -set_global('window', { +global.patch_builtin('window', { addEventListener: noop, }); global.stub_out_jquery(); diff --git a/frontend_tests/node_tests/subs.js b/frontend_tests/node_tests/subs.js index 9a15b3dd0e..3df00ce056 100644 --- a/frontend_tests/node_tests/subs.js +++ b/frontend_tests/node_tests/subs.js @@ -2,7 +2,7 @@ global.stub_out_jquery(); set_global('ui', {}); set_global('stream_data', {}); -set_global('window', { +global.patch_builtin('window', { location: { hash: "#streams/1/announce", }, @@ -212,4 +212,3 @@ run_test('filter_table', () => { subs.filter_table({input: "", subscribed_only: false}); assert($(".stream-row[data-stream-id='1']").hasClass("active")); }); - diff --git a/frontend_tests/node_tests/upload.js b/frontend_tests/node_tests/upload.js index 28d741e996..62da4b7940 100644 --- a/frontend_tests/node_tests/upload.js +++ b/frontend_tests/node_tests/upload.js @@ -8,7 +8,7 @@ set_global('navigator', { set_global('i18n', global.stub_i18n); set_global('page_params', { }); set_global('csrf_token', { }); -set_global('window', { +global.patch_builtin('window', { bridge: false, }); diff --git a/frontend_tests/node_tests/util.js b/frontend_tests/node_tests/util.js index c8fb2daf08..7ca03807ad 100644 --- a/frontend_tests/node_tests/util.js +++ b/frontend_tests/node_tests/util.js @@ -1,6 +1,5 @@ set_global('$', global.make_zjquery()); set_global('document', {}); -set_global('window', {}); zrequire('util'); diff --git a/frontend_tests/zjsunit/index.js b/frontend_tests/zjsunit/index.js index afd43f8523..73a85870af 100644 --- a/frontend_tests/zjsunit/index.js +++ b/frontend_tests/zjsunit/index.js @@ -4,9 +4,20 @@ var fs = require('fs'); global.assert = require('assert'); require('node_modules/string.prototype.codepointat/codepointat.js'); -global.Dict = require('js/dict'); global._ = require('node_modules/underscore/underscore.js'); var _ = global._; +const windowObj = { + location: { + hash: '#', + }, +}; +global.window = _.extend({}, windowObj, { + to_$: () => { + return windowObj; + }, +}); + +global.Dict = require('js/dict'); // Create a helper function to avoid sneaky delays in tests. function immediate(f) {