mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[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:
@@ -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(() => {
|
||||
|
@@ -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);
|
||||
},
|
||||
() => {
|
||||
|
@@ -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 */
|
||||
|
@@ -17,25 +17,39 @@
|
||||
|
||||
import { DateCellComponent } from '../date-cell/date-cell.component';
|
||||
import { Subject } from 'rxjs';
|
||||
import { AlfrescoApiServiceMock, AppConfigService, StorageService } from '@alfresco/adf-core';
|
||||
import { AppConfigService } from '@alfresco/adf-core';
|
||||
import { Node } from '@alfresco/js-api';
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { CoreTestingModule } from '../../../testing';
|
||||
import { AlfrescoApiService } from '../../../services/alfresco-api.service';
|
||||
import { CoreModule } from '../../../core.module';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
|
||||
describe('DataTableCellComponent', () => {
|
||||
let alfrescoApiService: AlfrescoApiServiceMock;
|
||||
let alfrescoApiService: AlfrescoApiService;
|
||||
let appConfigService: AppConfigService;
|
||||
|
||||
beforeEach(() => {
|
||||
alfrescoApiService = new AlfrescoApiServiceMock(new AppConfigService(null), new StorageService());
|
||||
TestBed.configureTestingModule({
|
||||
imports: [
|
||||
CoreModule.forRoot(),
|
||||
TranslateModule.forRoot(),
|
||||
CoreTestingModule
|
||||
]
|
||||
});
|
||||
alfrescoApiService = TestBed.inject(AlfrescoApiService);
|
||||
appConfigService = TestBed.inject(AppConfigService);
|
||||
});
|
||||
|
||||
it('should use medium format by default', () => {
|
||||
const component = new DateCellComponent(null, null, new AppConfigService(null));
|
||||
const component = new DateCellComponent(null, null, appConfigService);
|
||||
expect(component.format).toBe('medium');
|
||||
});
|
||||
|
||||
it('should use column format', () => {
|
||||
const component = new DateCellComponent(null, <any> {
|
||||
nodeUpdated: new Subject<any>()
|
||||
}, new AppConfigService(null));
|
||||
}, appConfigService);
|
||||
component.column = {
|
||||
key: 'created',
|
||||
type: 'date',
|
||||
@@ -50,7 +64,7 @@ describe('DataTableCellComponent', () => {
|
||||
const component = new DateCellComponent(
|
||||
null,
|
||||
alfrescoApiService,
|
||||
new AppConfigService(null)
|
||||
appConfigService
|
||||
);
|
||||
|
||||
component.column = {
|
||||
@@ -85,7 +99,7 @@ describe('DataTableCellComponent', () => {
|
||||
const component = new DateCellComponent(
|
||||
null,
|
||||
alfrescoApiService,
|
||||
new AppConfigService(null)
|
||||
appConfigService
|
||||
);
|
||||
|
||||
component.column = {
|
||||
@@ -120,7 +134,7 @@ describe('DataTableCellComponent', () => {
|
||||
const component = new DateCellComponent(
|
||||
null,
|
||||
alfrescoApiService,
|
||||
new AppConfigService(null)
|
||||
appConfigService
|
||||
);
|
||||
|
||||
component.column = {
|
||||
|
@@ -23,7 +23,7 @@ import { of, throwError, Subject } from 'rxjs';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
import { DirectiveModule } from './directive.module';
|
||||
import { CoreModule } from '../core.module';
|
||||
import { AlfrescoApiServiceMock } from '../mock/alfresco-api.service.mock';
|
||||
import { CoreTestingModule } from '../testing/core.testing.module';
|
||||
|
||||
describe('LibraryMembershipDirective', () => {
|
||||
let alfrescoApiService: AlfrescoApiService;
|
||||
@@ -40,9 +40,11 @@ describe('LibraryMembershipDirective', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [TranslateModule.forRoot(), DirectiveModule, CoreModule.forRoot()],
|
||||
providers: [
|
||||
{ provide: AlfrescoApiService, useClass: AlfrescoApiServiceMock }
|
||||
imports: [
|
||||
TranslateModule.forRoot(),
|
||||
DirectiveModule,
|
||||
CoreModule.forRoot(),
|
||||
CoreTestingModule
|
||||
],
|
||||
schemas: [NO_ERRORS_SCHEMA]
|
||||
});
|
||||
|
@@ -16,19 +16,17 @@
|
||||
*/
|
||||
|
||||
import { SimpleChange } from '@angular/core';
|
||||
import { fakeAsync, tick } from '@angular/core/testing';
|
||||
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
|
||||
import { NodeFavoriteDirective } from './node-favorite.directive';
|
||||
import { AlfrescoApiServiceMock } from '../mock/alfresco-api.service.mock';
|
||||
import { AppConfigService } from '../app-config/app-config.service';
|
||||
import { setupTestBed } from '../testing/setup-test-bed';
|
||||
import { CoreTestingModule } from '../testing/core.testing.module';
|
||||
import { StorageService } from '../services/storage.service';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
import { AlfrescoApiService } from '../services/alfresco-api.service';
|
||||
|
||||
describe('NodeFavoriteDirective', () => {
|
||||
|
||||
let directive;
|
||||
let alfrescoApiService;
|
||||
let directive: NodeFavoriteDirective;
|
||||
let alfrescoApiService: AlfrescoApiService;
|
||||
|
||||
setupTestBed({
|
||||
imports: [
|
||||
@@ -38,7 +36,7 @@ describe('NodeFavoriteDirective', () => {
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
alfrescoApiService = new AlfrescoApiServiceMock(new AppConfigService(null), new StorageService());
|
||||
alfrescoApiService = TestBed.inject(AlfrescoApiService);
|
||||
directive = new NodeFavoriteDirective( alfrescoApiService);
|
||||
});
|
||||
|
||||
|
@@ -35,7 +35,7 @@ export class AlfrescoApiServiceMock extends AlfrescoApiService {
|
||||
initialize(): Promise<any> {
|
||||
return new Promise((resolve) => {
|
||||
this.alfrescoApiInitialized.next(true);
|
||||
resolve();
|
||||
resolve({});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -16,8 +16,9 @@
|
||||
*/
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { AppConfigService } from '../app-config/app-config.service';
|
||||
import { AppConfigService, Status } from '../app-config/app-config.service';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { ExtensionService } from '@alfresco/adf-extensions';
|
||||
@Injectable()
|
||||
export class AppConfigServiceMock extends AppConfigService {
|
||||
|
||||
@@ -30,13 +31,14 @@ export class AppConfigServiceMock extends AppConfigService {
|
||||
logLevel: 'silent'
|
||||
};
|
||||
|
||||
constructor(http: HttpClient) {
|
||||
super(http);
|
||||
constructor(http: HttpClient, extensionService: ExtensionService) {
|
||||
super(http, extensionService);
|
||||
}
|
||||
|
||||
load(): Promise<any> {
|
||||
return new Promise((resolve) => {
|
||||
this.onLoadSubject.next(this.config);
|
||||
this.status = Status.LOADED;
|
||||
this.onDataLoaded(this.config);
|
||||
resolve(this.config);
|
||||
});
|
||||
}
|
||||
|
@@ -59,5 +59,8 @@ export let searchMockApi = {
|
||||
queriesApi: {
|
||||
findNodes: () => Promise.resolve(fakeSearch)
|
||||
}
|
||||
},
|
||||
isEcmLoggedIn() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
@@ -39,7 +39,7 @@ describe('DecimalNumberPipe', () => {
|
||||
beforeEach(async(() => {
|
||||
userPreferences = TestBed.inject(UserPreferencesService);
|
||||
spyOn(userPreferences, 'select').and.returnValue(of(''));
|
||||
pipe = new DecimalNumberPipe(userPreferences, new AppConfigService(null));
|
||||
pipe = new DecimalNumberPipe(userPreferences, TestBed.inject(AppConfigService));
|
||||
}));
|
||||
|
||||
it('should return number localized and rounded following the default config', () => {
|
||||
|
@@ -42,7 +42,7 @@ describe('LocalizedDatePipe', () => {
|
||||
beforeEach(async(() => {
|
||||
userPreferences = TestBed.inject(UserPreferencesService);
|
||||
spyOn(userPreferences, 'select').and.returnValue(of(''));
|
||||
pipe = new LocalizedDatePipe(userPreferences, new AppConfigService(null));
|
||||
pipe = new LocalizedDatePipe(userPreferences, TestBed.inject(AppConfigService));
|
||||
}));
|
||||
|
||||
it('should return time with locale en-US', () => {
|
||||
|
@@ -39,7 +39,7 @@ describe('TimeAgoPipe', () => {
|
||||
beforeEach(async(() => {
|
||||
userPreferences = TestBed.inject(UserPreferencesService);
|
||||
spyOn(userPreferences, 'select').and.returnValue(of(''));
|
||||
pipe = new TimeAgoPipe(userPreferences, new AppConfigService(null));
|
||||
pipe = new TimeAgoPipe(userPreferences, TestBed.inject(AppConfigService));
|
||||
}));
|
||||
|
||||
it('should return time difference for a given date', () => {
|
||||
|
@@ -45,6 +45,9 @@ describe('NodesApiService', () => {
|
||||
getNodeChildren: jasmine.createSpy('getNodeChildren'),
|
||||
addNode: jasmine.createSpy('addNode')
|
||||
}
|
||||
},
|
||||
isEcmLoggedIn() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
|
@@ -23,7 +23,8 @@ import { UserPreferencesService, UserPreferenceValues } from './user-preferences
|
||||
import { setupTestBed } from '../testing/setup-test-bed';
|
||||
import { CoreTestingModule } from '../testing/core.testing.module';
|
||||
import { AppConfigServiceMock } from '../mock/app-config.service.mock';
|
||||
import { AlfrescoApiServiceMock } from '../mock/alfresco-api.service.mock';
|
||||
import { AlfrescoApiService } from './alfresco-api.service';
|
||||
import { AlfrescoApiServiceMock } from '../mock';
|
||||
|
||||
describe('UserPreferencesService', () => {
|
||||
|
||||
@@ -53,7 +54,7 @@ describe('UserPreferencesService', () => {
|
||||
|
||||
storage = TestBed.inject(StorageService);
|
||||
translate = TestBed.inject(TranslateService);
|
||||
alfrescoApiService = new AlfrescoApiServiceMock(new AppConfigService(null), new StorageService());
|
||||
alfrescoApiService = TestBed.inject(AlfrescoApiService) as AlfrescoApiServiceMock;
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -71,7 +72,7 @@ describe('UserPreferencesService', () => {
|
||||
describe(' with pagination config', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
preferences = new UserPreferencesService(translate, appConfig, storage, alfrescoApiService);
|
||||
preferences = TestBed.inject(UserPreferencesService);
|
||||
});
|
||||
|
||||
it('should get default pagination from app config', (done) => {
|
||||
@@ -183,7 +184,6 @@ describe('UserPreferencesService', () => {
|
||||
}
|
||||
];
|
||||
appConfig.config.locale = 'fake-locale-config';
|
||||
preferences = new UserPreferencesService(translate, appConfig, storage, alfrescoApiService);
|
||||
alfrescoApiService.initialize();
|
||||
const textOrientation = preferences.getPropertyKey('textOrientation');
|
||||
expect(storage.getItem(textOrientation)).toBe('ltr');
|
||||
@@ -197,7 +197,6 @@ describe('UserPreferencesService', () => {
|
||||
}
|
||||
];
|
||||
appConfig.config.locale = 'fake-locale-config';
|
||||
preferences = new UserPreferencesService(translate, appConfig, storage, alfrescoApiService);
|
||||
alfrescoApiService.initialize();
|
||||
const textOrientation = preferences.getPropertyKey('textOrientation');
|
||||
expect(storage.getItem(textOrientation)).toBe('rtl');
|
||||
@@ -209,7 +208,6 @@ describe('UserPreferencesService', () => {
|
||||
key: 'fake-locale-browser'
|
||||
}
|
||||
];
|
||||
preferences = new UserPreferencesService(translate, appConfig, storage, alfrescoApiService);
|
||||
alfrescoApiService.initialize();
|
||||
|
||||
const textOrientation = preferences.getPropertyKey('textOrientation');
|
||||
@@ -224,7 +222,6 @@ describe('UserPreferencesService', () => {
|
||||
}
|
||||
];
|
||||
spyOn(translate, 'getBrowserCultureLang').and.returnValue('fake-locale-browser');
|
||||
preferences = new UserPreferencesService(translate, appConfig, storage, alfrescoApiService);
|
||||
alfrescoApiService.initialize();
|
||||
|
||||
changeDisposable = preferences.onChange
|
||||
|
@@ -40,8 +40,8 @@ import { VersionCompatibilityService } from '../services/version-compatibility.s
|
||||
NoopAnimationsModule,
|
||||
RouterTestingModule,
|
||||
HttpClientModule,
|
||||
TranslateModule,
|
||||
CoreModule
|
||||
TranslateModule.forRoot(),
|
||||
CoreModule.forRoot()
|
||||
],
|
||||
providers: [
|
||||
DatePipe,
|
||||
|
@@ -21,7 +21,6 @@ import { MediaPlayerComponent } from './media-player.component';
|
||||
import { setupTestBed } from '../../testing/setup-test-bed';
|
||||
import { CoreTestingModule } from '../../testing/core.testing.module';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
import { AlfrescoApiServiceMock } from '../../mock/alfresco-api.service.mock';
|
||||
import { AlfrescoApiService } from '../../services';
|
||||
import { NodeEntry } from '@alfresco/js-api';
|
||||
|
||||
@@ -36,9 +35,6 @@ describe('Test Media player component ', () => {
|
||||
imports: [
|
||||
TranslateModule.forRoot(),
|
||||
CoreTestingModule
|
||||
],
|
||||
providers: [
|
||||
{ provide: AlfrescoApiService, useClass: AlfrescoApiServiceMock }
|
||||
]
|
||||
});
|
||||
|
||||
|
@@ -26,7 +26,6 @@ import { EventMock } from '../../mock/event.mock';
|
||||
import { RenderingQueueServices } from '../services/rendering-queue.services';
|
||||
import { ViewerComponent } from './viewer.component';
|
||||
import { setupTestBed } from '../../testing/setup-test-bed';
|
||||
import { AlfrescoApiServiceMock } from '../../mock/alfresco-api.service.mock';
|
||||
import { NodeEntry, VersionEntry } from '@alfresco/js-api';
|
||||
import { CoreTestingModule } from '../../testing/core.testing.module';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
@@ -151,7 +150,6 @@ describe('ViewerComponent', () => {
|
||||
ViewerWithCustomToolbarActionsComponent
|
||||
],
|
||||
providers: [
|
||||
{ provide: AlfrescoApiService, useClass: AlfrescoApiServiceMock },
|
||||
{
|
||||
provide: RenditionsService, useValue: {
|
||||
getRendition: () => {
|
||||
|
Reference in New Issue
Block a user