From 4b3b6603543fca4e22976805e6d8eb5217b47c21 Mon Sep 17 00:00:00 2001 From: abhinav Date: Fri, 11 Apr 2025 12:09:10 +0200 Subject: [PATCH] Fix export button enabled in bulk access management without selecting step 2 --- .../bulk-access/bulk-access.component.spec.ts | 45 ++++++++++++++----- .../bulk-access/bulk-access.component.ts | 4 +- .../bulk-access-settings.component.ts | 4 ++ .../access-control-array-form.component.ts | 4 ++ ...access-control-form-container.component.ts | 4 ++ 5 files changed, 50 insertions(+), 11 deletions(-) diff --git a/src/app/access-control/bulk-access/bulk-access.component.spec.ts b/src/app/access-control/bulk-access/bulk-access.component.spec.ts index 8bfbe1fe5d..fda1db0aa3 100644 --- a/src/app/access-control/bulk-access/bulk-access.component.spec.ts +++ b/src/app/access-control/bulk-access/bulk-access.component.spec.ts @@ -1,4 +1,7 @@ -import { NO_ERRORS_SCHEMA } from '@angular/core'; +import { + Component, + NO_ERRORS_SCHEMA, +} from '@angular/core'; import { ComponentFixture, TestBed, @@ -62,10 +65,17 @@ describe('BulkAccessComponent', () => { 'file': { }, }; - const mockSettings: any = jasmine.createSpyObj('AccessControlFormContainerComponent', { - getValue: jasmine.createSpy('getValue'), - reset: jasmine.createSpy('reset'), - }); + @Component({ + selector: 'ds-bulk-access-settings', + template: '', + exportAs: 'dsBulkSettings', + standalone: true, + }) + class MockBulkAccessSettingsComponent { + isFormValid = jasmine.createSpy('isFormValid').and.returnValue(false); + getValue = jasmine.createSpy('getValue'); + reset = jasmine.createSpy('reset'); + } const selection: any[] = [{ indexableObject: { uuid: '1234' } }, { indexableObject: { uuid: '5678' } }]; const selectableListState: SelectableListState = { id: 'test', selection }; const expectedIdList = ['1234', '5678']; @@ -93,6 +103,9 @@ describe('BulkAccessComponent', () => { BulkAccessSettingsComponent, ], }, + add: { + imports: [MockBulkAccessSettingsComponent], + }, }) .compileComponents(); }); @@ -109,13 +122,12 @@ describe('BulkAccessComponent', () => { fixture.destroy(); }); - describe('when there are no elements selected', () => { + describe('when there are no elements selected and step two form is invalid', () => { beforeEach(() => { (component as any).selectableListService.getSelectableList.and.returnValue(of(selectableListStateEmpty)); fixture.detectChanges(); - component.settings = mockSettings; }); it('should create', () => { @@ -138,7 +150,6 @@ describe('BulkAccessComponent', () => { (component as any).selectableListService.getSelectableList.and.returnValue(of(selectableListState)); fixture.detectChanges(); - component.settings = mockSettings; }); it('should create', () => { @@ -149,15 +160,29 @@ describe('BulkAccessComponent', () => { expect(component.objectsSelected$.value).toEqual(expectedIdList); }); - it('should enable the execute button when there are objects selected', () => { + it('should not enable the execute button when there are objects selected and step two form is invalid', () => { component.objectsSelected$.next(['1234']); - expect(component.canExport()).toBe(true); + expect(component.canExport()).toBe(false); }); it('should call the settings reset method when reset is called', () => { component.reset(); expect(component.settings.reset).toHaveBeenCalled(); }); + }); + describe('when there are elements selected and the step two form is valid', () => { + + beforeEach(() => { + + (component as any).selectableListService.getSelectableList.and.returnValue(of(selectableListState)); + fixture.detectChanges(); + (component as any).settings.isFormValid.and.returnValue(true); + }); + + it('should enable the execute button when there are objects selected and step two form is valid', () => { + component.objectsSelected$.next(['1234']); + expect(component.canExport()).toBe(true); + }); it('should call the bulkAccessControlService executeScript method when submit is called', () => { (component.settings as any).getValue.and.returnValue(mockFormState); diff --git a/src/app/access-control/bulk-access/bulk-access.component.ts b/src/app/access-control/bulk-access/bulk-access.component.ts index a1608d27d0..e158f3c446 100644 --- a/src/app/access-control/bulk-access/bulk-access.component.ts +++ b/src/app/access-control/bulk-access/bulk-access.component.ts @@ -1,4 +1,5 @@ import { + ChangeDetectionStrategy, Component, OnInit, ViewChild, @@ -31,6 +32,7 @@ import { BulkAccessSettingsComponent } from './settings/bulk-access-settings.com BtnDisabledDirective, ], standalone: true, + changeDetection: ChangeDetectionStrategy.OnPush, }) export class BulkAccessComponent implements OnInit { @@ -70,7 +72,7 @@ export class BulkAccessComponent implements OnInit { } canExport(): boolean { - return this.objectsSelected$.value?.length > 0; + return this.objectsSelected$.value?.length > 0 && this.settings?.isFormValid(); } /** diff --git a/src/app/access-control/bulk-access/settings/bulk-access-settings.component.ts b/src/app/access-control/bulk-access/settings/bulk-access-settings.component.ts index a8c527c755..7082b7dc4f 100644 --- a/src/app/access-control/bulk-access/settings/bulk-access-settings.component.ts +++ b/src/app/access-control/bulk-access/settings/bulk-access-settings.component.ts @@ -42,4 +42,8 @@ export class BulkAccessSettingsComponent { this.controlForm.reset(); } + isFormValid() { + return this.controlForm.isValid(); + } + } diff --git a/src/app/shared/access-control-form-container/access-control-array-form/access-control-array-form.component.ts b/src/app/shared/access-control-form-container/access-control-array-form/access-control-array-form.component.ts index 10366446f6..a396c5a72c 100644 --- a/src/app/shared/access-control-form-container/access-control-array-form/access-control-array-form.component.ts +++ b/src/app/shared/access-control-form-container/access-control-array-form/access-control-array-form.component.ts @@ -135,6 +135,10 @@ export class AccessControlArrayFormComponent implements OnInit { return item.id; } + isValid() { + return this.ngForm.valid; + } + } diff --git a/src/app/shared/access-control-form-container/access-control-form-container.component.ts b/src/app/shared/access-control-form-container/access-control-form-container.component.ts index 515faabbac..e1aa7dae38 100644 --- a/src/app/shared/access-control-form-container/access-control-form-container.component.ts +++ b/src/app/shared/access-control-form-container/access-control-form-container.component.ts @@ -176,5 +176,9 @@ export class AccessControlFormContainerComponent impleme this.selectableListService.deselectAll(ITEM_ACCESS_CONTROL_SELECT_BITSTREAMS_LIST_ID); } + isValid() { + return this.bitstreamAccessCmp.isValid() || this.itemAccessCmp.isValid(); + } + }