Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
node_tests: Mock window.location with JSDOM.
Anders Kaseorgafa47b9ae318
diff --git a/frontend_tests/node_tests/billing.js b/frontend_tests/node_tests/billing.jsindex cd47c8502e4..deddc32c700 100644--- a/frontend_tests/node_tests/billing.js+++ b/frontend_tests/node_tests/billing.js@@ -12,6 +12,7 @@ const $ = require("../zjsunit/zjquery");const template = fs.readFileSync("templates/corporate/billing.html", "utf-8");const dom = new JSDOM(template, {pretendToBeVisual: true});const document = dom.window.document;+const location = set_global("location", {});const StripeCheckout = set_global("StripeCheckout", {configure: () => {},@@ -46,7 +47,7 @@ run_test("initialize", ({override}) => {assert.equal(stripe_token, "stripe_token");assert.deepEqual(ignored_inputs, []);assert.equal(method, "POST");- window.location.replace = (new_location) => {+ location.replace = (new_location) => {assert.equal(new_location, "/billing");};success_callback();@@ -111,7 +112,7 @@ run_test("initialize", ({override}) => {assert.equal(stripe_token, undefined);assert.deepEqual(ignored_inputs, []);assert.equal(method, "PATCH");- window.location.replace = (new_location) => {+ location.replace = (new_location) => {assert.equal(new_location, "/billing");};success_callback();@@ -139,7 +140,7 @@ run_test("initialize", ({override}) => {assert.equal(stripe_token, undefined);assert.deepEqual(ignored_inputs, ["licenses_at_next_renewal"]);assert.equal(method, "PATCH");- window.location.replace = (new_location) => {+ location.replace = (new_location) => {assert.equal(new_location, "/billing");};success_callback();@@ -212,7 +213,7 @@ run_test("initialize", ({override}) => {assert.equal(stripe_token, undefined);assert.deepEqual(ignored_inputs, ["licenses"]);assert.equal(method, "PATCH");- window.location.replace = (new_location) => {+ location.replace = (new_location) => {assert.equal(new_location, "/billing");};success_callback();diff --git a/frontend_tests/node_tests/billing_helpers.js b/frontend_tests/node_tests/billing_helpers.jsindex f1269963e63..fb1f22a9570 100644--- a/frontend_tests/node_tests/billing_helpers.js+++ b/frontend_tests/node_tests/billing_helpers.js@@ -12,7 +12,10 @@ const $ = require("../zjsunit/zjquery");const {page_params} = require("../zjsunit/zpage_params");const template = fs.readFileSync("templates/corporate/upgrade.html", "utf-8");-const dom = new JSDOM(template, {pretendToBeVisual: true});+const dom = new JSDOM(template, {+ pretendToBeVisual: true,+ url: "http://zulip.zulipdev.com/upgrade/#billing",+});const jquery = jQueryFactory(dom.window);const history = set_global("history", {});@@ -20,11 +23,7 @@ const loading = mock_esm("../../static/js/loading");set_global("document", {title: "Zulip",});-const location = set_global("location", {- pathname: "/upgrade/",- search: "",- hash: "#billing",-});+const location = set_global("location", dom.window.location);const helpers = zrequire("billing/helpers");diff --git a/frontend_tests/node_tests/browser_history.js b/frontend_tests/node_tests/browser_history.jsindex 68af4cee431..537c104b41d 100644--- a/frontend_tests/node_tests/browser_history.js+++ b/frontend_tests/node_tests/browser_history.js@@ -2,20 +2,18 @@const {strict: assert} = require("assert");-const {set_global, zrequire} = require("../zjsunit/namespace");+const {zrequire} = require("../zjsunit/namespace");const {make_stub} = require("../zjsunit/stub");const {run_test} = require("../zjsunit/test");const blueslip = require("../zjsunit/zblueslip");-const browser_history = zrequire("browser_history");+window.location.hash = "#bogus";-const location = set_global("location", {- hash: "bogus",-});+const browser_history = zrequire("browser_history");function test(label, f) {run_test(label, ({override}) => {- location.hash = "bogus";+ window.location.hash = "#bogus";browser_history.clear_for_testing();f({override});});@@ -25,10 +23,10 @@ test("basics", () => {const hash1 = "#settings/profile";const hash2 = "#narrow/is/private";browser_history.go_to_location(hash1);- assert.equal(location.hash, hash1);+ assert.equal(window.location.hash, hash1);browser_history.update(hash2);- assert.equal(location.hash, hash2);+ assert.equal(window.location.hash, hash2);assert.equal(browser_history.old_hash(), hash1);const was_internal_change = browser_history.save_old_hash();@@ -40,9 +38,9 @@ test("update with same hash", () => {const hash = "#keyboard-shortcuts";browser_history.update(hash);- assert.equal(location.hash, hash);+ assert.equal(window.location.hash, hash);browser_history.update(hash);- assert.equal(location.hash, hash);+ assert.equal(window.location.hash, hash);});test("error for bad hashes", () => {@@ -58,7 +56,7 @@ test("update internal hash if required", ({override}) => {browser_history.update_hash_internally_if_required(hash);assert.equal(stub.num_calls, 1);- location.hash = "#test/hash";+ window.location.hash = "#test/hash";// update with same hashbrowser_history.update_hash_internally_if_required(hash);// but no update was made since the@@ -69,12 +67,11 @@ test("update internal hash if required", ({override}) => {});test("web public view hash restore", () => {- const allowed_hash = "#";- browser_history.update(allowed_hash);- assert.equal(location.hash, allowed_hash);+ browser_history.update("#");+ assert.equal(window.location.hash, "");const new_hash = "#narrow/is/private";browser_history.update(new_hash);- assert.equal(location.hash, new_hash);+ assert.equal(window.location.hash, new_hash);browser_history.return_to_web_public_hash();- assert.equal(location.hash, allowed_hash);+ assert.equal(window.location.hash, "");});diff --git a/frontend_tests/node_tests/compose.js b/frontend_tests/node_tests/compose.jsindex 79184701c97..bed192f78fa 100644--- a/frontend_tests/node_tests/compose.js+++ b/frontend_tests/node_tests/compose.js@@ -16,13 +16,6 @@ const noop = () => {};set_global("document", {});set_global("navigator", {});-// Setting these up so that we can test that links to uploads within messages are-// automatically converted to server relative links.-document.location = {- protocol: "https:",- host: "foo.com",-};-const fake_now = 555;const channel = mock_esm("../../static/js/channel");diff --git a/frontend_tests/node_tests/compose_actions.js b/frontend_tests/node_tests/compose_actions.jsindex e571f328efb..2ba4982e4e6 100644--- a/frontend_tests/node_tests/compose_actions.js+++ b/frontend_tests/node_tests/compose_actions.js@@ -9,7 +9,6 @@ const $ = require("../zjsunit/zjquery");const noop = () => {};set_global("document", {- location: {}, // we need this to load compose.jsto_$: () => $("document-stub"),});diff --git a/frontend_tests/node_tests/compose_validate.js b/frontend_tests/node_tests/compose_validate.jsindex 0bd91de7a21..dc75c8aa325 100644--- a/frontend_tests/node_tests/compose_validate.js+++ b/frontend_tests/node_tests/compose_validate.js@@ -3,14 +3,12 @@const {strict: assert} = require("assert");const {$t_html} = require("../zjsunit/i18n");-const {mock_esm, set_global, zrequire} = require("../zjsunit/namespace");+const {mock_esm, zrequire} = require("../zjsunit/namespace");const {run_test} = require("../zjsunit/test");const blueslip = require("../zjsunit/zblueslip");const $ = require("../zjsunit/zjquery");const {page_params} = require("../zjsunit/zpage_params");-set_global("document", {location: {}});-const channel = mock_esm("../../static/js/channel");const compose_actions = mock_esm("../../static/js/compose_actions");const compose_state = zrequire("compose_state");diff --git a/frontend_tests/node_tests/compose_video.js b/frontend_tests/node_tests/compose_video.jsindex 75633c292c4..55a58727db4 100644--- a/frontend_tests/node_tests/compose_video.js+++ b/frontend_tests/node_tests/compose_video.js@@ -2,7 +2,7 @@const {strict: assert} = require("assert");-const {mock_esm, set_global, zrequire} = require("../zjsunit/namespace");+const {mock_esm, zrequire} = require("../zjsunit/namespace");const {run_test} = require("../zjsunit/test");const $ = require("../zjsunit/zjquery");const {page_params} = require("../zjsunit/zpage_params");@@ -17,13 +17,6 @@ mock_esm("../../static/js/giphy", {mock_esm("../../static/js/resize", {watch_manual_resize() {},});-set_global("document", {- execCommand() {- return false;- },- location: {},- to_$: () => $("document-stub"),-});const server_events_dispatch = zrequire("server_events_dispatch");const compose_ui = zrequire("compose_ui");diff --git a/frontend_tests/node_tests/hash_util.js b/frontend_tests/node_tests/hash_util.jsindex 0b2e9bcde0d..8d3f36e1638 100644--- a/frontend_tests/node_tests/hash_util.js+++ b/frontend_tests/node_tests/hash_util.js@@ -2,7 +2,7 @@const {strict: assert} = require("assert");-const {mock_esm, set_global, zrequire} = require("../zjsunit/namespace");+const {mock_esm, zrequire} = require("../zjsunit/namespace");const {run_test} = require("../zjsunit/test");const ui_report = mock_esm("../../static/js/ui_report", {@@ -12,11 +12,6 @@ const ui_report = mock_esm("../../static/js/ui_report", {ui_report.displayed_error = true;},});-const location = set_global("location", {- protocol: "https:",- host: "example.com",- pathname: "/",-});const hash_util = zrequire("hash_util");const stream_data = zrequire("stream_data");@@ -86,7 +81,7 @@ run_test("test_get_hash_category", () => {assert.deepEqual(hash_util.get_hash_category("#drafts"), "drafts");assert.deepEqual(hash_util.get_hash_category("invites"), "invites");- location.hash = "#settings/profile";+ window.location.hash = "#settings/profile";assert.deepEqual(hash_util.get_current_hash_category(), "settings");});@@ -99,38 +94,38 @@ run_test("test_get_hash_section", () => {assert.equal(hash_util.get_hash_section("#drafts"), "");assert.equal(hash_util.get_hash_section(""), "");- location.hash = "#settings/profile";+ window.location.hash = "#settings/profile";assert.deepEqual(hash_util.get_current_hash_section(), "profile");});run_test("build_reload_url", () => {- location.hash = "#settings/profile";+ window.location.hash = "#settings/profile";assert.equal(hash_util.build_reload_url(), "+oldhash=settings%2Fprofile");- location.hash = "#test";+ window.location.hash = "#test";assert.equal(hash_util.build_reload_url(), "+oldhash=test");- location.hash = "#";+ window.location.hash = "#";assert.equal(hash_util.build_reload_url(), "+oldhash=");- location.hash = "";+ window.location.hash = "";assert.equal(hash_util.build_reload_url(), "+oldhash=");});run_test("test_active_stream", () => {- location.hash = "#streams/1/announce";+ window.location.hash = "#streams/1/announce";assert.equal(hash_util.active_stream().id, 1);assert.equal(hash_util.active_stream().name, "announce");- location.hash = "#test/narrow";+ window.location.hash = "#test/narrow";assert.equal(hash_util.active_stream(), undefined);});run_test("test_is_create_new_stream_narrow", () => {- location.hash = "#streams/new";+ window.location.hash = "#streams/new";assert.equal(hash_util.is_create_new_stream_narrow(), true);- location.hash = "#some/random/hash";+ window.location.hash = "#some/random/hash";assert.equal(hash_util.is_create_new_stream_narrow(), false);});@@ -170,7 +165,7 @@ run_test("test_by_conversation_and_time_uri", () => {assert.equal(hash_util.by_conversation_and_time_uri(message),- "https://example.com/#narrow/stream/99-frontend/topic/testing/near/42",+ "http://zulip.zulipdev.com/#narrow/stream/99-frontend/topic/testing/near/42",);message = {@@ -185,7 +180,7 @@ run_test("test_by_conversation_and_time_uri", () => {assert.equal(hash_util.by_conversation_and_time_uri(message),- "https://example.com/#narrow/pm-with/15-pm/near/43",+ "http://zulip.zulipdev.com/#narrow/pm-with/15-pm/near/43",);});diff --git a/frontend_tests/node_tests/hashchange.js b/frontend_tests/node_tests/hashchange.jsindex b648e0fe232..18a44ab248d 100644--- a/frontend_tests/node_tests/hashchange.js+++ b/frontend_tests/node_tests/hashchange.js@@ -9,10 +9,6 @@ const $ = require("../zjsunit/zjquery");const {user_settings} = require("../zjsunit/zpage_params");let window_stub;-set_global("location", {- protocol: "http:",- host: "example.com",-});set_global("to_$", () => window_stub);mock_esm("../../static/js/search", {@@ -330,5 +326,5 @@ run_test("save_narrow", ({override}) => {helper.clear_events();hashchange.save_narrow(operators);helper.assert_events([[message_viewport, "stop_auto_scrolling"]]);- assert.equal(url_pushed, "http://example.com/#narrow/is/starred");+ assert.equal(url_pushed, "http://zulip.zulipdev.com/#narrow/is/starred");});diff --git a/frontend_tests/node_tests/markdown.js b/frontend_tests/node_tests/markdown.jsindex b1eebcb4f3b..d95f1de7397 100644--- a/frontend_tests/node_tests/markdown.js+++ b/frontend_tests/node_tests/markdown.js@@ -9,10 +9,6 @@ const {run_test} = require("../zjsunit/test");const blueslip = require("../zjsunit/zblueslip");const {page_params, user_settings} = require("../zjsunit/zpage_params");-set_global("location", {- origin: "http://zulip.zulipdev.com",-});-const example_realm_linkifiers = [{pattern: "#(?P<id>[0-9]{2,8})",diff --git a/frontend_tests/node_tests/popovers.js b/frontend_tests/node_tests/popovers.jsindex d7a8fa5b188..b8df9ec38b3 100644--- a/frontend_tests/node_tests/popovers.js+++ b/frontend_tests/node_tests/popovers.js@@ -202,9 +202,6 @@ test_ui("sender_hover", ({override, mock_template}) => {$.create(".user_popover_email", {children: []});const image_stubber = make_image_stubber();- window.location = {- href: "http://chat.zulip.org/",- };const base_url = window.location.href;handler.call(target, e);@@ -222,12 +219,6 @@ test_ui("actions_popover", ({override, mock_template}) => {const handler = $("#main_div").get_on_handler("click", ".actions_hover");- window.location = {- protocol: "http:",- host: "chat.zulip.org",- pathname: "/",- };-const message = {id: 999,topic: "Actions (1)",@@ -265,7 +256,7 @@ test_ui("actions_popover", ({override, mock_template}) => {// TODO: Test all the properties of the popoverassert.equal(opts.conversation_time_uri,- "http://chat.zulip.org/#narrow/stream/Bracket.20.28.20stream/topic/Actions.20.281.29/near/999",+ "http://zulip.zulipdev.com/#narrow/stream/Bracket.20.28.20stream/topic/Actions.20.281.29/near/999",);return "actions-content";});diff --git a/frontend_tests/node_tests/upgrade.js b/frontend_tests/node_tests/upgrade.jsindex b94bb36c9ad..c445246fbf6 100644--- a/frontend_tests/node_tests/upgrade.js+++ b/frontend_tests/node_tests/upgrade.js@@ -14,6 +14,7 @@ const noop = () => {};const template = fs.readFileSync("templates/corporate/upgrade.html", "utf-8");const dom = new JSDOM(template, {pretendToBeVisual: true});const document = dom.window.document;+const location = set_global("location", {});const StripeCheckout = set_global("StripeCheckout", {configure: noop,@@ -44,7 +45,7 @@ run_test("initialize", ({override}) => {assert.equal(stripe_token, "stripe_add_card_token");assert.deepEqual(ignored_inputs, []);assert.equal(type, "POST");- window.location.replace = (new_location) => {+ location.replace = (new_location) => {assert.equal(new_location, "/billing");};success_callback();@@ -54,7 +55,7 @@ run_test("initialize", ({override}) => {assert.equal(stripe_token, undefined);assert.deepEqual(ignored_inputs, []);assert.equal(type, "POST");- window.location.replace = (new_location) => {+ location.replace = (new_location) => {assert.equal(new_location, "/billing");};success_callback();@@ -64,7 +65,7 @@ run_test("initialize", ({override}) => {assert.equal(stripe_token, undefined);assert.deepEqual(ignored_inputs, []);assert.equal(type, "POST");- window.location.replace = (new_location) => {+ location.replace = (new_location) => {assert.equal(new_location, "/");};success_callback();diff --git a/frontend_tests/node_tests/upload.js b/frontend_tests/node_tests/upload.jsindex 8dd3eb0ce06..3d6e41a5f36 100644--- a/frontend_tests/node_tests/upload.js+++ b/frontend_tests/node_tests/upload.js@@ -7,18 +7,10 @@ const {run_test} = require("../zjsunit/test");const $ = require("../zjsunit/zjquery");const {page_params} = require("../zjsunit/zpage_params");-set_global("document", {- location: {},-});set_global("navigator", {userAgent: "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",});-// Setting these up so that we can test that links to uploads within messages are-// automatically converted to server relative links.-document.location.protocol = "https:";-document.location.host = "foo.com";-let uppy_stub;function Uppy(options) {return uppy_stub.call(this, options);diff --git a/frontend_tests/node_tests/util.js b/frontend_tests/node_tests/util.jsindex cbb55440e1a..35321e9ad90 100644--- a/frontend_tests/node_tests/util.js+++ b/frontend_tests/node_tests/util.js@@ -280,8 +280,6 @@ run_test("move_array_elements_to_front", () => {});run_test("clean_user_content_links", () => {- window.location.href = "http://zulip.zulipdev.com/";- window.location.origin = "http://zulip.zulipdev.com";assert.equal(util.clean_user_content_links('<a href="http://example.com">good</a> ' +diff --git a/frontend_tests/zjsunit/index.js b/frontend_tests/zjsunit/index.jsindex 6346459aa9c..46f81cc86aa 100644--- a/frontend_tests/zjsunit/index.js+++ b/frontend_tests/zjsunit/index.js@@ -15,7 +15,8 @@ const blueslip = require("./zblueslip");const zjquery = require("./zjquery");const zpage_params = require("./zpage_params");-global.DOMParser = new JSDOM().window.DOMParser;+const dom = new JSDOM("", {url: "http://zulip.zulipdev.com/"});+global.DOMParser = dom.window.DOMParser;require("@babel/register")({extensions: [".es6", ".es", ".jsx", ".js", ".mjs", ".ts"],@@ -85,9 +86,8 @@ try {namespace.start();namespace.set_global("window", window);namespace.set_global("to_$", () => window);- namespace.set_global("location", {- hash: "#",- });+ namespace.set_global("location", dom.window.location);+ window.location.href = "http://zulip.zulipdev.com/#";namespace.set_global("setTimeout", noop);namespace.set_global("setInterval", noop);_.throttle = immediate;