Skip to content

Commit 7472e69

Browse files
committed
[DURACOM-191] Fix dso-edit-metadata.component which still used getDataServiceFor decorator
1 parent 58512b6 commit 7472e69

5 files changed

Lines changed: 96 additions & 50 deletions

File tree

src/app/collection-page/edit-item-template-page/edit-item-template-page.component.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { of as observableOf } from 'rxjs';
1212

1313
import { ItemTemplateDataService } from '../../core/data/item-template-data.service';
1414
import { Collection } from '../../core/shared/collection.model';
15+
import { DsoEditMetadataComponent } from '../../dso-shared/dso-edit-metadata/dso-edit-metadata.component';
16+
import { ThemedDsoEditMetadataComponent } from '../../dso-shared/dso-edit-metadata/themed-dso-edit-metadata.component';
1517
import { getMockThemeService } from '../../shared/mocks/theme-service.mock';
1618
import { NotificationsService } from '../../shared/notifications/notifications.service';
1719
import {
@@ -47,6 +49,10 @@ describe('EditItemTemplatePageComponent', () => {
4749
{ provide: NotificationsService, useValue: new NotificationsServiceStub() },
4850
],
4951
schemas: [NO_ERRORS_SCHEMA],
52+
}).overrideComponent(EditItemTemplatePageComponent, {
53+
remove: {
54+
imports: [ThemedDsoEditMetadataComponent, DsoEditMetadataComponent],
55+
},
5056
}).compileComponents();
5157
}));
5258

src/app/collection-page/edit-item-template-page/edit-item-template-page.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { RemoteData } from '../../core/data/remote-data';
2424
import { Collection } from '../../core/shared/collection.model';
2525
import { Item } from '../../core/shared/item.model';
2626
import { getFirstSucceededRemoteDataPayload } from '../../core/shared/operators';
27+
import { DsoEditMetadataComponent } from '../../dso-shared/dso-edit-metadata/dso-edit-metadata.component';
2728
import { ThemedDsoEditMetadataComponent } from '../../dso-shared/dso-edit-metadata/themed-dso-edit-metadata.component';
2829
import { AlertComponent } from '../../shared/alert/alert.component';
2930
import { AlertType } from '../../shared/alert/alert-type';
@@ -36,6 +37,7 @@ import { getCollectionEditRoute } from '../collection-page-routing-paths';
3637
templateUrl: './edit-item-template-page.component.html',
3738
imports: [
3839
ThemedDsoEditMetadataComponent,
40+
DsoEditMetadataComponent,
3941
RouterLink,
4042
AsyncPipe,
4143
VarDirective,

src/app/dso-shared/dso-edit-metadata/dso-edit-metadata.component.spec.ts

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1+
import { CommonModule } from '@angular/common';
12
import {
2-
ChangeDetectionStrategy,
33
DebugElement,
4-
Injectable,
54
NO_ERRORS_SCHEMA,
65
} from '@angular/core';
76
import {
87
ComponentFixture,
98
TestBed,
109
waitForAsync,
1110
} from '@angular/core/testing';
12-
import { By } from '@angular/platform-browser';
11+
import {
12+
BrowserModule,
13+
By,
14+
} from '@angular/platform-browser';
1315
import { RouterTestingModule } from '@angular/router/testing';
1416
import { TranslateModule } from '@ngx-translate/core';
15-
import { Operation } from 'fast-json-patch';
16-
import { Observable } from 'rxjs';
1717

18+
import { APP_DATA_SERVICES_MAP } from '../../../config/app-config.interface';
1819
import { ArrayMoveChangeAnalyzer } from '../../core/data/array-move-change-analyzer.service';
19-
import { DATA_SERVICE_FACTORY } from '../../core/data/base/data-service.decorator';
20-
import { RemoteData } from '../../core/data/remote-data';
2120
import { DSpaceObject } from '../../core/shared/dspace-object.model';
2221
import { Item } from '../../core/shared/item.model';
2322
import { ITEM } from '../../core/shared/item.resource-type';
2423
import { MetadataValue } from '../../core/shared/metadata.models';
2524
import { AlertComponent } from '../../shared/alert/alert.component';
2625
import { LoadingComponent } from '../../shared/loading/loading.component';
2726
import { NotificationsService } from '../../shared/notifications/notifications.service';
28-
import { createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils';
27+
import { TestDataService } from '../../shared/testing/test-data-service.mock';
2928
import { VarDirective } from '../../shared/utils/var.directive';
3029
import { DsoEditMetadataComponent } from './dso-edit-metadata.component';
3130
import { DsoEditMetadataFieldValuesComponent } from './dso-edit-metadata-field-values/dso-edit-metadata-field-values.component';
@@ -39,12 +38,9 @@ const REINSTATE_BTN = 'reinstate';
3938
const SAVE_BTN = 'save';
4039
const DISCARD_BTN = 'discard';
4140

42-
@Injectable()
43-
class TestDataService {
44-
patch(object: Item, operations: Operation[]): Observable<RemoteData<Item>> {
45-
return createSuccessfulRemoteDataObject$(object);
46-
}
47-
}
41+
const mockDataServiceMap: any = {
42+
[ITEM.value]: () => import('../../shared/testing/test-data-service.mock').then(m => m.TestDataService),
43+
};
4844

4945
describe('DsoEditMetadataComponent', () => {
5046
let component: DsoEditMetadataComponent;
@@ -92,21 +88,18 @@ describe('DsoEditMetadataComponent', () => {
9288

9389
TestBed.configureTestingModule({
9490
imports: [
91+
CommonModule,
92+
BrowserModule,
9593
TranslateModule.forRoot(),
9694
RouterTestingModule.withRoutes([]),
9795
DsoEditMetadataComponent,
9896
VarDirective,
9997
],
10098
providers: [
101-
TestDataService,
102-
{
103-
provide: DATA_SERVICE_FACTORY,
104-
useValue: jasmine
105-
.createSpy('getDataServiceFor')
106-
.and.returnValue(TestDataService),
107-
},
99+
{ provide: APP_DATA_SERVICES_MAP, useValue: mockDataServiceMap },
108100
{ provide: NotificationsService, useValue: notificationsService },
109101
ArrayMoveChangeAnalyzer,
102+
TestDataService,
110103
],
111104
schemas: [NO_ERRORS_SCHEMA],
112105
})
@@ -122,19 +115,16 @@ describe('DsoEditMetadataComponent', () => {
122115
LoadingComponent,
123116
],
124117
},
125-
add: {
126-
changeDetection: ChangeDetectionStrategy.Default,
127-
},
128118
})
129119
.compileComponents();
130120
}));
131121

132-
beforeEach(() => {
122+
beforeEach(waitForAsync(() => {
133123
fixture = TestBed.createComponent(DsoEditMetadataComponent);
134124
component = fixture.componentInstance;
135125
component.dso = dso;
136126
fixture.detectChanges();
137-
});
127+
}));
138128

139129
describe('when no changes have been made', () => {
140130
assertButton(ADD_BTN, true, false);

src/app/dso-shared/dso-edit-metadata/dso-edit-metadata.component.ts

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import {
44
NgIf,
55
} from '@angular/common';
66
import {
7+
ChangeDetectorRef,
78
Component,
89
Inject,
10+
InjectionToken,
911
Injector,
1012
Input,
1113
OnDestroy,
@@ -23,25 +25,33 @@ import {
2325
import {
2426
BehaviorSubject,
2527
combineLatest as observableCombineLatest,
28+
EMPTY,
2629
Observable,
2730
Subscription,
2831
} from 'rxjs';
29-
import { map } from 'rxjs/operators';
32+
import {
33+
map,
34+
mergeMap,
35+
tap,
36+
} from 'rxjs/operators';
3037

38+
import {
39+
APP_DATA_SERVICES_MAP,
40+
LazyDataServicesMap,
41+
} from '../../../config/app-config.interface';
3142
import { ArrayMoveChangeAnalyzer } from '../../core/data/array-move-change-analyzer.service';
32-
import { DATA_SERVICE_FACTORY } from '../../core/data/base/data-service.decorator';
33-
import { HALDataService } from '../../core/data/base/hal-data-service.interface';
3443
import { RemoteData } from '../../core/data/remote-data';
3544
import { UpdateDataService } from '../../core/data/update-data.service';
45+
import { lazyService } from '../../core/lazy-service';
3646
import { DSpaceObject } from '../../core/shared/dspace-object.model';
37-
import { GenericConstructor } from '../../core/shared/generic-constructor';
3847
import { getFirstCompletedRemoteData } from '../../core/shared/operators';
3948
import { ResourceType } from '../../core/shared/resource-type';
4049
import { AlertComponent } from '../../shared/alert/alert.component';
4150
import { AlertType } from '../../shared/alert/alert-type';
4251
import {
4352
hasNoValue,
4453
hasValue,
54+
isNotEmpty,
4555
} from '../../shared/empty.util';
4656
import { LoadingComponent } from '../../shared/loading/loading.component';
4757
import { NotificationsService } from '../../shared/notifications/notifications.service';
@@ -141,7 +151,8 @@ export class DsoEditMetadataComponent implements OnInit, OnDestroy {
141151
protected translateService: TranslateService,
142152
protected parentInjector: Injector,
143153
protected arrayMoveChangeAnalyser: ArrayMoveChangeAnalyzer<number>,
144-
@Inject(DATA_SERVICE_FACTORY) protected getDataServiceFor: (resourceType: ResourceType) => GenericConstructor<HALDataService<any>>) {
154+
protected cdr: ChangeDetectorRef,
155+
@Inject(APP_DATA_SERVICES_MAP) private dataServiceMap: InjectionToken<LazyDataServicesMap>) {
145156
}
146157

147158
/**
@@ -152,48 +163,74 @@ export class DsoEditMetadataComponent implements OnInit, OnDestroy {
152163
if (hasNoValue(this.dso)) {
153164
this.dsoUpdateSubscription = observableCombineLatest([this.route.data, this.route.parent.data]).pipe(
154165
map(([data, parentData]: [Data, Data]) => Object.assign({}, data, parentData)),
155-
map((data: any) => data.dso),
156-
).subscribe((rd: RemoteData<DSpaceObject>) => {
157-
this.dso = rd.payload;
158-
this.initDataService();
166+
tap((data: any) => this.initDSO(data.dso.payload)),
167+
mergeMap(() => this.retrieveDataService()),
168+
).subscribe((dataService: UpdateDataService<DSpaceObject>) => {
169+
this.initDataService(dataService);
159170
this.initForm();
160171
});
161172
} else {
162-
this.initDataService();
163-
this.initForm();
173+
this.initDSOType(this.dso);
174+
this.retrieveDataService().subscribe((dataService: UpdateDataService<DSpaceObject>) => {
175+
this.initDataService(dataService);
176+
this.initForm();
177+
});
164178
}
165179
this.savingOrLoadingFieldValidation$ = observableCombineLatest([this.saving$, this.loadingFieldValidation$]).pipe(
166180
map(([saving, loading]: [boolean, boolean]) => saving || loading),
167181
);
168182
}
169183

170184
/**
171-
* Initialise (resolve) the data-service for the current DSpaceObject
185+
* Resolve the data-service for the current DSpaceObject and retrieve its instance
172186
*/
173-
initDataService(): void {
174-
let type: ResourceType;
175-
if (typeof this.dso.type === 'string') {
176-
type = new ResourceType(this.dso.type);
187+
retrieveDataService(): Observable<UpdateDataService<DSpaceObject>> {
188+
if (hasNoValue(this.updateDataService)) {
189+
const lazyProvider$: Observable<UpdateDataService<DSpaceObject>> = lazyService(this.dataServiceMap[this.dsoType], this.parentInjector);
190+
return lazyProvider$;
177191
} else {
178-
type = this.dso.type;
192+
return EMPTY;
179193
}
180-
if (hasNoValue(this.updateDataService)) {
181-
const provider = this.getDataServiceFor(type);
182-
this.updateDataService = Injector.create({
183-
providers: [],
184-
parent: this.parentInjector,
185-
}).get(provider);
194+
}
195+
196+
/**
197+
* Initialise the current DSpaceObject
198+
*/
199+
initDSO(object: DSpaceObject) {
200+
this.dso = object;
201+
this.initDSOType(object);
202+
}
203+
204+
/**
205+
* Initialise the current DSpaceObject's type
206+
*/
207+
initDSOType(object: DSpaceObject) {
208+
let type: ResourceType;
209+
if (typeof object.type === 'string') {
210+
type = new ResourceType(object.type);
211+
} else {
212+
type = object.type;
186213
}
187214
this.dsoType = type.value;
188215
}
189216

217+
/**
218+
* Initialise the data-service for the current DSpaceObject
219+
*/
220+
initDataService(dataService: UpdateDataService<DSpaceObject>): void {
221+
if (isNotEmpty(dataService)) {
222+
this.updateDataService = dataService;
223+
}
224+
}
225+
190226
/**
191227
* Initialise the dynamic form object by passing the DSpaceObject's metadata
192228
* Call onValueSaved() to update the form's state properties
193229
*/
194230
initForm(): void {
195231
this.form = new DsoEditMetadataForm(this.dso.metadata);
196232
this.onValueSaved();
233+
this.cdr.detectChanges();
197234
}
198235

199236
/**

src/app/shared/testing/test-data-service.mock.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { Injectable } from '@angular/core';
2-
import { of } from 'rxjs';
2+
import { Operation } from 'fast-json-patch';
3+
import {
4+
Observable,
5+
of,
6+
} from 'rxjs';
37

48
import { FindListOptions } from '../../core/data/find-list-options.model';
9+
import { RemoteData } from '../../core/data/remote-data';
10+
import { Item } from '../../core/shared/item.model';
11+
import { createSuccessfulRemoteDataObject$ } from '../remote-data.utils';
512
import { FollowLinkConfig } from '../utils/follow-link-config.model';
613

714
@Injectable()
@@ -13,4 +20,8 @@ export class TestDataService {
1320
findByHref(href: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<any>[]) {
1421
return of('findByHref');
1522
}
23+
24+
patch(object: Item, operations: Operation[]): Observable<RemoteData<Item>> {
25+
return createSuccessfulRemoteDataObject$(object);
26+
}
1627
}

0 commit comments

Comments
 (0)