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:
parent
1e6d9ed634
commit
d64cff7692
|
@ -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;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue