11import { CommonModule } from '@angular/common' ;
22import { HttpClient } from '@angular/common/http' ;
3- import { NO_ERRORS_SCHEMA } from '@angular/core' ;
3+ import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core' ;
44import { ComponentFixture , TestBed , waitForAsync } from '@angular/core/testing' ;
55import { UntypedFormControl , UntypedFormGroup , FormsModule , ReactiveFormsModule , Validators } from '@angular/forms' ;
66import { BrowserModule , By } from '@angular/platform-browser' ;
77import { ActivatedRoute , Router } from '@angular/router' ;
88import { NgbModule } from '@ng-bootstrap/ng-bootstrap' ;
99import { Store } from '@ngrx/store' ;
10- import { TranslateLoader , TranslateModule , TranslateService } from '@ngx-translate/core' ;
10+ import { TranslateModule } from '@ngx-translate/core' ;
1111import { Observable , of as observableOf } from 'rxjs' ;
1212import { RemoteDataBuildService } from '../../../core/cache/builders/remote-data-build.service' ;
1313import { ObjectCacheService } from '../../../core/cache/object-cache.service' ;
@@ -29,32 +29,31 @@ import { GroupMock, GroupMock2 } from '../../../shared/testing/group-mock';
2929import { GroupFormComponent } from './group-form.component' ;
3030import { createSuccessfulRemoteDataObject$ } from '../../../shared/remote-data.utils' ;
3131import { getMockFormBuilderService } from '../../../shared/mocks/form-builder-service.mock' ;
32- import { getMockTranslateService } from '../../../shared/mocks/translate.service.mock' ;
33- import { TranslateLoaderMock } from '../../../shared/testing/translate-loader.mock' ;
3432import { RouterMock } from '../../../shared/mocks/router.mock' ;
3533import { NotificationsServiceStub } from '../../../shared/testing/notifications-service.stub' ;
3634import { Operation } from 'fast-json-patch' ;
3735import { ValidateGroupExists } from './validators/group-exists.validator' ;
3836import { NoContent } from '../../../core/shared/NoContent.model' ;
3937import { DSONameService } from '../../../core/breadcrumbs/dso-name.service' ;
4038import { DSONameServiceMock } from '../../../shared/mocks/dso-name.service.mock' ;
39+ import { ActivatedRouteStub } from '../../../shared/testing/active-router.stub' ;
4140
4241describe ( 'GroupFormComponent' , ( ) => {
4342 let component : GroupFormComponent ;
4443 let fixture : ComponentFixture < GroupFormComponent > ;
45- let translateService : TranslateService ;
4644 let builderService : FormBuilderService ;
4745 let ePersonDataServiceStub : any ;
4846 let groupsDataServiceStub : any ;
4947 let dsoDataServiceStub : any ;
5048 let authorizationService : AuthorizationDataService ;
5149 let notificationService : NotificationsServiceStub ;
52- let router ;
50+ let router : RouterMock ;
51+ let route : ActivatedRouteStub ;
5352
54- let groups ;
55- let groupName ;
56- let groupDescription ;
57- let expected ;
53+ let groups : Group [ ] ;
54+ let groupName : string ;
55+ let groupDescription : string ;
56+ let expected : Group ;
5857
5958 beforeEach ( waitForAsync ( ( ) => {
6059 groups = [ GroupMock , GroupMock2 ] ;
@@ -69,6 +68,15 @@ describe('GroupFormComponent', () => {
6968 }
7069 ] ,
7170 } ,
71+ object : createSuccessfulRemoteDataObject$ ( undefined ) ,
72+ _links : {
73+ self : {
74+ href : 'group-selflink' ,
75+ } ,
76+ object : {
77+ href : 'group-objectlink' ,
78+ }
79+ } ,
7280 } ) ;
7381 ePersonDataServiceStub = { } ;
7482 groupsDataServiceStub = {
@@ -105,7 +113,14 @@ describe('GroupFormComponent', () => {
105113 create ( group : Group ) : Observable < RemoteData < Group > > {
106114 this . allGroups = [ ...this . allGroups , group ] ;
107115 this . createdGroup = Object . assign ( { } , group , {
108- _links : { self : { href : 'group-selflink' } }
116+ _links : {
117+ self : {
118+ href : 'group-selflink' ,
119+ } ,
120+ object : {
121+ href : 'group-objectlink' ,
122+ } ,
123+ } ,
109124 } ) ;
110125 return createSuccessfulRemoteDataObject$ ( this . createdGroup ) ;
111126 } ,
@@ -187,17 +202,13 @@ describe('GroupFormComponent', () => {
187202 return typeof value === 'object' && value !== null ;
188203 }
189204 } ) ;
190- translateService = getMockTranslateService ( ) ;
191205 router = new RouterMock ( ) ;
206+ route = new ActivatedRouteStub ( ) ;
192207 notificationService = new NotificationsServiceStub ( ) ;
208+
193209 return TestBed . configureTestingModule ( {
194210 imports : [ CommonModule , NgbModule , FormsModule , ReactiveFormsModule , BrowserModule ,
195- TranslateModule . forRoot ( {
196- loader : {
197- provide : TranslateLoader ,
198- useClass : TranslateLoaderMock
199- }
200- } ) ,
211+ TranslateModule . forRoot ( ) ,
201212 ] ,
202213 declarations : [ GroupFormComponent ] ,
203214 providers : [
@@ -214,14 +225,11 @@ describe('GroupFormComponent', () => {
214225 { provide : Store , useValue : { } } ,
215226 { provide : RemoteDataBuildService , useValue : { } } ,
216227 { provide : HALEndpointService , useValue : { } } ,
217- {
218- provide : ActivatedRoute ,
219- useValue : { data : observableOf ( { dso : { payload : { } } } ) , params : observableOf ( { } ) }
220- } ,
228+ { provide : ActivatedRoute , useValue : route } ,
221229 { provide : Router , useValue : router } ,
222230 { provide : AuthorizationDataService , useValue : authorizationService } ,
223231 ] ,
224- schemas : [ NO_ERRORS_SCHEMA ]
232+ schemas : [ CUSTOM_ELEMENTS_SCHEMA ] ,
225233 } ) . compileComponents ( ) ;
226234 } ) ) ;
227235
@@ -234,23 +242,31 @@ describe('GroupFormComponent', () => {
234242 describe ( 'when submitting the form' , ( ) => {
235243 beforeEach ( ( ) => {
236244 spyOn ( component . submitForm , 'emit' ) ;
237- component . groupName . value = groupName ;
238- component . groupDescription . value = groupDescription ;
245+ component . groupName . setValue ( groupName ) ;
246+ component . groupDescription . setValue ( groupDescription ) ;
239247 } ) ;
240248 describe ( 'without active Group' , ( ) => {
241249 beforeEach ( ( ) => {
242250 component . onSubmit ( ) ;
243251 fixture . detectChanges ( ) ;
244252 } ) ;
245253
246- it ( 'should emit a new group using the correct values' , ( async ( ) => {
247- await fixture . whenStable ( ) . then ( ( ) => {
248- expect ( component . submitForm . emit ) . toHaveBeenCalledWith ( expected ) ;
249- } ) ;
254+ it ( 'should emit a new group using the correct values' , ( ( ) => {
255+ expect ( component . submitForm . emit ) . toHaveBeenCalledWith ( jasmine . objectContaining ( {
256+ name : groupName ,
257+ metadata : {
258+ 'dc.description' : [
259+ {
260+ value : groupDescription ,
261+ } ,
262+ ] ,
263+ } ,
264+ } ) ) ;
250265 } ) ) ;
251266 } ) ;
267+
252268 describe ( 'with active Group' , ( ) => {
253- let expected2 ;
269+ let expected2 : Group ;
254270 beforeEach ( ( ) => {
255271 expected2 = Object . assign ( new Group ( ) , {
256272 name : 'newGroupName' ,
@@ -261,15 +277,24 @@ describe('GroupFormComponent', () => {
261277 }
262278 ] ,
263279 } ,
280+ object : createSuccessfulRemoteDataObject$ ( undefined ) ,
281+ _links : {
282+ self : {
283+ href : 'group-selflink' ,
284+ } ,
285+ object : {
286+ href : 'group-objectlink' ,
287+ } ,
288+ } ,
264289 } ) ;
265290 spyOn ( groupsDataServiceStub , 'getActiveGroup' ) . and . returnValue ( observableOf ( expected ) ) ;
266291 spyOn ( groupsDataServiceStub , 'patch' ) . and . returnValue ( createSuccessfulRemoteDataObject$ ( expected2 ) ) ;
267- component . groupName . value = 'newGroupName' ;
268- component . onSubmit ( ) ;
269- fixture . detectChanges ( ) ;
292+ component . ngOnInit ( ) ;
270293 } ) ;
271294
272295 it ( 'should edit with name and description operations' , ( ) => {
296+ component . groupName . setValue ( 'newGroupName' ) ;
297+ component . onSubmit ( ) ;
273298 const operations = [ {
274299 op : 'add' ,
275300 path : '/metadata/dc.description' ,
@@ -283,9 +308,8 @@ describe('GroupFormComponent', () => {
283308 } ) ;
284309
285310 it ( 'should edit with description operations' , ( ) => {
286- component . groupName . value = null ;
311+ component . groupName . setValue ( null ) ;
287312 component . onSubmit ( ) ;
288- fixture . detectChanges ( ) ;
289313 const operations = [ {
290314 op : 'add' ,
291315 path : '/metadata/dc.description' ,
@@ -295,9 +319,9 @@ describe('GroupFormComponent', () => {
295319 } ) ;
296320
297321 it ( 'should edit with name operations' , ( ) => {
298- component . groupDescription . value = null ;
322+ component . groupName . setValue ( 'newGroupName' ) ;
323+ component . groupDescription . setValue ( null ) ;
299324 component . onSubmit ( ) ;
300- fixture . detectChanges ( ) ;
301325 const operations = [ {
302326 op : 'replace' ,
303327 path : '/name' ,
@@ -306,12 +330,13 @@ describe('GroupFormComponent', () => {
306330 expect ( groupsDataServiceStub . patch ) . toHaveBeenCalledWith ( expected , operations ) ;
307331 } ) ;
308332
309- it ( 'should emit the existing group using the correct new values' , ( async ( ) => {
310- await fixture . whenStable ( ) . then ( ( ) => {
311- expect ( component . submitForm . emit ) . toHaveBeenCalledWith ( expected2 ) ;
312- } ) ;
313- } ) ) ;
333+ it ( 'should emit the existing group using the correct new values' , ( ) => {
334+ component . onSubmit ( ) ;
335+ expect ( component . submitForm . emit ) . toHaveBeenCalledWith ( expected2 ) ;
336+ } ) ;
337+
314338 it ( 'should emit success notification' , ( ) => {
339+ component . onSubmit ( ) ;
315340 expect ( notificationService . success ) . toHaveBeenCalled ( ) ;
316341 } ) ;
317342 } ) ;
@@ -326,11 +351,8 @@ describe('GroupFormComponent', () => {
326351
327352
328353 describe ( 'check form validation' , ( ) => {
329- let groupCommunity ;
330-
331354 beforeEach ( ( ) => {
332355 groupName = 'testName' ;
333- groupCommunity = 'testgroupCommunity' ;
334356 groupDescription = 'testgroupDescription' ;
335357
336358 expected = Object . assign ( new Group ( ) , {
@@ -342,8 +364,17 @@ describe('GroupFormComponent', () => {
342364 }
343365 ] ,
344366 } ,
367+ _links : {
368+ self : {
369+ href : 'group-selflink' ,
370+ } ,
371+ object : {
372+ href : 'group-objectlink' ,
373+ } ,
374+ } ,
345375 } ) ;
346376 spyOn ( component . submitForm , 'emit' ) ;
377+ spyOn ( dsoDataServiceStub , 'findByHref' ) . and . returnValue ( observableOf ( expected ) ) ;
347378
348379 fixture . detectChanges ( ) ;
349380 component . initialisePage ( ) ;
@@ -393,21 +424,20 @@ describe('GroupFormComponent', () => {
393424 } ) ;
394425
395426 describe ( 'delete' , ( ) => {
396- let deleteButton ;
397-
398- beforeEach ( ( ) => {
399- component . initialisePage ( ) ;
427+ let deleteButton : HTMLButtonElement ;
400428
429+ beforeEach ( async ( ) => {
430+ spyOn ( groupsDataServiceStub , 'delete' ) . and . callThrough ( ) ;
431+ component . activeGroup$ = observableOf ( {
432+ id : 'active-group' ,
433+ permanent : false ,
434+ } as Group ) ;
401435 component . canEdit$ = observableOf ( true ) ;
402- component . groupBeingEdited = {
403- permanent : false
404- } as Group ;
436+
437+ component . initialisePage ( ) ;
405438
406439 fixture . detectChanges ( ) ;
407440 deleteButton = fixture . debugElement . query ( By . css ( '.delete-button' ) ) . nativeElement ;
408-
409- spyOn ( groupsDataServiceStub , 'delete' ) . and . callThrough ( ) ;
410- spyOn ( groupsDataServiceStub , 'getActiveGroup' ) . and . returnValue ( observableOf ( { id : 'active-group' } ) ) ;
411441 } ) ;
412442
413443 describe ( 'if confirmed via modal' , ( ) => {
0 commit comments