diff --git a/lib/process-services-cloud/src/lib/form/components/widgets/attach-file/attach-file-cloud-widget.component.spec.ts b/lib/process-services-cloud/src/lib/form/components/widgets/attach-file/attach-file-cloud-widget.component.spec.ts index bb41c162e3..1e9e7bb1ef 100644 --- a/lib/process-services-cloud/src/lib/form/components/widgets/attach-file/attach-file-cloud-widget.component.spec.ts +++ b/lib/process-services-cloud/src/lib/form/components/widgets/attach-file/attach-file-cloud-widget.component.spec.ts @@ -663,48 +663,54 @@ describe('AttachFileCloudWidgetComponent', () => { expect(contentModelFormFileHandlerSpy).not.toHaveBeenCalled(); }); - it('should have been called onInit when widget only one file', async () => { + it('should be called during initialization when widget has only one file', async () => { widget.field.value = [fakeNodeWithProperties]; widget.ngOnInit(); fixture.detectChanges(); await fixture.whenStable(); expect(contentModelFormFileHandlerSpy).toHaveBeenCalledWith(fakeNodeWithProperties); + expect(apiServiceSpy).toHaveBeenCalledWith(fakeNodeWithProperties.id); expect(updateFormSpy).toHaveBeenCalledWith(expectedValues); expect(contentClickedSpy).toHaveBeenCalledWith(new UploadWidgetContentLinkModel(fakeNodeWithProperties, widget.field.id)); }); - it('should not be called onInit when widget has more than one file', async () => { + it('should be called during initialization with the first file when widget has more than one', async () => { widget.field.value = [fakeNodeWithProperties, fakeNode]; + widget.ngOnInit(); fixture.detectChanges(); await fixture.whenStable(); - expect(contentModelFormFileHandlerSpy).not.toHaveBeenCalled(); + expect(contentModelFormFileHandlerSpy).toHaveBeenCalledWith(fakeNodeWithProperties); + expect(apiServiceSpy).toHaveBeenCalledWith(fakeNodeWithProperties.id); + expect(updateFormSpy).toHaveBeenCalledWith(expectedValues); + expect(contentClickedSpy).toHaveBeenCalledWith(new UploadWidgetContentLinkModel(fakeNodeWithProperties, widget.field.id)); }); - it('should not be called on remove node if node removed is not the selected one', async () => { - widget.field.value = [fakeNodeWithProperties, fakeNode]; - widget.selectedNode = fakeNodeWithProperties; - - fixture.detectChanges(); - - widget.onRemoveAttachFile(fakeNode); - - await fixture.whenStable(); - expect(contentModelFormFileHandlerSpy).not.toHaveBeenCalled(); - }); - - it('should have been called on remove node if node removed is the selected one', async () => { - widget.field.value = [fakeNodeWithProperties, fakeNode]; - widget.selectedNode = fakeNodeWithProperties; + it('should be called with null onRemoveAttachFile if the removed file was being displayed by viewers and theres no more files to display', async () => { + widget.field.value = [fakeNodeWithProperties]; fixture.detectChanges(); widget.onRemoveAttachFile(fakeNodeWithProperties); await fixture.whenStable(); - expect(contentModelFormFileHandlerSpy).toHaveBeenCalled(); + expect(contentModelFormFileHandlerSpy).toHaveBeenCalledWith(null); + expect(apiServiceSpy).not.toHaveBeenCalled(); expect(updateFormSpy).not.toHaveBeenCalled(); - expect(contentClickedSpy).toHaveBeenCalledWith(new UploadWidgetContentLinkModel(undefined, widget.field.id)); + expect(contentClickedSpy).toHaveBeenCalledWith(new UploadWidgetContentLinkModel(null, widget.field.id)); + }); + + it('should be called with next file onRemoveAttachFile if the removed file was being displayed by viewers and theres more files to display', async () => { + widget.field.value = [fakeNodeWithProperties, fakeNode]; + fixture.detectChanges(); + + widget.onRemoveAttachFile(fakeNodeWithProperties); + + await fixture.whenStable(); + expect(contentModelFormFileHandlerSpy).toHaveBeenCalledWith(fakeNode); + expect(apiServiceSpy).toHaveBeenCalledWith(fakeNode.id); + expect(updateFormSpy).toHaveBeenCalledWith(expectedValues); + expect(contentClickedSpy).toHaveBeenCalledWith(new UploadWidgetContentLinkModel(fakeNode, widget.field.id)); }); it('should have been called on attach file when value was empty', async () => { @@ -714,11 +720,12 @@ describe('AttachFileCloudWidgetComponent', () => { await fixture.whenStable(); expect(contentModelFormFileHandlerSpy).toHaveBeenCalledWith(fakeNodeWithProperties); + expect(apiServiceSpy).toHaveBeenCalledWith(fakeNodeWithProperties.id); expect(updateFormSpy).toHaveBeenCalledWith(expectedValues); expect(contentClickedSpy).toHaveBeenCalledWith(new UploadWidgetContentLinkModel(fakeNodeWithProperties, widget.field.id)); }); - it('should not be called on attach file when has a file previously', async () => { + it('should not be called on newly attached file if there were files attached previously', async () => { spyOn(contentCloudNodeSelectorService, 'getNodeIdFromPath').and.returnValue(mockNodeId); widget.field.value = [fakeNode]; @@ -729,6 +736,20 @@ describe('AttachFileCloudWidgetComponent', () => { expect(contentModelFormFileHandlerSpy).not.toHaveBeenCalled(); }); + it('should be called on newly attached file if there werent files attached previously', async () => { + spyOn(contentCloudNodeSelectorService, 'getNodeIdFromPath').and.returnValue(mockNodeId); + widget.field.value = []; + + clickOnAttachFileWidget('attach-file-alfresco'); + fixture.detectChanges(); + await fixture.whenStable(); + + expect(contentModelFormFileHandlerSpy).toHaveBeenCalledWith(fakeNodeWithProperties); + expect(apiServiceSpy).toHaveBeenCalledWith(fakeNodeWithProperties.id); + expect(updateFormSpy).toHaveBeenCalledWith(expectedValues); + expect(contentClickedSpy).toHaveBeenCalledWith(new UploadWidgetContentLinkModel(fakeNodeWithProperties, widget.field.id)); + }); + it('should be called when selecting a row if no previous row was selected', async () => { widget.field.value = [fakeNodeWithProperties, fakeNode]; widget.selectedNode = null; @@ -769,7 +790,8 @@ describe('AttachFileCloudWidgetComponent', () => { await fixture.whenStable(); expect(widget.selectedNode).toBeNull(); - expect(contentModelFormFileHandlerSpy).toHaveBeenCalled(); + expect(contentModelFormFileHandlerSpy).toHaveBeenCalledWith(null); + expect(apiServiceSpy).not.toHaveBeenCalled(); expect(updateFormSpy).not.toHaveBeenCalled(); expect(contentClickedSpy).toHaveBeenCalledWith(new UploadWidgetContentLinkModel(null, widget.field.id)); }); diff --git a/lib/process-services-cloud/src/lib/form/components/widgets/attach-file/attach-file-cloud-widget.component.ts b/lib/process-services-cloud/src/lib/form/components/widgets/attach-file/attach-file-cloud-widget.component.ts index c43bc0b88e..d41566e704 100644 --- a/lib/process-services-cloud/src/lib/form/components/widgets/attach-file/attach-file-cloud-widget.component.ts +++ b/lib/process-services-cloud/src/lib/form/components/widgets/attach-file/attach-file-cloud-widget.component.ts @@ -89,7 +89,7 @@ export class AttachFileCloudWidgetComponent extends UploadCloudWidgetComponent i ngOnInit() { super.ngOnInit(); - if (this.hasFile && this.field.value.length === 1) { + if (this.hasFile && this.field.value.length > 0) { const files = this.field.value || this.field.form.values[this.field.id]; this.contentModelFormFileHandler(files[0]); } @@ -109,8 +109,8 @@ export class AttachFileCloudWidgetComponent extends UploadCloudWidgetComponent i this.removeFile(file); if (file['id'] === this.selectedNode?.id) { this.selectedNode = null; - this.contentModelFormFileHandler(); } + this.contentModelFormFileHandler(this.field.value.length > 0 ? this.field.value[0] : null); } fetchAppNameFromAppConfig(): string { @@ -136,9 +136,10 @@ export class AttachFileCloudWidgetComponent extends UploadCloudWidgetComponent i .subscribe((selections: Node[]) => { selections.forEach(node => (node['isExternal'] = true)); const selectionWithoutDuplication = this.removeExistingSelection(selections); + const hadFilesAttached = this.field.value?.length > 0; this.fixIncompatibilityFromPreviousAndNewForm(selectionWithoutDuplication); - if (this.field.value.length === 1) { - this.contentModelFormFileHandler(selections && selections.length > 0 ? selections[0] : null); + if(!hadFilesAttached) { + this.contentModelFormFileHandler(this.field.value.length > 0 ? this.field.value[0] : null); } }); }