diff --git a/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.spec.ts b/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.spec.ts index 719e4dd51580abcda04722fffbe5fac05b652a37..27dfda65c0234821b443e77f4c04ff361a456194 100644 --- a/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.spec.ts +++ b/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.spec.ts @@ -92,16 +92,33 @@ describe('BinaryFileService', () => { uploadedFile: of(createStateResource(createBinaryFileResource())), }; - beforeEach(() => { + it('should return uploaded files by key', (done) => { service.uploadFiles.next({ [type]: [uploadFile] }); - }); - it('should return uploaded files by key', (done) => { service.getUploadedFiles(type).subscribe((uploadedFiles: UploadFile[]) => { expect(uploadedFiles).toEqual([uploadFile]); done(); }); }); + + describe('on non existing key', () => { + beforeEach(() => { + service.uploadFiles.next({}); + }); + + it('should return empty array', (done) => { + service.getUploadedFiles(type).subscribe((uploadedFiles: UploadFile[]) => { + expect(uploadedFiles).toEqual([]); + done(); + }); + }); + + it('should set state value', () => { + service.getUploadedFiles(type).subscribe(); + + expect(service.uploadFiles.value[type]).toEqual([]); + }); + }); }); describe('remove uploaded file', () => { @@ -135,6 +152,17 @@ describe('BinaryFileService', () => { expect(service.uploadFiles.value[type][0]).toBe(uploadFile); }); + it('should add entry', () => { + service.uploadFiles.next({ [type]: [uploadFile] }); + service._buildUploadFile = jest.fn().mockReturnValue(uploadFile); + + service.uploadFileNew(toUploadFile); + + expect(service.uploadFiles.value[type]).toHaveLength(2); + expect(service.uploadFiles.value[type][0]).toBe(uploadFile); + expect(service.uploadFiles.value[type][1]).toBe(uploadFile); + }); + describe('build upload file', () => { beforeEach(() => { service._handleUpload = jest.fn().mockReturnValue(binaryFileStateResource$); diff --git a/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.ts b/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.ts index 96de3580c5e317de7c9c4a18ab5abc2f82bb94bc..e7be2d1b5cc72f149145fecee24173578b65d5e6 100644 --- a/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.ts +++ b/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.ts @@ -21,7 +21,21 @@ * Die sprachspezifischen Genehmigungen und Beschränkungen * unter der Lizenz sind dem Lizenztext zu entnehmen. */ -import { BlobWithFileName, createEmptyStateResource, createErrorStateResource, createStateResource, EMPTY_ARRAY, EMPTY_STRING, getMessageForInvalidParam, HttpError, HttpHeader, isNotNil, isUnprocessableEntity, isValidationFieldFileSizeExceedError, sanitizeFileName, StateResource, } from '@alfa-client/tech-shared'; +import { + BlobWithFileName, + createEmptyStateResource, + createErrorStateResource, + createStateResource, + EMPTY_STRING, + getMessageForInvalidParam, + HttpError, + HttpHeader, + isNotNil, + isUnprocessableEntity, + isValidationFieldFileSizeExceedError, + sanitizeFileName, + StateResource, +} from '@alfa-client/tech-shared'; import { SnackBarService } from '@alfa-client/ui'; import { HttpErrorResponse, HttpResponse } from '@angular/common/http'; import { Injectable } from '@angular/core'; @@ -31,7 +45,14 @@ import { saveAs } from 'file-saver'; import { isNil } from 'lodash-es'; import { BehaviorSubject, forkJoin, Observable, of, throwError } from 'rxjs'; import { catchError, map, mergeMap, startWith, switchMap } from 'rxjs/operators'; -import { BinaryFileListResource, BinaryFileResource, FileUploadType, ToUploadFile, UploadFile, UploadFilesByType, } from './binary-file.model'; +import { + BinaryFileListResource, + BinaryFileResource, + FileUploadType, + ToUploadFile, + UploadFile, + UploadFilesByType, +} from './binary-file.model'; import { BinaryFileRepository } from './binary-file.repository'; @Injectable({ providedIn: 'root' }) @@ -52,23 +73,20 @@ export class BinaryFileService { } public getUploadedFiles(type: FileUploadType): Observable<UploadFile[]> { + this.createEmptyUploadedFilesArrayIfKeyNotExists(type); return this.uploadFiles.asObservable().pipe(map((files: UploadFilesByType) => files[type])); } - public deleteUploadedFile(type: FileUploadType, key: string): void { - //Implement me - } - - public clearUploadedFiles(type: FileUploadType): void { - //Implement me - } - //TODO Rename public uploadFileNew(toUploadFile: ToUploadFile): void { - if (!(toUploadFile.uploadUrl in this.uploadFiles.value)) this.uploadFiles.value[toUploadFile.type] = EMPTY_ARRAY; + this.createEmptyUploadedFilesArrayIfKeyNotExists(toUploadFile.type); this.uploadFiles.value[toUploadFile.type].push(this._buildUploadFile(toUploadFile)); } + private createEmptyUploadedFilesArrayIfKeyNotExists(type: FileUploadType): void { + if (!(type in this.uploadFiles.value)) this.uploadFiles.value[type] = []; + } + _buildUploadFile(toUploadFile: ToUploadFile): UploadFile { return { key: faker.string.uuid(), @@ -85,6 +103,14 @@ export class BinaryFileService { ); } + public deleteUploadedFile(type: FileUploadType, key: string): void { + //Implement me + } + + public clearUploadedFiles(type: FileUploadType): void { + //Implement me + } + //TODO Rename to uploadFileOld OR refactor all use cases to uploadFileNew public uploadFile( resource: Resource,