Skip to content

Commit

Permalink
Prevent download tests from actually downloading a file in the test r…
Browse files Browse the repository at this point in the history
…unner
  • Loading branch information
davepagurek committed Nov 3, 2024
1 parent 9b61464 commit 8558837
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions test/js/p5_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ export function testWithDownload(name, fn, asyncFn = false) {
return new Promise((resolve, reject) => {
let blobContainer = {};

const prevClick = HTMLAnchorElement.prototype.click;
const prevDispatchEvent = HTMLAnchorElement.prototype.dispatchEvent;
const blockDownloads = () => {
HTMLAnchorElement.prototype.click = () => {};
HTMLAnchorElement.prototype.dispatchEvent = () => {};
}
const unblockDownloads = () => {
HTMLAnchorElement.prototype.click = prevClick;
HTMLAnchorElement.prototype.dispatchEvent = prevDispatchEvent;
}

// create a backup of createObjectURL
let couBackup = window.URL.createObjectURL;

Expand All @@ -40,6 +51,7 @@ export function testWithDownload(name, fn, asyncFn = false) {
blobContainer.blob = blob;
return couBackup(blob);
};
blockDownloads();

let error;
if (asyncFn) {
Expand All @@ -54,6 +66,7 @@ export function testWithDownload(name, fn, asyncFn = false) {
// restore createObjectURL to the original one
window.URL.createObjectURL = couBackup;
error ? reject(error) : resolve();
unblockDownloads();
});
} else {
try {
Expand All @@ -64,6 +77,7 @@ export function testWithDownload(name, fn, asyncFn = false) {
// restore createObjectURL to the original one
window.URL.createObjectURL = couBackup;
error ? reject(error) : resolve();
unblockDownloads();
}
});
};
Expand Down

0 comments on commit 8558837

Please sign in to comment.