@@ -12,24 +12,49 @@ import { of as observableOf } from 'rxjs';
1212
1313import { DSONameService } from '../../core/breadcrumbs/dso-name.service' ;
1414import { CollectionDataService } from '../../core/data/collection-data.service' ;
15- import { RequestService } from '../../core/data/request.service' ;
15+ import {
16+ DSPACE_OBJECT_DELETION_SCRIPT_NAME ,
17+ ScriptDataService ,
18+ } from '../../core/data/processes/script-data.service' ;
19+ import { Collection } from '../../core/shared/collection.model' ;
20+ import { ProcessParameter } from '../../process-page/processes/process-parameter.model' ;
1621import { DSONameServiceMock } from '../../shared/mocks/dso-name.service.mock' ;
1722import { NotificationsService } from '../../shared/notifications/notifications.service' ;
23+ import {
24+ createFailedRemoteDataObject$ ,
25+ createSuccessfulRemoteDataObject$ ,
26+ } from '../../shared/remote-data.utils' ;
27+ import { NotificationsServiceStub } from '../../shared/testing/notifications-service.stub' ;
1828import { DeleteCollectionPageComponent } from './delete-collection-page.component' ;
1929
2030describe ( 'DeleteCollectionPageComponent' , ( ) => {
31+
32+ let scriptService ;
2133 let comp : DeleteCollectionPageComponent ;
34+ let notificationService : NotificationsServiceStub ;
2235 let fixture : ComponentFixture < DeleteCollectionPageComponent > ;
2336
37+ const mockCollection : Collection = Object . assign ( new Collection ( ) , {
38+ uuid : 'test-uuid' ,
39+ id : 'test-uuid' ,
40+ name : 'Test Collection' ,
41+ type : 'collection' ,
42+ } ) ;
43+
2444 beforeEach ( waitForAsync ( ( ) => {
45+ notificationService = new NotificationsServiceStub ( ) ;
46+ scriptService = jasmine . createSpyObj ( 'scriptService' , {
47+ invoke : createSuccessfulRemoteDataObject$ ( { processId : '123' } ) ,
48+ } ) ;
49+
2550 TestBed . configureTestingModule ( {
2651 imports : [ TranslateModule . forRoot ( ) , CommonModule , RouterTestingModule , DeleteCollectionPageComponent ] ,
2752 providers : [
2853 { provide : DSONameService , useValue : new DSONameServiceMock ( ) } ,
2954 { provide : CollectionDataService , useValue : { } } ,
3055 { provide : ActivatedRoute , useValue : { data : observableOf ( { dso : { payload : { } } } ) } } ,
31- { provide : NotificationsService , useValue : { } } ,
32- { provide : RequestService , useValue : { } } ,
56+ { provide : NotificationsService , useValue : notificationService } ,
57+ { provide : ScriptDataService , useValue : scriptService } ,
3358 ] ,
3459 schemas : [ NO_ERRORS_SCHEMA ] ,
3560 } ) . compileComponents ( ) ;
@@ -41,9 +66,37 @@ describe('DeleteCollectionPageComponent', () => {
4166 fixture . detectChanges ( ) ;
4267 } ) ;
4368
44- describe ( 'frontendURL' , ( ) => {
45- it ( 'should have the right frontendURL set' , ( ) => {
46- expect ( ( comp as any ) . frontendURL ) . toEqual ( '/collections/' ) ;
69+ it ( 'should create' , ( ) => {
70+ expect ( comp ) . toBeTruthy ( ) ;
71+ } ) ;
72+
73+ it ( 'should have the right frontendURL set' , ( ) => {
74+ expect ( ( comp as any ) . frontendURL ) . toEqual ( '/collections/' ) ;
75+ } ) ;
76+
77+ describe ( 'onConfirm' , ( ) => {
78+ it ( 'should invoke the deletion script with correct params, show success notification and redirect on success' , ( done ) => {
79+ const parameterValues : ProcessParameter [ ] = [
80+ Object . assign ( new ProcessParameter ( ) , { name : '-i' , value : mockCollection . uuid } ) ,
81+ ] ;
82+ ( scriptService . invoke as jasmine . Spy ) . and . returnValue ( createSuccessfulRemoteDataObject$ ( { processId : '123' } ) ) ;
83+ comp . onConfirm ( mockCollection ) ;
84+ setTimeout ( ( ) => {
85+ expect ( scriptService . invoke ) . toHaveBeenCalledWith ( DSPACE_OBJECT_DELETION_SCRIPT_NAME , parameterValues , [ ] ) ;
86+ expect ( notificationService . success ) . toHaveBeenCalledWith ( 'collection.delete.notification.success' ) ;
87+ expect ( notificationService . process ) . toHaveBeenCalledWith ( '123' , 5000 , jasmine . any ( Object ) ) ;
88+ done ( ) ;
89+ } , 0 ) ;
90+ } ) ;
91+
92+ it ( 'error notification is shown' , ( done ) => {
93+ ( scriptService . invoke as jasmine . Spy ) . and . returnValue ( createFailedRemoteDataObject$ ( 'Error' , 500 ) ) ;
94+ comp . onConfirm ( mockCollection ) ;
95+ setTimeout ( ( ) => {
96+ expect ( notificationService . error ) . toHaveBeenCalledWith ( 'collection.delete.notification.fail' ) ;
97+ done ( ) ;
98+ } , 0 ) ;
4799 } ) ;
48100 } ) ;
101+
49102} ) ;
0 commit comments