@@ -5,31 +5,61 @@ import {
55 TestBed ,
66 waitForAsync ,
77} from '@angular/core/testing' ;
8- import { ActivatedRoute } from '@angular/router' ;
8+ import {
9+ ActivatedRoute ,
10+ Router ,
11+ } from '@angular/router' ;
912import { RouterTestingModule } from '@angular/router/testing' ;
1013import { TranslateModule } from '@ngx-translate/core' ;
1114import { of } from 'rxjs' ;
1215
1316import { DSONameService } from '../../core/breadcrumbs/dso-name.service' ;
1417import { CollectionDataService } from '../../core/data/collection-data.service' ;
15- import { RequestService } from '../../core/data/request.service' ;
18+ import {
19+ DSPACE_OBJECT_DELETION_SCRIPT_NAME ,
20+ ScriptDataService ,
21+ } from '../../core/data/processes/script-data.service' ;
22+ import { Collection } from '../../core/shared/collection.model' ;
23+ import { getProcessDetailRoute } from '../../process-page/process-page-routing.paths' ;
24+ import { ProcessParameter } from '../../process-page/processes/process-parameter.model' ;
1625import { DSONameServiceMock } from '../../shared/mocks/dso-name.service.mock' ;
1726import { NotificationsService } from '../../shared/notifications/notifications.service' ;
27+ import {
28+ createFailedRemoteDataObject$ ,
29+ createSuccessfulRemoteDataObject$ ,
30+ } from '../../shared/remote-data.utils' ;
31+ import { NotificationsServiceStub } from '../../shared/testing/notifications-service.stub' ;
1832import { DeleteCollectionPageComponent } from './delete-collection-page.component' ;
1933
2034describe ( 'DeleteCollectionPageComponent' , ( ) => {
2135 let comp : DeleteCollectionPageComponent ;
2236 let fixture : ComponentFixture < DeleteCollectionPageComponent > ;
37+ let scriptService ;
38+ let notificationService : NotificationsServiceStub ;
39+ let router ;
40+
41+ const mockCollection : Collection = Object . assign ( new Collection ( ) , {
42+ uuid : 'test-uuid' ,
43+ id : 'test-uuid' ,
44+ name : 'Test Collection' ,
45+ type : 'collection' ,
46+ } ) ;
2347
2448 beforeEach ( waitForAsync ( ( ) => {
49+ notificationService = new NotificationsServiceStub ( ) ;
50+ scriptService = jasmine . createSpyObj ( 'scriptService' , {
51+ invoke : createSuccessfulRemoteDataObject$ ( { processId : '123' } ) ,
52+ } ) ;
53+ router = jasmine . createSpyObj ( 'router' , [ 'navigateByUrl' ] ) ;
2554 TestBed . configureTestingModule ( {
2655 imports : [ TranslateModule . forRoot ( ) , CommonModule , RouterTestingModule , DeleteCollectionPageComponent ] ,
2756 providers : [
2857 { provide : DSONameService , useValue : new DSONameServiceMock ( ) } ,
2958 { provide : CollectionDataService , useValue : { } } ,
30- { provide : ActivatedRoute , useValue : { data : of ( { dso : { payload : { } } } ) } } ,
31- { provide : NotificationsService , useValue : { } } ,
32- { provide : RequestService , useValue : { } } ,
59+ { provide : ActivatedRoute , useValue : { data : of ( { dso : { payload : mockCollection } } ) } } ,
60+ { provide : NotificationsService , useValue : notificationService } ,
61+ { provide : ScriptDataService , useValue : scriptService } ,
62+ { provide : Router , useValue : router } ,
3363 ] ,
3464 schemas : [ NO_ERRORS_SCHEMA ] ,
3565 } ) . compileComponents ( ) ;
@@ -41,9 +71,28 @@ describe('DeleteCollectionPageComponent', () => {
4171 fixture . detectChanges ( ) ;
4272 } ) ;
4373
44- describe ( 'frontendURL' , ( ) => {
45- it ( 'should have the right frontendURL set' , ( ) => {
46- expect ( ( comp as any ) . frontendURL ) . toEqual ( '/collections/' ) ;
74+ it ( 'should create' , ( ) => {
75+ expect ( comp ) . toBeTruthy ( ) ;
76+ } ) ;
77+
78+ it ( 'should have the right frontendURL set' , ( ) => {
79+ expect ( ( comp as any ) . frontendURL ) . toEqual ( '/collections/' ) ;
80+ } ) ;
81+
82+ describe ( 'onConfirm' , ( ) => {
83+ it ( 'should invoke the deletion script with correct params, show success notification and redirect on success' , ( done ) => {
84+ const parameterValues : ProcessParameter [ ] = [
85+ Object . assign ( new ProcessParameter ( ) , { name : '-i' , value : mockCollection . uuid } ) ,
86+ ] ;
87+ ( scriptService . invoke as jasmine . Spy ) . and . returnValue ( createSuccessfulRemoteDataObject$ ( { processId : '123' } ) ) ;
88+ expect ( scriptService . invoke ) . toHaveBeenCalledWith ( DSPACE_OBJECT_DELETION_SCRIPT_NAME , parameterValues , [ ] ) ;
89+ expect ( notificationService . success ) . toHaveBeenCalledWith ( 'collection.delete.notification.success' ) ;
90+ expect ( router . navigateByUrl ) . toHaveBeenCalledWith ( getProcessDetailRoute ( '123' ) ) ;
91+ } ) ;
92+
93+ it ( 'error notification is shown' , ( done ) => {
94+ ( scriptService . invoke as jasmine . Spy ) . and . returnValue ( createFailedRemoteDataObject$ ( 'Error' , 500 ) ) ;
95+ expect ( notificationService . error ) . toHaveBeenCalledWith ( 'collection.delete.notification.fail' ) ;
4796 } ) ;
4897 } ) ;
4998} ) ;
0 commit comments