Skip to content

Commit

Permalink
feat(download): add download resource utils functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ysfscream committed Nov 30, 2023
1 parent efa3e26 commit caca8e2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
36 changes: 36 additions & 0 deletions packages/utils/lib/__test__/download.test.ts
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()
})
})
2 changes: 2 additions & 0 deletions packages/utils/lib/__test__/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ describe('formatSizeUnit', () => {

it('should throw an error for invalid input', () => {
expect(() => formatSizeUnit(-1)).toThrow('Invalid input: input should be a non-negative number')
// @ts-ignore
expect(() => formatSizeUnit(null)).toThrow(
'Invalid input: input should be a non-negative number',
)
// @ts-ignore
expect(() => formatSizeUnit(undefined)).toThrow(
'Invalid input: input should be a non-negative number',
)
Expand Down
17 changes: 17 additions & 0 deletions packages/utils/lib/download.ts
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)
}

0 comments on commit caca8e2

Please sign in to comment.