@bespunky/angular-zen
Version:
The Angular tools you always wished were there.
1 lines • 8.41 kB
Source Map (JSON)
{"version":3,"file":"bespunky-angular-zen-language-testing.mjs","sources":["../../../../libs/angular-zen/language/testing/src/utils/language-integration-config.ts","../../../../libs/angular-zen/language/testing/src/utils/setup.ts","../../../../libs/angular-zen/language/testing/src/bespunky-angular-zen-language-testing.ts"],"sourcesContent":["import { of } from 'rxjs';\n\nimport { LanguageIntegrationConfig } from '@bespunky/angular-zen/language';\n\n/** A dummy array of supported languages to use when testing. Includes 'en', 'fr' and 'he'. */\nexport const SupportedLanguages = ['en', 'fr', 'he'];\n/** A dummy language name to use as a default language when testing. This always equals to `SupportedLanguages[0]`. */\nexport const DefaultLanguage = SupportedLanguages[0];\n\n/**\n * A dummy language integration configuration to use when testing.\n * \n * Operation: \n * \n * `changed` - will only emit once, and will provide `DefaultLanguage` as the emitted language. \n * \n * `supported` - will provide `SupportedLanguages` as the array of language names. \n * \n * `default` - will provide `DefaultLanguage` as the name of the default language. \n * \n * `translate` - will prefix any given value with `TRANSLATED: `. \n * \n * `ready` - will emit once immediately. \n */\nexport const LanguageConfig: LanguageIntegrationConfig = {\n changed : of(DefaultLanguage),\n supported: SupportedLanguages,\n default : DefaultLanguage,\n translate: (value: string) => `TRANSLATED: ${value}`,\n ready : of(true)\n};\n","import { TestBed } from '@angular/core/testing';\nimport { Route, Router } from '@angular/router';\nimport { RouterTestingModule } from '@angular/router/testing';\n\nimport { forceRoutingInsideAngularZone } from '@bespunky/angular-zen/core/testing';\nimport { createDeeplyNestedRoutes, DeepRouteSegments, NoopModule, NoopComponent } from '@bespunky/angular-zen/router-x/testing';\nimport { UrlLocalizationStrategy, LanguageIntegrationModule, UrlLocalizer, UrlLocalizationService, UrlLocalizationConfig } from '@bespunky/angular-zen/language';\nimport { UrlReflectionService } from '@bespunky/angular-zen/router-x';\nimport { LanguageConfig } from './language-integration-config';\n\n/**\n * Configures the testing module with localized routes for testing and language integration services.\n * Routes are created by running `createDeeplyNestedRoutes(DeepRouteSegments)`.\n * Language integration tools are read from `LanguageConfig`.\n * \n * The function extracts services from the testbed and returns them in the returned object for quick deconstruction.\n * \n * @export\n * @param {(UrlLocalizationStrategy | UrlLocalizationConfig)} strategyOrConfig The url localization strategy or full config.\n * @returns A deconstructable object with ready to use language tools. See intellisense.\n */\nexport function setupUrlLocalizerTest(strategyOrConfig?: UrlLocalizationStrategy | UrlLocalizationConfig)\n{\n const nestedRoutes = createLocalizedDeeplyNestedRoutes(DeepRouteSegments);\n const config = isUrlLocalizationConfig(strategyOrConfig) ? strategyOrConfig : { strategy: strategyOrConfig } as UrlLocalizationConfig;\n\n TestBed.configureTestingModule({\n imports: [\n NoopModule,\n RouterTestingModule.withRoutes([nestedRoutes]),\n LanguageIntegrationModule.forRoot({\n useFactory : () => LanguageConfig,\n urlLocalization: config\n })\n ]\n });\n\n const localizer = TestBed.inject(UrlLocalizer);\n const urlLocalization = TestBed.inject(UrlLocalizationService);\n const urlReflection = TestBed.inject(UrlReflectionService);\n const router = TestBed.inject(Router);\n \n forceRoutingInsideAngularZone(router);\n\n router.initialNavigation();\n\n return { localizer, urlLocalization, urlReflection, router };\n}\n\n/**\n * Recoursively creates nested routes for the specified segments.\n * Each specified segment will be a child route of its previous segment.\n * Each created route will support 2 paths:\n * 1. The segment name.\n * 2. An 'en' path.\n * \n * #### Example\n * Running the function on `['some', 'route']` will result in the following supported routes: \n * / \n * \n * /some \n * /some/route \n * \n * /en/ \n * /en/some \n * /en/some/route \n * \n * /some/en \n * /some/en/route \n * \n * /some/route/en \n * \n * @export\n * @param {string[]} segments\n * @returns {Route}\n */\nexport function createLocalizedDeeplyNestedRoutes(segments: string[]): Route\n{\n const nestedRoutes = createDeeplyNestedRoutes(segments);\n \n localizeRoute(nestedRoutes);\n\n return nestedRoutes;\n}\n\n/**\n * Recoursively adds an 'en' segment to all routes in the tree.\n *\n * @param {Route} route The top most route to localize.\n */\nfunction localizeRoute(route: Route): void\n{\n route.children ||= [];\n route.children.forEach(child => localizeRoute(child));\n\n const localizedRoute: Route = { path: 'en', component: NoopComponent };\n\n // Copy the children of the current route to the localized route as well to ensure a route can be\n // accessed with or without localization (i.e. the /en) prefix.\n localizedRoute.children = [...route.children];\n // Add the english segment as the first child of the route\n route.children.unshift(localizedRoute);\n}\n\n/**\n * Checks whether the given value is a url localization config object.\n *\n * @param {*} value The value to test.\n * @returns {value is UrlLocalizationConfig} `true` if the value is a `UrlLocalizationConfig` object; otherwise `false`.\n */\nfunction isUrlLocalizationConfig(value: any): value is UrlLocalizationConfig\n{\n const strategy = value?.strategy;\n\n return strategy && (typeof strategy === 'number' || typeof strategy === 'string' || strategy.useClass || strategy.useFactory);\n}","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;;;;AAIA;AACa,MAAA,kBAAkB,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE;AACrD;MACa,eAAe,GAAM,kBAAkB,CAAC,CAAC,EAAE;AAExD;;;;;;;;;;;;;;AAcG;AACU,MAAA,cAAc,GAA8B;AACrD,IAAA,OAAO,EAAI,EAAE,CAAC,eAAe,CAAC;AAC9B,IAAA,SAAS,EAAE,kBAAkB;AAC7B,IAAA,OAAO,EAAI,eAAe;IAC1B,SAAS,EAAE,CAAC,KAAa,KAAK,CAAA,YAAA,EAAe,KAAK,CAAE,CAAA;AACpD,IAAA,KAAK,EAAM,EAAE,CAAC,IAAI,CAAC;;;ACnBvB;;;;;;;;;;AAUG;AACG,SAAU,qBAAqB,CAAC,gBAAkE,EAAA;AAEpG,IAAA,MAAM,YAAY,GAAG,iCAAiC,CAAC,iBAAiB,CAAC,CAAC;AAC1E,IAAA,MAAM,MAAM,GAAS,uBAAuB,CAAC,gBAAgB,CAAC,GAAG,gBAAgB,GAAG,EAAE,QAAQ,EAAE,gBAAgB,EAA2B,CAAC;IAE5I,OAAO,CAAC,sBAAsB,CAAC;AAC3B,QAAA,OAAO,EAAE;YACL,UAAU;AACV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