diff --git a/src/app/submission/sections/form/section-form.component.spec.ts b/src/app/submission/sections/form/section-form.component.spec.ts index ff134c620a..5e559a9e4a 100644 --- a/src/app/submission/sections/form/section-form.component.spec.ts +++ b/src/app/submission/sections/form/section-form.component.spec.ts @@ -236,6 +236,7 @@ describe('FormSectionComponent test suite', () => { it('should init section properly', () => { const sectionData = {}; + formService.isValid.and.returnValue(observableOf(true)); formConfigService.getConfigByHref.and.returnValue(observableOf(formConfigData)); sectionsServiceStub.getSectionData.and.returnValue(observableOf(sectionData)); spyOn(comp, 'initForm'); diff --git a/src/app/submission/sections/upload/file/file.component.spec.ts b/src/app/submission/sections/upload/file/file.component.spec.ts index 8557efd451..4500b4bfe9 100644 --- a/src/app/submission/sections/upload/file/file.component.spec.ts +++ b/src/app/submission/sections/upload/file/file.component.spec.ts @@ -35,6 +35,8 @@ import { POLICY_DEFAULT_WITH_LIST } from '../section-upload.component'; import { JsonPatchOperationPathCombiner } from '../../../../core/json-patch/builder/json-patch-operation-path-combiner'; import { getMockSectionUploadService } from '../../../../shared/mocks/mock-section-upload.service'; import { FormFieldMetadataValueObject } from '../../../../shared/form/builder/models/form-field-metadata-value.model'; +import { Group } from '../../../../core/eperson/models/group.model'; +import { UploadSectionFileEditComponent } from './edit/file-edit.component'; function getMockFileService(): FileService { return jasmine.createSpyObj('FileService', { @@ -61,7 +63,10 @@ describe('UploadSectionFileComponent test suite', () => { const sectionId = 'upload'; const collectionId = mockSubmissionCollectionId; const availableAccessConditionOptions = mockUploadConfigResponse.accessConditionOptions; - const availableGroupsMap = new Map([[mockGroup.id, { name: mockGroup.name, uuid: mockGroup.uuid }]]); + const availableGroupsMap: Map = new Map([ + [mockUploadConfigResponse.accessConditionOptions[1].name, [mockGroup as any]], + [mockUploadConfigResponse.accessConditionOptions[2].name, [mockGroup as any]], + ]); const collectionPolicyType = POLICY_DEFAULT_WITH_LIST; const fileIndex = '0'; const fileName = '123456-test-upload.jpg'; @@ -98,7 +103,8 @@ describe('UploadSectionFileComponent test suite', () => { { provide: SectionUploadService, useValue: getMockSectionUploadService() }, ChangeDetectorRef, NgbModal, - UploadSectionFileComponent + UploadSectionFileComponent, + UploadSectionFileEditComponent ], schemas: [NO_ERRORS_SCHEMA] }).compileComponents().then(); @@ -228,10 +234,14 @@ describe('UploadSectionFileComponent test suite', () => { expect(fileService.downloadFile).toHaveBeenCalled() })); - it('should download Bitstream File properly', fakeAsync(() => { + it('should save Bitstream File data properly when form is valid', fakeAsync(() => { + compAsAny.fileEditComp = TestBed.get(UploadSectionFileEditComponent); + compAsAny.fileEditComp.formRef = {formGroup: null}; compAsAny.pathCombiner = pathCombiner; const event = new Event('click', null); spyOn(comp, 'switchMode'); + formService.validateAllFormFields.and.callFake(() => null); + formService.isValid.and.returnValue(observableOf(true)); formService.getFormData.and.returnValue(observableOf(mockFileFormData)); const response = [ @@ -279,6 +289,20 @@ describe('UploadSectionFileComponent test suite', () => { })); + it('should not save Bitstream File data properly when form is not valid', fakeAsync(() => { + compAsAny.fileEditComp = TestBed.get(UploadSectionFileEditComponent); + compAsAny.fileEditComp.formRef = {formGroup: null}; + compAsAny.pathCombiner = pathCombiner; + const event = new Event('click', null); + spyOn(comp, 'switchMode'); + formService.validateAllFormFields.and.callFake(() => null); + formService.isValid.and.returnValue(observableOf(false)); + + expect(comp.switchMode).not.toHaveBeenCalled(); + expect(uploadService.updateFileData).not.toHaveBeenCalled(); + + })); + it('should retrieve Value From Field properly', () => { let field; expect(compAsAny.retrieveValueFromField(field)).toBeUndefined(); diff --git a/src/app/submission/sections/upload/file/file.component.ts b/src/app/submission/sections/upload/file/file.component.ts index adf035bf23..71d4585e85 100644 --- a/src/app/submission/sections/upload/file/file.component.ts +++ b/src/app/submission/sections/upload/file/file.component.ts @@ -1,7 +1,7 @@ -import { ChangeDetectorRef, Component, Input, OnChanges, OnInit } from '@angular/core'; +import { ChangeDetectorRef, Component, Input, OnChanges, OnInit, ViewChild } from '@angular/core'; import { BehaviorSubject } from 'rxjs'; -import { filter, first, flatMap } from 'rxjs/operators'; +import { filter, first, flatMap, take } from 'rxjs/operators'; import { DynamicFormControlModel, } from '@ng-dynamic-forms/core'; import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; @@ -20,6 +20,8 @@ import { HALEndpointService } from '../../../../core/shared/hal-endpoint.service import { SubmissionJsonPatchOperationsService } from '../../../../core/submission/submission-json-patch-operations.service'; import { SubmissionObject } from '../../../../core/submission/models/submission-object.model'; import { WorkspaceitemSectionUploadObject } from '../../../../core/submission/models/workspaceitem-section-upload.model'; +import { UploadSectionFileEditComponent } from './edit/file-edit.component'; +import { Group } from '../../../../core/eperson/models/group.model'; @Component({ selector: 'ds-submission-upload-section-file', @@ -29,7 +31,7 @@ import { WorkspaceitemSectionUploadObject } from '../../../../core/submission/mo export class UploadSectionFileComponent implements OnChanges, OnInit { @Input() availableAccessConditionOptions: any[]; - @Input() availableAccessConditionGroups: Map; + @Input() availableAccessConditionGroups: Map; @Input() collectionId; @Input() collectionPolicyType; @Input() configMetadataForm: SubmissionFormsModel; @@ -48,6 +50,8 @@ export class UploadSectionFileComponent implements OnChanges, OnInit { protected pathCombiner: JsonPatchOperationPathCombiner; protected subscriptions = []; + @ViewChild(UploadSectionFileEditComponent) fileEditComp: UploadSectionFileEditComponent; + constructor(private cdr: ChangeDetectorRef, private fileService: FileService, private formService: FormService, @@ -116,8 +120,12 @@ export class UploadSectionFileComponent implements OnChanges, OnInit { public saveBitstreamData(event) { event.preventDefault(); - this.subscriptions.push(this.formService.getFormData(this.formId).pipe( - first(), + this.formService.validateAllFormFields(this.fileEditComp.formRef.formGroup); + this.subscriptions.push(this.formService.isValid(this.formId).pipe( + take(1), + filter((isValid) => isValid), + flatMap(() => this.formService.getFormData(this.formId)), + take(1), flatMap((formData: any) => { Object.keys((formData.metadata)) .filter((key) => isNotEmpty(formData.metadata[key]))