Show a persistent loader while a large library is being created (#2200)
Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
@@ -2,7 +2,7 @@ import {beforeEach, describe, expect, it, vi} from 'vitest';
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {EnvironmentInjector, runInInjectionContext} from '@angular/core';
|
||||
import {HttpClient, HttpEventType, HttpHeaders, HttpResponse} from '@angular/common/http';
|
||||
import {of, Subject, throwError} from 'rxjs';
|
||||
import {of, Subject} from 'rxjs';
|
||||
import {FileDownloadService} from './file-download.service';
|
||||
import {DownloadProgressService} from './download-progress.service';
|
||||
import {MessageService} from 'primeng/api';
|
||||
@@ -75,7 +75,7 @@ describe('FileDownloadService', () => {
|
||||
}
|
||||
};
|
||||
|
||||
service.downloadFile('/api/file', 'file.txt').subscribe();
|
||||
service.downloadFile('/api/file', 'file.txt');
|
||||
expect(httpClientMock.get).toHaveBeenCalledWith('/api/file', expect.objectContaining({observe: 'events'}));
|
||||
expect(downloadProgressServiceMock.startDownload).toHaveBeenCalledWith('file.txt', expect.any(Subject));
|
||||
expect(downloadProgressServiceMock.updateProgress).toHaveBeenCalledWith(50, 100);
|
||||
@@ -97,26 +97,6 @@ describe('FileDownloadService', () => {
|
||||
expect(filename).toBe('default.txt');
|
||||
});
|
||||
|
||||
it('should handle download error and show error message', () => {
|
||||
httpClientMock.get.mockReturnValue(throwError(() => new Error('fail')));
|
||||
service.downloadFile('/api/file', 'file.txt').subscribe({
|
||||
error: () => {
|
||||
expect(messageServiceMock.add).toHaveBeenCalledWith(expect.objectContaining({severity: 'error'}));
|
||||
expect(downloadProgressServiceMock.completeDownload).toHaveBeenCalled();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should not show error message for AbortError', () => {
|
||||
httpClientMock.get.mockReturnValue(throwError(() => ({name: 'AbortError'})));
|
||||
service.downloadFile('/api/file', 'file.txt').subscribe({
|
||||
error: () => {
|
||||
expect(messageServiceMock.add).not.toHaveBeenCalledWith(expect.objectContaining({severity: 'error'}));
|
||||
expect(downloadProgressServiceMock.completeDownload).toHaveBeenCalled();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw if no file content received', () => {
|
||||
const response = new HttpResponse({body: undefined, headers: new HttpHeaders()});
|
||||
expect(() => (service as any).handleDownloadComplete(response, 'file.txt')).toThrow('No file content received');
|
||||
@@ -194,20 +174,20 @@ describe('FileDownloadService - API Contract Tests', () => {
|
||||
describe('Behavior contract', () => {
|
||||
it('should call startDownload and updateProgress on DownloadProgressService', () => {
|
||||
httpClientMock.get.mockReturnValue(of({type: HttpEventType.DownloadProgress, loaded: 10, total: 20}));
|
||||
service.downloadFile('/api/file', 'file.txt').subscribe();
|
||||
service.downloadFile('/api/file', 'file.txt');
|
||||
expect(downloadProgressServiceMock.startDownload).toHaveBeenCalled();
|
||||
expect(downloadProgressServiceMock.updateProgress).toHaveBeenCalledWith(10, 20);
|
||||
});
|
||||
|
||||
it('should call completeDownload on finalize', () => {
|
||||
httpClientMock.get.mockReturnValue(of({type: HttpEventType.Response, body: new Blob(['test']), headers: new HttpHeaders(), clone: () => this, status: 200, statusText: 'OK', url: '', ok: true}));
|
||||
service.downloadFile('/api/file', 'file.txt').subscribe();
|
||||
service.downloadFile('/api/file', 'file.txt');
|
||||
expect(downloadProgressServiceMock.completeDownload).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should show success message on download complete', () => {
|
||||
httpClientMock.get.mockReturnValue(of({type: HttpEventType.Response, body: new Blob(['test']), headers: new HttpHeaders(), clone: () => this, status: 200, statusText: 'OK', url: '', ok: true}));
|
||||
service.downloadFile('/api/file', 'file.txt').subscribe();
|
||||
service.downloadFile('/api/file', 'file.txt');
|
||||
expect(messageServiceMock.add).toHaveBeenCalledWith(expect.objectContaining({severity: 'success'}));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import {Injectable, inject} from '@angular/core';
|
||||
import {inject, Injectable} from '@angular/core';
|
||||
import {HttpClient, HttpEvent, HttpEventType, HttpResponse} from '@angular/common/http';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {DownloadProgressService} from './download-progress.service';
|
||||
@@ -13,10 +13,10 @@ export class FileDownloadService {
|
||||
private downloadProgressService = inject(DownloadProgressService);
|
||||
private messageService = inject(MessageService);
|
||||
|
||||
downloadFile(url: string, defaultFilename: string): Observable<any> {
|
||||
downloadFile(url: string, defaultFilename: string): void {
|
||||
const cancelSubject = new Subject<void>();
|
||||
|
||||
return this.initiateDownload(url)
|
||||
this.initiateDownload(url)
|
||||
.pipe(
|
||||
takeUntil(cancelSubject),
|
||||
tap(event => this.handleDownloadProgress(event, defaultFilename, cancelSubject)),
|
||||
@@ -25,7 +25,8 @@ export class FileDownloadService {
|
||||
this.handleDownloadError(error);
|
||||
return throwError(() => error);
|
||||
})
|
||||
);
|
||||
)
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
private initiateDownload(url: string): Observable<HttpEvent<Blob>> {
|
||||
|
||||
Reference in New Issue
Block a user