diff --git a/frontend_tests/node_tests/hashchange.js b/frontend_tests/node_tests/hashchange.js index ac1d5e6302..a9bebe06b7 100644 --- a/frontend_tests/node_tests/hashchange.js +++ b/frontend_tests/node_tests/hashchange.js @@ -1,8 +1,11 @@ +set_global('$', global.make_zjquery()); +const window_stub = $.create('window-stub'); global.patch_builtin('window', { location: { protocol: 'http:', host: 'example.com', }, + to_$: () => window_stub, }); zrequire('people'); zrequire('hash_util'); @@ -177,7 +180,7 @@ run_test('hash_interactions', () => { ]); helper.clear_events(); - window.onhashchange(); + $(window).trigger($.Event('hashchange', {})); helper.assert_events([ 'overlays.close_for_hash_change', 'message_viewport.stop_auto_scrolling', @@ -189,7 +192,7 @@ run_test('hash_interactions', () => { window.location.hash = '#narrow/stream/Denmark'; helper.clear_events(); - window.onhashchange(); + $(window).trigger($.Event('hashchange', {})); helper.assert_events([ 'overlays.close_for_hash_change', 'message_viewport.stop_auto_scrolling', @@ -203,7 +206,7 @@ run_test('hash_interactions', () => { window.location.hash = '#narrow'; helper.clear_events(); - window.onhashchange(); + $(window).trigger($.Event('hashchange', {})); helper.assert_events([ 'overlays.close_for_hash_change', 'message_viewport.stop_auto_scrolling', @@ -217,7 +220,7 @@ run_test('hash_interactions', () => { window.location.hash = '#streams/whatever'; helper.clear_events(); - window.onhashchange(); + $(window).trigger($.Event('hashchange', {})); helper.assert_events([ 'overlays.close_for_hash_change', 'subs.launch', @@ -226,7 +229,7 @@ run_test('hash_interactions', () => { window.location.hash = '#keyboard-shortcuts/whatever'; helper.clear_events(); - window.onhashchange(); + $(window).trigger($.Event('hashchange', {})); helper.assert_events([ 'overlays.close_for_hash_change', 'message_viewport.stop_auto_scrolling', @@ -236,7 +239,7 @@ run_test('hash_interactions', () => { window.location.hash = '#message-formatting/whatever'; helper.clear_events(); - window.onhashchange(); + $(window).trigger($.Event('hashchange', {})); helper.assert_events([ 'overlays.close_for_hash_change', 'message_viewport.stop_auto_scrolling', @@ -246,7 +249,7 @@ run_test('hash_interactions', () => { window.location.hash = '#search-operators/whatever'; helper.clear_events(); - window.onhashchange(); + $(window).trigger($.Event('hashchange', {})); helper.assert_events([ 'overlays.close_for_hash_change', 'message_viewport.stop_auto_scrolling', @@ -256,7 +259,7 @@ run_test('hash_interactions', () => { window.location.hash = '#drafts'; helper.clear_events(); - window.onhashchange(); + $(window).trigger($.Event('hashchange', {})); helper.assert_events([ 'overlays.close_for_hash_change', 'drafts.launch', @@ -265,7 +268,7 @@ run_test('hash_interactions', () => { window.location.hash = '#settings/alert-words'; helper.clear_events(); - window.onhashchange(); + $(window).trigger($.Event('hashchange', {})); helper.assert_events([ 'overlays.close_for_hash_change', 'settings.launch', @@ -274,7 +277,7 @@ run_test('hash_interactions', () => { window.location.hash = '#organization/user-list-admin'; helper.clear_events(); - window.onhashchange(); + $(window).trigger($.Event('hashchange', {})); helper.assert_events([ 'overlays.close_for_hash_change', 'admin.launch', diff --git a/static/js/hashchange.js b/static/js/hashchange.js index 8bfa5a965a..0d30b4fc8f 100644 --- a/static/js/hashchange.js +++ b/static/js/hashchange.js @@ -295,10 +295,8 @@ exports.go_to_location = function (hash) { }; exports.initialize = function () { - // jQuery doesn't have a hashchange event, so we manually wrap - // our event handler - window.onhashchange = blueslip.wrap_function(function (e) { - hashchanged(false, e); + $(window).on('hashchange', function (e) { + hashchanged(false, e.originalEvent); }); hashchanged(true); };