[AAE-14704] Fix viewers showing removed file (#8890)

This commit is contained in:
Robert Duda
2023-09-08 10:11:47 +02:00
committed by GitHub
parent 76e2870c66
commit a8db044092
2 changed files with 49 additions and 26 deletions

View File

@@ -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));
});

View File

@@ -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);
}
});
}