@@ -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 { DSONameService } from '@dspace/core/breadcrumbs/dso-name.service' ;
1114import { CollectionDataService } from '@dspace/core/data/collection-data.service' ;
12- import { RequestService } from '@dspace/core/data/request.service' ;
1315import { NotificationsService } from '@dspace/core/notification-system/notifications.service' ;
16+ import { ProcessParameter } from '@dspace/core/processes/process-parameter.model' ;
1417import { DSONameServiceMock } from '@dspace/core/testing/dso-name.service.mock' ;
18+ import { NotificationsServiceStub } from '@dspace/core/testing/notifications-service.stub' ;
19+ import {
20+ createFailedRemoteDataObject$ ,
21+ createSuccessfulRemoteDataObject$ ,
22+ } from '@dspace/core/utilities/remote-data.utils' ;
1523import { TranslateModule } from '@ngx-translate/core' ;
1624import { of } from 'rxjs' ;
1725
26+ import {
27+ DSPACE_OBJECT_DELETION_SCRIPT_NAME ,
28+ ScriptDataService ,
29+ } from '../../core/data/processes/script-data.service' ;
30+ import { Collection } from '../../core/shared/collection.model' ;
31+ import { getProcessDetailRoute } from '../../process-page/process-page-routing.paths' ;
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' , 'navigate' ] ) ;
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,38 @@ 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+ comp . onConfirm ( mockCollection ) ;
89+ setTimeout ( ( ) => {
90+ expect ( scriptService . invoke ) . toHaveBeenCalledWith ( DSPACE_OBJECT_DELETION_SCRIPT_NAME , parameterValues , [ ] ) ;
91+ expect ( notificationService . success ) . toHaveBeenCalledWith ( 'collection.delete.notification.success' ) ;
92+ expect ( router . navigateByUrl ) . toHaveBeenCalledWith ( getProcessDetailRoute ( '123' ) ) ;
93+ done ( ) ;
94+ } , 0 ) ;
95+ } ) ;
96+
97+ it ( 'error notification is shown' , ( done ) => {
98+ ( scriptService . invoke as jasmine . Spy ) . and . returnValue ( createFailedRemoteDataObject$ ( 'Error' , 500 ) ) ;
99+ comp . onConfirm ( mockCollection ) ;
100+ setTimeout ( ( ) => {
101+ expect ( notificationService . error ) . toHaveBeenCalledWith ( 'collection.delete.notification.fail' ) ;
102+ expect ( router . navigate ) . toHaveBeenCalledWith ( [ '/' ] ) ;
103+ done ( ) ;
104+ } , 0 ) ;
47105 } ) ;
48106 } ) ;
107+
49108} ) ;
0 commit comments