Skip to content

Commit 8c55e89

Browse files
committed
[DURACOM-191] Fix eperson-group-list.component which still used getDataServiceFor decorator
1 parent 7472e69 commit 8c55e89

2 files changed

Lines changed: 79 additions & 64 deletions

File tree

src/app/shared/eperson-group-list/eperson-group-list.component.spec.ts

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from '@angular/core';
77
import {
88
ComponentFixture,
9+
fakeAsync,
910
inject,
1011
TestBed,
1112
waitForAsync,
@@ -15,19 +16,15 @@ import { TranslateModule } from '@ngx-translate/core';
1516
import { cold } from 'jasmine-marbles';
1617
import uniqueId from 'lodash/uniqueId';
1718
import { of as observableOf } from 'rxjs';
18-
import { DSONameService } from 'src/app/core/breadcrumbs/dso-name.service';
19-
import {
20-
dataService,
21-
getDataServiceFor,
22-
} from 'src/app/core/data/base/data-service.decorator';
23-
import { EPERSON } from 'src/app/core/eperson/models/eperson.resource-type';
24-
import { GROUP } from 'src/app/core/eperson/models/group.resource-type';
25-
import { ResourceType } from 'src/app/core/shared/resource-type';
2619

20+
import { APP_DATA_SERVICES_MAP } from '../../../config/app-config.interface';
21+
import { DSONameService } from '../../core/breadcrumbs/dso-name.service';
2722
import { buildPaginatedList } from '../../core/data/paginated-list.model';
2823
import { RequestService } from '../../core/data/request.service';
2924
import { EPersonDataService } from '../../core/eperson/eperson-data.service';
3025
import { GroupDataService } from '../../core/eperson/group-data.service';
26+
import { EPERSON } from '../../core/eperson/models/eperson.resource-type';
27+
import { GROUP } from '../../core/eperson/models/group.resource-type';
3128
import { PaginationService } from '../../core/pagination/pagination.service';
3229
import { PageInfo } from '../../core/shared/page-info.model';
3330
import { DSONameServiceMock } from '../mocks/dso-name.service.mock';
@@ -44,6 +41,11 @@ import { SearchEvent } from './eperson-group-list-event-type';
4441
import { EpersonSearchBoxComponent } from './eperson-search-box/eperson-search-box.component';
4542
import { GroupSearchBoxComponent } from './group-search-box/group-search-box.component';
4643

44+
const mockDataServiceMap: any = {
45+
[EPERSON.value]: () => import('../../core/eperson/eperson-data.service').then(m => m.EPersonDataService),
46+
[GROUP.value]: () => import('../../core/eperson/group-data.service').then(m => m.GroupDataService),
47+
};
48+
4749
describe('EpersonGroupListComponent test suite', () => {
4850
let comp: EpersonGroupListComponent;
4951
let compAsAny: any;
@@ -59,7 +61,6 @@ describe('EpersonGroupListComponent test suite', () => {
5961

6062
const mockEpersonService = jasmine.createSpyObj('epersonService',
6163
{
62-
getDataServiceFor: jasmine.createSpy('getDataServiceFor'),
6364
findByHref: jasmine.createSpy('findByHref'),
6465
findAll: jasmine.createSpy('findAll'),
6566
searchByScope: jasmine.createSpy('searchByScope'),
@@ -71,7 +72,6 @@ describe('EpersonGroupListComponent test suite', () => {
7172

7273
const mockGroupService = jasmine.createSpyObj('groupService',
7374
{
74-
getDataServiceFor: jasmine.createSpy('getDataServiceFor'),
7575
findByHref: jasmine.createSpy('findByHref'),
7676
findAll: jasmine.createSpy('findAll'),
7777
searchGroups: jasmine.createSpy('searchGroups'),
@@ -106,6 +106,7 @@ describe('EpersonGroupListComponent test suite', () => {
106106
{ provide: GroupDataService, useValue: mockGroupService },
107107
{ provide: RequestService, useValue: getMockRequestService() },
108108
{ provide: PaginationService, useValue: paginationService },
109+
{ provide: APP_DATA_SERVICES_MAP, useValue: mockDataServiceMap },
109110
EpersonGroupListComponent,
110111
ChangeDetectorRef,
111112
Injector,
@@ -151,19 +152,14 @@ describe('EpersonGroupListComponent test suite', () => {
151152

152153
describe('when is list of eperson', () => {
153154

154-
beforeEach(() => {
155+
beforeEach(waitForAsync(() => {
155156
// initTestScheduler();
156157
fixture = TestBed.createComponent(EpersonGroupListComponent);
157158
epersonService = TestBed.inject(EPersonDataService);
158159
comp = fixture.componentInstance;
159160
compAsAny = fixture.componentInstance;
160161
comp.isListOfEPerson = true;
161-
const resourceType: ResourceType = (comp.isListOfEPerson) ? EPERSON : GROUP;
162-
const mockDataService = dataService(resourceType);
163-
if (!getDataServiceFor(resourceType)) {
164-
mockDataService(EPersonDataService);
165-
}
166-
});
162+
}));
167163

168164
afterEach(() => {
169165
comp = null;
@@ -172,33 +168,40 @@ describe('EpersonGroupListComponent test suite', () => {
172168
fixture.destroy();
173169
});
174170

175-
it('should inject EPersonDataService', () => {
171+
it('should inject EPersonDataService', fakeAsync(() => {
176172
spyOn(comp, 'updateList');
177173
fixture.detectChanges();
178174

179-
expect(compAsAny.dataService).toBeDefined();
180-
expect(comp.updateList).toHaveBeenCalled();
181-
});
175+
fixture.whenStable().then(() => {
176+
expect(compAsAny.dataService).toBeDefined();
177+
expect(comp.updateList).toHaveBeenCalled();
178+
});
179+
}));
182180

183-
it('should init entrySelectedId', () => {
181+
it('should init entrySelectedId', fakeAsync(() => {
184182
spyOn(comp, 'updateList');
185183
comp.initSelected = EPersonMock.id;
186184

187185
fixture.detectChanges();
188186

189-
expect(compAsAny.entrySelectedId.value).toBe(EPersonMock.id);
190-
});
187+
fixture.whenStable().then(() => {
188+
expect(compAsAny.entrySelectedId.value).toBe(EPersonMock.id);
189+
});
191190

192-
it('should init the list of eperson', () => {
191+
}));
192+
193+
it('should init the list of eperson', fakeAsync(() => {
193194
epersonService.searchByScope.and.returnValue(observableOf(epersonPaginatedListRD));
194195

195196
fixture.detectChanges();
196197

197-
expect(compAsAny.list$.value).toEqual(epersonPaginatedListRD);
198-
expect(comp.getList()).toBeObservable(cold('a', {
199-
a: epersonPaginatedListRD,
200-
}));
201-
});
198+
fixture.whenStable().then(() => {
199+
expect(compAsAny.list$.value).toEqual(epersonPaginatedListRD);
200+
expect(comp.getList()).toBeObservable(cold('a', {
201+
a: epersonPaginatedListRD,
202+
}));
203+
});
204+
}));
202205

203206
it('should emit select event', () => {
204207
spyOn(comp.select, 'emit');
@@ -234,11 +237,6 @@ describe('EpersonGroupListComponent test suite', () => {
234237
comp = fixture.componentInstance;
235238
compAsAny = fixture.componentInstance;
236239
comp.isListOfEPerson = false;
237-
const resourceType: ResourceType = (comp.isListOfEPerson) ? EPERSON : GROUP;
238-
const mockDataService = dataService(resourceType);
239-
if (!getDataServiceFor(resourceType)) {
240-
mockDataService(GroupDataService);
241-
}
242240
});
243241

244242
afterEach(() => {
@@ -248,32 +246,39 @@ describe('EpersonGroupListComponent test suite', () => {
248246
fixture.destroy();
249247
});
250248

251-
it('should inject GroupDataService', () => {
249+
it('should inject GroupDataService', fakeAsync(() => {
252250
spyOn(comp, 'updateList');
253251
fixture.detectChanges();
254252

255-
expect(compAsAny.dataService).toBeDefined();
256-
expect(comp.updateList).toHaveBeenCalled();
257-
});
253+
fixture.whenStable().then(() => {
254+
expect(compAsAny.dataService).toBeDefined();
255+
expect(comp.updateList).toHaveBeenCalled();
256+
});
258257

259-
it('should init entrySelectedId', () => {
258+
}));
259+
260+
it('should init entrySelectedId', fakeAsync(() => {
260261
spyOn(comp, 'updateList');
261262
comp.initSelected = GroupMock.id;
262263

263264
fixture.detectChanges();
264265

265-
expect(compAsAny.entrySelectedId.value).toBe(GroupMock.id);
266-
});
266+
fixture.whenStable().then(() => {
267+
expect(compAsAny.entrySelectedId.value).toBe(GroupMock.id);
268+
});
269+
}));
267270

268-
it('should init the list of group', () => {
271+
it('should init the list of group', fakeAsync(() => {
269272
groupService.searchGroups.and.returnValue(observableOf(groupPaginatedListRD));
270273
fixture.detectChanges();
271274

272-
expect(compAsAny.list$.value).toEqual(groupPaginatedListRD);
273-
expect(comp.getList()).toBeObservable(cold('a', {
274-
a: groupPaginatedListRD,
275-
}));
276-
});
275+
fixture.whenStable().then(() => {
276+
expect(compAsAny.list$.value).toEqual(groupPaginatedListRD);
277+
expect(comp.getList()).toBeObservable(cold('a', {
278+
a: groupPaginatedListRD,
279+
}));
280+
});
281+
}));
277282

278283
it('should emit select event', () => {
279284
spyOn(comp.select, 'emit');

src/app/shared/eperson-group-list/eperson-group-list.component.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
import {
77
Component,
88
EventEmitter,
9+
Inject,
10+
InjectionToken,
911
Injector,
1012
Input,
1113
OnDestroy,
@@ -21,15 +23,19 @@ import {
2123
} from 'rxjs';
2224
import { map } from 'rxjs/operators';
2325

26+
import {
27+
APP_DATA_SERVICES_MAP,
28+
LazyDataServicesMap,
29+
} from '../../../config/app-config.interface';
2430
import { DSONameService } from '../../core/breadcrumbs/dso-name.service';
25-
import { getDataServiceFor } from '../../core/data/base/data-service.decorator';
2631
import { FindListOptions } from '../../core/data/find-list-options.model';
2732
import { PaginatedList } from '../../core/data/paginated-list.model';
2833
import { RemoteData } from '../../core/data/remote-data';
2934
import { EPersonDataService } from '../../core/eperson/eperson-data.service';
3035
import { GroupDataService } from '../../core/eperson/group-data.service';
3136
import { EPERSON } from '../../core/eperson/models/eperson.resource-type';
3237
import { GROUP } from '../../core/eperson/models/group.resource-type';
38+
import { lazyService } from '../../core/lazy-service';
3339
import { PaginationService } from '../../core/pagination/pagination.service';
3440
import { DSpaceObject } from '../../core/shared/dspace-object.model';
3541
import { getFirstCompletedRemoteData } from '../../core/shared/operators';
@@ -121,29 +127,33 @@ export class EpersonGroupListComponent implements OnInit, OnDestroy {
121127
*
122128
* @param {DSONameService} dsoNameService
123129
* @param {Injector} parentInjector
130+
* @param {PaginationService} paginationService
131+
* @param {APP_DATA_SERVICES_MAP} dataServiceMap
124132
*/
125-
constructor(public dsoNameService: DSONameService, private parentInjector: Injector,
126-
private paginationService: PaginationService) {
133+
constructor(public dsoNameService: DSONameService,
134+
private parentInjector: Injector,
135+
private paginationService: PaginationService,
136+
@Inject(APP_DATA_SERVICES_MAP) private dataServiceMap: InjectionToken<LazyDataServicesMap>) {
127137
}
128138

129139
/**
130140
* Initialize the component
131141
*/
132142
ngOnInit(): void {
133143
const resourceType: ResourceType = (this.isListOfEPerson) ? EPERSON : GROUP;
134-
const provider = getDataServiceFor(resourceType);
135-
this.dataService = Injector.create({
136-
providers: [],
137-
parent: this.parentInjector,
138-
}).get(provider);
139-
this.paginationOptions.id = uniqueId('egl');
140-
this.paginationOptions.pageSize = 5;
141-
142-
if (this.initSelected) {
143-
this.entrySelectedId.next(this.initSelected);
144-
}
145-
146-
this.updateList(this.currentSearchScope, this.currentSearchQuery);
144+
const lazyProvider$: Observable<EPersonDataService | GroupDataService> = lazyService(this.dataServiceMap[resourceType.value], this.parentInjector);
145+
lazyProvider$.subscribe((dataService: EPersonDataService | GroupDataService) => {
146+
this.dataService = dataService;
147+
console.log(dataService);
148+
this.paginationOptions.id = uniqueId('egl');
149+
this.paginationOptions.pageSize = 5;
150+
151+
if (this.initSelected) {
152+
this.entrySelectedId.next(this.initSelected);
153+
}
154+
155+
this.updateList(this.currentSearchScope, this.currentSearchQuery);
156+
});
147157
}
148158

149159
/**

0 commit comments

Comments
 (0)