Skip to content

Commit

Permalink
Merge pull request #6810 from abpframework/testing/6802
Browse files Browse the repository at this point in the history
Add ThemeSharedTestingModule and improved CoreTestingModule
  • Loading branch information
mehmet-erim authored Dec 25, 2020
2 parents e23805f + 4fc4172 commit 594bb00
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 81 deletions.
4 changes: 2 additions & 2 deletions npm/ng-packs/packages/core/src/lib/models/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventEmitter, Type } from '@angular/core';
import { Router, Routes } from '@angular/router';
import { Routes } from '@angular/router';
import { Subject } from 'rxjs';
import { eLayoutType } from '../enums/common';
import { Environment } from './environment';
Expand All @@ -13,7 +13,7 @@ export namespace ABP {
}

export interface Test extends Partial<Root> {
baseHref?: Router;
baseHref?: string;
routes?: Routes;
}

Expand Down
10 changes: 5 additions & 5 deletions npm/ng-packs/packages/core/src/lib/services/rest.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import { EnvironmentService } from './environment.service';
})
export class RestService {
constructor(
@Inject(CORE_OPTIONS) private options: ABP.Root,
private http: HttpClient,
private store: Store,
private environment: EnvironmentService,
@Inject(CORE_OPTIONS) protected options: ABP.Root,
protected http: HttpClient,
protected environment: EnvironmentService,
protected store: Store,
) {}

private getApiFromStore(apiName: string): string {
protected getApiFromStore(apiName: string): string {
return this.environment.getApiUrl(apiName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import {
coreOptionsFactory,
CORE_OPTIONS,
LocalizationPipe,
RestService,
} from '@abp/ng.core';
import { APP_BASE_HREF } from '@angular/common';
import { ModuleWithProviders, NgModule } from '@angular/core';
import { provideRoutes } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { MockLocalizationPipe } from './pipes/mock-localization.pipe';
import { MockRestService } from './services/mock-rest.service';

/**
* CoreTestingModule is the module that will be used in tests
Expand All @@ -21,7 +23,7 @@ import { MockLocalizationPipe } from './pipes/mock-localization.pipe';
declarations: [MockLocalizationPipe],
})
export class CoreTestingModule {
static forTest(
static withConfig(
{ baseHref = '/', routes = [], ...options } = {} as ABP.Test,
): ModuleWithProviders<CoreTestingModule> {
return {
Expand All @@ -41,6 +43,10 @@ export class CoreTestingModule {
provide: LocalizationPipe,
useClass: MockLocalizationPipe,
},
{
provide: RestService,
useClass: MockRestService,
},
provideRoutes(routes),
],
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './mock-rest.service';
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ABP, CORE_OPTIONS, EnvironmentService, RestService } from '@abp/ng.core';
import { HttpClient } from '@angular/common/http';
import { Inject, Injectable } from '@angular/core';
import { Observable, throwError } from 'rxjs';

@Injectable({
providedIn: 'root',
})
export class MockRestService extends RestService {
constructor(
@Inject(CORE_OPTIONS) protected options: ABP.Root,
protected http: HttpClient,
protected environment: EnvironmentService,
) {
super(options, http, environment, null);
}

handleError(err: any): Observable<any> {
return throwError(err);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export class ErrorHandler {
private actions: Actions,
private router: Router,
private confirmationService: ConfirmationService,
private appRef: ApplicationRef,
private cfRes: ComponentFactoryResolver,
private rendererFactory: RendererFactory2,
private injector: Injector,
Expand Down Expand Up @@ -281,14 +280,16 @@ export class ErrorHandler {
}

this.componentRef.instance.hideCloseIcon = this.httpErrorConfig.errorScreen.hideCloseIcon;
const appRef = this.injector.get(ApplicationRef);

if (this.canCreateCustomError(instance.status as ErrorScreenErrorCodes)) {
this.componentRef.instance.cfRes = this.cfRes;
this.componentRef.instance.appRef = this.appRef;
this.componentRef.instance.appRef = appRef;
this.componentRef.instance.injector = this.injector;
this.componentRef.instance.customComponent = this.httpErrorConfig.errorScreen.component;
}

this.appRef.attachView(this.componentRef.hostView);
appRef.attachView(this.componentRef.hostView);
renderer.appendChild(host, (this.componentRef.hostView as EmbeddedViewRef<any>).rootNodes[0]);

const destroy$ = new Subject<void>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { ConfirmationService } from '../services';
exports: [ConfirmationComponent],
entryComponents: [ConfirmationComponent],
declarations: [ConfirmationComponent],
imports: [CoreTestingModule.forTest()],
imports: [CoreTestingModule.withConfig()],
})
export class MockModule {}

Expand All @@ -22,7 +22,7 @@ describe('ConfirmationService', () => {
let service: ConfirmationService;
const createService = createServiceFactory({
service: ConfirmationService,
imports: [NgxsModule.forRoot(), CoreTestingModule.forTest(), MockModule],
imports: [NgxsModule.forRoot(), CoreTestingModule.withConfig(), MockModule],
});

beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const CONFIRMATION_BUTTONS = {
describe('ErrorHandler', () => {
const createService = createServiceFactory({
service: ErrorHandler,
imports: [NgxsModule.forRoot([]), CoreTestingModule.forTest(), MockModule],
imports: [NgxsModule.forRoot([]), CoreTestingModule.withConfig(), MockModule],
mocks: [OAuthService],
providers: [
{ provide: APP_BASE_HREF, useValue: '/' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ToasterService } from '../services/toaster.service';
exports: [ToastContainerComponent],
entryComponents: [ToastContainerComponent],
declarations: [ToastContainerComponent, ToastComponent],
imports: [CoreTestingModule.forTest()],
imports: [CoreTestingModule.withConfig()],
})
export class MockModule {}
const toastClassPrefix = 'abp-toast';
Expand All @@ -21,7 +21,7 @@ describe('ToasterService', () => {
let service: ToasterService;
const createService = createServiceFactory({
service: ToasterService,
imports: [NgxsModule.forRoot(), CoreTestingModule.forTest(), MockModule],
imports: [NgxsModule.forRoot(), CoreTestingModule.withConfig(), MockModule],
});

beforeEach(() => {
Expand Down
28 changes: 16 additions & 12 deletions npm/ng-packs/packages/theme-shared/src/lib/theme-shared.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { initLazyStyleHandler } from './handlers/lazy-style.handler';
import { RootParams } from './models/common';
import { THEME_SHARED_ROUTE_PROVIDERS } from './providers/route.provider';
import { THEME_SHARED_APPEND_CONTENT } from './tokens/append-content.token';
import { HTTP_ERROR_CONFIG, httpErrorConfigFactory } from './tokens/http-error.token';
import { httpErrorConfigFactory, HTTP_ERROR_CONFIG } from './tokens/http-error.token';
import { DateParserFormatter } from './utils/date-parser-formatter';

const declarationsWithExports = [
Expand All @@ -48,17 +48,11 @@ const declarationsWithExports = [
LoadingDirective,
TableSortDirective,
];

@NgModule({
imports: [CoreModule, NgxDatatableModule, NgxValidateCoreModule, NgbPaginationModule],
declarations: [
...declarationsWithExports,
HttpErrorWrapperComponent,
ModalContainerComponent,
],
exports: [
NgxDatatableModule,
...declarationsWithExports,
],
declarations: [...declarationsWithExports, HttpErrorWrapperComponent, ModalContainerComponent],
exports: [NgxDatatableModule, ...declarationsWithExports],
providers: [DatePipe],
entryComponents: [
HttpErrorWrapperComponent,
Expand All @@ -68,13 +62,23 @@ const declarationsWithExports = [
ConfirmationComponent,
],
})
export class ThemeSharedModule {
constructor(private errorHandler: ErrorHandler) {}
export class BaseThemeSharedModule {}

@NgModule({
imports: [BaseThemeSharedModule],
exports: [BaseThemeSharedModule],
})
export class ThemeSharedModule {
static forRoot(options = {} as RootParams): ModuleWithProviders<ThemeSharedModule> {
return {
ngModule: ThemeSharedModule,
providers: [
{
provide: APP_INITIALIZER,
multi: true,
deps: [ErrorHandler],
useFactory: noop,
},
THEME_SHARED_ROUTE_PROVIDERS,
{
provide: APP_INITIALIZER,
Expand Down
7 changes: 7 additions & 0 deletions npm/ng-packs/packages/theme-shared/testing/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "../../../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../../dist/theme-shared/testing",
"lib": {
"entryFile": "src/public-api.ts"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {
BaseThemeSharedModule,
DateParserFormatter,
THEME_SHARED_ROUTE_PROVIDERS,
} from '@abp/ng.theme.shared';
import { ModuleWithProviders, NgModule } from '@angular/core';
import { RouterTestingModule } from '@angular/router/testing';
import { NgbDateParserFormatter } from '@ng-bootstrap/ng-bootstrap';

/**
* ThemeSharedTestingModule is the module that will be used in tests
*/
@NgModule({
exports: [RouterTestingModule, BaseThemeSharedModule],
imports: [RouterTestingModule, BaseThemeSharedModule],
})
export class ThemeSharedTestingModule {
static withConfig(): ModuleWithProviders<ThemeSharedTestingModule> {
return {
ngModule: ThemeSharedTestingModule,
providers: [
THEME_SHARED_ROUTE_PROVIDERS,
{ provide: NgbDateParserFormatter, useClass: DateParserFormatter },
],
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/theme-shared-testing.module';
Loading

0 comments on commit 594bb00

Please sign in to comment.