Skip to content

Commit 64f968b

Browse files
committed
Fix subgroups-list specs so they align with new members-list specs
1 parent b598f1b commit 64f968b

File tree

1 file changed

+102
-82
lines changed

1 file changed

+102
-82
lines changed

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

+102-82
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { CommonModule } from '@angular/common';
22
import { NO_ERRORS_SCHEMA, DebugElement } from '@angular/core';
3-
import { ComponentFixture, fakeAsync, flush, inject, TestBed, tick, waitForAsync } from '@angular/core/testing';
3+
import { ComponentFixture, fakeAsync, flush, inject, TestBed, waitForAsync } from '@angular/core/testing';
44
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
55
import { BrowserModule, By } from '@angular/platform-browser';
66
import { Router } from '@angular/router';
77
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
88
import { TranslateLoader, TranslateModule, TranslateService } from '@ngx-translate/core';
9-
import { Observable, of as observableOf, BehaviorSubject } from 'rxjs';
9+
import { Observable, of as observableOf } from 'rxjs';
1010
import { RestResponse } from '../../../../core/cache/response.models';
1111
import { buildPaginatedList, PaginatedList } from '../../../../core/data/paginated-list.model';
1212
import { RemoteData } from '../../../../core/data/remote-data';
@@ -18,19 +18,18 @@ import { NotificationsService } from '../../../../shared/notifications/notificat
1818
import { GroupMock, GroupMock2 } from '../../../../shared/testing/group-mock';
1919
import { SubgroupsListComponent } from './subgroups-list.component';
2020
import {
21-
createSuccessfulRemoteDataObject$,
22-
createSuccessfulRemoteDataObject
21+
createSuccessfulRemoteDataObject$
2322
} from '../../../../shared/remote-data.utils';
2423
import { RouterMock } from '../../../../shared/mocks/router.mock';
2524
import { getMockFormBuilderService } from '../../../../shared/mocks/form-builder-service.mock';
2625
import { getMockTranslateService } from '../../../../shared/mocks/translate.service.mock';
2726
import { TranslateLoaderMock } from '../../../../shared/testing/translate-loader.mock';
2827
import { NotificationsServiceStub } from '../../../../shared/testing/notifications-service.stub';
29-
import { map } from 'rxjs/operators';
3028
import { PaginationService } from '../../../../core/pagination/pagination.service';
3129
import { PaginationServiceStub } from '../../../../shared/testing/pagination-service.stub';
3230
import { DSONameService } from '../../../../core/breadcrumbs/dso-name.service';
3331
import { DSONameServiceMock } from '../../../../shared/mocks/dso-name.service.mock';
32+
import { EPersonMock2 } from 'src/app/shared/testing/eperson.mock';
3433

3534
describe('SubgroupsListComponent', () => {
3635
let component: SubgroupsListComponent;
@@ -39,44 +38,70 @@ describe('SubgroupsListComponent', () => {
3938
let builderService: FormBuilderService;
4039
let ePersonDataServiceStub: any;
4140
let groupsDataServiceStub: any;
42-
let activeGroup;
41+
let activeGroup: Group;
4342
let subgroups: Group[];
44-
let allGroups: Group[];
43+
let groupNonMembers: Group[];
4544
let routerStub;
4645
let paginationService;
46+
// Define a new mock activegroup for all tests below
47+
let mockActiveGroup: Group = Object.assign(new Group(), {
48+
handle: null,
49+
subgroups: [GroupMock2],
50+
epersons: [EPersonMock2],
51+
selfRegistered: false,
52+
permanent: false,
53+
_links: {
54+
self: {
55+
href: 'https://rest.api/server/api/eperson/groups/activegroupid',
56+
},
57+
subgroups: { href: 'https://rest.api/server/api/eperson/groups/activegroupid/subgroups' },
58+
object: { href: 'https://rest.api/server/api/eperson/groups/activegroupid/object' },
59+
epersons: { href: 'https://rest.api/server/api/eperson/groups/activegroupid/epersons' }
60+
},
61+
_name: 'activegroupname',
62+
id: 'activegroupid',
63+
uuid: 'activegroupid',
64+
type: 'group',
65+
});
4766

4867
beforeEach(waitForAsync(() => {
49-
activeGroup = GroupMock;
68+
activeGroup = mockActiveGroup;
5069
subgroups = [GroupMock2];
51-
allGroups = [GroupMock, GroupMock2];
70+
groupNonMembers = [GroupMock];
5271
ePersonDataServiceStub = {};
5372
groupsDataServiceStub = {
5473
activeGroup: activeGroup,
55-
subgroups$: new BehaviorSubject(subgroups),
74+
subgroups: subgroups,
75+
groupNonMembers: groupNonMembers,
5676
getActiveGroup(): Observable<Group> {
5777
return observableOf(this.activeGroup);
5878
},
5979
getSubgroups(): Group {
60-
return this.activeGroup;
80+
return this.subgroups;
6181
},
82+
// This method is used to get all the current subgroups
6283
findListByHref(_href: string): Observable<RemoteData<PaginatedList<Group>>> {
63-
return this.subgroups$.pipe(
64-
map((currentGroups: Group[]) => {
65-
return createSuccessfulRemoteDataObject(buildPaginatedList<Group>(new PageInfo(), currentGroups));
66-
})
67-
);
84+
return createSuccessfulRemoteDataObject$(buildPaginatedList<Group>(new PageInfo(), groupsDataServiceStub.getSubgroups()));
6885
},
6986
getGroupEditPageRouterLink(group: Group): string {
7087
return '/access-control/groups/' + group.id;
7188
},
89+
// This method is used to get all groups which are NOT currently a subgroup member
7290
searchGroups(query: string): Observable<RemoteData<PaginatedList<Group>>> {
7391
if (query === '') {
74-
return createSuccessfulRemoteDataObject$(buildPaginatedList(new PageInfo(), allGroups));
92+
return createSuccessfulRemoteDataObject$(buildPaginatedList(new PageInfo(), groupNonMembers));
7593
}
7694
return createSuccessfulRemoteDataObject$(buildPaginatedList(new PageInfo(), []));
7795
},
78-
addSubGroupToGroup(parentGroup, subgroup: Group): Observable<RestResponse> {
79-
this.subgroups$.next([...this.subgroups$.getValue(), subgroup]);
96+
addSubGroupToGroup(parentGroup, subgroupToAdd: Group): Observable<RestResponse> {
97+
// Add group to list of subgroups
98+
this.subgroups = [...this.subgroups, subgroupToAdd];
99+
// Remove group from list of non-members
100+
this.groupNonMembers.forEach( (group: Group, index: number) => {
101+
if (group.id === subgroupToAdd.id) {
102+
this.groupNonMembers.splice(index, 1);
103+
}
104+
});
80105
return observableOf(new RestResponse(true, 200, 'Success'));
81106
},
82107
clearGroupsRequests() {
@@ -85,12 +110,15 @@ describe('SubgroupsListComponent', () => {
85110
clearGroupLinkRequests() {
86111
// empty
87112
},
88-
deleteSubGroupFromGroup(parentGroup, subgroup: Group): Observable<RestResponse> {
89-
this.subgroups$.next(this.subgroups$.getValue().filter((group: Group) => {
90-
if (group.id !== subgroup.id) {
91-
return group;
113+
deleteSubGroupFromGroup(parentGroup, subgroupToDelete: Group): Observable<RestResponse> {
114+
// Remove group from list of subgroups
115+
this.subgroups.forEach( (group: Group, index: number) => {
116+
if (group.id === subgroupToDelete.id) {
117+
this.subgroups.splice(index, 1);
92118
}
93-
}));
119+
});
120+
// Add group to list of non-members
121+
this.groupNonMembers = [...this.groupNonMembers, subgroupToDelete];
94122
return observableOf(new RestResponse(true, 200, 'Success'));
95123
}
96124
};
@@ -99,7 +127,7 @@ describe('SubgroupsListComponent', () => {
99127
translateService = getMockTranslateService();
100128

101129
paginationService = new PaginationServiceStub();
102-
TestBed.configureTestingModule({
130+
return TestBed.configureTestingModule({
103131
imports: [CommonModule, NgbModule, FormsModule, ReactiveFormsModule, BrowserModule,
104132
TranslateModule.forRoot({
105133
loader: {
@@ -137,30 +165,38 @@ describe('SubgroupsListComponent', () => {
137165
expect(comp).toBeDefined();
138166
}));
139167

140-
it('should show list of subgroups of current active group', () => {
141-
const groupIdsFound = fixture.debugElement.queryAll(By.css('#subgroupsOfGroup tr td:first-child'));
142-
expect(groupIdsFound.length).toEqual(1);
143-
activeGroup.subgroups.map((group: Group) => {
144-
expect(groupIdsFound.find((foundEl) => {
145-
return (foundEl.nativeElement.textContent.trim() === group.uuid);
146-
})).toBeTruthy();
168+
describe('current subgroup list', () => {
169+
it('should show list of subgroups of current active group', () => {
170+
const groupIdsFound = fixture.debugElement.queryAll(By.css('#subgroupsOfGroup tr td:first-child'));
171+
expect(groupIdsFound.length).toEqual(1);
172+
subgroups.map((group: Group) => {
173+
expect(groupIdsFound.find((foundEl) => {
174+
return (foundEl.nativeElement.textContent.trim() === group.uuid);
175+
})).toBeTruthy();
176+
});
147177
});
148-
});
149178

150-
describe('if first group delete button is pressed', () => {
151-
let groupsFound: DebugElement[];
152-
beforeEach(fakeAsync(() => {
153-
const addButton = fixture.debugElement.query(By.css('#subgroupsOfGroup tbody .deleteButton'));
154-
addButton.triggerEventHandler('click', {
155-
preventDefault: () => {/**/
156-
}
179+
it('should show a delete button next to each subgroup', () => {
180+
const subgroupsFound = fixture.debugElement.queryAll(By.css('#subgroupsOfGroup tbody tr'));
181+
subgroupsFound.map((foundGroupRowElement: DebugElement) => {
182+
const addButton: DebugElement = foundGroupRowElement.query(By.css('td:last-child .fa-plus'));
183+
const deleteButton: DebugElement = foundGroupRowElement.query(By.css('td:last-child .fa-trash-alt'));
184+
expect(addButton).toBeNull();
185+
expect(deleteButton).not.toBeNull();
186+
});
187+
});
188+
189+
describe('if first group delete button is pressed', () => {
190+
let groupsFound: DebugElement[];
191+
beforeEach(() => {
192+
const deleteButton = fixture.debugElement.query(By.css('#subgroupsOfGroup tbody .deleteButton'));
193+
deleteButton.nativeElement.click();
194+
fixture.detectChanges();
195+
});
196+
it('then no subgroup remains as a member of the active group', () => {
197+
groupsFound = fixture.debugElement.queryAll(By.css('#subgroupsOfGroup tbody tr'));
198+
expect(groupsFound.length).toEqual(0);
157199
});
158-
tick();
159-
fixture.detectChanges();
160-
}));
161-
it('one less subgroup in list from 1 to 0 (of 2 total groups)', () => {
162-
groupsFound = fixture.debugElement.queryAll(By.css('#subgroupsOfGroup tbody tr'));
163-
expect(groupsFound.length).toEqual(0);
164200
});
165201
});
166202

@@ -169,54 +205,38 @@ describe('SubgroupsListComponent', () => {
169205
let groupsFound: DebugElement[];
170206
beforeEach(fakeAsync(() => {
171207
component.search({ query: '' });
208+
fixture.detectChanges();
172209
groupsFound = fixture.debugElement.queryAll(By.css('#groupsSearch tbody tr'));
173210
}));
174211

175-
it('should display all groups', () => {
176-
fixture.detectChanges();
177-
groupsFound = fixture.debugElement.queryAll(By.css('#groupsSearch tbody tr'));
178-
expect(groupsFound.length).toEqual(2);
179-
groupsFound = fixture.debugElement.queryAll(By.css('#groupsSearch tbody tr'));
212+
it('should display only non-member groups (i.e. groups that are not a subgroup)', () => {
180213
const groupIdsFound: DebugElement[] = fixture.debugElement.queryAll(By.css('#groupsSearch tbody tr td:first-child'));
181-
allGroups.map((group: Group) => {
214+
expect(groupIdsFound.length).toEqual(1);
215+
groupNonMembers.map((group: Group) => {
182216
expect(groupIdsFound.find((foundEl: DebugElement) => {
183217
return (foundEl.nativeElement.textContent.trim() === group.uuid);
184218
})).toBeTruthy();
185219
});
186220
});
187221

188-
describe('if group is already a subgroup', () => {
189-
it('should have delete button, else it should have add button', () => {
222+
it('should display an add button next to non-member groups, not a delete button', () => {
223+
groupsFound.map((foundGroupRowElement: DebugElement) => {
224+
const addButton: DebugElement = foundGroupRowElement.query(By.css('td:last-child .fa-plus'));
225+
const deleteButton: DebugElement = foundGroupRowElement.query(By.css('td:last-child .fa-trash-alt'));
226+
expect(addButton).not.toBeNull();
227+
expect(deleteButton).toBeNull();
228+
});
229+
});
230+
231+
describe('if first add button is pressed', () => {
232+
beforeEach(() => {
233+
const addButton: DebugElement = fixture.debugElement.query(By.css('#groupsSearch tbody .fa-plus'));
234+
addButton.nativeElement.click();
190235
fixture.detectChanges();
236+
});
237+
it('then all (two) Groups are subgroups of the active group. No non-members left', () => {
191238
groupsFound = fixture.debugElement.queryAll(By.css('#groupsSearch tbody tr'));
192-
const getSubgroups = groupsDataServiceStub.getSubgroups().subgroups;
193-
if (getSubgroups !== undefined && getSubgroups.length > 0) {
194-
groupsFound.map((foundGroupRowElement: DebugElement) => {
195-
const groupId: DebugElement = foundGroupRowElement.query(By.css('td:first-child'));
196-
const addButton: DebugElement = foundGroupRowElement.query(By.css('td:last-child .fa-plus'));
197-
const deleteButton: DebugElement = foundGroupRowElement.query(By.css('td:last-child .fa-trash-alt'));
198-
expect(addButton).toBeNull();
199-
if (activeGroup.id === groupId.nativeElement.textContent) {
200-
expect(deleteButton).toBeNull();
201-
} else {
202-
expect(deleteButton).not.toBeNull();
203-
}
204-
});
205-
} else {
206-
const subgroupIds: string[] = activeGroup.subgroups.map((group: Group) => group.id);
207-
groupsFound.map((foundGroupRowElement: DebugElement) => {
208-
const groupId: DebugElement = foundGroupRowElement.query(By.css('td:first-child'));
209-
const addButton: DebugElement = foundGroupRowElement.query(By.css('td:last-child .fa-plus'));
210-
const deleteButton: DebugElement = foundGroupRowElement.query(By.css('td:last-child .fa-trash-alt'));
211-
if (subgroupIds.includes(groupId.nativeElement.textContent)) {
212-
expect(addButton).toBeNull();
213-
expect(deleteButton).not.toBeNull();
214-
} else {
215-
expect(deleteButton).toBeNull();
216-
expect(addButton).not.toBeNull();
217-
}
218-
});
219-
}
239+
expect(groupsFound.length).toEqual(0);
220240
});
221241
});
222242
});

0 commit comments

Comments
 (0)