Skip to content

Commit

Permalink
update code and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DenysVuika committed Apr 12, 2021
1 parent 159a330 commit 62f139d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 29 deletions.
42 changes: 28 additions & 14 deletions lib/core/app-config/app-config.service.spec.ts
Expand Up @@ -15,13 +15,12 @@
* limitations under the License.
*/

import { HttpClientModule } from '@angular/common/http';
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';

declare let jasmine: any;
import { of } from 'rxjs';

class TestExtensionService extends ExtensionService {

Expand All @@ -34,6 +33,7 @@ describe('AppConfigService', () => {

let appConfigService: AppConfigService;
let extensionService: ExtensionService;
let httpClient: HttpClient;

const mockResponse = {
ecmHost: 'http://localhost:4000/ecm',
Expand All @@ -58,28 +58,37 @@ describe('AppConfigService', () => {
{ provide: ExtensionService, useClass: TestExtensionService }
]
});

jasmine.Ajax.install();
});

beforeEach(() => {
httpClient = TestBed.inject(HttpClient);
spyOn(httpClient, 'get').and.returnValue(of(mockResponse));

extensionService = TestBed.inject(ExtensionService);

appConfigService = TestBed.inject(AppConfigService);
appConfigService.load();

jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(mockResponse)
});
});

afterEach(() => {
jasmine.Ajax.uninstall();
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 from extensions', () => {
it('should merge the configs upon new data loaded', async (done) => {
appConfigService.config = {
application: {
name: 'application name'
Expand All @@ -95,6 +104,11 @@ describe('AppConfigService', () => {
} 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(() => {
Expand Down
42 changes: 29 additions & 13 deletions lib/core/app-config/app-config.service.ts
Expand Up @@ -19,8 +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 { ExtensionService, mergeObjects } from '@alfresco/adf-extensions';
import { map, distinctUntilChanged, take } from 'rxjs/operators';
import { ExtensionConfig, ExtensionService, mergeObjects } from '@alfresco/adf-extensions';

/* spellchecker: disable */
export enum AppConfigValues {
Expand Down Expand Up @@ -70,18 +70,12 @@ export class AppConfigService {
protected onLoadSubject: Subject<any>;
onLoad: Observable<any>;

constructor(private http: HttpClient, extensionService: ExtensionService) {
constructor(protected http: HttpClient, protected extensionService: ExtensionService) {
this.onLoadSubject = new Subject();
this.onLoad = this.onLoadSubject.asObservable();

extensionService.setup$.subscribe((config) => {
if (config) {
const customConfig = config.appConfig;

if (customConfig) {
this.config = mergeObjects(this.config, customConfig);
}
}
this.onExtensionsLoaded(config);
});
}

Expand Down Expand Up @@ -153,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
Expand All @@ -163,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);
},
() => {
Expand Down
5 changes: 3 additions & 2 deletions lib/core/mock/app-config.service.mock.ts
Expand Up @@ -16,7 +16,7 @@
*/

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()
Expand All @@ -37,7 +37,8 @@ export class AppConfigServiceMock extends AppConfigService {

load(): Promise<any> {
return new Promise((resolve) => {
this.onLoadSubject.next(this.config);
this.status = Status.LOADED;
this.onDataLoaded(this.config);
resolve(this.config);
});
}
Expand Down

0 comments on commit 62f139d

Please sign in to comment.