DEV: use sinon for safer stubbing (#624)

Follow up to https://github.com/discourse/discourse-ai/pull/622#discussion_r1601199538
This commit is contained in:
Régis Hanol 2024-05-15 11:54:19 +02:00 committed by GitHub
parent 1e6d9ed634
commit d64cff7692
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 12 deletions

View File

@ -1,5 +1,6 @@
import { setupTest } from "ember-qunit"; import { setupTest } from "ember-qunit";
import { module, test } from "qunit"; import { module, test } from "qunit";
import sinon from "sinon";
module( module(
"Unit | Route | discourse-ai-shared-conversation-show", "Unit | Route | discourse-ai-shared-conversation-show",
@ -18,47 +19,43 @@ module(
"route:discourse-ai-shared-conversation-show" "route:discourse-ai-shared-conversation-show"
); );
const originalOpen = window.open; const windowOpenStub = sinon.stub(window, "open");
const originalRedirect = route.redirect; const routeRedirectStub = sinon.stub(route, "redirect");
// Test when external_links_in_new_tab is true // external_links_in_new_tab = true
route.set("currentUser", { route.set("currentUser", {
user_option: { user_option: {
external_links_in_new_tab: true, external_links_in_new_tab: true,
}, },
}); });
window.open = (url, target) => { windowOpenStub.callsFake((url, target) => {
assert.equal( assert.equal(
url, url,
"https://www.discourse.org", "https://www.discourse.org",
"window.open was called with the correct URL" "window.open was called with the correct URL"
); );
assert.equal(target, "_blank", 'window.open was called with "_blank"'); assert.equal(target, "_blank", 'window.open was called with "_blank"');
}; });
route.beforeModel(transition); route.beforeModel(transition);
// Test when external_links_in_new_tab is false // external_links_in_new_tab = false
route.set("currentUser", { route.set("currentUser", {
user_option: { user_option: {
external_links_in_new_tab: false, external_links_in_new_tab: false,
}, },
}); });
route.redirect = (url) => { routeRedirectStub.callsFake((url) => {
assert.equal( assert.equal(
url, url,
"https://www.discourse.org", "https://www.discourse.org",
"redirect was called with the correct URL" "redirect was called with the correct URL"
); );
}; });
route.beforeModel(transition); route.beforeModel(transition);
// Reset
window.open = originalOpen;
route.redirect = originalRedirect;
}); });
} }
); );