I have a function that needs to be tested for code coverage. The function, called downloadPdfDataContent, creates a link element and appends it to the document body in order to trigger a file download. After downloading, the link is removed from the DOM using URL.revokeObjectURL.
const downloadPdfDataContent = (title: string, url: string): void => {
const link = document.createElement('a');
link.target = title;
link.href = url;
link.download = title;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
};
How can I test the assignment of variables and interactions with the document object within this function scope? It seems like using spyOn could be a solution, but I'm not sure how to go about it given the locally scoped variable. One thought was to return the variable, but if possible, I'd like to avoid that approach.