fix(editor): PDF export download keeps its URL alive (WebKit may cancel)
This commit is contained in:
1 parent
ae17097afb
commit
2b8000e5ab
3 files changed
+32
-15
No files matched your search
@@ -31,6 +31,7 @@ const PDF_BYTES = new Uint8Array([37, 80, 68, 70])
|
||||
|
||||
let sheet: SheetIndex
|
||||
const downloads: string[] = []
|
||||
const clicked: { connectedAtClick: boolean }[] = []
|
||||
const realCreateObjectURL = URL.createObjectURL
|
||||
const realRevokeObjectURL = URL.revokeObjectURL
|
||||
|
||||
@@ -48,17 +49,20 @@ beforeEach(() => {
|
||||
.hydrateImport({ report: minimalReport(sheet), archive: null }, sheet)
|
||||
exportMock.mockReset()
|
||||
downloads.length = 0
|
||||
clicked.length = 0
|
||||
URL.createObjectURL = vi.fn(() => 'blob:faunosteo-test')
|
||||
URL.revokeObjectURL = vi.fn()
|
||||
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(function (
|
||||
this: HTMLAnchorElement,
|
||||
) {
|
||||
downloads.push(this.download)
|
||||
clicked.push({ connectedAtClick: this.isConnected })
|
||||
})
|
||||
vi.stubGlobal('alert', vi.fn())
|
||||
})
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
vi.useRealTimers()
|
||||
vi.restoreAllMocks()
|
||||
vi.unstubAllGlobals()
|
||||
URL.createObjectURL = realCreateObjectURL
|
||||
@@ -94,7 +98,8 @@ describe('EditorRoute export', () => {
|
||||
expect(exportMock.mock.calls[0]![2]).toEqual({ locale: 'en' })
|
||||
})
|
||||
|
||||
it('downloads bilan-<animal>-<date>.pdf through a blob URL it then revokes', async () => {
|
||||
it('downloads bilan-<animal>-<date>.pdf through a blob URL it revokes after a delay', async () => {
|
||||
vi.useFakeTimers({ shouldAdvanceTime: true })
|
||||
exportMock.mockResolvedValue({ pdf: PDF_BYTES, warnings: [] })
|
||||
await mountAndExport(fr['editor.export']!)
|
||||
await waitFor(() => expect(downloads).toHaveLength(1))
|
||||
@@ -102,6 +107,10 @@ describe('EditorRoute export', () => {
|
||||
expect(URL.createObjectURL).toHaveBeenCalledTimes(1)
|
||||
const blob = vi.mocked(URL.createObjectURL).mock.calls[0]![0] as Blob
|
||||
expect(blob.type).toBe('application/pdf')
|
||||
expect(clicked[0]!.connectedAtClick).toBe(true)
|
||||
expect(document.body.querySelector('a[download]')).toBeNull()
|
||||
expect(URL.revokeObjectURL).not.toHaveBeenCalled()
|
||||
vi.advanceTimersByTime(30_000)
|
||||
expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:faunosteo-test')
|
||||
expect(window.alert).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user