Skip to content

Commit 680ed3b

Browse files
117287: Removed method calls returning observables from the Group form
1 parent d3019e4 commit 680ed3b

3 files changed

Lines changed: 256 additions & 250 deletions

File tree

src/app/access-control/group-registry/group-form/group-form.component.html

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="group-form row">
33
<div class="col-12">
44

5-
<div *ngIf="groupDataService.getActiveGroup() | async; then editheader; else createHeader"></div>
5+
<div *ngIf="activeGroup$ | async; then editheader; else createHeader"></div>
66

77
<ng-template #createHeader>
88
<h2 class="border-bottom pb-2">{{messagePrefix + '.head.create' | translate}}</h2>
@@ -23,11 +23,15 @@ <h2 class="border-bottom pb-2">
2323
</h2>
2424
</ng-template>
2525

26-
<ds-alert *ngIf="groupBeingEdited?.permanent" [type]="AlertTypeEnum.Warning"
27-
[content]="messagePrefix + '.alert.permanent'"></ds-alert>
28-
<ds-alert *ngIf="!(canEdit$ | async) && (groupDataService.getActiveGroup() | async)" [type]="AlertTypeEnum.Warning"
29-
[content]="(messagePrefix + '.alert.workflowGroup' | translate:{ name: dsoNameService.getName((getLinkedDSO(groupBeingEdited) | async)?.payload), comcol: (getLinkedDSO(groupBeingEdited) | async)?.payload?.type, comcolEditRolesRoute: (getLinkedEditRolesRoute(groupBeingEdited) | async) })">
30-
</ds-alert>
26+
<ng-container *ngIf="(activeGroup$ | async) as groupBeingEdited">
27+
<ds-alert *ngIf="groupBeingEdited?.permanent" [type]="AlertType.Warning"
28+
[content]="messagePrefix + '.alert.permanent'"></ds-alert>
29+
<ng-container *ngIf="(activeGroupLinkedDSO$ | async) as activeGroupLinkedDSO">
30+
<ds-alert *ngIf="!(canEdit$ | async)" [type]="AlertType.Warning"
31+
[content]="(messagePrefix + '.alert.workflowGroup' | translate:{ name: dsoNameService.getName(activeGroupLinkedDSO), comcol: activeGroupLinkedDSO.type, comcolEditRolesRoute: (linkedEditRolesRoute$ | async) })">
32+
</ds-alert>
33+
</ng-container>
34+
</ng-container>
3135

3236
<ds-form [formId]="formId"
3337
[formModel]="formModel"
@@ -39,23 +43,22 @@ <h2 class="border-bottom pb-2">
3943
<button (click)="onCancel()"
4044
class="btn btn-outline-secondary"><i class="fas fa-arrow-left"></i> {{messagePrefix + '.return' | translate}}</button>
4145
</div>
42-
<div after *ngIf="groupBeingEdited != null" class="btn-group">
46+
<div after *ngIf="(activeGroup$ | async) as groupBeingEdited" class="btn-group">
4347
<button class="btn btn-danger delete-button" [disabled]="!(canEdit$ | async) || groupBeingEdited.permanent"
4448
(click)="delete()">
4549
<i class="fa fa-trash"></i> {{ messagePrefix + '.actions.delete' | translate}}
4650
</button>
4751
</div>
4852
</ds-form>
4953

50-
<div class="mb-5">
51-
<ds-members-list *ngIf="groupBeingEdited != null"
52-
[messagePrefix]="messagePrefix + '.members-list'"></ds-members-list>
53-
</div>
54-
<ds-subgroups-list *ngIf="groupBeingEdited != null"
55-
[messagePrefix]="messagePrefix + '.subgroups-list'"></ds-subgroups-list>
56-
57-
58-
54+
<ng-container *ngIf="(activeGroup$ | async) as groupBeingEdited">
55+
<div class="mb-5">
56+
<ds-members-list *ngIf="groupBeingEdited != null"
57+
[messagePrefix]="messagePrefix + '.members-list'"></ds-members-list>
58+
</div>
59+
<ds-subgroups-list *ngIf="groupBeingEdited != null"
60+
[messagePrefix]="messagePrefix + '.subgroups-list'"></ds-subgroups-list>
61+
</ng-container>
5962
</div>
6063
</div>
6164
</div>

src/app/access-control/group-registry/group-form/group-form.component.spec.ts

Lines changed: 85 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { CommonModule } from '@angular/common';
22
import { HttpClient } from '@angular/common/http';
3-
import { NO_ERRORS_SCHEMA } from '@angular/core';
3+
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
44
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
55
import { UntypedFormControl, UntypedFormGroup, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms';
66
import { BrowserModule, By } from '@angular/platform-browser';
77
import { ActivatedRoute, Router } from '@angular/router';
88
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
99
import { Store } from '@ngrx/store';
10-
import { TranslateLoader, TranslateModule, TranslateService } from '@ngx-translate/core';
10+
import { TranslateModule } from '@ngx-translate/core';
1111
import { Observable, of as observableOf } from 'rxjs';
1212
import { RemoteDataBuildService } from '../../../core/cache/builders/remote-data-build.service';
1313
import { ObjectCacheService } from '../../../core/cache/object-cache.service';
@@ -29,32 +29,31 @@ import { GroupMock, GroupMock2 } from '../../../shared/testing/group-mock';
2929
import { GroupFormComponent } from './group-form.component';
3030
import { createSuccessfulRemoteDataObject$ } from '../../../shared/remote-data.utils';
3131
import { 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';
3432
import { RouterMock } from '../../../shared/mocks/router.mock';
3533
import { NotificationsServiceStub } from '../../../shared/testing/notifications-service.stub';
3634
import { Operation } from 'fast-json-patch';
3735
import { ValidateGroupExists } from './validators/group-exists.validator';
3836
import { NoContent } from '../../../core/shared/NoContent.model';
3937
import { DSONameService } from '../../../core/breadcrumbs/dso-name.service';
4038
import { DSONameServiceMock } from '../../../shared/mocks/dso-name.service.mock';
39+
import { ActivatedRouteStub } from '../../../shared/testing/active-router.stub';
4140

4241
describe('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

Comments
 (0)