From caca8e2e2fd2a81da61c55a3d5db902533dc28ca Mon Sep 17 00:00:00 2001 From: YuShifan <894402575bt@gmail.com> Date: Thu, 30 Nov 2023 23:17:53 +0800 Subject: [PATCH] feat(download): add download resource utils functions --- packages/utils/lib/__test__/download.test.ts | 36 ++++++++++++++++++++ packages/utils/lib/__test__/format.test.ts | 2 ++ packages/utils/lib/download.ts | 17 +++++++++ 3 files changed, 55 insertions(+) create mode 100644 packages/utils/lib/__test__/download.test.ts create mode 100644 packages/utils/lib/download.ts diff --git a/packages/utils/lib/__test__/download.test.ts b/packages/utils/lib/__test__/download.test.ts new file mode 100644 index 0000000..d00b784 --- /dev/null +++ b/packages/utils/lib/__test__/download.test.ts @@ -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() + }) +}) diff --git a/packages/utils/lib/__test__/format.test.ts b/packages/utils/lib/__test__/format.test.ts index 9064d22..62e9d94 100644 --- a/packages/utils/lib/__test__/format.test.ts +++ b/packages/utils/lib/__test__/format.test.ts @@ -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', ) diff --git a/packages/utils/lib/download.ts b/packages/utils/lib/download.ts new file mode 100644 index 0000000..21c4d99 --- /dev/null +++ b/packages/utils/lib/download.ts @@ -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) +}