11import { CommonModule } from '@angular/common' ;
22import { NO_ERRORS_SCHEMA } from '@angular/core' ;
3- import { ComponentFixture , TestBed , waitForAsync } from '@angular/core/testing' ;
4- import { ActivatedRoute } from '@angular/router' ;
3+ import {
4+ ComponentFixture ,
5+ TestBed ,
6+ waitForAsync ,
7+ } from '@angular/core/testing' ;
8+ import {
9+ ActivatedRoute ,
10+ Router ,
11+ } from '@angular/router' ;
512import { RouterTestingModule } from '@angular/router/testing' ;
613import { TranslateModule } from '@ngx-translate/core' ;
7- import { of as observableOf } from 'rxjs' ;
8- import { CommunityDataService } from '../../core/data/community-data.service' ;
14+ import { of } from 'rxjs' ;
15+
16+ import {
17+ DSPACE_OBJECT_DELETION_SCRIPT_NAME ,
18+ ScriptDataService ,
19+ } from '../../core/data/processes/script-data.service' ;
20+ import { Community } from '../../core/shared/community.model' ;
21+ import { ProcessParameter } from '../../process-page/processes/process-parameter.model' ;
922import { NotificationsService } from '../../shared/notifications/notifications.service' ;
10- import { SharedModule } from '../../shared/shared.module' ;
23+ import {
24+ createFailedRemoteDataObject$ ,
25+ createSuccessfulRemoteDataObject$ ,
26+ } from '../../shared/remote-data.utils' ;
27+ import { NotificationsServiceStub } from '../../shared/testing/notifications-service.stub' ;
1128import { DeleteCommunityPageComponent } from './delete-community-page.component' ;
12- import { RequestService } from '../../core/data/request.service' ;
1329import { DSONameService } from '../../core/breadcrumbs/dso-name.service' ;
1430import { DSONameServiceMock } from '../../shared/mocks/dso-name.service.mock' ;
31+ import { SharedModule } from '../../shared/shared.module' ;
32+ import { CommunityDataService } from '../../core/data/community-data.service' ;
1533
1634describe ( 'DeleteCommunityPageComponent' , ( ) => {
1735 let comp : DeleteCommunityPageComponent ;
1836 let fixture : ComponentFixture < DeleteCommunityPageComponent > ;
37+ let scriptService ;
38+ let notificationService : NotificationsServiceStub ;
39+ let router ;
40+
41+ const mockCommunity : Community = Object . assign ( new Community ( ) , {
42+ uuid : 'test-uuid' ,
43+ id : 'test-uuid' ,
44+ name : 'Test Community' ,
45+ type : 'community' ,
46+ } ) ;
1947
2048 beforeEach ( waitForAsync ( ( ) => {
49+ notificationService = new NotificationsServiceStub ( ) ;
50+ scriptService = jasmine . createSpyObj ( 'scriptService' , {
51+ invoke : createSuccessfulRemoteDataObject$ ( { processId : '123' } ) ,
52+ } ) ;
53+ router = jasmine . createSpyObj ( 'router' , [ 'navigateByUrl' , 'navigate' ] ) ;
2154 TestBed . configureTestingModule ( {
2255 imports : [ TranslateModule . forRoot ( ) , SharedModule , CommonModule , RouterTestingModule ] ,
2356 declarations : [ DeleteCommunityPageComponent ] ,
2457 providers : [
2558 { provide : DSONameService , useValue : new DSONameServiceMock ( ) } ,
2659 { provide : CommunityDataService , useValue : { } } ,
27- { provide : ActivatedRoute , useValue : { data : observableOf ( { dso : { payload : { } } } ) } } ,
28- { provide : NotificationsService , useValue : { } } ,
29- { provide : RequestService , useValue : { } }
60+ { provide : ActivatedRoute , useValue : { data : of ( { dso : { payload : mockCommunity } } ) } } ,
61+ { provide : NotificationsService , useValue : notificationService } ,
62+ { provide : ScriptDataService , useValue : scriptService } ,
63+ { provide : Router , useValue : router } ,
3064 ] ,
3165 schemas : [ NO_ERRORS_SCHEMA ]
3266 } ) . compileComponents ( ) ;
@@ -38,9 +72,37 @@ describe('DeleteCommunityPageComponent', () => {
3872 fixture . detectChanges ( ) ;
3973 } ) ;
4074
41- describe ( 'frontendURL' , ( ) => {
42- it ( 'should have the right frontendURL set' , ( ) => {
43- expect ( ( comp as any ) . frontendURL ) . toEqual ( '/communities/' ) ;
75+ it ( 'should create' , ( ) => {
76+ expect ( comp ) . toBeTruthy ( ) ;
77+ } ) ;
78+
79+ it ( 'should have the right frontendURL set' , ( ) => {
80+ expect ( ( comp as any ) . frontendURL ) . toEqual ( '/communities/' ) ;
81+ } ) ;
82+
83+ describe ( 'onConfirm' , ( ) => {
84+ it ( 'should invoke the deletion script with correct params, show success notification and redirect on success' , ( done ) => {
85+ const parameterValues : ProcessParameter [ ] = [
86+ Object . assign ( new ProcessParameter ( ) , { name : '-i' , value : mockCommunity . uuid } ) ,
87+ ] ;
88+ ( scriptService . invoke as jasmine . Spy ) . and . returnValue ( createSuccessfulRemoteDataObject$ ( { processId : '123' } ) ) ;
89+ comp . onConfirm ( mockCommunity ) ;
90+ setTimeout ( ( ) => {
91+ expect ( scriptService . invoke ) . toHaveBeenCalledWith ( DSPACE_OBJECT_DELETION_SCRIPT_NAME , parameterValues , [ ] ) ;
92+ expect ( notificationService . success ) . toHaveBeenCalledWith ( 'community.delete.notification.success' ) ;
93+ expect ( notificationService . process ) . toHaveBeenCalledWith ( '123' , 5000 , jasmine . any ( Object ) ) ;
94+ done ( ) ;
95+ } , 0 ) ;
96+ } ) ;
97+
98+ it ( 'error notification is shown' , ( done ) => {
99+ ( scriptService . invoke as jasmine . Spy ) . and . returnValue ( createFailedRemoteDataObject$ ( 'Error' , 500 ) ) ;
100+ comp . onConfirm ( mockCommunity ) ;
101+ setTimeout ( ( ) => {
102+ expect ( notificationService . error ) . toHaveBeenCalledWith ( 'community.delete.notification.fail' ) ;
103+ done ( ) ;
104+ } , 0 ) ;
44105 } ) ;
45106 } ) ;
107+
46108} ) ;
0 commit comments