From e96f6aa481ebb546cb65add88015c1aeb9395b71 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Thu, 14 Sep 2017 10:06:40 +0100 Subject: [PATCH] [ADF-1544] app config file management refactoring (#2331) * application configuration service refactoring * fix demo shell * remove logging to console --- demo-shell-ng2/app/app.module.ts | 9 +- .../app/components/about/about.component.ts | 5 +- demo-shell-ng2/config/webpack.common.js | 6 - demo-shell-ng2/config/webpack.dev.js | 8 +- demo-shell-ng2/config/webpack.prod.js | 4 + .../analytics-report-list.component.spec.ts | 10 +- ...lytics-report-parameters.component.spec.ts | 10 +- .../components/task-filters.component.spec.ts | 14 +- .../src/components/tasklist.component.spec.ts | 13 +- ng2-components/ng2-alfresco-core/README.md | 63 ------- ng2-components/ng2-alfresco-core/index.ts | 13 +- .../services/alfresco-content.service.spec.ts | 7 +- .../src/services/app-config.service.spec.ts | 31 +--- .../src/services/app-config.service.ts | 53 ++---- .../services/discovery-api.service.spec.ts | 10 +- .../services/shared-links-api.service.spec.ts | 169 ------------------ .../src/services/sites-api.service.spec.ts | 17 +- .../src/services/upload.service.spec.ts | 26 +-- .../services/user-preferences.service.spec.ts | 12 +- .../services/folder-actions.service.spec.ts | 10 +- .../components/tag-actions.component.spec.ts | 10 +- .../src/components/tag-list.component.spec.ts | 10 +- .../tag-node-list.component.spec.ts | 10 +- .../src/services/tag.service.spec.ts | 10 +- .../src/webscript.component.spec.ts | 10 +- 25 files changed, 136 insertions(+), 404 deletions(-) delete mode 100644 ng2-components/ng2-alfresco-core/src/services/shared-links-api.service.spec.ts diff --git a/demo-shell-ng2/app/app.module.ts b/demo-shell-ng2/app/app.module.ts index 7e1a812a66..df75368448 100644 --- a/demo-shell-ng2/app/app.module.ts +++ b/demo-shell-ng2/app/app.module.ts @@ -70,18 +70,11 @@ import { WebscriptComponent } from './components/index'; -let appConfigFile = 'app.config-dev.json'; -if (process.env.ENV === 'production') { - appConfigFile = 'app.config-prod.json'; -} - @NgModule({ imports: [ BrowserModule, routing, - CoreModule.forRoot({ - appConfigFile: appConfigFile - }), + CoreModule, MaterialModule, LoginModule.forRoot(), SearchModule.forRoot(), diff --git a/demo-shell-ng2/app/components/about/about.component.ts b/demo-shell-ng2/app/components/about/about.component.ts index 5b54dc5b33..7fb556da91 100644 --- a/demo-shell-ng2/app/components/about/about.component.ts +++ b/demo-shell-ng2/app/components/about/about.component.ts @@ -21,7 +21,7 @@ import { AlfrescoAuthenticationService, AppConfigService, BpmProductVersionModel import { ObjectDataTableAdapter } from 'ng2-alfresco-datatable'; @Component({ - selector: 'about-page', + selector: 'adf-about-page', templateUrl: './about.component.html', styleUrls: ['./about.component.css'] }) @@ -30,7 +30,7 @@ export class AboutComponent implements OnInit { data: ObjectDataTableAdapter; githubUrlCommitAlpha: string = 'https://github.com/Alfresco/alfresco-ng2-components/commits/'; - configFile: string = ''; + configFile: string = 'app.config.json'; ecmHost: string = ''; bpmHost: string = ''; @@ -85,7 +85,6 @@ export class AboutComponent implements OnInit { ]); }); - this.configFile = this.appConfig.configFile; this.ecmHost = this.appConfig.get('ecmHost'); this.bpmHost = this.appConfig.get('bpmHost'); } diff --git a/demo-shell-ng2/config/webpack.common.js b/demo-shell-ng2/config/webpack.common.js index 9436c1774d..af9c79a902 100644 --- a/demo-shell-ng2/config/webpack.common.js +++ b/demo-shell-ng2/config/webpack.common.js @@ -98,12 +98,6 @@ module.exports = { from: '**/*.json', to: 'resources/i18n' }, - { - from: 'app.config-dev.json' - }, - { - from: 'app.config-prod.json' - }, { from: 'favicon-96x96.png' }, diff --git a/demo-shell-ng2/config/webpack.dev.js b/demo-shell-ng2/config/webpack.dev.js index 2795886c12..782d0dcd32 100644 --- a/demo-shell-ng2/config/webpack.dev.js +++ b/demo-shell-ng2/config/webpack.dev.js @@ -5,7 +5,7 @@ const commonConfig = require('./webpack.common.js'); const helpers = require('./helpers'); const path = require('path'); const CopyWebpackPlugin = require('copy-webpack-plugin'); -var HappyPack = require('happypack'); +const HappyPack = require('happypack'); const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin'); const alfrescoLibs = [ @@ -131,7 +131,11 @@ module.exports = webpackMerge(commonConfig, { from: '**/*', to: `assets/${lib}/i18n/` } - }) + }), + { + from: 'app.config-dev.json', + to: 'app.config.json' + } ]), new CopyWebpackPlugin([ { diff --git a/demo-shell-ng2/config/webpack.prod.js b/demo-shell-ng2/config/webpack.prod.js index 6c632c5484..92012b9bf2 100644 --- a/demo-shell-ng2/config/webpack.prod.js +++ b/demo-shell-ng2/config/webpack.prod.js @@ -100,6 +100,10 @@ module.exports = webpackMerge(commonConfig, { context: `node_modules/ng2-alfresco-core/prebuilt-themes/`, from: '**/*.css', to: 'prebuilt-themes' + }, + { + from: 'app.config-prod.json', + to: 'app.config.json' } ]), new webpack.NoEmitOnErrorsPlugin(), diff --git a/ng2-components/ng2-activiti-analytics/src/components/analytics-report-list.component.spec.ts b/ng2-components/ng2-activiti-analytics/src/components/analytics-report-list.component.spec.ts index 33c30c0b46..706244409c 100644 --- a/ng2-components/ng2-activiti-analytics/src/components/analytics-report-list.component.spec.ts +++ b/ng2-components/ng2-activiti-analytics/src/components/analytics-report-list.component.spec.ts @@ -18,7 +18,7 @@ import { DebugElement } from '@angular/core'; import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { ReportParametersModel } from 'ng2-activiti-diagrams'; -import { AlfrescoTranslationService, AppConfigModule, CoreModule } from 'ng2-alfresco-core'; +import { AlfrescoTranslationService, AppConfigService, CoreModule } from 'ng2-alfresco-core'; import { Observable } from 'rxjs/Rx'; import { AnalyticsReportListComponent } from '../components/analytics-report-list.component'; import { AnalyticsService } from '../services/analytics.service'; @@ -45,10 +45,7 @@ describe('AnalyticsReportListComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ - CoreModule.forRoot(), - AppConfigModule.forRoot('app.config.json', { - bpmHost: 'http://localhost:9876/bpm' - }) + CoreModule ], declarations: [ AnalyticsReportListComponent @@ -66,6 +63,9 @@ describe('AnalyticsReportListComponent', () => { })); beforeEach(() => { + let appConfig: AppConfigService = TestBed.get(AppConfigService); + appConfig.config.bpmHost = 'http://localhost:9876/bpm'; + fixture = TestBed.createComponent(AnalyticsReportListComponent); component = fixture.componentInstance; debug = fixture.debugElement; diff --git a/ng2-components/ng2-activiti-analytics/src/components/analytics-report-parameters.component.spec.ts b/ng2-components/ng2-activiti-analytics/src/components/analytics-report-parameters.component.spec.ts index ac2eb4bef0..c6d08826d3 100644 --- a/ng2-components/ng2-activiti-analytics/src/components/analytics-report-parameters.component.spec.ts +++ b/ng2-components/ng2-activiti-analytics/src/components/analytics-report-parameters.component.spec.ts @@ -18,7 +18,7 @@ import { DebugElement, SimpleChange } from '@angular/core'; import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { ReportParametersModel } from 'ng2-activiti-diagrams'; -import { AlfrescoTranslationService, AppConfigModule, CoreModule } from 'ng2-alfresco-core'; +import { AlfrescoTranslationService, AppConfigService, CoreModule } from 'ng2-alfresco-core'; import { Observable } from 'rxjs/Rx'; import * as analyticParamsMock from '../assets/analyticsParamsReportComponent.mock'; import { AnalyticsReportParametersComponent } from '../components/analytics-report-parameters.component'; @@ -41,10 +41,7 @@ describe('AnalyticsReportParametersComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ - CoreModule.forRoot(), - AppConfigModule.forRoot('app.config.json', { - bpmHost: 'http://localhost:9876/bpm' - }), + CoreModule, MaterialModule ], declarations: [ @@ -70,6 +67,9 @@ describe('AnalyticsReportParametersComponent', () => { })); beforeEach(() => { + let appConfig: AppConfigService = TestBed.get(AppConfigService); + appConfig.config.bpmHost = 'http://localhost:9876/bpm'; + fixture = TestBed.createComponent(AnalyticsReportParametersComponent); component = fixture.componentInstance; debug = fixture.debugElement; diff --git a/ng2-components/ng2-activiti-tasklist/src/components/task-filters.component.spec.ts b/ng2-components/ng2-activiti-tasklist/src/components/task-filters.component.spec.ts index 96916f3643..6445500437 100644 --- a/ng2-components/ng2-activiti-tasklist/src/components/task-filters.component.spec.ts +++ b/ng2-components/ng2-activiti-tasklist/src/components/task-filters.component.spec.ts @@ -17,10 +17,9 @@ import { SimpleChange } from '@angular/core'; import { async, ComponentFixture, TestBed } from '@angular/core/testing'; -import { AppConfigModule, AppConfigService, CoreModule, TranslationService } from 'ng2-alfresco-core'; +import { AppConfigService, CoreModule, TranslationService } from 'ng2-alfresco-core'; import { Observable } from 'rxjs/Rx'; -import { AppConfigServiceMock } from '../assets/app-config.service.mock'; import { TranslationMock } from '../assets/translation.service.mock'; import { FilterParamsModel, FilterRepresentationModel } from '../models/filter.model'; import { TaskListService } from '../services/tasklist.service'; @@ -70,24 +69,23 @@ describe('TaskFiltersComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ - CoreModule.forRoot(), - AppConfigModule.forRoot('app.config.json', { - bpmHost: 'http://localhost:9876/bpm' - }) + CoreModule ], declarations: [ TaskFiltersComponent ], providers: [ TaskListService, - { provide: TranslationService, useClass: TranslationMock }, - { provide: AppConfigService, useClass: AppConfigServiceMock } + { provide: TranslationService, useClass: TranslationMock } ] }).compileComponents(); })); beforeEach(() => { + let appConfig: AppConfigService = TestBed.get(AppConfigService); + appConfig.config.bpmHost = 'http://localhost:9876/bpm'; + fixture = TestBed.createComponent(TaskFiltersComponent); component = fixture.componentInstance; diff --git a/ng2-components/ng2-activiti-tasklist/src/components/tasklist.component.spec.ts b/ng2-components/ng2-activiti-tasklist/src/components/tasklist.component.spec.ts index a7c8c92f49..84b0c2a50f 100644 --- a/ng2-components/ng2-activiti-tasklist/src/components/tasklist.component.spec.ts +++ b/ng2-components/ng2-activiti-tasklist/src/components/tasklist.component.spec.ts @@ -18,10 +18,9 @@ import { SimpleChange } from '@angular/core'; import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { MdProgressSpinnerModule } from '@angular/material'; -import { AppConfigModule, AppConfigService, CoreModule, TranslationService } from 'ng2-alfresco-core'; +import { AppConfigService, CoreModule, TranslationService } from 'ng2-alfresco-core'; import { DataTableModule } from 'ng2-alfresco-datatable'; import { DataRowEvent, ObjectDataRow, ObjectDataTableAdapter } from 'ng2-alfresco-datatable'; -import { AppConfigServiceMock } from '../assets/app-config.service.mock'; import { TranslationMock } from '../assets/translation.service.mock'; import { TaskListService } from '../services/tasklist.service'; import { TaskListComponent } from './tasklist.component'; @@ -93,10 +92,7 @@ describe('TaskListComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ - CoreModule.forRoot(), - AppConfigModule.forRoot('app.config.json', { - bpmHost: 'http://localhost:9876/bpm' - }), + CoreModule, DataTableModule, MdProgressSpinnerModule ], @@ -105,14 +101,15 @@ describe('TaskListComponent', () => { ], providers: [ TaskListService, - {provide: TranslationService, useClass: TranslationMock}, - {provide: AppConfigService, useClass: AppConfigServiceMock} + {provide: TranslationService, useClass: TranslationMock} ] }).compileComponents(); })); beforeEach(() => { + let appConfig: AppConfigService = TestBed.get(AppConfigService); + appConfig.config.bpmHost = 'http://localhost:9876/bpm'; fixture = TestBed.createComponent(TaskListComponent); component = fixture.componentInstance; diff --git a/ng2-components/ng2-alfresco-core/README.md b/ng2-components/ng2-alfresco-core/README.md index c70184d467..7e6ac741a4 100644 --- a/ng2-components/ng2-alfresco-core/README.md +++ b/ng2-components/ng2-alfresco-core/README.md @@ -808,22 +808,6 @@ Example of the default settings file content: Please note that settings above are default ones coming with the server. You can override the values in your custom `app.config.json` file if needed. -You can also change the path or name of the configuration file when importing the CoreModule in your main application. - -```ts -... -@NgModule({ - imports: [ - ... - CoreModule.forRoot({ - appConfigFile: 'app.production.config.json' - }) - ], - ... -} -export class AppModule { } -``` - Below is a simple example of using the AppConfigService in practice. **app.component.ts** @@ -849,28 +833,6 @@ export class AppComponent { Your custom components can also benefit from the `AppConfigService`, you can put an unlimited number of settings and optionally a nested JSON hierarchy. -#### Different configurations based on environment settings - -The CoreModule allows you to provide custom application configuration path. -That means you can evaluate the final file name based on conditions, for example environment settings: - -```ts -let appConfigFile = 'app.config-dev.json'; -if (process.env.ENV === 'production') { - appConfigFile = 'app.config-prod.json'; -} - -@NgModule({ - imports: [ - ... - CoreModule.forRoot({ - appConfigFile: appConfigFile - }), - ... - ] -}) -``` - #### Variable substitution in configuration strings The `AppConfigService` also supports a limited set of variable substitutions to greatly simplify certain scenarios. @@ -892,31 +854,6 @@ The supported variables are: | hostname | `location.hostname` | | port | `location.port` | -#### Unit testing - -You can also provide custom values for the entire service. -This might become handy when creating unit tests. - -```ts -describe('MyTest', () => { - - beforeEach(() => { - TestBed.configureTestingModule({ - imports: [ - CoreModule.forRoot(), - AppConfigModule.forRoot('app.config.json', { - ecmHost: 'http://localhost:9876/ecm' - }) - ] - }); - }); - -}); -``` - -In the example above custom values are applied on the top of all the values the `AppConfigService` has previously loaded. -If there is an 'app.config.json' file loaded at unit test run time then your custom values will overwrite exiting values with the same keys if present. - ## User Preferences Service Stores preferences for components. diff --git a/ng2-components/ng2-alfresco-core/index.ts b/ng2-components/ng2-alfresco-core/index.ts index 373d4bd815..e0801caff3 100644 --- a/ng2-components/ng2-alfresco-core/index.ts +++ b/ng2-components/ng2-alfresco-core/index.ts @@ -36,8 +36,6 @@ import { DownloadZipDialogComponent } from './src/dialogs/download-zip.dialog'; import { AlfrescoApiService } from './src/services/alfresco-api.service'; import { AlfrescoContentService } from './src/services/alfresco-content.service'; import { AlfrescoSettingsService } from './src/services/alfresco-settings.service'; -import { AppConfigService } from './src/services/app-config.service'; -import { InitAppConfigServiceProvider } from './src/services/app-config.service'; import { AuthGuardBpm } from './src/services/auth-guard-bpm.service'; import { AuthGuardEcm } from './src/services/auth-guard-ecm.service'; import { AuthGuard } from './src/services/auth-guard.service'; @@ -90,7 +88,6 @@ export { AuthenticationService } from './src/services/authentication.service'; export { TranslationService, TRANSLATION_PROVIDER, TranslationProvider } from './src/services/translation.service'; export { AlfrescoTranslateLoader } from './src/services/translate-loader.service'; export { AppConfigService } from './src/services/app-config.service'; -export { InitAppConfigServiceProvider } from './src/services/app-config.service'; export { ThumbnailService } from './src/services/thumbnail.service'; export { UploadService } from './src/services/upload.service'; export { CardViewUpdateService } from './src/services/card-view-update.service'; @@ -279,6 +276,7 @@ export function createTranslateLoader(http: Http, logService: LogService) { } ], exports: [ + AppConfigModule, BrowserAnimationsModule, CommonModule, FormsModule, @@ -313,16 +311,13 @@ export function createTranslateLoader(http: Http, logService: LogService) { ] }) export class CoreModule { + /** @deprecated in 1.9.0 */ static forRoot(opts: any = {}): ModuleWithProviders { - - const appConfigFile = opts.appConfigFile || 'app.config.json'; - + console.log('CoreModule.forRoot is deprecated and will be removed in future versions'); return { ngModule: CoreModule, providers: [ - ...providers(), - AppConfigService, - InitAppConfigServiceProvider(appConfigFile) + ...providers() ] }; } diff --git a/ng2-components/ng2-alfresco-core/src/services/alfresco-content.service.spec.ts b/ng2-components/ng2-alfresco-core/src/services/alfresco-content.service.spec.ts index dbdb21d172..76983c118e 100644 --- a/ng2-components/ng2-alfresco-core/src/services/alfresco-content.service.spec.ts +++ b/ng2-components/ng2-alfresco-core/src/services/alfresco-content.service.spec.ts @@ -20,7 +20,7 @@ import { CookieServiceMock } from './../assets/cookie.service.mock'; import { AlfrescoApiService } from './alfresco-api.service'; import { AlfrescoContentService } from './alfresco-content.service'; import { AlfrescoSettingsService } from './alfresco-settings.service'; -import { AppConfigModule } from './app-config.service'; +import { AppConfigModule, AppConfigService } from './app-config.service'; import { AuthenticationService } from './authentication.service'; import { CookieService } from './cookie.service'; import { LogService } from './log.service'; @@ -72,6 +72,11 @@ describe('AlfrescoContentService', () => { }; jasmine.Ajax.install(); + + let appConfig: AppConfigService = TestBed.get(AppConfigService); + appConfig.config = { + ecmHost: 'http://localhost:9876/ecm' + }; }); afterEach(() => { diff --git a/ng2-components/ng2-alfresco-core/src/services/app-config.service.spec.ts b/ng2-components/ng2-alfresco-core/src/services/app-config.service.spec.ts index 07547ff0fc..079d58648b 100644 --- a/ng2-components/ng2-alfresco-core/src/services/app-config.service.spec.ts +++ b/ng2-components/ng2-alfresco-core/src/services/app-config.service.spec.ts @@ -24,12 +24,12 @@ describe('AppConfigService', () => { let appConfigService: AppConfigService; const mockResponse = { - 'ecmHost': 'http://localhost:4000/ecm', - 'bpmHost': 'http://localhost:4000/ecm', - 'application': { - 'name': 'Custom Name' + ecmHost: 'http://localhost:4000/ecm', + bpmHost: 'http://localhost:4000/ecm', + application: { + name: 'Custom Name' }, - 'files': { + files: { 'excluded': ['exluded'] } }; @@ -76,27 +76,8 @@ describe('AppConfigService', () => { }); }); - it('should use default config file', () => { - expect(appConfigService.configFile).toBeNull(); - appConfigService.load().then(() => { - expect(appConfigService.configFile).toBe('app.config.json'); - }); - }); - - it('should take custom config file', () => { - expect(appConfigService.configFile).toBeNull(); - - const name = 'custom.config.json'; - appConfigService.load(name).then(() => { - expect(appConfigService.configFile).toBe(name); - }); - }); - it('should take excluded file list', () => { - expect(appConfigService.configFile).toBeNull(); - - const name = 'custom.config.json'; - appConfigService.load(name).then(() => { + appConfigService.load().then(() => { expect(appConfigService.get('files.excluded')[0]).toBe('exluded'); }); }); diff --git a/ng2-components/ng2-alfresco-core/src/services/app-config.service.ts b/ng2-components/ng2-alfresco-core/src/services/app-config.service.ts index ebc730f69b..18cde51f98 100644 --- a/ng2-components/ng2-alfresco-core/src/services/app-config.service.ts +++ b/ng2-components/ng2-alfresco-core/src/services/app-config.service.ts @@ -15,20 +15,18 @@ * limitations under the License. */ -import { APP_INITIALIZER, Injectable, ModuleWithProviders, NgModule } from '@angular/core'; +import { APP_INITIALIZER, Injectable, NgModule } from '@angular/core'; import { Http, HttpModule } from '@angular/http'; import { ObjectUtils } from '../utils/object-utils'; @Injectable() export class AppConfigService { - private config: any = { - 'ecmHost': 'http://{hostname}:{port}/ecm', - 'bpmHost': 'http://{hostname}:{port}/bpm' + config: any = { + ecmHost: 'http://{hostname}:{port}/ecm', + bpmHost: 'http://{hostname}:{port}/bpm' }; - configFile: string = null; - constructor(private http: Http) {} get(key: string, defaultValue?: T): T { @@ -45,18 +43,14 @@ export class AppConfigService { return result; } - load(resource: string = 'app.config.json', values?: {}): Promise { - this.configFile = resource; + load(): Promise { return new Promise(resolve => { - this.config = Object.assign({}, values || {}); - this.http.get(resource).subscribe( + this.http.get('app.config.json').subscribe( data => { this.config = Object.assign({}, this.config, data.json() || {}); resolve(this.config); }, () => { - const errorMessage = `Error loading ${resource}`; - console.log(errorMessage); resolve(this.config); } ); @@ -75,17 +69,8 @@ export class AppConfigService { } } -export function InitAppConfigServiceProvider(resource: string, values?: {}): any { - return { - provide: APP_INITIALIZER, - useFactory: (configService: AppConfigService) => { - return () => configService.load(resource, values); - }, - deps: [ - AppConfigService - ], - multi: true - }; +export function startupServiceFactory(configService: AppConfigService): Function { + return () => configService.load(); } @NgModule({ @@ -93,17 +78,15 @@ export function InitAppConfigServiceProvider(resource: string, values?: {}): any HttpModule ], providers: [ - AppConfigService + AppConfigService, + { + provide: APP_INITIALIZER, + useFactory: startupServiceFactory, + deps: [ + AppConfigService + ], + multi: true + } ] }) -export class AppConfigModule { - static forRoot(resource: string, values?: {}): ModuleWithProviders { - return { - ngModule: AppConfigModule, - providers: [ - AppConfigService, - InitAppConfigServiceProvider(resource, values) - ] - }; - } -} +export class AppConfigModule {} diff --git a/ng2-components/ng2-alfresco-core/src/services/discovery-api.service.spec.ts b/ng2-components/ng2-alfresco-core/src/services/discovery-api.service.spec.ts index 20b1e10db3..7e5e767f6f 100644 --- a/ng2-components/ng2-alfresco-core/src/services/discovery-api.service.spec.ts +++ b/ng2-components/ng2-alfresco-core/src/services/discovery-api.service.spec.ts @@ -19,7 +19,7 @@ import { async, TestBed } from '@angular/core/testing'; import { BpmProductVersionModel, EcmProductVersionModel } from '../models/product-version.model'; import { AlfrescoApiService } from './alfresco-api.service'; import { AlfrescoSettingsService } from './alfresco-settings.service'; -import { AppConfigModule } from './app-config.service'; +import { AppConfigModule, AppConfigService } from './app-config.service'; import { AuthenticationService } from './authentication.service'; import { DiscoveryApiService } from './discovery-api.service'; import { StorageService } from './storage.service'; @@ -97,9 +97,7 @@ describe('Discovery Api Service', () => { beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ - AppConfigModule.forRoot('app.config.json', { - ecmHost: 'http://localhost:9876/ecm' - }) + AppConfigModule ], providers: [ DiscoveryApiService, @@ -113,6 +111,10 @@ describe('Discovery Api Service', () => { })); beforeEach(() => { + let appConfig: AppConfigService = TestBed.get(AppConfigService); + appConfig.config = { + ecmHost: 'http://localhost:9876/ecm' + }; service = TestBed.get(DiscoveryApiService); jasmine.Ajax.install(); }); diff --git a/ng2-components/ng2-alfresco-core/src/services/shared-links-api.service.spec.ts b/ng2-components/ng2-alfresco-core/src/services/shared-links-api.service.spec.ts deleted file mode 100644 index 7f2e751b31..0000000000 --- a/ng2-components/ng2-alfresco-core/src/services/shared-links-api.service.spec.ts +++ /dev/null @@ -1,169 +0,0 @@ -/*! - * @license - * Copyright 2016 Alfresco Software, Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { async, inject, TestBed } from '@angular/core/testing'; -import { AlfrescoApiService } from './alfresco-api.service'; -import { AppConfigModule } from './app-config.service'; -import { SharedLinksApiService } from './shared-links-api.service'; -import { StorageService } from './storage.service'; -import { UserPreferencesService } from './user-preferences.service'; - -class TestConfig { - service: any = null; - setup: any = { - rejectFindSharedLinks: false - }; - - constructor(setup: any = {}) { - Object.assign(this.setup, setup); - - const { alfrescoApiServiceMock } = this; - - const alfrescoApiServiceProvider = { - provide: AlfrescoApiService, - useValue: alfrescoApiServiceMock - }; - - TestBed.configureTestingModule({ - imports: [ - AppConfigModule.forRoot('app.config.json', { - pagination: { - size: 20 - } - }) - ], - providers: [ - alfrescoApiServiceProvider, - SharedLinksApiService, - StorageService, - UserPreferencesService - ] - }); - - inject([ SharedLinksApiService ], (service: SharedLinksApiService) => { - this.service = service; - })(); - } - - private get alfrescoApiServiceMock(): any { - const { setup } = this; - - const nodePagingSample = { - list: { - entries: [ - { entry: {} }, - { entry: {} } - ], - pagination: {} - } - }; - - const sharedLinksApiMock = { - findSharedLinks: jasmine.createSpy('findSharedLinks').and.callFake(() => { - return new Promise((resolve, reject) => { - setup.rejectFindSharedLinks - ? reject() - : resolve(nodePagingSample); - }); - }) - }; - - return { - getInstance: () => { - return { - core: { sharedlinksApi: sharedLinksApiMock } - }; - } - }; - } - - get findSharedLinksSpy(): any { - return this.service.sharedLinksApi.findSharedLinks; - } - - get findSharedLinksArgs(): any[] { - return this.findSharedLinksSpy.calls.mostRecent().args; - } -} - -describe('SharedLinks API', () => { - describe('getSharedLinks', () => { - describe('Provide a NodePaging', () => { - beforeEach(() => { - this.config = new TestConfig(); - }); - - it('provides a node paging with entries', async(() => { - this.config.service.getSharedLinks().subscribe((paging) => { - const { list: { entries, pagination } } = paging; - - expect(entries).toEqual(jasmine.any(Array)); - expect(pagination).toEqual(jasmine.any(Object)); - expect(entries.length).toBe(2); - }); - })); - }); - - describe('Manage query options', () => { - beforeEach(() => { - this.config = new TestConfig(); - - this.getCalledArgs = () => { - return this.config.findSharedLinksArgs; - }; - }); - - it('has default options', async(() => { - this.config.service.getSharedLinks(); - - const [ { maxItems, skipCount } ] = this.getCalledArgs(); - - expect(maxItems).toBe(20); - expect(skipCount).toBe(0); - })); - - it('combines custom and default options', async(() => { - this.config.service.getSharedLinks({ - maxItems: 5 - }); - - const [ { maxItems, skipCount } ] = this.getCalledArgs(); - - expect(maxItems).toBe(5); - expect(skipCount).toBe(0); - })); - }); - - describe('Error handling', () => { - beforeEach(() => { - const config = new TestConfig({ - rejectFindSharedLinks: true - }); - - this.service = config.service; - this.spy = spyOn(config.service, 'handleError') - .and.callThrough(); - }); - - it('handles error on failure', async(() => { - this.service.getSharedLinks().subscribe(() => { - expect(this.spy).toHaveBeenCalled(); - }); - })); - }); - }); -}); diff --git a/ng2-components/ng2-alfresco-core/src/services/sites-api.service.spec.ts b/ng2-components/ng2-alfresco-core/src/services/sites-api.service.spec.ts index 4bf1838dbe..0149b1829d 100644 --- a/ng2-components/ng2-alfresco-core/src/services/sites-api.service.spec.ts +++ b/ng2-components/ng2-alfresco-core/src/services/sites-api.service.spec.ts @@ -18,7 +18,7 @@ import { async, TestBed } from '@angular/core/testing'; import { AlfrescoApiService } from './alfresco-api.service'; import { AlfrescoSettingsService } from './alfresco-settings.service'; -import { AppConfigModule } from './app-config.service'; +import { AppConfigModule, AppConfigService } from './app-config.service'; import { AuthenticationService } from './authentication.service'; import { SitesApiService } from './sites-api.service'; import { StorageService } from './storage.service'; @@ -33,12 +33,7 @@ describe('Sites service', () => { beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ - AppConfigModule.forRoot('app.config.json', { - ecmHost: 'http://localhost:9876/ecm', - files: { - excluded: ['.DS_Store', 'desktop.ini', '.git', '*.git'] - } - }) + AppConfigModule ], providers: [ SitesApiService, @@ -52,6 +47,14 @@ describe('Sites service', () => { })); beforeEach(() => { + let appConfig: AppConfigService = TestBed.get(AppConfigService); + appConfig.config = { + ecmHost: 'http://localhost:9876/ecm', + files: { + excluded: ['.DS_Store', 'desktop.ini', '.git', '*.git'] + } + }; + service = TestBed.get(SitesApiService); jasmine.Ajax.install(); }); diff --git a/ng2-components/ng2-alfresco-core/src/services/upload.service.spec.ts b/ng2-components/ng2-alfresco-core/src/services/upload.service.spec.ts index 8ce994cfc2..334b3ef13a 100644 --- a/ng2-components/ng2-alfresco-core/src/services/upload.service.spec.ts +++ b/ng2-components/ng2-alfresco-core/src/services/upload.service.spec.ts @@ -16,11 +16,11 @@ */ import { EventEmitter } from '@angular/core'; -import { TestBed } from '@angular/core/testing'; +import { async, TestBed } from '@angular/core/testing'; import { FileModel, FileUploadOptions, FileUploadStatus } from '../models/file.model'; import { AlfrescoApiService } from './alfresco-api.service'; import { AlfrescoSettingsService } from './alfresco-settings.service'; -import { AppConfigModule } from './app-config.service'; +import { AppConfigModule, AppConfigService } from './app-config.service'; import { AuthenticationService } from './authentication.service'; import { StorageService } from './storage.service'; import { UploadService } from './upload.service'; @@ -30,15 +30,10 @@ declare let jasmine: any; describe('UploadService', () => { let service: UploadService; - beforeEach(() => { + beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ - AppConfigModule.forRoot('app.config.json', { - ecmHost: 'http://localhost:9876/ecm', - files: { - excluded: ['.DS_Store', 'desktop.ini', '.git', '*.git'] - } - }) + AppConfigModule ], providers: [ UploadService, @@ -47,7 +42,18 @@ describe('UploadService', () => { AuthenticationService, StorageService ] - }); + }).compileComponents(); + })); + + beforeEach(() => { + let appConfig: AppConfigService = TestBed.get(AppConfigService); + appConfig.config = { + ecmHost: 'http://localhost:9876/ecm', + files: { + excluded: ['.DS_Store', 'desktop.ini', '.git', '*.git'] + } + }; + service = TestBed.get(UploadService); jasmine.Ajax.install(); }); diff --git a/ng2-components/ng2-alfresco-core/src/services/user-preferences.service.spec.ts b/ng2-components/ng2-alfresco-core/src/services/user-preferences.service.spec.ts index bbc39ceae9..67901ed591 100644 --- a/ng2-components/ng2-alfresco-core/src/services/user-preferences.service.spec.ts +++ b/ng2-components/ng2-alfresco-core/src/services/user-preferences.service.spec.ts @@ -17,7 +17,7 @@ import { async, TestBed } from '@angular/core/testing'; import { AlfrescoApiService } from './alfresco-api.service'; -import { AppConfigModule } from './app-config.service'; +import { AppConfigModule, AppConfigService } from './app-config.service'; import { StorageService } from './storage.service'; import { UserPreferencesService } from './user-preferences.service'; @@ -30,11 +30,7 @@ describe('UserPreferencesService', () => { beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ - AppConfigModule.forRoot('app.config.json', { - 'pagination': { - 'size': defaultPaginationSize - } - }) + AppConfigModule ], providers: [ AlfrescoApiService, @@ -45,6 +41,10 @@ describe('UserPreferencesService', () => { })); beforeEach(() => { + let appConfig: AppConfigService = TestBed.get(AppConfigService); + appConfig.config.pagination = { + size: 10 + }; preferences = TestBed.get(UserPreferencesService); storage = TestBed.get(StorageService); }); diff --git a/ng2-components/ng2-alfresco-documentlist/src/services/folder-actions.service.spec.ts b/ng2-components/ng2-alfresco-documentlist/src/services/folder-actions.service.spec.ts index 6c3f6ce354..87a21b7c29 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/services/folder-actions.service.spec.ts +++ b/ng2-components/ng2-alfresco-documentlist/src/services/folder-actions.service.spec.ts @@ -16,7 +16,7 @@ */ import { async, TestBed } from '@angular/core/testing'; -import { AlfrescoTranslationService, AppConfigModule, CoreModule, NotificationService } from 'ng2-alfresco-core'; +import { AlfrescoTranslationService, AppConfigService, CoreModule, NotificationService } from 'ng2-alfresco-core'; import { Observable } from 'rxjs/Rx'; import { FileNode, FolderNode } from '../assets/document-library.model.mock'; import { ContentActionHandler } from '../models/content-action.model'; @@ -32,10 +32,7 @@ describe('FolderActionsService', () => { beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ - CoreModule.forRoot(), - AppConfigModule.forRoot('app.config.json', { - ecmHost: 'http://localhost:9876/ecm' - }) + CoreModule ], providers: [ DocumentListService, @@ -48,6 +45,9 @@ describe('FolderActionsService', () => { })); beforeEach(() => { + let appConfig: AppConfigService = TestBed.get(AppConfigService); + appConfig.config.ecmHost = 'http://localhost:9876/ecm'; + service = TestBed.get(FolderActionsService); documentListService = TestBed.get(DocumentListService); }); diff --git a/ng2-components/ng2-alfresco-tag/src/components/tag-actions.component.spec.ts b/ng2-components/ng2-alfresco-tag/src/components/tag-actions.component.spec.ts index 4cd090c0f0..009d5c30ef 100644 --- a/ng2-components/ng2-alfresco-tag/src/components/tag-actions.component.spec.ts +++ b/ng2-components/ng2-alfresco-tag/src/components/tag-actions.component.spec.ts @@ -17,7 +17,7 @@ import { DebugElement } from '@angular/core'; import { async, ComponentFixture, TestBed } from '@angular/core/testing'; -import { AppConfigModule, CoreModule } from 'ng2-alfresco-core'; +import { AppConfigService, CoreModule } from 'ng2-alfresco-core'; import { TagActionsComponent } from '../components/tag-actions.component'; import { TagService } from '../services/tag.service'; import { MaterialModule } from './material.module'; @@ -35,10 +35,7 @@ describe('TagActionsComponent', () => { TestBed.configureTestingModule({ imports: [ MaterialModule, - CoreModule.forRoot(), - AppConfigModule.forRoot('app.config.json', { - ecmHost: 'http://localhost:9876/ecm' - }) + CoreModule ], declarations: [ TagActionsComponent @@ -50,6 +47,9 @@ describe('TagActionsComponent', () => { })); beforeEach(() => { + let appConfig: AppConfigService = TestBed.get(AppConfigService); + appConfig.config.ecmHost = 'http://localhost:9876/ecm'; + fixture = TestBed.createComponent(TagActionsComponent); debug = fixture.debugElement; diff --git a/ng2-components/ng2-alfresco-tag/src/components/tag-list.component.spec.ts b/ng2-components/ng2-alfresco-tag/src/components/tag-list.component.spec.ts index da45fa29ea..a8bbd0cdad 100644 --- a/ng2-components/ng2-alfresco-tag/src/components/tag-list.component.spec.ts +++ b/ng2-components/ng2-alfresco-tag/src/components/tag-list.component.spec.ts @@ -17,7 +17,7 @@ import { DebugElement } from '@angular/core'; import { async, ComponentFixture, TestBed } from '@angular/core/testing'; -import { AppConfigModule, CoreModule } from 'ng2-alfresco-core'; +import { AppConfigService, CoreModule } from 'ng2-alfresco-core'; import { TagService } from '../services/tag.service'; import { TagListComponent } from './../components/tag-list.component'; import { MaterialModule } from './material.module'; @@ -52,10 +52,7 @@ describe('TagList', () => { TestBed.configureTestingModule({ imports: [ MaterialModule, - CoreModule.forRoot(), - AppConfigModule.forRoot('app.config.json', { - ecmHost: 'http://localhost:9876/ecm' - }) + CoreModule ], declarations: [ TagListComponent @@ -67,6 +64,9 @@ describe('TagList', () => { })); beforeEach(() => { + let appConfig: AppConfigService = TestBed.get(AppConfigService); + appConfig.config.ecmHost = 'http://localhost:9876/ecm'; + fixture = TestBed.createComponent(TagListComponent); debug = fixture.debugElement; diff --git a/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.spec.ts b/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.spec.ts index 36b5a24b08..ea4c08e0ad 100644 --- a/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.spec.ts +++ b/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.spec.ts @@ -17,7 +17,7 @@ import { DebugElement } from '@angular/core'; import { async, ComponentFixture, TestBed } from '@angular/core/testing'; -import { AppConfigModule, CoreModule } from 'ng2-alfresco-core'; +import { AppConfigService, CoreModule } from 'ng2-alfresco-core'; import { TagNodeListComponent } from '../components/tag-node-list.component'; import { TagService } from '../services/tag.service'; import { MaterialModule } from './material.module'; @@ -52,10 +52,7 @@ describe('TagNodeList', () => { TestBed.configureTestingModule({ imports: [ MaterialModule, - CoreModule.forRoot(), - AppConfigModule.forRoot('app.config.json', { - ecmHost: 'http://localhost:9876/ecm' - }) + CoreModule ], declarations: [ TagNodeListComponent @@ -67,6 +64,9 @@ describe('TagNodeList', () => { })); beforeEach(() => { + let appConfig: AppConfigService = TestBed.get(AppConfigService); + appConfig.config.ecmHost = 'http://localhost:9876/ecm'; + fixture = TestBed.createComponent(TagNodeListComponent); debug = fixture.debugElement; diff --git a/ng2-components/ng2-alfresco-tag/src/services/tag.service.spec.ts b/ng2-components/ng2-alfresco-tag/src/services/tag.service.spec.ts index 6f45a9d143..278411309d 100644 --- a/ng2-components/ng2-alfresco-tag/src/services/tag.service.spec.ts +++ b/ng2-components/ng2-alfresco-tag/src/services/tag.service.spec.ts @@ -16,7 +16,7 @@ */ import { async, TestBed } from '@angular/core/testing'; -import { AppConfigModule, CoreModule } from 'ng2-alfresco-core'; +import { AppConfigService, CoreModule } from 'ng2-alfresco-core'; import { TagService } from '../services/tag.service'; declare let jasmine: any; @@ -28,10 +28,7 @@ describe('TagService', () => { beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ - CoreModule.forRoot(), - AppConfigModule.forRoot('app.config.json', { - ecmHost: 'http://localhost:9876/ecm' - }) + CoreModule ], providers: [ TagService @@ -40,6 +37,9 @@ describe('TagService', () => { })); beforeEach(() => { + let appConfig: AppConfigService = TestBed.get(AppConfigService); + appConfig.config.ecmHost = 'http://localhost:9876/ecm'; + service = TestBed.get(TagService); }); diff --git a/ng2-components/ng2-alfresco-webscript/src/webscript.component.spec.ts b/ng2-components/ng2-alfresco-webscript/src/webscript.component.spec.ts index d699164cf4..2021eae7af 100644 --- a/ng2-components/ng2-alfresco-webscript/src/webscript.component.spec.ts +++ b/ng2-components/ng2-alfresco-webscript/src/webscript.component.spec.ts @@ -17,7 +17,7 @@ import { DebugElement } from '@angular/core'; import { async, ComponentFixture, TestBed } from '@angular/core/testing'; -import { AppConfigModule, CoreModule } from 'ng2-alfresco-core'; +import { AppConfigService, CoreModule } from 'ng2-alfresco-core'; import { DataTableModule } from 'ng2-alfresco-datatable'; import { WebscriptComponent } from '../src/webscript.component'; @@ -33,10 +33,7 @@ describe('WebscriptComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ - CoreModule.forRoot(), - AppConfigModule.forRoot('app.config.json', { - ecmHost: 'http://localhost:9876/ecm' - }), + CoreModule, DataTableModule ], declarations: [ @@ -46,6 +43,9 @@ describe('WebscriptComponent', () => { })); beforeEach(() => { + let appConfig: AppConfigService = TestBed.get(AppConfigService); + appConfig.config.ecmHost = 'http://localhost:9876/ecm'; + fixture = TestBed.createComponent(WebscriptComponent); component = fixture.componentInstance;