-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(download): add download resource utils functions
- Loading branch information
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { describe, it, expect, vi } from 'vitest' | ||
import { createDownloadBlobLink } from '../download' | ||
|
||
describe('createDownloadBlobLink', () => { | ||
it('should create a download link and trigger a click', () => { | ||
// Setup | ||
const fakeURL = 'http://fakeurl.com/blob' | ||
const blobData = new Blob(['test data'], { type: 'text/plain' }) | ||
const filename = 'test.txt' | ||
|
||
// Ensure window.URL and createObjectURL exist | ||
if (!window.URL) { | ||
window.URL = new URL('http://example.com') as typeof window.URL | ||
} | ||
window.URL.createObjectURL = vi.fn(() => fakeURL) | ||
|
||
// Mocking the necessary APIs | ||
const createElementSpy = vi.spyOn(document, 'createElement') | ||
const appendChildSpy = vi.spyOn(document.body, 'appendChild') | ||
const removeChildSpy = vi.spyOn(document.body, 'removeChild') | ||
|
||
// Execute | ||
createDownloadBlobLink(blobData, filename) | ||
|
||
// Assertions | ||
expect(window.URL.createObjectURL).toHaveBeenCalledWith(new Blob([blobData])) | ||
expect(createElementSpy).toHaveBeenCalledWith('a') | ||
expect(appendChildSpy).toHaveBeenCalled() | ||
expect(removeChildSpy).toHaveBeenCalled() | ||
|
||
// Clean up | ||
createElementSpy.mockRestore() | ||
appendChildSpy.mockRestore() | ||
removeChildSpy.mockRestore() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Creates a download link for a given Blob data with the specified filename. | ||
* The link is automatically clicked to initiate the download, and then removed from the document body. | ||
* | ||
* @param blobData - The Blob data to be downloaded. | ||
* @param filename - The name of the file to be downloaded. | ||
*/ | ||
export const createDownloadBlobLink = (blobData: Blob, filename: string) => { | ||
const url = window.URL.createObjectURL(new Blob([blobData])) | ||
const link = document.createElement('a') | ||
link.style.display = 'none' | ||
link.href = url | ||
link.download = filename | ||
document.body.appendChild(link) | ||
link.click() | ||
document.body.removeChild(link) | ||
} |