]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Chore: add coverage for frontend download content-disposition parsing
authorshamoon <4887959+shamoon@users.noreply.github.com>
Wed, 9 Apr 2025 16:07:51 +0000 (09:07 -0700)
committershamoon <4887959+shamoon@users.noreply.github.com>
Wed, 9 Apr 2025 20:23:40 +0000 (13:23 -0700)
src-ui/src/app/components/document-detail/document-detail.component.spec.ts

index b85a7eaf4b9acdb57eb405121a6a98f9d2e5d69a..76517d33617cde4003bcd671b41b8f9b8f0c49ba 100644 (file)
@@ -1,5 +1,10 @@
 import { DatePipe } from '@angular/common'
-import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'
+import {
+  HttpHeaders,
+  HttpResponse,
+  provideHttpClient,
+  withInterceptorsFromDi,
+} from '@angular/common/http'
 import {
   HttpTestingController,
   provideHttpClientTesting,
@@ -1331,6 +1336,34 @@ describe('DocumentDetailComponent', () => {
     expect(urlRevokeSpy).toHaveBeenCalled()
   })
 
+  it('should download a file with the correct filename', () => {
+    const mockBlob = new Blob(['test content'], { type: 'text/plain' })
+    const mockResponse = new HttpResponse({
+      body: mockBlob,
+      headers: new HttpHeaders({
+        'Content-Disposition': 'attachment; filename="test-file.txt"',
+      }),
+    })
+
+    const downloadUrl = 'http://example.com/download'
+    component.documentId = 123
+    jest.spyOn(documentService, 'getDownloadUrl').mockReturnValue(downloadUrl)
+
+    const createSpy = jest.spyOn(document, 'createElement')
+    const anchor: HTMLAnchorElement = {} as HTMLAnchorElement
+    createSpy.mockReturnValueOnce(anchor)
+
+    component.download(false)
+
+    httpTestingController
+      .expectOne(downloadUrl)
+      .flush(mockBlob, { headers: mockResponse.headers })
+
+    expect(createSpy).toHaveBeenCalledWith('a')
+    expect(anchor.download).toBe('test-file.txt')
+    createSpy.mockClear()
+  })
+
   it('should get email enabled status from settings', () => {
     jest.spyOn(settingsService, 'get').mockReturnValue(true)
     expect(component.emailEnabled).toBeTruthy()