diff --git a/npm/ng-packs/packages/core/src/lib/core.module.ts b/npm/ng-packs/packages/core/src/lib/core.module.ts index fd4a41e0f49..a661012ac7a 100644 --- a/npm/ng-packs/packages/core/src/lib/core.module.ts +++ b/npm/ng-packs/packages/core/src/lib/core.module.ts @@ -1,4 +1,4 @@ -import { APP_BASE_HREF, CommonModule } from '@angular/common'; +import { CommonModule } from '@angular/common'; import { HttpClientModule, HttpClientXsrfModule, HTTP_INTERCEPTORS } from '@angular/common/http'; import { APP_INITIALIZER, Injector, ModuleWithProviders, NgModule } from '@angular/core'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; @@ -24,7 +24,7 @@ import { RoutesHandler } from './handlers/routes.handler'; import { ApiInterceptor } from './interceptors/api.interceptor'; import { LocalizationModule } from './localization.module'; import { ABP } from './models/common'; -import { LocalizationPipe, MockLocalizationPipe } from './pipes/localization.pipe'; +import { LocalizationPipe } from './pipes/localization.pipe'; import { SortPipe } from './pipes/sort.pipe'; import { LocaleProvider } from './providers/locale.provider'; import { LocalizationService } from './services/localization.service'; @@ -121,17 +121,6 @@ export class BaseCoreModule {} }) export class RootCoreModule {} -/** - * TestCoreModule is the module that will be used in tests - * and it provides mock alternatives - */ -@NgModule({ - exports: [RouterModule, BaseCoreModule, MockLocalizationPipe], - imports: [RouterModule.forRoot([], { relativeLinkResolution: 'legacy' }), BaseCoreModule], - declarations: [MockLocalizationPipe], -}) -export class TestCoreModule {} - /** * CoreModule is the module that is publicly available */ @@ -141,19 +130,6 @@ export class TestCoreModule {} providers: [LocalizationPipe], }) export class CoreModule { - static forTest({ baseHref = '/' } = {} as ABP.Test): ModuleWithProviders { - return { - ngModule: TestCoreModule, - providers: [ - { provide: APP_BASE_HREF, useValue: baseHref }, - { - provide: LocalizationPipe, - useClass: MockLocalizationPipe, - }, - ], - }; - } - static forRoot(options = {} as ABP.Root): ModuleWithProviders { return { ngModule: RootCoreModule, diff --git a/npm/ng-packs/packages/core/src/lib/models/common.ts b/npm/ng-packs/packages/core/src/lib/models/common.ts index 652cc07ea3e..88bd75c027e 100644 --- a/npm/ng-packs/packages/core/src/lib/models/common.ts +++ b/npm/ng-packs/packages/core/src/lib/models/common.ts @@ -1,5 +1,5 @@ import { EventEmitter, Type } from '@angular/core'; -import { Router } from '@angular/router'; +import { Router, Routes } from '@angular/router'; import { Subject } from 'rxjs'; import { eLayoutType } from '../enums/common'; import { Environment } from './environment'; @@ -12,8 +12,9 @@ export namespace ABP { sendNullsAsQueryParam?: boolean; } - export interface Test { + export interface Test extends Partial { baseHref?: Router; + routes?: Routes; } export type PagedResponse = { diff --git a/npm/ng-packs/packages/core/src/lib/pipes/localization.pipe.ts b/npm/ng-packs/packages/core/src/lib/pipes/localization.pipe.ts index cc84cfebdd6..cf076d414c4 100644 --- a/npm/ng-packs/packages/core/src/lib/pipes/localization.pipe.ts +++ b/npm/ng-packs/packages/core/src/lib/pipes/localization.pipe.ts @@ -22,13 +22,3 @@ export class LocalizationPipe implements PipeTransform { ); } } - -@Injectable() -@Pipe({ - name: 'abpLocalization', -}) -export class MockLocalizationPipe implements PipeTransform { - transform(value: string | Config.LocalizationWithDefault = '', ..._: string[]) { - return typeof value === 'string' ? value : value.defaultValue; - } -} diff --git a/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts index e9014a52db2..0e227134494 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts @@ -1,3 +1,4 @@ +import snq from 'snq'; import { ApplicationLocalizationConfigurationDto } from '../proxy/volo/abp/asp-net-core/mvc/application-configurations/models'; // This will not be necessary when only Angukar 9.1+ is supported @@ -13,7 +14,7 @@ export function createLocalizer(localization: ApplicationLocalizationConfigurati return (resourceName: string, key: string, defaultValue: string) => { if (resourceName === '_') return key; - const resource = localization.values[resourceName]; + const resource = snq(() => localization.values[resourceName]); if (!resource) return defaultValue; diff --git a/npm/ng-packs/packages/core/testing/ng-package.json b/npm/ng-packs/packages/core/testing/ng-package.json new file mode 100644 index 00000000000..e647bd0d3e5 --- /dev/null +++ b/npm/ng-packs/packages/core/testing/ng-package.json @@ -0,0 +1,7 @@ +{ + "$schema": "../../../node_modules/ng-packagr/ng-package.schema.json", + "dest": "../../dist/core/testing", + "lib": { + "entryFile": "src/public-api.ts" + } +} diff --git a/npm/ng-packs/packages/core/testing/src/lib/core-testing.module.ts b/npm/ng-packs/packages/core/testing/src/lib/core-testing.module.ts new file mode 100644 index 00000000000..5a43aa9a4c8 --- /dev/null +++ b/npm/ng-packs/packages/core/testing/src/lib/core-testing.module.ts @@ -0,0 +1,48 @@ +import { + ABP, + BaseCoreModule, + coreOptionsFactory, + CORE_OPTIONS, + LocalizationPipe, +} 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'; + +/** + * CoreTestingModule is the module that will be used in tests + * and it provides mock alternatives + */ +@NgModule({ + exports: [RouterTestingModule, BaseCoreModule, MockLocalizationPipe], + imports: [RouterTestingModule, BaseCoreModule], + declarations: [MockLocalizationPipe], +}) +export class CoreTestingModule { + static forTest( + { baseHref = '/', routes = [], ...options } = {} as ABP.Test, + ): ModuleWithProviders { + return { + ngModule: CoreTestingModule, + providers: [ + { provide: APP_BASE_HREF, useValue: baseHref }, + { + provide: 'CORE_OPTIONS', + useValue: options, + }, + { + provide: CORE_OPTIONS, + useFactory: coreOptionsFactory, + deps: ['CORE_OPTIONS'], + }, + { + provide: LocalizationPipe, + useClass: MockLocalizationPipe, + }, + provideRoutes(routes), + ], + }; + } +} diff --git a/npm/ng-packs/packages/core/testing/src/lib/pipes/index.ts b/npm/ng-packs/packages/core/testing/src/lib/pipes/index.ts new file mode 100644 index 00000000000..60bb9b7dce5 --- /dev/null +++ b/npm/ng-packs/packages/core/testing/src/lib/pipes/index.ts @@ -0,0 +1 @@ +export * from './mock-localization.pipe'; diff --git a/npm/ng-packs/packages/core/testing/src/lib/pipes/mock-localization.pipe.ts b/npm/ng-packs/packages/core/testing/src/lib/pipes/mock-localization.pipe.ts new file mode 100644 index 00000000000..302173cb0f2 --- /dev/null +++ b/npm/ng-packs/packages/core/testing/src/lib/pipes/mock-localization.pipe.ts @@ -0,0 +1,12 @@ +import { Config } from '@abp/ng.core'; +import { Injectable, Pipe, PipeTransform } from '@angular/core'; + +@Injectable() +@Pipe({ + name: 'abpLocalization', +}) +export class MockLocalizationPipe implements PipeTransform { + transform(value: string | Config.LocalizationWithDefault = '', ..._: string[]) { + return typeof value === 'string' ? value : value.defaultValue; + } +} diff --git a/npm/ng-packs/packages/core/testing/src/public-api.ts b/npm/ng-packs/packages/core/testing/src/public-api.ts new file mode 100644 index 00000000000..8028a99aacc --- /dev/null +++ b/npm/ng-packs/packages/core/testing/src/public-api.ts @@ -0,0 +1,2 @@ +export * from './lib/core-testing.module'; +export * from './lib/pipes'; diff --git a/npm/ng-packs/packages/theme-shared/src/lib/tests/confirmation.service.spec.ts b/npm/ng-packs/packages/theme-shared/src/lib/tests/confirmation.service.spec.ts index 3cbdcbd7dec..88813c44dea 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/tests/confirmation.service.spec.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/tests/confirmation.service.spec.ts @@ -1,4 +1,4 @@ -import { CoreModule } from '@abp/ng.core'; +import { CoreTestingModule } from '@abp/ng.core/testing'; import { NgModule } from '@angular/core'; import { fakeAsync, tick } from '@angular/core/testing'; import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest'; @@ -13,7 +13,7 @@ import { ConfirmationService } from '../services'; exports: [ConfirmationComponent], entryComponents: [ConfirmationComponent], declarations: [ConfirmationComponent], - imports: [CoreModule.forTest()], + imports: [CoreTestingModule.forTest()], }) export class MockModule {} @@ -22,7 +22,7 @@ describe('ConfirmationService', () => { let service: ConfirmationService; const createService = createServiceFactory({ service: ConfirmationService, - imports: [NgxsModule.forRoot(), CoreModule.forTest(), MockModule], + imports: [NgxsModule.forRoot(), CoreTestingModule.forTest(), MockModule], }); beforeEach(() => { diff --git a/npm/ng-packs/packages/theme-shared/src/lib/tests/error.handler.spec.ts b/npm/ng-packs/packages/theme-shared/src/lib/tests/error.handler.spec.ts index e4fb8e3d37b..939f87e5eea 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/tests/error.handler.spec.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/tests/error.handler.spec.ts @@ -1,14 +1,14 @@ -import { CoreModule, RestOccurError } from '@abp/ng.core'; +import { RestOccurError } from '@abp/ng.core'; +import { CoreTestingModule } from '@abp/ng.core/testing'; import { APP_BASE_HREF } from '@angular/common'; import { HttpErrorResponse, HttpHeaders } from '@angular/common/http'; import { Component, NgModule } from '@angular/core'; -import { NavigationError, ResolveEnd, RouterModule } from '@angular/router'; import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest'; -import { Actions, NgxsModule, ofActionDispatched, Store } from '@ngxs/store'; +import { NgxsModule, Store } from '@ngxs/store'; import { OAuthService } from 'angular-oauth2-oidc'; import { of } from 'rxjs'; import { HttpErrorWrapperComponent } from '../components/http-error-wrapper/http-error-wrapper.component'; -import { DEFAULT_ERROR_MESSAGES, ErrorHandler, DEFAULT_ERROR_LOCALIZATIONS } from '../handlers'; +import { DEFAULT_ERROR_LOCALIZATIONS, DEFAULT_ERROR_MESSAGES, ErrorHandler } from '../handlers'; import { ConfirmationService } from '../services'; import { httpErrorConfigFactory } from '../tokens/http-error.token'; @@ -16,7 +16,7 @@ import { httpErrorConfigFactory } from '../tokens/http-error.token'; exports: [HttpErrorWrapperComponent], declarations: [HttpErrorWrapperComponent], entryComponents: [HttpErrorWrapperComponent], - imports: [CoreModule], + imports: [CoreTestingModule], }) class MockModule {} @@ -31,12 +31,7 @@ const CONFIRMATION_BUTTONS = { describe('ErrorHandler', () => { const createService = createServiceFactory({ service: ErrorHandler, - imports: [ - RouterModule.forRoot([], { relativeLinkResolution: 'legacy' }), - NgxsModule.forRoot([]), - CoreModule, - MockModule, - ], + imports: [NgxsModule.forRoot([]), CoreTestingModule.forTest(), MockModule], mocks: [OAuthService], providers: [ { provide: APP_BASE_HREF, useValue: '/' }, diff --git a/npm/ng-packs/packages/theme-shared/src/lib/tests/toaster.service.spec.ts b/npm/ng-packs/packages/theme-shared/src/lib/tests/toaster.service.spec.ts index d47e4a7cf5d..fa6e0e578c3 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/tests/toaster.service.spec.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/tests/toaster.service.spec.ts @@ -1,4 +1,4 @@ -import { CoreModule } from '@abp/ng.core'; +import { CoreTestingModule } from '@abp/ng.core/testing'; import { NgModule } from '@angular/core'; import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest'; import { NgxsModule } from '@ngxs/store'; @@ -11,7 +11,7 @@ import { ToasterService } from '../services/toaster.service'; exports: [ToastContainerComponent], entryComponents: [ToastContainerComponent], declarations: [ToastContainerComponent, ToastComponent], - imports: [CoreModule.forTest()], + imports: [CoreTestingModule.forTest()], }) export class MockModule {} const toastClassPrefix = 'abp-toast'; @@ -21,7 +21,7 @@ describe('ToasterService', () => { let service: ToasterService; const createService = createServiceFactory({ service: ToasterService, - imports: [NgxsModule.forRoot(), CoreModule.forTest(), MockModule], + imports: [NgxsModule.forRoot(), CoreTestingModule.forTest(), MockModule], }); beforeEach(() => { diff --git a/npm/ng-packs/tsconfig.json b/npm/ng-packs/tsconfig.json index 213f5bd4377..c67c628f64d 100644 --- a/npm/ng-packs/tsconfig.json +++ b/npm/ng-packs/tsconfig.json @@ -17,6 +17,7 @@ "paths": { "@abp/ng.core": ["packages/core/src/public-api.ts"], "@abp/ng.core/locale": ["packages/core/locale/src/public-api.ts"], + "@abp/ng.core/testing": ["packages/core/testing/src/public-api.ts"], "@abp/ng.theme.shared": ["packages/theme-shared/src/public-api.ts"], "@abp/ng.theme.shared/extensions": ["packages/theme-shared/extensions/src/public-api.ts"], "@abp/ng.components/tree": ["packages/components/tree/src/public-api.ts"],