UNPKG

@angular/core

Version:

Angular - the core framework

1,318 lines 129 kB
/** * @fileoverview added by tsickle * Generated from: packages/core/testing/src/r3_test_bed_compiler.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ import { __awaiter } from "tslib"; /** * @license * Copyright Google Inc. All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import { ResourceLoader } from '@angular/compiler'; import { ApplicationInitStatus, Compiler, COMPILER_OPTIONS, LOCALE_ID, ModuleWithComponentFactories, NgZone, ɵcompileComponent as compileComponent, ɵcompileDirective as compileDirective, ɵcompileNgModuleDefs as compileNgModuleDefs, ɵcompilePipe as compilePipe, ɵDEFAULT_LOCALE_ID as DEFAULT_LOCALE_ID, ɵgetInjectableDef as getInjectableDef, ɵNG_COMP_DEF as NG_COMP_DEF, ɵNG_DIR_DEF as NG_DIR_DEF, ɵNG_INJ_DEF as NG_INJ_DEF, ɵNG_MOD_DEF as NG_MOD_DEF, ɵNG_PIPE_DEF as NG_PIPE_DEF, ɵNgModuleFactory as R3NgModuleFactory, ɵpatchComponentDefWithScope as patchComponentDefWithScope, ɵRender3ComponentFactory as ComponentFactory, ɵRender3NgModuleRef as NgModuleRef, ɵsetLocaleId as setLocaleId, ɵtransitiveScopesFor as transitiveScopesFor } from '@angular/core'; import { clearResolutionOfComponentResourcesQueue, isComponentDefPendingResolution, resolveComponentResources, restoreComponentResolutionQueue } from '../../src/metadata/resource_loading'; import { ComponentResolver, DirectiveResolver, NgModuleResolver, PipeResolver } from './resolvers'; /** @enum {number} */ const TestingModuleOverride = { DECLARATION: 0, OVERRIDE_TEMPLATE: 1, }; TestingModuleOverride[TestingModuleOverride.DECLARATION] = 'DECLARATION'; TestingModuleOverride[TestingModuleOverride.OVERRIDE_TEMPLATE] = 'OVERRIDE_TEMPLATE'; /** * @param {?} value * @return {?} */ function isTestingModuleOverride(value) { return value === TestingModuleOverride.DECLARATION || value === TestingModuleOverride.OVERRIDE_TEMPLATE; } /** * @record */ function CleanupOperation() { } if (false) { /** @type {?} */ CleanupOperation.prototype.fieldName; /** @type {?} */ CleanupOperation.prototype.object; /** @type {?} */ CleanupOperation.prototype.originalValue; } export class R3TestBedCompiler { /** * @param {?} platform * @param {?} additionalModuleTypes */ constructor(platform, additionalModuleTypes) { this.platform = platform; this.additionalModuleTypes = additionalModuleTypes; this.originalComponentResolutionQueue = null; // Testing module configuration this.declarations = []; this.imports = []; this.providers = []; this.schemas = []; // Queues of components/directives/pipes that should be recompiled. this.pendingComponents = new Set(); this.pendingDirectives = new Set(); this.pendingPipes = new Set(); // Keep track of all components and directives, so we can patch Providers onto defs later. this.seenComponents = new Set(); this.seenDirectives = new Set(); // Keep track of overridden modules, so that we can collect all affected ones in the module tree. this.overriddenModules = new Set(); // Store resolved styles for Components that have template overrides present and `styleUrls` // defined at the same time. this.existingComponentStyles = new Map(); this.resolvers = initResolvers(); this.componentToModuleScope = new Map(); // Map that keeps initial version of component/directive/pipe defs in case // we compile a Type again, thus overriding respective static fields. This is // required to make sure we restore defs to their initial states between test runs // TODO: we should support the case with multiple defs on a type this.initialNgDefs = new Map(); // Array that keeps cleanup operations for initial versions of component/directive/pipe/module // defs in case TestBed makes changes to the originals. this.defCleanupOps = []; this._injector = null; this.compilerProviders = null; this.providerOverrides = []; this.rootProviderOverrides = []; // Overrides for injectables with `{providedIn: SomeModule}` need to be tracked and added to that // module's provider list. this.providerOverridesByModule = new Map(); this.providerOverridesByToken = new Map(); this.moduleProvidersOverridden = new Set(); this.testModuleRef = null; class DynamicTestModule { } this.testModuleType = (/** @type {?} */ (DynamicTestModule)); } /** * @param {?} providers * @return {?} */ setCompilerProviders(providers) { this.compilerProviders = providers; this._injector = null; } /** * @param {?} moduleDef * @return {?} */ configureTestingModule(moduleDef) { // Enqueue any compilation tasks for the directly declared component. if (moduleDef.declarations !== undefined) { this.queueTypeArray(moduleDef.declarations, TestingModuleOverride.DECLARATION); this.declarations.push(...moduleDef.declarations); } // Enqueue any compilation tasks for imported modules. if (moduleDef.imports !== undefined) { this.queueTypesFromModulesArray(moduleDef.imports); this.imports.push(...moduleDef.imports); } if (moduleDef.providers !== undefined) { this.providers.push(...moduleDef.providers); } if (moduleDef.schemas !== undefined) { this.schemas.push(...moduleDef.schemas); } } /** * @param {?} ngModule * @param {?} override * @return {?} */ overrideModule(ngModule, override) { this.overriddenModules.add((/** @type {?} */ (ngModule))); // Compile the module right away. this.resolvers.module.addOverride(ngModule, override); /** @type {?} */ const metadata = this.resolvers.module.resolve(ngModule); if (metadata === null) { throw invalidTypeError(ngModule.name, 'NgModule'); } this.recompileNgModule(ngModule, metadata); // At this point, the module has a valid module def (ɵmod), but the override may have introduced // new declarations or imported modules. Ingest any possible new types and add them to the // current queue. this.queueTypesFromModulesArray([ngModule]); } /** * @param {?} component * @param {?} override * @return {?} */ overrideComponent(component, override) { this.resolvers.component.addOverride(component, override); this.pendingComponents.add(component); } /** * @param {?} directive * @param {?} override * @return {?} */ overrideDirective(directive, override) { this.resolvers.directive.addOverride(directive, override); this.pendingDirectives.add(directive); } /** * @param {?} pipe * @param {?} override * @return {?} */ overridePipe(pipe, override) { this.resolvers.pipe.addOverride(pipe, override); this.pendingPipes.add(pipe); } /** * @param {?} token * @param {?} provider * @return {?} */ overrideProvider(token, provider) { /** @type {?} */ let providerDef; if (provider.useFactory !== undefined) { providerDef = { provide: token, useFactory: provider.useFactory, deps: provider.deps || [], multi: provider.multi }; } else if (provider.useValue !== undefined) { providerDef = { provide: token, useValue: provider.useValue, multi: provider.multi }; } else { providerDef = { provide: token }; } /** @type {?} */ const injectableDef = typeof token !== 'string' ? getInjectableDef(token) : null; /** @type {?} */ const isRoot = injectableDef !== null && injectableDef.providedIn === 'root'; /** @type {?} */ const overridesBucket = isRoot ? this.rootProviderOverrides : this.providerOverrides; overridesBucket.push(providerDef); // Keep overrides grouped by token as well for fast lookups using token this.providerOverridesByToken.set(token, providerDef); if (injectableDef !== null && injectableDef.providedIn !== null && typeof injectableDef.providedIn !== 'string') { /** @type {?} */ const existingOverrides = this.providerOverridesByModule.get(injectableDef.providedIn); if (existingOverrides !== undefined) { existingOverrides.push(providerDef); } else { this.providerOverridesByModule.set(injectableDef.providedIn, [providerDef]); } } } /** * @param {?} type * @param {?} template * @return {?} */ overrideTemplateUsingTestingModule(type, template) { /** @type {?} */ const def = ((/** @type {?} */ (type)))[NG_COMP_DEF]; /** @type {?} */ const hasStyleUrls = (/** * @return {?} */ () => { /** @type {?} */ const metadata = (/** @type {?} */ ((/** @type {?} */ (this.resolvers.component.resolve(type))))); return !!metadata.styleUrls && metadata.styleUrls.length > 0; }); /** @type {?} */ const overrideStyleUrls = !!def && !isComponentDefPendingResolution(type) && hasStyleUrls(); // In Ivy, compiling a component does not require knowing the module providing the // component's scope, so overrideTemplateUsingTestingModule can be implemented purely via // overrideComponent. Important: overriding template requires full Component re-compilation, // which may fail in case styleUrls are also present (thus Component is considered as required // resolution). In order to avoid this, we preemptively set styleUrls to an empty array, // preserve current styles available on Component def and restore styles back once compilation // is complete. /** @type {?} */ const override = overrideStyleUrls ? { template, styles: [], styleUrls: [] } : { template }; this.overrideComponent(type, { set: override }); if (overrideStyleUrls && def.styles && def.styles.length > 0) { this.existingComponentStyles.set(type, def.styles); } // Set the component's scope to be the testing module. this.componentToModuleScope.set(type, TestingModuleOverride.OVERRIDE_TEMPLATE); } /** * @return {?} */ compileComponents() { return __awaiter(this, void 0, void 0, function* () { this.clearComponentResolutionQueue(); // Run compilers for all queued types. /** @type {?} */ let needsAsyncResources = this.compileTypesSync(); // compileComponents() should not be async unless it needs to be. if (needsAsyncResources) { /** @type {?} */ let resourceLoader; /** @type {?} */ let resolver = (/** * @param {?} url * @return {?} */ (url) => { if (!resourceLoader) { resourceLoader = this.injector.get(ResourceLoader); } return Promise.resolve(resourceLoader.get(url)); }); yield resolveComponentResources(resolver); } }); } /** * @return {?} */ finalize() { // One last compile this.compileTypesSync(); // Create the testing module itself. this.compileTestModule(); this.applyTransitiveScopes(); this.applyProviderOverrides(); // Patch previously stored `styles` Component values (taken from ɵcmp), in case these // Components have `styleUrls` fields defined and template override was requested. this.patchComponentsWithExistingStyles(); // Clear the componentToModuleScope map, so that future compilations don't reset the scope of // every component. this.componentToModuleScope.clear(); /** @type {?} */ const parentInjector = this.platform.injector; this.testModuleRef = new NgModuleRef(this.testModuleType, parentInjector); // ApplicationInitStatus.runInitializers() is marked @internal to core. // Cast it to any before accessing it. ((/** @type {?} */ (this.testModuleRef.injector.get(ApplicationInitStatus)))).runInitializers(); // Set locale ID after running app initializers, since locale information might be updated while // running initializers. This is also consistent with the execution order while bootstrapping an // app (see `packages/core/src/application_ref.ts` file). /** @type {?} */ const localeId = this.testModuleRef.injector.get(LOCALE_ID, DEFAULT_LOCALE_ID); setLocaleId(localeId); return this.testModuleRef; } /** * \@internal * @param {?} moduleType * @return {?} */ _compileNgModuleSync(moduleType) { this.queueTypesFromModulesArray([moduleType]); this.compileTypesSync(); this.applyProviderOverrides(); this.applyProviderOverridesToModule(moduleType); this.applyTransitiveScopes(); } /** * \@internal * @param {?} moduleType * @return {?} */ _compileNgModuleAsync(moduleType) { return __awaiter(this, void 0, void 0, function* () { this.queueTypesFromModulesArray([moduleType]); yield this.compileComponents(); this.applyProviderOverrides(); this.applyProviderOverridesToModule(moduleType); this.applyTransitiveScopes(); }); } /** * \@internal * @return {?} */ _getModuleResolver() { return this.resolvers.module; } /** * \@internal * @param {?} moduleType * @return {?} */ _getComponentFactories(moduleType) { return maybeUnwrapFn(moduleType.ɵmod.declarations).reduce((/** * @param {?} factories * @param {?} declaration * @return {?} */ (factories, declaration) => { /** @type {?} */ const componentDef = ((/** @type {?} */ (declaration))).ɵcmp; componentDef && factories.push(new ComponentFactory(componentDef, (/** @type {?} */ (this.testModuleRef)))); return factories; }), (/** @type {?} */ ([]))); } /** * @private * @return {?} */ compileTypesSync() { // Compile all queued components, directives, pipes. /** @type {?} */ let needsAsyncResources = false; this.pendingComponents.forEach((/** * @param {?} declaration * @return {?} */ declaration => { needsAsyncResources = needsAsyncResources || isComponentDefPendingResolution(declaration); /** @type {?} */ const metadata = this.resolvers.component.resolve(declaration); if (metadata === null) { throw invalidTypeError(declaration.name, 'Component'); } this.maybeStoreNgDef(NG_COMP_DEF, declaration); compileComponent(declaration, metadata); })); this.pendingComponents.clear(); this.pendingDirectives.forEach((/** * @param {?} declaration * @return {?} */ declaration => { /** @type {?} */ const metadata = this.resolvers.directive.resolve(declaration); if (metadata === null) { throw invalidTypeError(declaration.name, 'Directive'); } this.maybeStoreNgDef(NG_DIR_DEF, declaration); compileDirective(declaration, metadata); })); this.pendingDirectives.clear(); this.pendingPipes.forEach((/** * @param {?} declaration * @return {?} */ declaration => { /** @type {?} */ const metadata = this.resolvers.pipe.resolve(declaration); if (metadata === null) { throw invalidTypeError(declaration.name, 'Pipe'); } this.maybeStoreNgDef(NG_PIPE_DEF, declaration); compilePipe(declaration, metadata); })); this.pendingPipes.clear(); return needsAsyncResources; } /** * @private * @return {?} */ applyTransitiveScopes() { if (this.overriddenModules.size > 0) { // Module overrides (via `TestBed.overrideModule`) might affect scopes that were previously // calculated and stored in `transitiveCompileScopes`. If module overrides are present, // collect all affected modules and reset scopes to force their re-calculatation. /** @type {?} */ const testingModuleDef = ((/** @type {?} */ (this.testModuleType)))[NG_MOD_DEF]; /** @type {?} */ const affectedModules = this.collectModulesAffectedByOverrides(testingModuleDef.imports); if (affectedModules.size > 0) { affectedModules.forEach((/** * @param {?} moduleType * @return {?} */ moduleType => { this.storeFieldOfDefOnType((/** @type {?} */ (moduleType)), NG_MOD_DEF, 'transitiveCompileScopes'); ((/** @type {?} */ (moduleType)))[NG_MOD_DEF].transitiveCompileScopes = null; })); } } /** @type {?} */ const moduleToScope = new Map(); /** @type {?} */ const getScopeOfModule = (/** * @param {?} moduleType * @return {?} */ (moduleType) => { if (!moduleToScope.has(moduleType)) { /** @type {?} */ const isTestingModule = isTestingModuleOverride(moduleType); /** @type {?} */ const realType = isTestingModule ? this.testModuleType : (/** @type {?} */ (moduleType)); moduleToScope.set(moduleType, transitiveScopesFor(realType)); } return (/** @type {?} */ (moduleToScope.get(moduleType))); }); this.componentToModuleScope.forEach((/** * @param {?} moduleType * @param {?} componentType * @return {?} */ (moduleType, componentType) => { /** @type {?} */ const moduleScope = getScopeOfModule(moduleType); this.storeFieldOfDefOnType(componentType, NG_COMP_DEF, 'directiveDefs'); this.storeFieldOfDefOnType(componentType, NG_COMP_DEF, 'pipeDefs'); patchComponentDefWithScope(((/** @type {?} */ (componentType))).ɵcmp, moduleScope); })); this.componentToModuleScope.clear(); } /** * @private * @return {?} */ applyProviderOverrides() { /** @type {?} */ const maybeApplyOverrides = (/** * @param {?} field * @return {?} */ (field) => (/** * @param {?} type * @return {?} */ (type) => { /** @type {?} */ const resolver = field === NG_COMP_DEF ? this.resolvers.component : this.resolvers.directive; /** @type {?} */ const metadata = (/** @type {?} */ (resolver.resolve(type))); if (this.hasProviderOverrides(metadata.providers)) { this.patchDefWithProviderOverrides(type, field); } })); this.seenComponents.forEach(maybeApplyOverrides(NG_COMP_DEF)); this.seenDirectives.forEach(maybeApplyOverrides(NG_DIR_DEF)); this.seenComponents.clear(); this.seenDirectives.clear(); } /** * @private * @param {?} moduleType * @return {?} */ applyProviderOverridesToModule(moduleType) { if (this.moduleProvidersOverridden.has(moduleType)) { return; } this.moduleProvidersOverridden.add(moduleType); /** @type {?} */ const injectorDef = ((/** @type {?} */ (moduleType)))[NG_INJ_DEF]; if (this.providerOverridesByToken.size > 0) { /** @type {?} */ const providers = [ ...injectorDef.providers, ...(this.providerOverridesByModule.get((/** @type {?} */ (moduleType))) || []) ]; if (this.hasProviderOverrides(providers)) { this.maybeStoreNgDef(NG_INJ_DEF, moduleType); this.storeFieldOfDefOnType(moduleType, NG_INJ_DEF, 'providers'); injectorDef.providers = this.getOverriddenProviders(providers); } // Apply provider overrides to imported modules recursively /** @type {?} */ const moduleDef = ((/** @type {?} */ (moduleType)))[NG_MOD_DEF]; /** @type {?} */ const imports = maybeUnwrapFn(moduleDef.imports); for (const importedModule of imports) { this.applyProviderOverridesToModule(importedModule); } // Also override the providers on any ModuleWithProviders imports since those don't appear in // the moduleDef. for (const importedModule of flatten(injectorDef.imports)) { if (isModuleWithProviders(importedModule)) { this.defCleanupOps.push({ object: importedModule, fieldName: 'providers', originalValue: importedModule.providers }); importedModule.providers = this.getOverriddenProviders(importedModule.providers); } } } } /** * @private * @return {?} */ patchComponentsWithExistingStyles() { this.existingComponentStyles.forEach((/** * @param {?} styles * @param {?} type * @return {?} */ (styles, type) => ((/** @type {?} */ (type)))[NG_COMP_DEF].styles = styles)); this.existingComponentStyles.clear(); } /** * @private * @param {?} arr * @param {?} moduleType * @return {?} */ queueTypeArray(arr, moduleType) { for (const value of arr) { if (Array.isArray(value)) { this.queueTypeArray(value, moduleType); } else { this.queueType(value, moduleType); } } } /** * @private * @param {?} ngModule * @param {?} metadata * @return {?} */ recompileNgModule(ngModule, metadata) { // Cache the initial ngModuleDef as it will be overwritten. this.maybeStoreNgDef(NG_MOD_DEF, ngModule); this.maybeStoreNgDef(NG_INJ_DEF, ngModule); compileNgModuleDefs((/** @type {?} */ (ngModule)), metadata); } /** * @private * @param {?} type * @param {?} moduleType * @return {?} */ queueType(type, moduleType) { /** @type {?} */ const component = this.resolvers.component.resolve(type); if (component) { // Check whether a give Type has respective NG def (ɵcmp) and compile if def is // missing. That might happen in case a class without any Angular decorators extends another // class where Component/Directive/Pipe decorator is defined. if (isComponentDefPendingResolution(type) || !type.hasOwnProperty(NG_COMP_DEF)) { this.pendingComponents.add(type); } this.seenComponents.add(type); // Keep track of the module which declares this component, so later the component's scope // can be set correctly. If the component has already been recorded here, then one of several // cases is true: // * the module containing the component was imported multiple times (common). // * the component is declared in multiple modules (which is an error). // * the component was in 'declarations' of the testing module, and also in an imported module // in which case the module scope will be TestingModuleOverride.DECLARATION. // * overrideTemplateUsingTestingModule was called for the component in which case the module // scope will be TestingModuleOverride.OVERRIDE_TEMPLATE. // // If the component was previously in the testing module's 'declarations' (meaning the // current value is TestingModuleOverride.DECLARATION), then `moduleType` is the component's // real module, which was imported. This pattern is understood to mean that the component // should use its original scope, but that the testing module should also contain the // component in its scope. if (!this.componentToModuleScope.has(type) || this.componentToModuleScope.get(type) === TestingModuleOverride.DECLARATION) { this.componentToModuleScope.set(type, moduleType); } return; } /** @type {?} */ const directive = this.resolvers.directive.resolve(type); if (directive) { if (!type.hasOwnProperty(NG_DIR_DEF)) { this.pendingDirectives.add(type); } this.seenDirectives.add(type); return; } /** @type {?} */ const pipe = this.resolvers.pipe.resolve(type); if (pipe && !type.hasOwnProperty(NG_PIPE_DEF)) { this.pendingPipes.add(type); return; } } /** * @private * @param {?} arr * @return {?} */ queueTypesFromModulesArray(arr) { // Because we may encounter the same NgModule while processing the imports and exports of an // NgModule tree, we cache them in this set so we can skip ones that have already been seen // encountered. In some test setups, this caching resulted in 10X runtime improvement. /** @type {?} */ const processedNgModuleDefs = new Set(); /** @type {?} */ const queueTypesFromModulesArrayRecur = (/** * @param {?} arr * @return {?} */ (arr) => { for (const value of arr) { if (Array.isArray(value)) { queueTypesFromModulesArrayRecur(value); } else if (hasNgModuleDef(value)) { /** @type {?} */ const def = value.ɵmod; if (processedNgModuleDefs.has(def)) { continue; } processedNgModuleDefs.add(def); // Look through declarations, imports, and exports, and queue // everything found there. this.queueTypeArray(maybeUnwrapFn(def.declarations), value); queueTypesFromModulesArrayRecur(maybeUnwrapFn(def.imports)); queueTypesFromModulesArrayRecur(maybeUnwrapFn(def.exports)); } } }); queueTypesFromModulesArrayRecur(arr); } // When module overrides (via `TestBed.overrideModule`) are present, it might affect all modules // that import (even transitively) an overridden one. For all affected modules we need to // recalculate their scopes for a given test run and restore original scopes at the end. The goal // of this function is to collect all affected modules in a set for further processing. Example: // if we have the following module hierarchy: A -> B -> C (where `->` means `imports`) and module // `C` is overridden, we consider `A` and `B` as affected, since their scopes might become // invalidated with the override. /** * @private * @param {?} arr * @return {?} */ collectModulesAffectedByOverrides(arr) { /** @type {?} */ const seenModules = new Set(); /** @type {?} */ const affectedModules = new Set(); /** @type {?} */ const calcAffectedModulesRecur = (/** * @param {?} arr * @param {?} path * @return {?} */ (arr, path) => { for (const value of arr) { if (Array.isArray(value)) { // If the value is an array, just flatten it (by invoking this function recursively), // keeping "path" the same. calcAffectedModulesRecur(value, path); } else if (hasNgModuleDef(value)) { if (seenModules.has(value)) { // If we've seen this module before and it's included into "affected modules" list, mark // the whole path that leads to that module as affected, but do not descend into its // imports, since we already examined them before. if (affectedModules.has(value)) { path.forEach((/** * @param {?} item * @return {?} */ item => affectedModules.add(item))); } continue; } seenModules.add(value); if (this.overriddenModules.has(value)) { path.forEach((/** * @param {?} item * @return {?} */ item => affectedModules.add(item))); } // Examine module imports recursively to look for overridden modules. /** @type {?} */ const moduleDef = ((/** @type {?} */ (value)))[NG_MOD_DEF]; calcAffectedModulesRecur(maybeUnwrapFn(moduleDef.imports), path.concat(value)); } } }); calcAffectedModulesRecur(arr, []); return affectedModules; } /** * @private * @param {?} prop * @param {?} type * @return {?} */ maybeStoreNgDef(prop, type) { if (!this.initialNgDefs.has(type)) { /** @type {?} */ const currentDef = Object.getOwnPropertyDescriptor(type, prop); this.initialNgDefs.set(type, [prop, currentDef]); } } /** * @private * @param {?} type * @param {?} defField * @param {?} fieldName * @return {?} */ storeFieldOfDefOnType(type, defField, fieldName) { /** @type {?} */ const def = ((/** @type {?} */ (type)))[defField]; /** @type {?} */ const originalValue = def[fieldName]; this.defCleanupOps.push({ object: def, fieldName, originalValue }); } /** * Clears current components resolution queue, but stores the state of the queue, so we can * restore it later. Clearing the queue is required before we try to compile components (via * `TestBed.compileComponents`), so that component defs are in sync with the resolution queue. * @private * @return {?} */ clearComponentResolutionQueue() { if (this.originalComponentResolutionQueue === null) { this.originalComponentResolutionQueue = new Map(); } clearResolutionOfComponentResourcesQueue().forEach((/** * @param {?} value * @param {?} key * @return {?} */ (value, key) => (/** @type {?} */ (this.originalComponentResolutionQueue)).set(key, value))); } /* * Restores component resolution queue to the previously saved state. This operation is performed * as a part of restoring the state after completion of the current set of tests (that might * potentially mutate the state). */ /** * @private * @return {?} */ restoreComponentResolutionQueue() { if (this.originalComponentResolutionQueue !== null) { restoreComponentResolutionQueue(this.originalComponentResolutionQueue); this.originalComponentResolutionQueue = null; } } /** * @return {?} */ restoreOriginalState() { // Process cleanup ops in reverse order so the field's original value is restored correctly (in // case there were multiple overrides for the same field). forEachRight(this.defCleanupOps, (/** * @param {?} op * @return {?} */ (op) => { op.object[op.fieldName] = op.originalValue; })); // Restore initial component/directive/pipe defs this.initialNgDefs.forEach((/** * @param {?} value * @param {?} type * @return {?} */ (value, type) => { const [prop, descriptor] = value; if (!descriptor) { // Delete operations are generally undesirable since they have performance implications // on objects they were applied to. In this particular case, situations where this code // is invoked should be quite rare to cause any noticeable impact, since it's applied // only to some test cases (for example when class with no annotations extends some // @Component) when we need to clear 'ɵcmp' field on a given class to restore // its original state (before applying overrides and running tests). delete ((/** @type {?} */ (type)))[prop]; } else { Object.defineProperty(type, prop, descriptor); } })); this.initialNgDefs.clear(); this.moduleProvidersOverridden.clear(); this.restoreComponentResolutionQueue(); // Restore the locale ID to the default value, this shouldn't be necessary but we never know setLocaleId(DEFAULT_LOCALE_ID); } /** * @private * @return {?} */ compileTestModule() { class RootScopeModule { } compileNgModuleDefs((/** @type {?} */ (RootScopeModule)), { providers: [...this.rootProviderOverrides], }); /** @type {?} */ const ngZone = new NgZone({ enableLongStackTrace: true }); /** @type {?} */ const providers = [ { provide: NgZone, useValue: ngZone }, { provide: Compiler, useFactory: (/** * @return {?} */ () => new R3TestCompiler(this)) }, ...this.providers, ...this.providerOverrides, ]; /** @type {?} */ const imports = [RootScopeModule, this.additionalModuleTypes, this.imports || []]; // clang-format off compileNgModuleDefs(this.testModuleType, { declarations: this.declarations, imports, schemas: this.schemas, providers, }, /* allowDuplicateDeclarationsInRoot */ true); // clang-format on this.applyProviderOverridesToModule(this.testModuleType); } /** * @return {?} */ get injector() { if (this._injector !== null) { return this._injector; } /** @type {?} */ const providers = []; /** @type {?} */ const compilerOptions = this.platform.injector.get(COMPILER_OPTIONS); compilerOptions.forEach((/** * @param {?} opts * @return {?} */ opts => { if (opts.providers) { providers.push(opts.providers); } })); if (this.compilerProviders !== null) { providers.push(...this.compilerProviders); } // TODO(ocombe): make this work with an Injector directly instead of creating a module for it class CompilerModule { } compileNgModuleDefs((/** @type {?} */ (CompilerModule)), { providers }); /** @type {?} */ const CompilerModuleFactory = new R3NgModuleFactory(CompilerModule); this._injector = CompilerModuleFactory.create(this.platform.injector).injector; return this._injector; } // get overrides for a specific provider (if any) /** * @private * @param {?} provider * @return {?} */ getSingleProviderOverrides(provider) { /** @type {?} */ const token = getProviderToken(provider); return this.providerOverridesByToken.get(token) || null; } /** * @private * @param {?=} providers * @return {?} */ getProviderOverrides(providers) { if (!providers || !providers.length || this.providerOverridesByToken.size === 0) return []; // There are two flattening operations here. The inner flatten() operates on the metadata's // providers and applies a mapping function which retrieves overrides for each incoming // provider. The outer flatten() then flattens the produced overrides array. If this is not // done, the array can contain other empty arrays (e.g. `[[], []]`) which leak into the // providers array and contaminate any error messages that might be generated. return flatten(flatten(providers, (/** * @param {?} provider * @return {?} */ (provider) => this.getSingleProviderOverrides(provider) || []))); } /** * @private * @param {?=} providers * @return {?} */ getOverriddenProviders(providers) { if (!providers || !providers.length || this.providerOverridesByToken.size === 0) return []; /** @type {?} */ const flattenedProviders = flatten(providers); /** @type {?} */ const overrides = this.getProviderOverrides(flattenedProviders); /** @type {?} */ const overriddenProviders = [...flattenedProviders, ...overrides]; /** @type {?} */ const final = []; /** @type {?} */ const seenOverriddenProviders = new Set(); // We iterate through the list of providers in reverse order to make sure provider overrides // take precedence over the values defined in provider list. We also filter out all providers // that have overrides, keeping overridden values only. This is needed, since presence of a // provider with `ngOnDestroy` hook will cause this hook to be registered and invoked later. forEachRight(overriddenProviders, (/** * @param {?} provider * @return {?} */ (provider) => { /** @type {?} */ const token = getProviderToken(provider); if (this.providerOverridesByToken.has(token)) { if (!seenOverriddenProviders.has(token)) { seenOverriddenProviders.add(token); // Treat all overridden providers as `{multi: false}` (even if it's a multi-provider) to // make sure that provided override takes highest precedence and is not combined with // other instances of the same multi provider. final.unshift(Object.assign(Object.assign({}, provider), { multi: false })); } } else { final.unshift(provider); } })); return final; } /** * @private * @param {?=} providers * @return {?} */ hasProviderOverrides(providers) { return this.getProviderOverrides(providers).length > 0; } /** * @private * @param {?} declaration * @param {?} field * @return {?} */ patchDefWithProviderOverrides(declaration, field) { /** @type {?} */ const def = ((/** @type {?} */ (declaration)))[field]; if (def && def.providersResolver) { this.maybeStoreNgDef(field, declaration); /** @type {?} */ const resolver = def.providersResolver; /** @type {?} */ const processProvidersFn = (/** * @param {?} providers * @return {?} */ (providers) => this.getOverriddenProviders(providers)); this.storeFieldOfDefOnType(declaration, field, 'providersResolver'); def.providersResolver = (/** * @param {?} ngDef * @return {?} */ (ngDef) => resolver(ngDef, processProvidersFn)); } } } if (false) { /** * @type {?} * @private */ R3TestBedCompiler.prototype.originalComponentResolutionQueue; /** * @type {?} * @private */ R3TestBedCompiler.prototype.declarations; /** * @type {?} * @private */ R3TestBedCompiler.prototype.imports; /** * @type {?} * @private */ R3TestBedCompiler.prototype.providers; /** * @type {?} * @private */ R3TestBedCompiler.prototype.schemas; /** * @type {?} * @private */ R3TestBedCompiler.prototype.pendingComponents; /** * @type {?} * @private */ R3TestBedCompiler.prototype.pendingDirectives; /** * @type {?} * @private */ R3TestBedCompiler.prototype.pendingPipes; /** * @type {?} * @private */ R3TestBedCompiler.prototype.seenComponents; /** * @type {?} * @private */ R3TestBedCompiler.prototype.seenDirectives; /** * @type {?} * @private */ R3TestBedCompiler.prototype.overriddenModules; /** * @type {?} * @private */ R3TestBedCompiler.prototype.existingComponentStyles; /** * @type {?} * @private */ R3TestBedCompiler.prototype.resolvers; /** * @type {?} * @private */ R3TestBedCompiler.prototype.componentToModuleScope; /** * @type {?} * @private */ R3TestBedCompiler.prototype.initialNgDefs; /** * @type {?} * @private */ R3TestBedCompiler.prototype.defCleanupOps; /** * @type {?} * @private */ R3TestBedCompiler.prototype._injector; /** * @type {?} * @private */ R3TestBedCompiler.prototype.compilerProviders; /** * @type {?} * @private */ R3TestBedCompiler.prototype.providerOverrides; /** * @type {?} * @private */ R3TestBedCompiler.prototype.rootProviderOverrides; /** * @type {?} * @private */ R3TestBedCompiler.prototype.providerOverridesByModule; /** * @type {?} * @private */ R3TestBedCompiler.prototype.providerOverridesByToken; /** * @type {?} * @private */ R3TestBedCompiler.prototype.moduleProvidersOverridden; /** * @type {?} * @private */ R3TestBedCompiler.prototype.testModuleType; /** * @type {?} * @private */ R3TestBedCompiler.prototype.testModuleRef; /** * @type {?} * @private */ R3TestBedCompiler.prototype.platform; /** * @type {?} * @private */ R3TestBedCompiler.prototype.additionalModuleTypes; } /** * @return {?} */ function initResolvers() { return { module: new NgModuleResolver(), component: new ComponentResolver(), directive: new DirectiveResolver(), pipe: new PipeResolver() }; } /** * @template T * @param {?} value * @return {?} */ function hasNgModuleDef(value) { return value.hasOwnProperty('ɵmod'); } /** * @template T * @param {?} maybeFn * @return {?} */ function maybeUnwrapFn(maybeFn) { return maybeFn instanceof Function ? maybeFn() : maybeFn; } /** * @template T * @param {?} values * @param {?=} mapFn * @return {?} */ function flatten(values, mapFn) { /** @type {?} */ const out = []; values.forEach((/** * @param {?} value * @return {?} */ value => { if (Array.isArray(value)) { out.push(...flatten(value, mapFn)); } else { out.push(mapFn ? mapFn(value) : value); } })); return out; } /** * @param {?} provider * @param {?} field * @return {?} */ function getProviderField(provider, field) { return provider && typeof provider === 'object' && ((/** @type {?} */ (provider)))[field]; } /** * @param {?} provider * @return {?} */ function getProviderToken(provider) { return getProviderField(provider, 'provide') || provider; } /** * @param {?} value * @return {?} */ function isModuleWithProviders(value) { return value.hasOwnProperty('ngModule'); } /** * @template T * @param {?} values * @param {?} fn * @return {?} */ function forEachRight(values, fn) { for (let idx = values.length - 1; idx >= 0; idx--) { fn(values[idx], idx); } } /** * @param {?} name * @param {?} expectedType * @return {?} */ function invalidTypeError(name, expectedType) { return new Error(`${name} class doesn't have @${expectedType} decorator or is missing metadata.`); } class R3TestCompiler { /** * @param {?} testBed */ constructor(testBed) { this.testBed = testBed; } /** * @template T * @param {?} moduleType * @return {?} */ compileModuleSync(moduleType) { this.testBed._compileNgModuleSync(moduleType); return new R3NgModuleFactory(moduleType); } /** * @template T * @param {?} moduleType * @return {?} */ compileModuleAsync(moduleType) { return __awaiter(this, void 0, void 0, function* () { yield this.testBed._compileNgModuleAsync(moduleType); return new R3NgModuleFactory(moduleType); }); } /** * @template T * @param {?} moduleType * @return {?} */ compileModuleAndAllComponentsSync(moduleType) { /** @type {?} */ const ngModuleFactory = this.compileModuleSync(moduleType); /** @type {?} */ const componentFactories = this.testBed._getComponentFactories((/** @type {?} */ (moduleType))); return new ModuleWithComponentFactories(ngModuleFactory, componentFactories); } /** * @template T * @param {?} moduleType * @return {?} */ compileModuleAndAllComponentsAsync(moduleType) { return __awaiter(this, void 0, void 0, function* () { /** @type {?} */ const ngModuleFactory = yield this.compileModuleAsync(moduleType); /** @type {?} */ const componentFactories = this.testBed._getComponentFactories((/** @type {?} */ (moduleType))); return new ModuleWithComponentFactories(ngModuleFactory, componentFactories); }); } /** * @return {?} */ clearCache() { } /** * @param {?} type * @return {?} */ clearCacheFor(type) { } /** * @param {?} moduleType * @return {?} */ getModuleId(moduleType) { /** @type {?} */ const meta = this.testBed._getModuleResolver().resolve(moduleType); return meta && meta.id || undefined; } } if (false) { /** * @type {?} * @private */ R3TestCompiler.prototype.testBed; } //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicjNfdGVzdF9iZWRfY29tcGlsZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi8uLi9wYWNrYWdlcy9jb3JlL3Rlc3Rpbmcvc3JjL3IzX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