style: use web APIs to set document instead of jQuery for compatibility

This commit is contained in:
rugk
2026-03-18 13:58:24 +00:00
parent 2cf32c1c75
commit 0f3831b423
+35 -39
View File
@@ -3,7 +3,7 @@ require('../common');
// DOM builder that mirrors bootstrap5.php navbar
function buildEmailDomNoShortUrl() {
$('body').html(
document.documentElement.innerHTML =
// TopNav expects initially hidden #emaillink BUTTON.
'<nav><div id="navbar"><ul>' +
'<li>' +
@@ -21,13 +21,12 @@ function buildEmailDomNoShortUrl() {
'<div id="emailconfirm-timezone-current"></div>' +
'<div id="emailconfirm-timezone-utc"></div>' +
'</div>'
);
}
// DOM builder that adds the shortener result block
function buildEmailDomWithShortUrl() {
buildEmailDomNoShortUrl();
$('body').html(
document.documentElement.innerHTML =
// TopNav expectsinitially hidden #emaillink BUTTON.
'<nav><div id="navbar"><ul>' +
'<li>' +
@@ -49,40 +48,35 @@ function buildEmailDomWithShortUrl() {
'<div id="emailconfirm-timezone-current"></div>' +
'<div id="emailconfirm-timezone-utc"></div>' +
'</div>'
);
}
function stubWinOpen($element) {
const win = $element[0].ownerDocument.defaultView;
// Some helpers in privatebin.js expect a global document.
global.document = win.document;
function makeWindowOpenMock() {
const originalOpen = window.open;
let openedUrl = null;
const origOpen = win.open;
let mockRestoreFn = null;
// Prefer simple assignment; if blocked, fall back to defineProperty.
try {
win.open = function (url) {
if (typeof jest !== 'undefined' && typeof jest.spyOn === 'function') {
const spy = jest.spyOn(window, 'open').mockImplementation((url) => {
openedUrl = url;
return {};
});
mockRestoreFn = () => spy.mockRestore();
} else {
window.open = function (url) {
openedUrl = url;
return {};
};
} catch (e) {
Object.defineProperty(win, 'open', {
value: function (url) {
openedUrl = url;
return {};
},
configurable: true,
writable: true
});
mockRestoreFn = () => { window.open = originalOpen; };
}
return {
getUrl: () => openedUrl,
restore: () => { try { win.open = origOpen; } catch (e) { /* suppress exception in restore */ } },
win
restore: () => {
if (mockRestoreFn) {
mockRestoreFn();
}
}
};
}
@@ -94,21 +88,23 @@ function extractMailtoBody(mailtoUrl) {
}
describe('Email - mail body content (short URL vs. fallback)', function () {
before(function () {
beforeEach(function () {
cleanup(); // provided by common
});
it('Uses the short URL when #pasteurl is present and never includes "undefined"', function () {
buildEmailDomWithShortUrl(); // with #pastelink/#pasteurl
buildEmailDomWithShortUrl(); // with #pastelink/#pasteurl
PrivateBin.TopNav.init();
PrivateBin.TopNav.showEmailButton(0);
const $emailBtn = $('#emaillink');
assert.ok(!$emailBtn.hasClass('hidden'), '#emaillink should be visible after showEmailButton');
const emailBtn = document.getElementById('emaillink');
assert.ok(!emailBtn.classList.contains('hidden'), '#emaillink should be visible after showEmailButton');
const { getUrl, restore } = stubWinOpen($emailBtn);
const { getUrl, restore } = makeWindowOpenMock();
try {
$emailBtn.trigger('click');
emailBtn.click();
document.getElementById('emailconfirm-timezone-current').click();
const openedUrl = getUrl();
assert.ok(openedUrl, 'window.open should have been called');
@@ -117,30 +113,30 @@ describe('Email - mail body content (short URL vs. fallback)', function () {
assert.doesNotMatch(body, /undefined/, 'email body must not contain "undefined"');
} finally {
restore();
cleanup();
}
});
it('Falls back to window.location.href when #pasteurl is absent and never includes "undefined"', function () {
buildEmailDomNoShortUrl(); // No #pasteurl
buildEmailDomNoShortUrl(); // No #pasteurl
PrivateBin.TopNav.init();
PrivateBin.TopNav.showEmailButton(0);
const $emailBtn = $('#emaillink');
assert.ok(!$emailBtn.hasClass('hidden'), '#emaillink should be visible after showEmailButton');
const emailBtn = document.getElementById('emaillink');
assert.ok(!emailBtn.classList.contains('hidden'), '#emaillink should be visible after showEmailButton');
const { getUrl, restore, win } = stubWinOpen($emailBtn);
const { getUrl, restore } = makeWindowOpenMock();
try {
$emailBtn.trigger('click');
emailBtn.click();
document.getElementById('emailconfirm-timezone-current').click();
const openedUrl = getUrl();
assert.ok(openedUrl, 'window.open should have been called');
const body = extractMailtoBody(openedUrl);
assert.match(body, new RegExp(win.location.href), 'email body should include the fallback page URL');
assert.match(body, new RegExp(window.location.href), 'email body should include the fallback page URL');
assert.doesNotMatch(body, /undefined/, 'email body must not contain "undefined"');
} finally {
restore();
cleanup();
}
});
});