feat: add cashbox export model and FileDownloadService
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import { RecurringTransactionInterval } from './recurring-transaction.model';
|
||||
|
||||
export type CashboxExportFormat = 'csv' | 'pdf';
|
||||
|
||||
export interface CashboxExportSubscription {
|
||||
recipients: string[];
|
||||
interval: RecurringTransactionInterval;
|
||||
active: boolean;
|
||||
nextRunDate: string | null;
|
||||
}
|
||||
|
||||
export interface UpdateCashboxExportSubscription {
|
||||
recipients: string[];
|
||||
interval: RecurringTransactionInterval;
|
||||
active: boolean;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { FileDownloadService } from './file-download.service';
|
||||
|
||||
describe('FileDownloadService', () => {
|
||||
let service: FileDownloadService;
|
||||
let clickSpy: ReturnType<typeof vi.fn>;
|
||||
let createObjectURLSpy: ReturnType<typeof vi.fn>;
|
||||
let revokeObjectURLSpy: ReturnType<typeof vi.fn>;
|
||||
|
||||
beforeEach(() => {
|
||||
service = TestBed.inject(FileDownloadService);
|
||||
clickSpy = vi.fn();
|
||||
createObjectURLSpy = vi.fn(() => 'blob:mock-url');
|
||||
revokeObjectURLSpy = vi.fn();
|
||||
vi.spyOn(URL, 'createObjectURL').mockImplementation(createObjectURLSpy as (obj: Blob | MediaSource) => string);
|
||||
vi.spyOn(URL, 'revokeObjectURL').mockImplementation(revokeObjectURLSpy as (url: string) => void);
|
||||
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(clickSpy as () => void);
|
||||
});
|
||||
|
||||
it('creates an object URL, clicks a temporary anchor with the given filename, and revokes the URL', () => {
|
||||
const blob = new Blob(['content'], { type: 'text/csv' });
|
||||
|
||||
service.save(blob, 'kassenbuch.csv');
|
||||
|
||||
expect(createObjectURLSpy).toHaveBeenCalledWith(blob);
|
||||
expect(clickSpy).toHaveBeenCalledTimes(1);
|
||||
expect(revokeObjectURLSpy).toHaveBeenCalledWith('blob:mock-url');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class FileDownloadService {
|
||||
save(blob: Blob, filename: string): void {
|
||||
const url = URL.createObjectURL(blob);
|
||||
const anchor = document.createElement('a');
|
||||
anchor.href = url;
|
||||
anchor.download = filename;
|
||||
anchor.click();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user