@bespunky/angular-zen
Version:
The Angular tools you always wished were there.
133 lines (128 loc) • 5.21 kB
JavaScript
import { of } from 'rxjs';
import { TestBed } from '@angular/core/testing';
import { Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { forceRoutingInsideAngularZone } from '@bespunky/angular-zen/core/testing';
import { DeepRouteSegments, NoopModule, createDeeplyNestedRoutes, NoopComponent } from '@bespunky/angular-zen/router-x/testing';
import { LanguageIntegrationModule, UrlLocalizer, UrlLocalizationService } from '@bespunky/angular-zen/language';
import { UrlReflectionService } from '@bespunky/angular-zen/router-x';
/** A dummy array of supported languages to use when testing. Includes 'en', 'fr' and 'he'. */
const SupportedLanguages = ['en', 'fr', 'he'];
/** A dummy language name to use as a default language when testing. This always equals to `SupportedLanguages[0]`. */
const DefaultLanguage = SupportedLanguages[0];
/**
* A dummy language integration configuration to use when testing.
*
* Operation:
*
* `changed` - will only emit once, and will provide `DefaultLanguage` as the emitted language.
*
* `supported` - will provide `SupportedLanguages` as the array of language names.
*
* `default` - will provide `DefaultLanguage` as the name of the default language.
*
* `translate` - will prefix any given value with `TRANSLATED: `.
*
* `ready` - will emit once immediately.
*/
const LanguageConfig = {
changed: of(DefaultLanguage),
supported: SupportedLanguages,
default: DefaultLanguage,
translate: (value) => `TRANSLATED: ${value}`,
ready: of(true)
};
/**
* Configures the testing module with localized routes for testing and language integration services.
* Routes are created by running `createDeeplyNestedRoutes(DeepRouteSegments)`.
* Language integration tools are read from `LanguageConfig`.
*
* The function extracts services from the testbed and returns them in the returned object for quick deconstruction.
*
* @export
* @param {(UrlLocalizationStrategy | UrlLocalizationConfig)} strategyOrConfig The url localization strategy or full config.
* @returns A deconstructable object with ready to use language tools. See intellisense.
*/
function setupUrlLocalizerTest(strategyOrConfig) {
const nestedRoutes = createLocalizedDeeplyNestedRoutes(DeepRouteSegments);
const config = isUrlLocalizationConfig(strategyOrConfig) ? strategyOrConfig : { strategy: strategyOrConfig };
TestBed.configureTestingModule({
imports: [
NoopModule,
RouterTestingModule.withRoutes([nestedRoutes]),
LanguageIntegrationModule.forRoot({
useFactory: () => LanguageConfig,
urlLocalization: config
})
]
});
const localizer = TestBed.inject(UrlLocalizer);
const urlLocalization = TestBed.inject(UrlLocalizationService);
const urlReflection = TestBed.inject(UrlReflectionService);
const router = TestBed.inject(Router);
forceRoutingInsideAngularZone(router);
router.initialNavigation();
return { localizer, urlLocalization, urlReflection, router };
}
/**
* Recoursively creates nested routes for the specified segments.
* Each specified segment will be a child route of its previous segment.
* Each created route will support 2 paths:
* 1. The segment name.
* 2. An 'en' path.
*
* #### Example
* Running the function on `['some', 'route']` will result in the following supported routes:
* /
*
* /some
* /some/route
*
* /en/
* /en/some
* /en/some/route
*
* /some/en
* /some/en/route
*
* /some/route/en
*
* @export
* @param {string[]} segments
* @returns {Route}
*/
function createLocalizedDeeplyNestedRoutes(segments) {
const nestedRoutes = createDeeplyNestedRoutes(segments);
localizeRoute(nestedRoutes);
return nestedRoutes;
}
/**
* Recoursively adds an 'en' segment to all routes in the tree.
*
* @param {Route} route The top most route to localize.
*/
function localizeRoute(route) {
route.children || (route.children = []);
route.children.forEach(child => localizeRoute(child));
const localizedRoute = { path: 'en', component: NoopComponent };
// Copy the children of the current route to the localized route as well to ensure a route can be
// accessed with or without localization (i.e. the /en) prefix.
localizedRoute.children = [...route.children];
// Add the english segment as the first child of the route
route.children.unshift(localizedRoute);
}
/**
* Checks whether the given value is a url localization config object.
*
* @param {*} value The value to test.
* @returns {value is UrlLocalizationConfig} `true` if the value is a `UrlLocalizationConfig` object; otherwise `false`.
*/
function isUrlLocalizationConfig(value) {
const strategy = value === null || value === void 0 ? void 0 : value.strategy;
return strategy && (typeof strategy === 'number' || typeof strategy === 'string' || strategy.useClass || strategy.useFactory);
}
/**
* Generated bundle index. Do not edit.
*/
export { DefaultLanguage, LanguageConfig, SupportedLanguages, createLocalizedDeeplyNestedRoutes, setupUrlLocalizerTest };
//# sourceMappingURL=bespunky-angular-zen-language-testing.mjs.map