@@ -14,25 +14,29 @@ import {
1414import { RouterTestingModule } from '@angular/router/testing' ;
1515import { NgbModule } from '@ng-bootstrap/ng-bootstrap' ;
1616import { TranslateModule } from '@ngx-translate/core' ;
17- import {
18- EMPTY ,
19- of ,
20- } from 'rxjs' ;
17+ import { of } from 'rxjs' ;
2118
2219import { LinkService } from '../../../core/cache/builders/link.service' ;
2320import { EntityTypeDataService } from '../../../core/data/entity-type-data.service' ;
2421import { ItemDataService } from '../../../core/data/item-data.service' ;
2522import { ObjectUpdatesService } from '../../../core/data/object-updates/object-updates.service' ;
23+ import {
24+ DSPACE_OBJECT_DELETION_SCRIPT_NAME ,
25+ ScriptDataService ,
26+ } from '../../../core/data/processes/script-data.service' ;
2627import { RelationshipDataService } from '../../../core/data/relationship-data.service' ;
2728import { RelationshipTypeDataService } from '../../../core/data/relationship-type-data.service' ;
2829import { Item } from '../../../core/shared/item.model' ;
2930import { ItemType } from '../../../core/shared/item-relationships/item-type.model' ;
3031import { Relationship } from '../../../core/shared/item-relationships/relationship.model' ;
3132import { RelationshipType } from '../../../core/shared/item-relationships/relationship-type.model' ;
33+ import { getProcessDetailRoute } from '../../../process-page/process-page-routing.paths' ;
34+ import { ProcessParameter } from '../../../process-page/processes/process-parameter.model' ;
3235import { getMockThemeService } from '../../../shared/mocks/theme-service.mock' ;
3336import { NotificationsService } from '../../../shared/notifications/notifications.service' ;
3437import { ListableObjectComponentLoaderComponent } from '../../../shared/object-collection/shared/listable-object/listable-object-component-loader.component' ;
3538import {
39+ createFailedRemoteDataObject$ ,
3640 createSuccessfulRemoteDataObject ,
3741 createSuccessfulRemoteDataObject$ ,
3842} from '../../../shared/remote-data.utils' ;
@@ -64,6 +68,7 @@ let linkService;
6468let entityTypeService ;
6569let notificationsServiceStub ;
6670let typesSelection ;
71+ let scriptDataService ;
6772
6873describe ( 'ItemDeleteComponent' , ( ) => {
6974 beforeEach ( waitForAsync ( ( ) => {
@@ -163,6 +168,10 @@ describe('ItemDeleteComponent', () => {
163168
164169 notificationsServiceStub = new NotificationsServiceStub ( ) ;
165170
171+ scriptDataService = {
172+ invoke : jasmine . createSpy ( 'invoke' ) . and . returnValue ( createSuccessfulRemoteDataObject$ ( { processId : '123' } ) ) ,
173+ } ;
174+
166175 TestBed . configureTestingModule ( {
167176 imports : [ CommonModule , FormsModule , RouterTestingModule . withRoutes ( [ ] ) , TranslateModule . forRoot ( ) , NgbModule , ItemDeleteComponent , VarDirective ] ,
168177 providers : [
@@ -176,6 +185,7 @@ describe('ItemDeleteComponent', () => {
176185 { provide : RelationshipTypeDataService , useValue : { } } ,
177186 { provide : LinkService , useValue : linkService } ,
178187 { provide : ThemeService , useValue : getMockThemeService ( ) } ,
188+ { provide : ScriptDataService , useValue : scriptDataService } ,
179189 ] , schemas : [
180190 CUSTOM_ELEMENTS_SCHEMA ,
181191 ] ,
@@ -189,6 +199,10 @@ describe('ItemDeleteComponent', () => {
189199 beforeEach ( ( ) => {
190200 fixture = TestBed . createComponent ( ItemDeleteComponent ) ;
191201 comp = fixture . componentInstance ;
202+ // Verifica che il componente stia usando il mock corretto
203+ expect ( comp . scriptDataService ) . toBe ( scriptDataService ) ;
204+ console . log ( 'scriptDataService mock:' , scriptDataService ) ;
205+ console . log ( 'comp scriptDataService:' , comp . scriptDataService ) ;
192206 fixture . detectChanges ( ) ;
193207 } ) ;
194208
@@ -204,54 +218,40 @@ describe('ItemDeleteComponent', () => {
204218 } ) ;
205219
206220 describe ( 'performAction' , ( ) => {
207- describe ( `when there are entitytypes` , ( ) => {
208- it ( 'should call delete function from the ItemDataService' , ( ) => {
209- spyOn ( comp , 'notify' ) ;
210- comp . performAction ( ) ;
211- expect ( mockItemDataService . delete )
212- . toHaveBeenCalledWith ( mockItem . id , types . filter ( ( type ) => typesSelection [ type ] ) . map ( ( type ) => type . id ) ) ;
213- expect ( comp . notify ) . toHaveBeenCalled ( ) ;
214- } ) ;
215-
216- it ( 'should call delete function from the ItemDataService with empty types' , ( ) => {
217-
218- spyOn ( comp , 'notify' ) ;
219- jasmine . getEnv ( ) . allowRespy ( true ) ;
220- spyOn ( entityTypeService , 'getEntityTypeRelationships' ) . and . returnValue ( [ ] ) ;
221- comp . ngOnInit ( ) ;
222-
223- comp . performAction ( ) ;
224-
225- expect ( mockItemDataService . delete ) . toHaveBeenCalledWith ( mockItem . id , [ ] ) ;
226- expect ( comp . notify ) . toHaveBeenCalled ( ) ;
227- } ) ;
221+ it ( 'should invoke the deletion script with correct params, show success notification and redirect on success' , ( done ) => {
222+ const parameterValues : ProcessParameter [ ] = [
223+ Object . assign ( new ProcessParameter ( ) , { name : '-i' , value : mockItem . uuid } ) ,
224+ ] ;
225+ scriptDataService . invoke . and . returnValue ( createSuccessfulRemoteDataObject$ ( { processId : '123' } ) ) ;
226+ console . log ( 'Before performAction - scriptDataService.invoke:' , scriptDataService . invoke ) ;
227+ console . log ( 'Before performAction - isSpy:' , jasmine . isSpy ( scriptDataService . invoke ) ) ;
228+ comp . performAction ( ) ;
229+ setTimeout ( ( ) => {
230+ console . log ( 'After performAction - scriptDataService.invoke:' , scriptDataService . invoke ) ;
231+ console . log ( 'After performAction - calls:' , scriptDataService . invoke . calls ) ;
232+ console . log ( 'After performAction - isSpy:' , jasmine . isSpy ( scriptDataService . invoke ) ) ;
233+ expect ( scriptDataService . invoke ) . toHaveBeenCalledWith ( DSPACE_OBJECT_DELETION_SCRIPT_NAME , parameterValues , [ ] ) ;
234+ expect ( comp . notificationsService . success ) . toHaveBeenCalled ( ) ;
235+ expect ( comp . router . navigateByUrl ) . toHaveBeenCalledWith ( getProcessDetailRoute ( '123' ) ) ;
236+ done ( ) ;
237+ } , 0 ) ;
228238 } ) ;
229-
230- describe ( `when there are no entity types` , ( ) => {
231- beforeEach ( ( ) => {
232- ( comp as any ) . entityTypeService = jasmine . createSpyObj ( 'entityTypeService' ,
233- {
234- getEntityTypeByLabel : EMPTY ,
235- } ,
236- ) ;
237- } ) ;
238-
239- it ( 'should call delete function from the ItemDataService' , ( ) => {
240- spyOn ( comp , 'notify' ) ;
241- comp . performAction ( ) ;
242- expect ( mockItemDataService . delete )
243- . toHaveBeenCalledWith ( mockItem . id , types . filter ( ( type ) => typesSelection [ type ] ) . map ( ( type ) => type . id ) ) ;
244- expect ( comp . notify ) . toHaveBeenCalled ( ) ;
245- } ) ;
239+ it ( 'should show error notification and redirect to item edit page on failure' , ( done ) => {
240+ scriptDataService . invoke . and . returnValue ( createFailedRemoteDataObject$ ( 'Error' , 500 ) ) ;
241+ comp . performAction ( ) ;
242+ setTimeout ( ( ) => {
243+ expect ( comp . notificationsService . error ) . toHaveBeenCalled ( ) ;
244+ expect ( comp . router . navigate ) . toHaveBeenCalledWith ( [ getItemEditRoute ( mockItem ) ] ) ;
245+ done ( ) ;
246+ } , 0 ) ;
246247 } ) ;
247248 } ) ;
248249 describe ( 'notify' , ( ) => {
249250 it ( 'should navigate to the homepage on successful deletion of the item' , ( ) => {
250251 comp . notify ( true ) ;
251252 expect ( routerStub . navigate ) . toHaveBeenCalledWith ( [ '' ] ) ;
252253 } ) ;
253- } ) ;
254- describe ( 'notify' , ( ) => {
254+
255255 it ( 'should navigate to the item edit page on failed deletion of the item' , ( ) => {
256256 comp . notify ( false ) ;
257257 expect ( routerStub . navigate ) . toHaveBeenCalledWith ( [ getItemEditRoute ( mockItem ) ] ) ;
0 commit comments