diff --git a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts index 6bf51c4b6..5c62bc2dd 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts +++ b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts @@ -1653,6 +1653,58 @@ describe('DocumentDetailComponent', () => { expect(component.previewText).toContain('An error occurred loading content') }) + it('selectVersion should show toast if version content retrieval fails', () => { + initNormally() + httpTestingController.expectOne(component.previewUrl).flush('preview') + + jest.spyOn(documentService, 'getPreviewUrl').mockReturnValue('preview-ok') + jest.spyOn(documentService, 'getThumbUrl').mockReturnValue('thumb-ok') + jest + .spyOn(documentService, 'getMetadata') + .mockReturnValue(of({ has_archive_version: true } as any)) + const contentError = new Error('content failed') + jest + .spyOn(documentService, 'get') + .mockReturnValue(throwError(() => contentError)) + const toastSpy = jest.spyOn(toastService, 'showError') + + component.selectVersion(10) + httpTestingController.expectOne('preview-ok').flush('preview text') + + expect(toastSpy).toHaveBeenCalledWith( + 'Error retrieving version content', + contentError + ) + }) + + it('onVersionSelected should delegate to selectVersion', () => { + const selectVersionSpy = jest + .spyOn(component, 'selectVersion') + .mockImplementation(() => {}) + + component.onVersionSelected(42) + + expect(selectVersionSpy).toHaveBeenCalledWith(42) + }) + + it('onVersionsUpdated should sync open document versions and save', () => { + component.documentId = doc.id + component.document = { ...doc, versions: [] } as Document + const updatedVersions = [ + { id: doc.id, is_root: true }, + { id: 10, is_root: false }, + ] as any + const openDoc = { ...doc, versions: [] } as Document + jest.spyOn(openDocumentsService, 'getOpenDocument').mockReturnValue(openDoc) + const saveSpy = jest.spyOn(openDocumentsService, 'save') + + component.onVersionsUpdated(updatedVersions) + + expect(component.document.versions).toEqual(updatedVersions) + expect(openDoc.versions).toEqual(updatedVersions) + expect(saveSpy).toHaveBeenCalled() + }) + it('createDisabled should return true if the user does not have permission to add the specified data type', () => { currentUserCan = false expect(component.createDisabled(DataType.Correspondent)).toBeTruthy() diff --git a/src-ui/src/app/components/document-detail/document-version-dropdown/document-version-dropdown.component.spec.ts b/src-ui/src/app/components/document-detail/document-version-dropdown/document-version-dropdown.component.spec.ts index 1035f34ae..3328152d6 100644 --- a/src-ui/src/app/components/document-detail/document-version-dropdown/document-version-dropdown.component.spec.ts +++ b/src-ui/src/app/components/document-detail/document-version-dropdown/document-version-dropdown.component.spec.ts @@ -178,6 +178,40 @@ describe('DocumentVersionDropdownComponent', () => { expect(documentService.getVersions).not.toHaveBeenCalled() }) + it('onVersionFileSelected should fail when backend response has no task id', () => { + const file = new File(['test'], 'new-version.pdf', { + type: 'application/pdf', + }) + const input = document.createElement('input') + Object.defineProperty(input, 'files', { value: [file] }) + documentService.uploadVersion.mockReturnValue(of({} as any)) + + component.onVersionFileSelected({ target: input } as Event) + + expect(component.versionUploadState).toEqual(UploadState.Failed) + expect(component.versionUploadError).toEqual('Missing task ID.') + expect(documentService.getVersions).not.toHaveBeenCalled() + }) + + it('onVersionFileSelected should show error when upload request fails', () => { + const file = new File(['test'], 'new-version.pdf', { + type: 'application/pdf', + }) + const input = document.createElement('input') + Object.defineProperty(input, 'files', { value: [file] }) + const error = new Error('upload failed') + documentService.uploadVersion.mockReturnValue(throwError(() => error)) + + component.onVersionFileSelected({ target: input } as Event) + + expect(component.versionUploadState).toEqual(UploadState.Failed) + expect(component.versionUploadError).toEqual('upload failed') + expect(toastService.showError).toHaveBeenCalledWith( + 'Error uploading new version', + error + ) + }) + it('ngOnChanges should clear upload status on document switch', () => { component.versionUploadState = UploadState.Failed component.versionUploadError = 'something failed'