[AAE-4966] Extensible app config (#6914)

* merge app config from the extensions

* improved service injection in unit tests

* extra unit test

* fix content tests

* update code as per review

* fix lint

* fix lint

* update code and tests

* update schema
This commit is contained in:
Denys Vuika
2021-04-13 14:16:29 +01:00
committed by GitHub
parent bd8242922b
commit 84ce202ad2
54 changed files with 383 additions and 204 deletions

View File

@@ -15,16 +15,25 @@
* limitations under the License.
*/
import { HttpClientModule } from '@angular/common/http';
import { async, inject, TestBed } from '@angular/core/testing';
import { HttpClient, HttpClientModule } from '@angular/common/http';
import { async, TestBed } from '@angular/core/testing';
import { AppConfigService } from './app-config.service';
import { AppConfigModule } from './app-config.module';
import { ExtensionConfig, ExtensionService } from '@alfresco/adf-extensions';
import { of } from 'rxjs';
declare let jasmine: any;
class TestExtensionService extends ExtensionService {
onSetup(config: ExtensionConfig) {
this.onSetup$.next(config);
}
}
describe('AppConfigService', () => {
let appConfigService: AppConfigService;
let extensionService: ExtensionService;
let httpClient: HttpClient;
const mockResponse = {
ecmHost: 'http://localhost:4000/ecm',
@@ -46,32 +55,60 @@ describe('AppConfigService', () => {
AppConfigModule
],
providers: [
{ provide: AppConfigService, useClass: AppConfigService }
{ provide: ExtensionService, useClass: TestExtensionService }
]
});
jasmine.Ajax.install();
});
beforeEach(
inject([AppConfigService], (appConfig: AppConfigService) => {
appConfigService = appConfig;
appConfigService.load();
beforeEach(() => {
httpClient = TestBed.inject(HttpClient);
spyOn(httpClient, 'get').and.returnValue(of(mockResponse));
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(mockResponse)
});
})
);
extensionService = TestBed.inject(ExtensionService);
afterEach(() => {
jasmine.Ajax.uninstall();
appConfigService = TestBed.inject(AppConfigService);
appConfigService.load();
});
it('should export service in the module', () => {
expect(appConfigService).toBeDefined();
it('should merge the configs from extensions', () => {
appConfigService.config = {
application: {
name: 'application name'
}
};
(extensionService as TestExtensionService).onSetup({
appConfig: {
application: {
name: 'custom name'
}
}
} as any);
expect(appConfigService.get('application.name')).toEqual('custom name');
});
it('should merge the configs upon new data loaded', async (done) => {
appConfigService.config = {
application: {
name: 'application name'
}
};
(extensionService as TestExtensionService).onSetup({
appConfig: {
application: {
name: 'custom name'
}
}
} as any);
expect(appConfigService.get('application.name')).toEqual('custom name');
await appConfigService.load();
expect(appConfigService.get('application.name')).toEqual('custom name');
done();
});
it('should stream only the selected attribute changes when using select', async(() => {

View File

@@ -19,7 +19,8 @@ import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ObjectUtils } from '../utils/object-utils';
import { Observable, Subject } from 'rxjs';
import { map, distinctUntilChanged } from 'rxjs/operators';
import { map, distinctUntilChanged, take } from 'rxjs/operators';
import { ExtensionConfig, ExtensionService, mergeObjects } from '@alfresco/adf-extensions';
/* spellchecker: disable */
export enum AppConfigValues {
@@ -69,9 +70,13 @@ export class AppConfigService {
protected onLoadSubject: Subject<any>;
onLoad: Observable<any>;
constructor(private http: HttpClient) {
constructor(protected http: HttpClient, protected extensionService: ExtensionService) {
this.onLoadSubject = new Subject();
this.onLoad = this.onLoadSubject.asObservable();
extensionService.setup$.subscribe((config) => {
this.onExtensionsLoaded(config);
});
}
/**
@@ -142,6 +147,29 @@ export class AppConfigService {
return location.port ? prefix + location.port : '';
}
protected onLoaded() {
this.onLoadSubject.next(this.config);
}
protected onDataLoaded(data: any) {
this.config = Object.assign({}, this.config, data || {});
this.onLoadSubject.next(this.config);
this.extensionService.setup$
.pipe(take(1))
.subscribe((config) => this.onExtensionsLoaded(config));
}
protected onExtensionsLoaded(config: ExtensionConfig) {
if (config) {
const customConfig = config.appConfig;
if (customConfig) {
this.config = mergeObjects(this.config, customConfig);
}
}
}
/**
* Loads the config file.
* @returns Notification when loading is complete
@@ -152,11 +180,10 @@ export class AppConfigService {
if (this.status === Status.INIT) {
this.status = Status.LOADING;
await this.http.get(configUrl).subscribe(
this.http.get(configUrl).subscribe(
(data: any) => {
this.status = Status.LOADED;
this.config = Object.assign({}, this.config, data || {});
this.onLoadSubject.next(this.config);
this.onDataLoaded(data);
resolve(this.config);
},
() => {

View File

@@ -19,11 +19,12 @@ import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { StorageService } from '../services/storage.service';
import { AppConfigService, AppConfigValues } from '../app-config/app-config.service';
import { ExtensionService } from '@alfresco/adf-extensions';
@Injectable()
export class DebugAppConfigService extends AppConfigService {
constructor(private storage: StorageService, http: HttpClient) {
super(http);
constructor(private storage: StorageService, http: HttpClient, extensionService: ExtensionService) {
super(http, extensionService);
}
/** @override */