From d64cff7692f47aca76533c3e50b55e20635cdc79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 15 May 2024 11:54:19 +0200 Subject: [PATCH] DEV: use sinon for safer stubbing (#624) Follow up to https://github.com/discourse/discourse-ai/pull/622#discussion_r1601199538 --- ...-ai-shared-conversation-show-route-test.js | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/test/javascripts/unit/routes/discourse-ai-shared-conversation-show-route-test.js b/test/javascripts/unit/routes/discourse-ai-shared-conversation-show-route-test.js index 462c5aad..65e89c30 100644 --- a/test/javascripts/unit/routes/discourse-ai-shared-conversation-show-route-test.js +++ b/test/javascripts/unit/routes/discourse-ai-shared-conversation-show-route-test.js @@ -1,5 +1,6 @@ import { setupTest } from "ember-qunit"; import { module, test } from "qunit"; +import sinon from "sinon"; module( "Unit | Route | discourse-ai-shared-conversation-show", @@ -18,47 +19,43 @@ module( "route:discourse-ai-shared-conversation-show" ); - const originalOpen = window.open; - const originalRedirect = route.redirect; + const windowOpenStub = sinon.stub(window, "open"); + const routeRedirectStub = sinon.stub(route, "redirect"); - // Test when external_links_in_new_tab is true + // external_links_in_new_tab = true route.set("currentUser", { user_option: { external_links_in_new_tab: true, }, }); - window.open = (url, target) => { + windowOpenStub.callsFake((url, target) => { assert.equal( url, "https://www.discourse.org", "window.open was called with the correct URL" ); assert.equal(target, "_blank", 'window.open was called with "_blank"'); - }; + }); route.beforeModel(transition); - // Test when external_links_in_new_tab is false + // external_links_in_new_tab = false route.set("currentUser", { user_option: { external_links_in_new_tab: false, }, }); - route.redirect = (url) => { + routeRedirectStub.callsFake((url) => { assert.equal( url, "https://www.discourse.org", "redirect was called with the correct URL" ); - }; + }); route.beforeModel(transition); - - // Reset - window.open = originalOpen; - route.redirect = originalRedirect; }); } );