testing.mjs 133 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256
  1. /**
  2. * @license Angular v19.2.13
  3. * (c) 2010-2025 Google LLC. https://angular.io/
  4. * License: MIT
  5. */
  6. import * as i0 from '@angular/core';
  7. import { inject as inject$1, NgZone, ErrorHandler, Injectable, ɵDeferBlockState as _DeferBlockState, ɵtriggerResourceLoading as _triggerResourceLoading, ɵrenderDeferBlockState as _renderDeferBlockState, ɵCONTAINER_HEADER_OFFSET as _CONTAINER_HEADER_OFFSET, ɵgetDeferBlocks as _getDeferBlocks, InjectionToken, ɵDeferBlockBehavior as _DeferBlockBehavior, ɵNoopNgZone as _NoopNgZone, ApplicationRef, ɵPendingTasksInternal as _PendingTasksInternal, ɵZONELESS_ENABLED as _ZONELESS_ENABLED, ɵChangeDetectionScheduler as _ChangeDetectionScheduler, ɵEffectScheduler as _EffectScheduler, ɵMicrotaskEffectScheduler as _MicrotaskEffectScheduler, getDebugNode, RendererFactory2, ɵstringify as _stringify, Pipe, Directive, Component, NgModule, ɵReflectionCapabilities as _ReflectionCapabilities, ɵUSE_RUNTIME_DEPS_TRACKER_FOR_JIT as _USE_RUNTIME_DEPS_TRACKER_FOR_JIT, ɵdepsTracker as _depsTracker, ɵgetInjectableDef as _getInjectableDef, resolveForwardRef, ɵisComponentDefPendingResolution as _isComponentDefPendingResolution, ɵgetAsyncClassMetadataFn as _getAsyncClassMetadataFn, ɵresolveComponentResources as _resolveComponentResources, ɵRender3NgModuleRef as _Render3NgModuleRef, ApplicationInitStatus, LOCALE_ID, ɵDEFAULT_LOCALE_ID as _DEFAULT_LOCALE_ID, ɵsetLocaleId as _setLocaleId, ɵRender3ComponentFactory as _Render3ComponentFactory, ɵNG_COMP_DEF as _NG_COMP_DEF, ɵcompileComponent as _compileComponent, ɵNG_DIR_DEF as _NG_DIR_DEF, ɵcompileDirective as _compileDirective, ɵNG_PIPE_DEF as _NG_PIPE_DEF, ɵcompilePipe as _compilePipe, ɵNG_MOD_DEF as _NG_MOD_DEF, ɵpatchComponentDefWithScope as _patchComponentDefWithScope, ɵNG_INJ_DEF as _NG_INJ_DEF, ɵcompileNgModuleDefs as _compileNgModuleDefs, ɵclearResolutionOfComponentResourcesQueue as _clearResolutionOfComponentResourcesQueue, ɵrestoreComponentResolutionQueue as _restoreComponentResolutionQueue, ɵinternalProvideZoneChangeDetection as _internalProvideZoneChangeDetection, ɵChangeDetectionSchedulerImpl as _ChangeDetectionSchedulerImpl, Compiler, ɵDEFER_BLOCK_CONFIG as _DEFER_BLOCK_CONFIG, ɵINTERNAL_APPLICATION_ERROR_HANDLER as _INTERNAL_APPLICATION_ERROR_HANDLER, COMPILER_OPTIONS, Injector, ɵtransitiveScopesFor as _transitiveScopesFor, ɵgenerateStandaloneInDeclarationsError as _generateStandaloneInDeclarationsError, ɵNgModuleFactory as _NgModuleFactory, ModuleWithComponentFactories, ɵisEnvironmentProviders as _isEnvironmentProviders, ɵconvertToBitFlags as _convertToBitFlags, InjectFlags, ɵsetAllowDuplicateNgModuleIdsForTest as _setAllowDuplicateNgModuleIdsForTest, ɵresetCompiledComponents as _resetCompiledComponents, ɵsetUnknownElementStrictMode as _setUnknownElementStrictMode, ɵsetUnknownPropertyStrictMode as _setUnknownPropertyStrictMode, ɵgetUnknownElementStrictMode as _getUnknownElementStrictMode, ɵgetUnknownPropertyStrictMode as _getUnknownPropertyStrictMode, runInInjectionContext, EnvironmentInjector, ɵflushModuleScopingQueueAsMuchAsPossible as _flushModuleScopingQueueAsMuchAsPossible } from '@angular/core';
  8. export { ɵDeferBlockBehavior as DeferBlockBehavior, ɵDeferBlockState as DeferBlockState } from '@angular/core';
  9. import { Subscription } from 'rxjs';
  10. import { ResourceLoader } from '@angular/compiler';
  11. /**
  12. * Wraps a test function in an asynchronous test zone. The test will automatically
  13. * complete when all asynchronous calls within this zone are done. Can be used
  14. * to wrap an {@link inject} call.
  15. *
  16. * Example:
  17. *
  18. * ```ts
  19. * it('...', waitForAsync(inject([AClass], (object) => {
  20. * object.doSomething.then(() => {
  21. * expect(...);
  22. * })
  23. * })));
  24. * ```
  25. *
  26. * @publicApi
  27. */
  28. function waitForAsync(fn) {
  29. const _Zone = typeof Zone !== 'undefined' ? Zone : null;
  30. if (!_Zone) {
  31. return function () {
  32. return Promise.reject('Zone is needed for the waitForAsync() test helper but could not be found. ' +
  33. 'Please make sure that your environment includes zone.js');
  34. };
  35. }
  36. const asyncTest = _Zone && _Zone[_Zone.__symbol__('asyncTest')];
  37. if (typeof asyncTest === 'function') {
  38. return asyncTest(fn);
  39. }
  40. return function () {
  41. return Promise.reject('zone-testing.js is needed for the async() test helper but could not be found. ' +
  42. 'Please make sure that your environment includes zone.js/testing');
  43. };
  44. }
  45. const RETHROW_APPLICATION_ERRORS_DEFAULT = true;
  46. class TestBedApplicationErrorHandler {
  47. zone = inject$1(NgZone);
  48. userErrorHandler = inject$1(ErrorHandler);
  49. whenStableRejectFunctions = new Set();
  50. handleError(e) {
  51. try {
  52. this.zone.runOutsideAngular(() => this.userErrorHandler.handleError(e));
  53. }
  54. catch (userError) {
  55. e = userError;
  56. }
  57. // Instead of throwing the error when there are outstanding `fixture.whenStable` promises,
  58. // reject those promises with the error. This allows developers to write
  59. // expectAsync(fix.whenStable()).toBeRejected();
  60. if (this.whenStableRejectFunctions.size > 0) {
  61. for (const fn of this.whenStableRejectFunctions.values()) {
  62. fn(e);
  63. }
  64. this.whenStableRejectFunctions.clear();
  65. }
  66. else {
  67. throw e;
  68. }
  69. }
  70. static ɵfac = function TestBedApplicationErrorHandler_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || TestBedApplicationErrorHandler)(); };
  71. static ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: TestBedApplicationErrorHandler, factory: TestBedApplicationErrorHandler.ɵfac });
  72. }
  73. (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(TestBedApplicationErrorHandler, [{
  74. type: Injectable
  75. }], null, null); })();
  76. /**
  77. * Represents an individual defer block for testing purposes.
  78. *
  79. * @publicApi
  80. */
  81. class DeferBlockFixture {
  82. block;
  83. componentFixture;
  84. /** @docs-private */
  85. constructor(block, componentFixture) {
  86. this.block = block;
  87. this.componentFixture = componentFixture;
  88. }
  89. /**
  90. * Renders the specified state of the defer fixture.
  91. * @param state the defer state to render
  92. */
  93. async render(state) {
  94. if (!hasStateTemplate(state, this.block)) {
  95. const stateAsString = getDeferBlockStateNameFromEnum(state);
  96. throw new Error(`Tried to render this defer block in the \`${stateAsString}\` state, ` +
  97. `but there was no @${stateAsString.toLowerCase()} block defined in a template.`);
  98. }
  99. if (state === _DeferBlockState.Complete) {
  100. await _triggerResourceLoading(this.block.tDetails, this.block.lView, this.block.tNode);
  101. }
  102. // If the `render` method is used explicitly - skip timer-based scheduling for
  103. // `@placeholder` and `@loading` blocks and render them immediately.
  104. const skipTimerScheduling = true;
  105. _renderDeferBlockState(state, this.block.tNode, this.block.lContainer, skipTimerScheduling);
  106. this.componentFixture.detectChanges();
  107. }
  108. /**
  109. * Retrieves all nested child defer block fixtures
  110. * in a given defer block.
  111. */
  112. getDeferBlocks() {
  113. const deferBlocks = [];
  114. // An LContainer that represents a defer block has at most 1 view, which is
  115. // located right after an LContainer header. Get a hold of that view and inspect
  116. // it for nested defer blocks.
  117. const deferBlockFixtures = [];
  118. if (this.block.lContainer.length >= _CONTAINER_HEADER_OFFSET) {
  119. const lView = this.block.lContainer[_CONTAINER_HEADER_OFFSET];
  120. _getDeferBlocks(lView, deferBlocks);
  121. for (const block of deferBlocks) {
  122. deferBlockFixtures.push(new DeferBlockFixture(block, this.componentFixture));
  123. }
  124. }
  125. return Promise.resolve(deferBlockFixtures);
  126. }
  127. }
  128. function hasStateTemplate(state, block) {
  129. switch (state) {
  130. case _DeferBlockState.Placeholder:
  131. return block.tDetails.placeholderTmplIndex !== null;
  132. case _DeferBlockState.Loading:
  133. return block.tDetails.loadingTmplIndex !== null;
  134. case _DeferBlockState.Error:
  135. return block.tDetails.errorTmplIndex !== null;
  136. case _DeferBlockState.Complete:
  137. return true;
  138. default:
  139. return false;
  140. }
  141. }
  142. function getDeferBlockStateNameFromEnum(state) {
  143. switch (state) {
  144. case _DeferBlockState.Placeholder:
  145. return 'Placeholder';
  146. case _DeferBlockState.Loading:
  147. return 'Loading';
  148. case _DeferBlockState.Error:
  149. return 'Error';
  150. default:
  151. return 'Main';
  152. }
  153. }
  154. /** Whether test modules should be torn down by default. */
  155. const TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT = true;
  156. /** Whether unknown elements in templates should throw by default. */
  157. const THROW_ON_UNKNOWN_ELEMENTS_DEFAULT = false;
  158. /** Whether unknown properties in templates should throw by default. */
  159. const THROW_ON_UNKNOWN_PROPERTIES_DEFAULT = false;
  160. /** Whether defer blocks should use manual triggering or play through normally. */
  161. const DEFER_BLOCK_DEFAULT_BEHAVIOR = _DeferBlockBehavior.Playthrough;
  162. /**
  163. * An abstract class for inserting the root test component element in a platform independent way.
  164. *
  165. * @publicApi
  166. */
  167. class TestComponentRenderer {
  168. insertRootElement(rootElementId) { }
  169. removeAllRootElements() { }
  170. }
  171. /**
  172. * @publicApi
  173. */
  174. const ComponentFixtureAutoDetect = new InjectionToken('ComponentFixtureAutoDetect');
  175. /**
  176. * @publicApi
  177. */
  178. const ComponentFixtureNoNgZone = new InjectionToken('ComponentFixtureNoNgZone');
  179. /**
  180. * Fixture for debugging and testing a component.
  181. *
  182. * @publicApi
  183. */
  184. class ComponentFixture {
  185. componentRef;
  186. /**
  187. * The DebugElement associated with the root element of this component.
  188. */
  189. debugElement;
  190. /**
  191. * The instance of the root component class.
  192. */
  193. componentInstance;
  194. /**
  195. * The native element at the root of the component.
  196. */
  197. nativeElement;
  198. /**
  199. * The ElementRef for the element at the root of the component.
  200. */
  201. elementRef;
  202. /**
  203. * The ChangeDetectorRef for the component
  204. */
  205. changeDetectorRef;
  206. _renderer;
  207. _isDestroyed = false;
  208. /** @internal */
  209. _noZoneOptionIsSet = inject$1(ComponentFixtureNoNgZone, { optional: true });
  210. /** @internal */
  211. _ngZone = this._noZoneOptionIsSet ? new _NoopNgZone() : inject$1(NgZone);
  212. // Inject ApplicationRef to ensure NgZone stableness causes after render hooks to run
  213. // This will likely happen as a result of fixture.detectChanges because it calls ngZone.run
  214. // This is a crazy way of doing things but hey, it's the world we live in.
  215. // The zoneless scheduler should instead do this more imperatively by attaching
  216. // the `ComponentRef` to `ApplicationRef` and calling `appRef.tick` as the `detectChanges`
  217. // behavior.
  218. /** @internal */
  219. _appRef = inject$1(ApplicationRef);
  220. _testAppRef = this._appRef;
  221. pendingTasks = inject$1(_PendingTasksInternal);
  222. appErrorHandler = inject$1(TestBedApplicationErrorHandler);
  223. zonelessEnabled = inject$1(_ZONELESS_ENABLED);
  224. scheduler = inject$1(_ChangeDetectionScheduler);
  225. rootEffectScheduler = inject$1(_EffectScheduler);
  226. microtaskEffectScheduler = inject$1(_MicrotaskEffectScheduler);
  227. autoDetectDefault = this.zonelessEnabled ? true : false;
  228. autoDetect = inject$1(ComponentFixtureAutoDetect, { optional: true }) ?? this.autoDetectDefault;
  229. subscriptions = new Subscription();
  230. // TODO(atscott): Remove this from public API
  231. ngZone = this._noZoneOptionIsSet ? null : this._ngZone;
  232. /** @docs-private */
  233. constructor(componentRef) {
  234. this.componentRef = componentRef;
  235. this.changeDetectorRef = componentRef.changeDetectorRef;
  236. this.elementRef = componentRef.location;
  237. this.debugElement = getDebugNode(this.elementRef.nativeElement);
  238. this.componentInstance = componentRef.instance;
  239. this.nativeElement = this.elementRef.nativeElement;
  240. this.componentRef = componentRef;
  241. if (this.autoDetect) {
  242. this._testAppRef.externalTestViews.add(this.componentRef.hostView);
  243. this.scheduler?.notify(8 /* ɵNotificationSource.ViewAttached */);
  244. this.scheduler?.notify(0 /* ɵNotificationSource.MarkAncestorsForTraversal */);
  245. }
  246. this.componentRef.hostView.onDestroy(() => {
  247. this._testAppRef.externalTestViews.delete(this.componentRef.hostView);
  248. });
  249. // Create subscriptions outside the NgZone so that the callbacks run outside
  250. // of NgZone.
  251. this._ngZone.runOutsideAngular(() => {
  252. this.subscriptions.add(this._ngZone.onError.subscribe({
  253. next: (error) => {
  254. throw error;
  255. },
  256. }));
  257. });
  258. }
  259. /**
  260. * Trigger a change detection cycle for the component.
  261. */
  262. detectChanges(checkNoChanges = true) {
  263. this.microtaskEffectScheduler.flush();
  264. const originalCheckNoChanges = this.componentRef.changeDetectorRef.checkNoChanges;
  265. try {
  266. if (!checkNoChanges) {
  267. this.componentRef.changeDetectorRef.checkNoChanges = () => { };
  268. }
  269. if (this.zonelessEnabled) {
  270. try {
  271. this._testAppRef.externalTestViews.add(this.componentRef.hostView);
  272. this._appRef.tick();
  273. }
  274. finally {
  275. if (!this.autoDetect) {
  276. this._testAppRef.externalTestViews.delete(this.componentRef.hostView);
  277. }
  278. }
  279. }
  280. else {
  281. // Run the change detection inside the NgZone so that any async tasks as part of the change
  282. // detection are captured by the zone and can be waited for in isStable.
  283. this._ngZone.run(() => {
  284. // Flush root effects before `detectChanges()`, to emulate the sequencing of `tick()`.
  285. this.rootEffectScheduler.flush();
  286. this.changeDetectorRef.detectChanges();
  287. this.checkNoChanges();
  288. });
  289. }
  290. }
  291. finally {
  292. this.componentRef.changeDetectorRef.checkNoChanges = originalCheckNoChanges;
  293. }
  294. this.microtaskEffectScheduler.flush();
  295. }
  296. /**
  297. * Do a change detection run to make sure there were no changes.
  298. */
  299. checkNoChanges() {
  300. this.changeDetectorRef.checkNoChanges();
  301. }
  302. /**
  303. * Set whether the fixture should autodetect changes.
  304. *
  305. * Also runs detectChanges once so that any existing change is detected.
  306. *
  307. * @param autoDetect Whether to autodetect changes. By default, `true`.
  308. */
  309. autoDetectChanges(autoDetect = true) {
  310. if (this._noZoneOptionIsSet && !this.zonelessEnabled) {
  311. throw new Error('Cannot call autoDetectChanges when ComponentFixtureNoNgZone is set.');
  312. }
  313. if (autoDetect !== this.autoDetect) {
  314. if (autoDetect) {
  315. this._testAppRef.externalTestViews.add(this.componentRef.hostView);
  316. }
  317. else {
  318. this._testAppRef.externalTestViews.delete(this.componentRef.hostView);
  319. }
  320. }
  321. this.autoDetect = autoDetect;
  322. this.detectChanges();
  323. }
  324. /**
  325. * Return whether the fixture is currently stable or has async tasks that have not been completed
  326. * yet.
  327. */
  328. isStable() {
  329. return !this.pendingTasks.hasPendingTasks.value;
  330. }
  331. /**
  332. * Get a promise that resolves when the fixture is stable.
  333. *
  334. * This can be used to resume testing after events have triggered asynchronous activity or
  335. * asynchronous change detection.
  336. */
  337. whenStable() {
  338. if (this.isStable()) {
  339. return Promise.resolve(false);
  340. }
  341. return new Promise((resolve, reject) => {
  342. this.appErrorHandler.whenStableRejectFunctions.add(reject);
  343. this._appRef.whenStable().then(() => {
  344. this.appErrorHandler.whenStableRejectFunctions.delete(reject);
  345. resolve(true);
  346. });
  347. });
  348. }
  349. /**
  350. * Retrieves all defer block fixtures in the component fixture.
  351. */
  352. getDeferBlocks() {
  353. const deferBlocks = [];
  354. const lView = this.componentRef.hostView['_lView'];
  355. _getDeferBlocks(lView, deferBlocks);
  356. const deferBlockFixtures = [];
  357. for (const block of deferBlocks) {
  358. deferBlockFixtures.push(new DeferBlockFixture(block, this));
  359. }
  360. return Promise.resolve(deferBlockFixtures);
  361. }
  362. _getRenderer() {
  363. if (this._renderer === undefined) {
  364. this._renderer = this.componentRef.injector.get(RendererFactory2, null);
  365. }
  366. return this._renderer;
  367. }
  368. /**
  369. * Get a promise that resolves when the ui state is stable following animations.
  370. */
  371. whenRenderingDone() {
  372. const renderer = this._getRenderer();
  373. if (renderer && renderer.whenRenderingDone) {
  374. return renderer.whenRenderingDone();
  375. }
  376. return this.whenStable();
  377. }
  378. /**
  379. * Trigger component destruction.
  380. */
  381. destroy() {
  382. this.subscriptions.unsubscribe();
  383. this._testAppRef.externalTestViews.delete(this.componentRef.hostView);
  384. if (!this._isDestroyed) {
  385. this.componentRef.destroy();
  386. this._isDestroyed = true;
  387. }
  388. }
  389. }
  390. const _Zone = typeof Zone !== 'undefined' ? Zone : null;
  391. const fakeAsyncTestModule = _Zone && _Zone[_Zone.__symbol__('fakeAsyncTest')];
  392. const fakeAsyncTestModuleNotLoadedErrorMessage = `zone-testing.js is needed for the fakeAsync() test helper but could not be found.
  393. Please make sure that your environment includes zone.js/testing`;
  394. /**
  395. * Clears out the shared fake async zone for a test.
  396. * To be called in a global `beforeEach`.
  397. *
  398. * @publicApi
  399. */
  400. function resetFakeAsyncZone() {
  401. if (fakeAsyncTestModule) {
  402. return fakeAsyncTestModule.resetFakeAsyncZone();
  403. }
  404. throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
  405. }
  406. function resetFakeAsyncZoneIfExists() {
  407. if (fakeAsyncTestModule && Zone['ProxyZoneSpec']?.isLoaded()) {
  408. fakeAsyncTestModule.resetFakeAsyncZone();
  409. }
  410. }
  411. /**
  412. * Wraps a function to be executed in the `fakeAsync` zone:
  413. * - Microtasks are manually executed by calling `flushMicrotasks()`.
  414. * - Timers are synchronous; `tick()` simulates the asynchronous passage of time.
  415. *
  416. * Can be used to wrap `inject()` calls.
  417. *
  418. * @param fn The function that you want to wrap in the `fakeAsync` zone.
  419. * @param options
  420. * - flush: When true, will drain the macrotask queue after the test function completes.
  421. * When false, will throw an exception at the end of the function if there are pending timers.
  422. *
  423. * @usageNotes
  424. * ### Example
  425. *
  426. * {@example core/testing/ts/fake_async.ts region='basic'}
  427. *
  428. *
  429. * @returns The function wrapped to be executed in the `fakeAsync` zone.
  430. * Any arguments passed when calling this returned function will be passed through to the `fn`
  431. * function in the parameters when it is called.
  432. *
  433. * @publicApi
  434. */
  435. function fakeAsync(fn, options) {
  436. if (fakeAsyncTestModule) {
  437. return fakeAsyncTestModule.fakeAsync(fn, options);
  438. }
  439. throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
  440. }
  441. /**
  442. * Simulates the asynchronous passage of time for the timers in the `fakeAsync` zone.
  443. *
  444. * The microtasks queue is drained at the very start of this function and after any timer callback
  445. * has been executed.
  446. *
  447. * @param millis The number of milliseconds to advance the virtual timer.
  448. * @param tickOptions The options to pass to the `tick()` function.
  449. *
  450. * @usageNotes
  451. *
  452. * The `tick()` option is a flag called `processNewMacroTasksSynchronously`,
  453. * which determines whether or not to invoke new macroTasks.
  454. *
  455. * If you provide a `tickOptions` object, but do not specify a
  456. * `processNewMacroTasksSynchronously` property (`tick(100, {})`),
  457. * then `processNewMacroTasksSynchronously` defaults to true.
  458. *
  459. * If you omit the `tickOptions` parameter (`tick(100))`), then
  460. * `tickOptions` defaults to `{processNewMacroTasksSynchronously: true}`.
  461. *
  462. * ### Example
  463. *
  464. * {@example core/testing/ts/fake_async.ts region='basic'}
  465. *
  466. * The following example includes a nested timeout (new macroTask), and
  467. * the `tickOptions` parameter is allowed to default. In this case,
  468. * `processNewMacroTasksSynchronously` defaults to true, and the nested
  469. * function is executed on each tick.
  470. *
  471. * ```ts
  472. * it ('test with nested setTimeout', fakeAsync(() => {
  473. * let nestedTimeoutInvoked = false;
  474. * function funcWithNestedTimeout() {
  475. * setTimeout(() => {
  476. * nestedTimeoutInvoked = true;
  477. * });
  478. * };
  479. * setTimeout(funcWithNestedTimeout);
  480. * tick();
  481. * expect(nestedTimeoutInvoked).toBe(true);
  482. * }));
  483. * ```
  484. *
  485. * In the following case, `processNewMacroTasksSynchronously` is explicitly
  486. * set to false, so the nested timeout function is not invoked.
  487. *
  488. * ```ts
  489. * it ('test with nested setTimeout', fakeAsync(() => {
  490. * let nestedTimeoutInvoked = false;
  491. * function funcWithNestedTimeout() {
  492. * setTimeout(() => {
  493. * nestedTimeoutInvoked = true;
  494. * });
  495. * };
  496. * setTimeout(funcWithNestedTimeout);
  497. * tick(0, {processNewMacroTasksSynchronously: false});
  498. * expect(nestedTimeoutInvoked).toBe(false);
  499. * }));
  500. * ```
  501. *
  502. *
  503. * @publicApi
  504. */
  505. function tick(millis = 0, tickOptions = {
  506. processNewMacroTasksSynchronously: true,
  507. }) {
  508. if (fakeAsyncTestModule) {
  509. return fakeAsyncTestModule.tick(millis, tickOptions);
  510. }
  511. throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
  512. }
  513. /**
  514. * Flushes any pending microtasks and simulates the asynchronous passage of time for the timers in
  515. * the `fakeAsync` zone by
  516. * draining the macrotask queue until it is empty.
  517. *
  518. * @param maxTurns The maximum number of times the scheduler attempts to clear its queue before
  519. * throwing an error.
  520. * @returns The simulated time elapsed, in milliseconds.
  521. *
  522. * @publicApi
  523. */
  524. function flush(maxTurns) {
  525. if (fakeAsyncTestModule) {
  526. return fakeAsyncTestModule.flush(maxTurns);
  527. }
  528. throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
  529. }
  530. /**
  531. * Discard all remaining periodic tasks.
  532. *
  533. * @publicApi
  534. */
  535. function discardPeriodicTasks() {
  536. if (fakeAsyncTestModule) {
  537. return fakeAsyncTestModule.discardPeriodicTasks();
  538. }
  539. throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
  540. }
  541. /**
  542. * Flush any pending microtasks.
  543. *
  544. * @publicApi
  545. */
  546. function flushMicrotasks() {
  547. if (fakeAsyncTestModule) {
  548. return fakeAsyncTestModule.flushMicrotasks();
  549. }
  550. throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
  551. }
  552. let _nextReferenceId = 0;
  553. class MetadataOverrider {
  554. _references = new Map();
  555. /**
  556. * Creates a new instance for the given metadata class
  557. * based on an old instance and overrides.
  558. */
  559. overrideMetadata(metadataClass, oldMetadata, override) {
  560. const props = {};
  561. if (oldMetadata) {
  562. _valueProps(oldMetadata).forEach((prop) => (props[prop] = oldMetadata[prop]));
  563. }
  564. if (override.set) {
  565. if (override.remove || override.add) {
  566. throw new Error(`Cannot set and add/remove ${_stringify(metadataClass)} at the same time!`);
  567. }
  568. setMetadata(props, override.set);
  569. }
  570. if (override.remove) {
  571. removeMetadata(props, override.remove, this._references);
  572. }
  573. if (override.add) {
  574. addMetadata(props, override.add);
  575. }
  576. return new metadataClass(props);
  577. }
  578. }
  579. function removeMetadata(metadata, remove, references) {
  580. const removeObjects = new Set();
  581. for (const prop in remove) {
  582. const removeValue = remove[prop];
  583. if (Array.isArray(removeValue)) {
  584. removeValue.forEach((value) => {
  585. removeObjects.add(_propHashKey(prop, value, references));
  586. });
  587. }
  588. else {
  589. removeObjects.add(_propHashKey(prop, removeValue, references));
  590. }
  591. }
  592. for (const prop in metadata) {
  593. const propValue = metadata[prop];
  594. if (Array.isArray(propValue)) {
  595. metadata[prop] = propValue.filter((value) => !removeObjects.has(_propHashKey(prop, value, references)));
  596. }
  597. else {
  598. if (removeObjects.has(_propHashKey(prop, propValue, references))) {
  599. metadata[prop] = undefined;
  600. }
  601. }
  602. }
  603. }
  604. function addMetadata(metadata, add) {
  605. for (const prop in add) {
  606. const addValue = add[prop];
  607. const propValue = metadata[prop];
  608. if (propValue != null && Array.isArray(propValue)) {
  609. metadata[prop] = propValue.concat(addValue);
  610. }
  611. else {
  612. metadata[prop] = addValue;
  613. }
  614. }
  615. }
  616. function setMetadata(metadata, set) {
  617. for (const prop in set) {
  618. metadata[prop] = set[prop];
  619. }
  620. }
  621. function _propHashKey(propName, propValue, references) {
  622. let nextObjectId = 0;
  623. const objectIds = new Map();
  624. const replacer = (key, value) => {
  625. if (value !== null && typeof value === 'object') {
  626. if (objectIds.has(value)) {
  627. return objectIds.get(value);
  628. }
  629. // Record an id for this object such that any later references use the object's id instead
  630. // of the object itself, in order to break cyclic pointers in objects.
  631. objectIds.set(value, `ɵobj#${nextObjectId++}`);
  632. // The first time an object is seen the object itself is serialized.
  633. return value;
  634. }
  635. else if (typeof value === 'function') {
  636. value = _serializeReference(value, references);
  637. }
  638. return value;
  639. };
  640. return `${propName}:${JSON.stringify(propValue, replacer)}`;
  641. }
  642. function _serializeReference(ref, references) {
  643. let id = references.get(ref);
  644. if (!id) {
  645. id = `${_stringify(ref)}${_nextReferenceId++}`;
  646. references.set(ref, id);
  647. }
  648. return id;
  649. }
  650. function _valueProps(obj) {
  651. const props = [];
  652. // regular public props
  653. Object.keys(obj).forEach((prop) => {
  654. if (!prop.startsWith('_')) {
  655. props.push(prop);
  656. }
  657. });
  658. // getters
  659. let proto = obj;
  660. while ((proto = Object.getPrototypeOf(proto))) {
  661. Object.keys(proto).forEach((protoProp) => {
  662. const desc = Object.getOwnPropertyDescriptor(proto, protoProp);
  663. if (!protoProp.startsWith('_') && desc && 'get' in desc) {
  664. props.push(protoProp);
  665. }
  666. });
  667. }
  668. return props;
  669. }
  670. const reflection = new _ReflectionCapabilities();
  671. /**
  672. * Allows to override ivy metadata for tests (via the `TestBed`).
  673. */
  674. class OverrideResolver {
  675. overrides = new Map();
  676. resolved = new Map();
  677. addOverride(type, override) {
  678. const overrides = this.overrides.get(type) || [];
  679. overrides.push(override);
  680. this.overrides.set(type, overrides);
  681. this.resolved.delete(type);
  682. }
  683. setOverrides(overrides) {
  684. this.overrides.clear();
  685. overrides.forEach(([type, override]) => {
  686. this.addOverride(type, override);
  687. });
  688. }
  689. getAnnotation(type) {
  690. const annotations = reflection.annotations(type);
  691. // Try to find the nearest known Type annotation and make sure that this annotation is an
  692. // instance of the type we are looking for, so we can use it for resolution. Note: there might
  693. // be multiple known annotations found due to the fact that Components can extend Directives (so
  694. // both Directive and Component annotations would be present), so we always check if the known
  695. // annotation has the right type.
  696. for (let i = annotations.length - 1; i >= 0; i--) {
  697. const annotation = annotations[i];
  698. const isKnownType = annotation instanceof Directive ||
  699. annotation instanceof Component ||
  700. annotation instanceof Pipe ||
  701. annotation instanceof NgModule;
  702. if (isKnownType) {
  703. return annotation instanceof this.type ? annotation : null;
  704. }
  705. }
  706. return null;
  707. }
  708. resolve(type) {
  709. let resolved = this.resolved.get(type) || null;
  710. if (!resolved) {
  711. resolved = this.getAnnotation(type);
  712. if (resolved) {
  713. const overrides = this.overrides.get(type);
  714. if (overrides) {
  715. const overrider = new MetadataOverrider();
  716. overrides.forEach((override) => {
  717. resolved = overrider.overrideMetadata(this.type, resolved, override);
  718. });
  719. }
  720. }
  721. this.resolved.set(type, resolved);
  722. }
  723. return resolved;
  724. }
  725. }
  726. class DirectiveResolver extends OverrideResolver {
  727. get type() {
  728. return Directive;
  729. }
  730. }
  731. class ComponentResolver extends OverrideResolver {
  732. get type() {
  733. return Component;
  734. }
  735. }
  736. class PipeResolver extends OverrideResolver {
  737. get type() {
  738. return Pipe;
  739. }
  740. }
  741. class NgModuleResolver extends OverrideResolver {
  742. get type() {
  743. return NgModule;
  744. }
  745. }
  746. var TestingModuleOverride;
  747. (function (TestingModuleOverride) {
  748. TestingModuleOverride[TestingModuleOverride["DECLARATION"] = 0] = "DECLARATION";
  749. TestingModuleOverride[TestingModuleOverride["OVERRIDE_TEMPLATE"] = 1] = "OVERRIDE_TEMPLATE";
  750. })(TestingModuleOverride || (TestingModuleOverride = {}));
  751. function isTestingModuleOverride(value) {
  752. return (value === TestingModuleOverride.DECLARATION || value === TestingModuleOverride.OVERRIDE_TEMPLATE);
  753. }
  754. function assertNoStandaloneComponents(types, resolver, location) {
  755. types.forEach((type) => {
  756. if (!_getAsyncClassMetadataFn(type)) {
  757. const component = resolver.resolve(type);
  758. if (component && (component.standalone == null || component.standalone)) {
  759. throw new Error(_generateStandaloneInDeclarationsError(type, location));
  760. }
  761. }
  762. });
  763. }
  764. class TestBedCompiler {
  765. platform;
  766. additionalModuleTypes;
  767. originalComponentResolutionQueue = null;
  768. // Testing module configuration
  769. declarations = [];
  770. imports = [];
  771. providers = [];
  772. schemas = [];
  773. // Queues of components/directives/pipes that should be recompiled.
  774. pendingComponents = new Set();
  775. pendingDirectives = new Set();
  776. pendingPipes = new Set();
  777. // Set of components with async metadata, i.e. components with `@defer` blocks
  778. // in their templates.
  779. componentsWithAsyncMetadata = new Set();
  780. // Keep track of all components and directives, so we can patch Providers onto defs later.
  781. seenComponents = new Set();
  782. seenDirectives = new Set();
  783. // Keep track of overridden modules, so that we can collect all affected ones in the module tree.
  784. overriddenModules = new Set();
  785. // Store resolved styles for Components that have template overrides present and `styleUrls`
  786. // defined at the same time.
  787. existingComponentStyles = new Map();
  788. resolvers = initResolvers();
  789. // Map of component type to an NgModule that declares it.
  790. //
  791. // There are a couple special cases:
  792. // - for standalone components, the module scope value is `null`
  793. // - when a component is declared in `TestBed.configureTestingModule()` call or
  794. // a component's template is overridden via `TestBed.overrideTemplateUsingTestingModule()`.
  795. // we use a special value from the `TestingModuleOverride` enum.
  796. componentToModuleScope = new Map();
  797. // Map that keeps initial version of component/directive/pipe defs in case
  798. // we compile a Type again, thus overriding respective static fields. This is
  799. // required to make sure we restore defs to their initial states between test runs.
  800. // Note: one class may have multiple defs (for example: ɵmod and ɵinj in case of an
  801. // NgModule), store all of them in a map.
  802. initialNgDefs = new Map();
  803. // Array that keeps cleanup operations for initial versions of component/directive/pipe/module
  804. // defs in case TestBed makes changes to the originals.
  805. defCleanupOps = [];
  806. _injector = null;
  807. compilerProviders = null;
  808. providerOverrides = [];
  809. rootProviderOverrides = [];
  810. // Overrides for injectables with `{providedIn: SomeModule}` need to be tracked and added to that
  811. // module's provider list.
  812. providerOverridesByModule = new Map();
  813. providerOverridesByToken = new Map();
  814. scopesWithOverriddenProviders = new Set();
  815. testModuleType;
  816. testModuleRef = null;
  817. deferBlockBehavior = DEFER_BLOCK_DEFAULT_BEHAVIOR;
  818. rethrowApplicationTickErrors = RETHROW_APPLICATION_ERRORS_DEFAULT;
  819. constructor(platform, additionalModuleTypes) {
  820. this.platform = platform;
  821. this.additionalModuleTypes = additionalModuleTypes;
  822. class DynamicTestModule {
  823. }
  824. this.testModuleType = DynamicTestModule;
  825. }
  826. setCompilerProviders(providers) {
  827. this.compilerProviders = providers;
  828. this._injector = null;
  829. }
  830. configureTestingModule(moduleDef) {
  831. // Enqueue any compilation tasks for the directly declared component.
  832. if (moduleDef.declarations !== undefined) {
  833. // Verify that there are no standalone components
  834. assertNoStandaloneComponents(moduleDef.declarations, this.resolvers.component, '"TestBed.configureTestingModule" call');
  835. this.queueTypeArray(moduleDef.declarations, TestingModuleOverride.DECLARATION);
  836. this.declarations.push(...moduleDef.declarations);
  837. }
  838. // Enqueue any compilation tasks for imported modules.
  839. if (moduleDef.imports !== undefined) {
  840. this.queueTypesFromModulesArray(moduleDef.imports);
  841. this.imports.push(...moduleDef.imports);
  842. }
  843. if (moduleDef.providers !== undefined) {
  844. this.providers.push(...moduleDef.providers);
  845. }
  846. if (moduleDef.schemas !== undefined) {
  847. this.schemas.push(...moduleDef.schemas);
  848. }
  849. this.deferBlockBehavior = moduleDef.deferBlockBehavior ?? DEFER_BLOCK_DEFAULT_BEHAVIOR;
  850. this.rethrowApplicationTickErrors =
  851. moduleDef.rethrowApplicationErrors ?? RETHROW_APPLICATION_ERRORS_DEFAULT;
  852. }
  853. overrideModule(ngModule, override) {
  854. if (_USE_RUNTIME_DEPS_TRACKER_FOR_JIT) {
  855. _depsTracker.clearScopeCacheFor(ngModule);
  856. }
  857. this.overriddenModules.add(ngModule);
  858. // Compile the module right away.
  859. this.resolvers.module.addOverride(ngModule, override);
  860. const metadata = this.resolvers.module.resolve(ngModule);
  861. if (metadata === null) {
  862. throw invalidTypeError(ngModule.name, 'NgModule');
  863. }
  864. this.recompileNgModule(ngModule, metadata);
  865. // At this point, the module has a valid module def (ɵmod), but the override may have introduced
  866. // new declarations or imported modules. Ingest any possible new types and add them to the
  867. // current queue.
  868. this.queueTypesFromModulesArray([ngModule]);
  869. }
  870. overrideComponent(component, override) {
  871. this.verifyNoStandaloneFlagOverrides(component, override);
  872. this.resolvers.component.addOverride(component, override);
  873. this.pendingComponents.add(component);
  874. // If this is a component with async metadata (i.e. a component with a `@defer` block
  875. // in a template) - store it for future processing.
  876. this.maybeRegisterComponentWithAsyncMetadata(component);
  877. }
  878. overrideDirective(directive, override) {
  879. this.verifyNoStandaloneFlagOverrides(directive, override);
  880. this.resolvers.directive.addOverride(directive, override);
  881. this.pendingDirectives.add(directive);
  882. }
  883. overridePipe(pipe, override) {
  884. this.verifyNoStandaloneFlagOverrides(pipe, override);
  885. this.resolvers.pipe.addOverride(pipe, override);
  886. this.pendingPipes.add(pipe);
  887. }
  888. verifyNoStandaloneFlagOverrides(type, override) {
  889. if (override.add?.hasOwnProperty('standalone') ||
  890. override.set?.hasOwnProperty('standalone') ||
  891. override.remove?.hasOwnProperty('standalone')) {
  892. throw new Error(`An override for the ${type.name} class has the \`standalone\` flag. ` +
  893. `Changing the \`standalone\` flag via TestBed overrides is not supported.`);
  894. }
  895. }
  896. overrideProvider(token, provider) {
  897. let providerDef;
  898. if (provider.useFactory !== undefined) {
  899. providerDef = {
  900. provide: token,
  901. useFactory: provider.useFactory,
  902. deps: provider.deps || [],
  903. multi: provider.multi,
  904. };
  905. }
  906. else if (provider.useValue !== undefined) {
  907. providerDef = { provide: token, useValue: provider.useValue, multi: provider.multi };
  908. }
  909. else {
  910. providerDef = { provide: token };
  911. }
  912. const injectableDef = typeof token !== 'string' ? _getInjectableDef(token) : null;
  913. const providedIn = injectableDef === null ? null : resolveForwardRef(injectableDef.providedIn);
  914. const overridesBucket = providedIn === 'root' ? this.rootProviderOverrides : this.providerOverrides;
  915. overridesBucket.push(providerDef);
  916. // Keep overrides grouped by token as well for fast lookups using token
  917. this.providerOverridesByToken.set(token, providerDef);
  918. if (injectableDef !== null && providedIn !== null && typeof providedIn !== 'string') {
  919. const existingOverrides = this.providerOverridesByModule.get(providedIn);
  920. if (existingOverrides !== undefined) {
  921. existingOverrides.push(providerDef);
  922. }
  923. else {
  924. this.providerOverridesByModule.set(providedIn, [providerDef]);
  925. }
  926. }
  927. }
  928. overrideTemplateUsingTestingModule(type, template) {
  929. const def = type[_NG_COMP_DEF];
  930. const hasStyleUrls = () => {
  931. const metadata = this.resolvers.component.resolve(type);
  932. return !!metadata.styleUrl || !!metadata.styleUrls?.length;
  933. };
  934. const overrideStyleUrls = !!def && !_isComponentDefPendingResolution(type) && hasStyleUrls();
  935. // In Ivy, compiling a component does not require knowing the module providing the
  936. // component's scope, so overrideTemplateUsingTestingModule can be implemented purely via
  937. // overrideComponent. Important: overriding template requires full Component re-compilation,
  938. // which may fail in case styleUrls are also present (thus Component is considered as required
  939. // resolution). In order to avoid this, we preemptively set styleUrls to an empty array,
  940. // preserve current styles available on Component def and restore styles back once compilation
  941. // is complete.
  942. const override = overrideStyleUrls
  943. ? { template, styles: [], styleUrls: [], styleUrl: undefined }
  944. : { template };
  945. this.overrideComponent(type, { set: override });
  946. if (overrideStyleUrls && def.styles && def.styles.length > 0) {
  947. this.existingComponentStyles.set(type, def.styles);
  948. }
  949. // Set the component's scope to be the testing module.
  950. this.componentToModuleScope.set(type, TestingModuleOverride.OVERRIDE_TEMPLATE);
  951. }
  952. async resolvePendingComponentsWithAsyncMetadata() {
  953. if (this.componentsWithAsyncMetadata.size === 0)
  954. return;
  955. const promises = [];
  956. for (const component of this.componentsWithAsyncMetadata) {
  957. const asyncMetadataFn = _getAsyncClassMetadataFn(component);
  958. if (asyncMetadataFn) {
  959. promises.push(asyncMetadataFn());
  960. }
  961. }
  962. this.componentsWithAsyncMetadata.clear();
  963. const resolvedDeps = await Promise.all(promises);
  964. const flatResolvedDeps = resolvedDeps.flat(2);
  965. this.queueTypesFromModulesArray(flatResolvedDeps);
  966. // Loaded standalone components might contain imports of NgModules
  967. // with providers, make sure we override providers there too.
  968. for (const component of flatResolvedDeps) {
  969. this.applyProviderOverridesInScope(component);
  970. }
  971. }
  972. async compileComponents() {
  973. this.clearComponentResolutionQueue();
  974. // Wait for all async metadata for components that were
  975. // overridden, we need resolved metadata to perform an override
  976. // and re-compile a component.
  977. await this.resolvePendingComponentsWithAsyncMetadata();
  978. // Verify that there were no standalone components present in the `declarations` field
  979. // during the `TestBed.configureTestingModule` call. We perform this check here in addition
  980. // to the logic in the `configureTestingModule` function, since at this point we have
  981. // all async metadata resolved.
  982. assertNoStandaloneComponents(this.declarations, this.resolvers.component, '"TestBed.configureTestingModule" call');
  983. // Run compilers for all queued types.
  984. let needsAsyncResources = this.compileTypesSync();
  985. // compileComponents() should not be async unless it needs to be.
  986. if (needsAsyncResources) {
  987. let resourceLoader;
  988. let resolver = (url) => {
  989. if (!resourceLoader) {
  990. resourceLoader = this.injector.get(ResourceLoader);
  991. }
  992. return Promise.resolve(resourceLoader.get(url));
  993. };
  994. await _resolveComponentResources(resolver);
  995. }
  996. }
  997. finalize() {
  998. // One last compile
  999. this.compileTypesSync();
  1000. // Create the testing module itself.
  1001. this.compileTestModule();
  1002. this.applyTransitiveScopes();
  1003. this.applyProviderOverrides();
  1004. // Patch previously stored `styles` Component values (taken from ɵcmp), in case these
  1005. // Components have `styleUrls` fields defined and template override was requested.
  1006. this.patchComponentsWithExistingStyles();
  1007. // Clear the componentToModuleScope map, so that future compilations don't reset the scope of
  1008. // every component.
  1009. this.componentToModuleScope.clear();
  1010. const parentInjector = this.platform.injector;
  1011. this.testModuleRef = new _Render3NgModuleRef(this.testModuleType, parentInjector, []);
  1012. // ApplicationInitStatus.runInitializers() is marked @internal to core.
  1013. // Cast it to any before accessing it.
  1014. this.testModuleRef.injector.get(ApplicationInitStatus).runInitializers();
  1015. // Set locale ID after running app initializers, since locale information might be updated while
  1016. // running initializers. This is also consistent with the execution order while bootstrapping an
  1017. // app (see `packages/core/src/application_ref.ts` file).
  1018. const localeId = this.testModuleRef.injector.get(LOCALE_ID, _DEFAULT_LOCALE_ID);
  1019. _setLocaleId(localeId);
  1020. return this.testModuleRef;
  1021. }
  1022. /**
  1023. * @internal
  1024. */
  1025. _compileNgModuleSync(moduleType) {
  1026. this.queueTypesFromModulesArray([moduleType]);
  1027. this.compileTypesSync();
  1028. this.applyProviderOverrides();
  1029. this.applyProviderOverridesInScope(moduleType);
  1030. this.applyTransitiveScopes();
  1031. }
  1032. /**
  1033. * @internal
  1034. */
  1035. async _compileNgModuleAsync(moduleType) {
  1036. this.queueTypesFromModulesArray([moduleType]);
  1037. await this.compileComponents();
  1038. this.applyProviderOverrides();
  1039. this.applyProviderOverridesInScope(moduleType);
  1040. this.applyTransitiveScopes();
  1041. }
  1042. /**
  1043. * @internal
  1044. */
  1045. _getModuleResolver() {
  1046. return this.resolvers.module;
  1047. }
  1048. /**
  1049. * @internal
  1050. */
  1051. _getComponentFactories(moduleType) {
  1052. return maybeUnwrapFn(moduleType.ɵmod.declarations).reduce((factories, declaration) => {
  1053. const componentDef = declaration.ɵcmp;
  1054. componentDef && factories.push(new _Render3ComponentFactory(componentDef, this.testModuleRef));
  1055. return factories;
  1056. }, []);
  1057. }
  1058. compileTypesSync() {
  1059. // Compile all queued components, directives, pipes.
  1060. let needsAsyncResources = false;
  1061. this.pendingComponents.forEach((declaration) => {
  1062. if (_getAsyncClassMetadataFn(declaration)) {
  1063. throw new Error(`Component '${declaration.name}' has unresolved metadata. ` +
  1064. `Please call \`await TestBed.compileComponents()\` before running this test.`);
  1065. }
  1066. needsAsyncResources = needsAsyncResources || _isComponentDefPendingResolution(declaration);
  1067. const metadata = this.resolvers.component.resolve(declaration);
  1068. if (metadata === null) {
  1069. throw invalidTypeError(declaration.name, 'Component');
  1070. }
  1071. this.maybeStoreNgDef(_NG_COMP_DEF, declaration);
  1072. if (_USE_RUNTIME_DEPS_TRACKER_FOR_JIT) {
  1073. _depsTracker.clearScopeCacheFor(declaration);
  1074. }
  1075. _compileComponent(declaration, metadata);
  1076. });
  1077. this.pendingComponents.clear();
  1078. this.pendingDirectives.forEach((declaration) => {
  1079. const metadata = this.resolvers.directive.resolve(declaration);
  1080. if (metadata === null) {
  1081. throw invalidTypeError(declaration.name, 'Directive');
  1082. }
  1083. this.maybeStoreNgDef(_NG_DIR_DEF, declaration);
  1084. _compileDirective(declaration, metadata);
  1085. });
  1086. this.pendingDirectives.clear();
  1087. this.pendingPipes.forEach((declaration) => {
  1088. const metadata = this.resolvers.pipe.resolve(declaration);
  1089. if (metadata === null) {
  1090. throw invalidTypeError(declaration.name, 'Pipe');
  1091. }
  1092. this.maybeStoreNgDef(_NG_PIPE_DEF, declaration);
  1093. _compilePipe(declaration, metadata);
  1094. });
  1095. this.pendingPipes.clear();
  1096. return needsAsyncResources;
  1097. }
  1098. applyTransitiveScopes() {
  1099. if (this.overriddenModules.size > 0) {
  1100. // Module overrides (via `TestBed.overrideModule`) might affect scopes that were previously
  1101. // calculated and stored in `transitiveCompileScopes`. If module overrides are present,
  1102. // collect all affected modules and reset scopes to force their re-calculation.
  1103. const testingModuleDef = this.testModuleType[_NG_MOD_DEF];
  1104. const affectedModules = this.collectModulesAffectedByOverrides(testingModuleDef.imports);
  1105. if (affectedModules.size > 0) {
  1106. affectedModules.forEach((moduleType) => {
  1107. if (!_USE_RUNTIME_DEPS_TRACKER_FOR_JIT) {
  1108. this.storeFieldOfDefOnType(moduleType, _NG_MOD_DEF, 'transitiveCompileScopes');
  1109. moduleType[_NG_MOD_DEF].transitiveCompileScopes = null;
  1110. }
  1111. else {
  1112. _depsTracker.clearScopeCacheFor(moduleType);
  1113. }
  1114. });
  1115. }
  1116. }
  1117. const moduleToScope = new Map();
  1118. const getScopeOfModule = (moduleType) => {
  1119. if (!moduleToScope.has(moduleType)) {
  1120. const isTestingModule = isTestingModuleOverride(moduleType);
  1121. const realType = isTestingModule ? this.testModuleType : moduleType;
  1122. moduleToScope.set(moduleType, _transitiveScopesFor(realType));
  1123. }
  1124. return moduleToScope.get(moduleType);
  1125. };
  1126. this.componentToModuleScope.forEach((moduleType, componentType) => {
  1127. if (moduleType !== null) {
  1128. const moduleScope = getScopeOfModule(moduleType);
  1129. this.storeFieldOfDefOnType(componentType, _NG_COMP_DEF, 'directiveDefs');
  1130. this.storeFieldOfDefOnType(componentType, _NG_COMP_DEF, 'pipeDefs');
  1131. _patchComponentDefWithScope(getComponentDef(componentType), moduleScope);
  1132. }
  1133. // `tView` that is stored on component def contains information about directives and pipes
  1134. // that are in the scope of this component. Patching component scope will cause `tView` to be
  1135. // changed. Store original `tView` before patching scope, so the `tView` (including scope
  1136. // information) is restored back to its previous/original state before running next test.
  1137. // Resetting `tView` is also needed for cases when we apply provider overrides and those
  1138. // providers are defined on component's level, in which case they may end up included into
  1139. // `tView.blueprint`.
  1140. this.storeFieldOfDefOnType(componentType, _NG_COMP_DEF, 'tView');
  1141. });
  1142. this.componentToModuleScope.clear();
  1143. }
  1144. applyProviderOverrides() {
  1145. const maybeApplyOverrides = (field) => (type) => {
  1146. const resolver = field === _NG_COMP_DEF ? this.resolvers.component : this.resolvers.directive;
  1147. const metadata = resolver.resolve(type);
  1148. if (this.hasProviderOverrides(metadata.providers)) {
  1149. this.patchDefWithProviderOverrides(type, field);
  1150. }
  1151. };
  1152. this.seenComponents.forEach(maybeApplyOverrides(_NG_COMP_DEF));
  1153. this.seenDirectives.forEach(maybeApplyOverrides(_NG_DIR_DEF));
  1154. this.seenComponents.clear();
  1155. this.seenDirectives.clear();
  1156. }
  1157. /**
  1158. * Applies provider overrides to a given type (either an NgModule or a standalone component)
  1159. * and all imported NgModules and standalone components recursively.
  1160. */
  1161. applyProviderOverridesInScope(type) {
  1162. const hasScope = isStandaloneComponent(type) || isNgModule(type);
  1163. // The function can be re-entered recursively while inspecting dependencies
  1164. // of an NgModule or a standalone component. Exit early if we come across a
  1165. // type that can not have a scope (directive or pipe) or the type is already
  1166. // processed earlier.
  1167. if (!hasScope || this.scopesWithOverriddenProviders.has(type)) {
  1168. return;
  1169. }
  1170. this.scopesWithOverriddenProviders.add(type);
  1171. // NOTE: the line below triggers JIT compilation of the module injector,
  1172. // which also invokes verification of the NgModule semantics, which produces
  1173. // detailed error messages. The fact that the code relies on this line being
  1174. // present here is suspicious and should be refactored in a way that the line
  1175. // below can be moved (for ex. after an early exit check below).
  1176. const injectorDef = type[_NG_INJ_DEF];
  1177. // No provider overrides, exit early.
  1178. if (this.providerOverridesByToken.size === 0)
  1179. return;
  1180. if (isStandaloneComponent(type)) {
  1181. // Visit all component dependencies and override providers there.
  1182. const def = getComponentDef(type);
  1183. const dependencies = maybeUnwrapFn(def.dependencies ?? []);
  1184. for (const dependency of dependencies) {
  1185. this.applyProviderOverridesInScope(dependency);
  1186. }
  1187. }
  1188. else {
  1189. const providers = [
  1190. ...injectorDef.providers,
  1191. ...(this.providerOverridesByModule.get(type) || []),
  1192. ];
  1193. if (this.hasProviderOverrides(providers)) {
  1194. this.maybeStoreNgDef(_NG_INJ_DEF, type);
  1195. this.storeFieldOfDefOnType(type, _NG_INJ_DEF, 'providers');
  1196. injectorDef.providers = this.getOverriddenProviders(providers);
  1197. }
  1198. // Apply provider overrides to imported modules recursively
  1199. const moduleDef = type[_NG_MOD_DEF];
  1200. const imports = maybeUnwrapFn(moduleDef.imports);
  1201. for (const importedModule of imports) {
  1202. this.applyProviderOverridesInScope(importedModule);
  1203. }
  1204. // Also override the providers on any ModuleWithProviders imports since those don't appear in
  1205. // the moduleDef.
  1206. for (const importedModule of flatten(injectorDef.imports)) {
  1207. if (isModuleWithProviders(importedModule)) {
  1208. this.defCleanupOps.push({
  1209. object: importedModule,
  1210. fieldName: 'providers',
  1211. originalValue: importedModule.providers,
  1212. });
  1213. importedModule.providers = this.getOverriddenProviders(importedModule.providers);
  1214. }
  1215. }
  1216. }
  1217. }
  1218. patchComponentsWithExistingStyles() {
  1219. this.existingComponentStyles.forEach((styles, type) => (type[_NG_COMP_DEF].styles = styles));
  1220. this.existingComponentStyles.clear();
  1221. }
  1222. queueTypeArray(arr, moduleType) {
  1223. for (const value of arr) {
  1224. if (Array.isArray(value)) {
  1225. this.queueTypeArray(value, moduleType);
  1226. }
  1227. else {
  1228. this.queueType(value, moduleType);
  1229. }
  1230. }
  1231. }
  1232. recompileNgModule(ngModule, metadata) {
  1233. // Cache the initial ngModuleDef as it will be overwritten.
  1234. this.maybeStoreNgDef(_NG_MOD_DEF, ngModule);
  1235. this.maybeStoreNgDef(_NG_INJ_DEF, ngModule);
  1236. _compileNgModuleDefs(ngModule, metadata);
  1237. }
  1238. maybeRegisterComponentWithAsyncMetadata(type) {
  1239. const asyncMetadataFn = _getAsyncClassMetadataFn(type);
  1240. if (asyncMetadataFn) {
  1241. this.componentsWithAsyncMetadata.add(type);
  1242. }
  1243. }
  1244. queueType(type, moduleType) {
  1245. // If this is a component with async metadata (i.e. a component with a `@defer` block
  1246. // in a template) - store it for future processing.
  1247. this.maybeRegisterComponentWithAsyncMetadata(type);
  1248. const component = this.resolvers.component.resolve(type);
  1249. if (component) {
  1250. // Check whether a give Type has respective NG def (ɵcmp) and compile if def is
  1251. // missing. That might happen in case a class without any Angular decorators extends another
  1252. // class where Component/Directive/Pipe decorator is defined.
  1253. if (_isComponentDefPendingResolution(type) || !type.hasOwnProperty(_NG_COMP_DEF)) {
  1254. this.pendingComponents.add(type);
  1255. }
  1256. this.seenComponents.add(type);
  1257. // Keep track of the module which declares this component, so later the component's scope
  1258. // can be set correctly. If the component has already been recorded here, then one of several
  1259. // cases is true:
  1260. // * the module containing the component was imported multiple times (common).
  1261. // * the component is declared in multiple modules (which is an error).
  1262. // * the component was in 'declarations' of the testing module, and also in an imported module
  1263. // in which case the module scope will be TestingModuleOverride.DECLARATION.
  1264. // * overrideTemplateUsingTestingModule was called for the component in which case the module
  1265. // scope will be TestingModuleOverride.OVERRIDE_TEMPLATE.
  1266. //
  1267. // If the component was previously in the testing module's 'declarations' (meaning the
  1268. // current value is TestingModuleOverride.DECLARATION), then `moduleType` is the component's
  1269. // real module, which was imported. This pattern is understood to mean that the component
  1270. // should use its original scope, but that the testing module should also contain the
  1271. // component in its scope.
  1272. if (!this.componentToModuleScope.has(type) ||
  1273. this.componentToModuleScope.get(type) === TestingModuleOverride.DECLARATION) {
  1274. this.componentToModuleScope.set(type, moduleType);
  1275. }
  1276. return;
  1277. }
  1278. const directive = this.resolvers.directive.resolve(type);
  1279. if (directive) {
  1280. if (!type.hasOwnProperty(_NG_DIR_DEF)) {
  1281. this.pendingDirectives.add(type);
  1282. }
  1283. this.seenDirectives.add(type);
  1284. return;
  1285. }
  1286. const pipe = this.resolvers.pipe.resolve(type);
  1287. if (pipe && !type.hasOwnProperty(_NG_PIPE_DEF)) {
  1288. this.pendingPipes.add(type);
  1289. return;
  1290. }
  1291. }
  1292. queueTypesFromModulesArray(arr) {
  1293. // Because we may encounter the same NgModule or a standalone Component while processing
  1294. // the dependencies of an NgModule or a standalone Component, we cache them in this set so we
  1295. // can skip ones that have already been seen encountered. In some test setups, this caching
  1296. // resulted in 10X runtime improvement.
  1297. const processedDefs = new Set();
  1298. const queueTypesFromModulesArrayRecur = (arr) => {
  1299. for (const value of arr) {
  1300. if (Array.isArray(value)) {
  1301. queueTypesFromModulesArrayRecur(value);
  1302. }
  1303. else if (hasNgModuleDef(value)) {
  1304. const def = value.ɵmod;
  1305. if (processedDefs.has(def)) {
  1306. continue;
  1307. }
  1308. processedDefs.add(def);
  1309. // Look through declarations, imports, and exports, and queue
  1310. // everything found there.
  1311. this.queueTypeArray(maybeUnwrapFn(def.declarations), value);
  1312. queueTypesFromModulesArrayRecur(maybeUnwrapFn(def.imports));
  1313. queueTypesFromModulesArrayRecur(maybeUnwrapFn(def.exports));
  1314. }
  1315. else if (isModuleWithProviders(value)) {
  1316. queueTypesFromModulesArrayRecur([value.ngModule]);
  1317. }
  1318. else if (isStandaloneComponent(value)) {
  1319. this.queueType(value, null);
  1320. const def = getComponentDef(value);
  1321. if (processedDefs.has(def)) {
  1322. continue;
  1323. }
  1324. processedDefs.add(def);
  1325. const dependencies = maybeUnwrapFn(def.dependencies ?? []);
  1326. dependencies.forEach((dependency) => {
  1327. // Note: in AOT, the `dependencies` might also contain regular
  1328. // (NgModule-based) Component, Directive and Pipes, so we handle
  1329. // them separately and proceed with recursive process for standalone
  1330. // Components and NgModules only.
  1331. if (isStandaloneComponent(dependency) || hasNgModuleDef(dependency)) {
  1332. queueTypesFromModulesArrayRecur([dependency]);
  1333. }
  1334. else {
  1335. this.queueType(dependency, null);
  1336. }
  1337. });
  1338. }
  1339. }
  1340. };
  1341. queueTypesFromModulesArrayRecur(arr);
  1342. }
  1343. // When module overrides (via `TestBed.overrideModule`) are present, it might affect all modules
  1344. // that import (even transitively) an overridden one. For all affected modules we need to
  1345. // recalculate their scopes for a given test run and restore original scopes at the end. The goal
  1346. // of this function is to collect all affected modules in a set for further processing. Example:
  1347. // if we have the following module hierarchy: A -> B -> C (where `->` means `imports`) and module
  1348. // `C` is overridden, we consider `A` and `B` as affected, since their scopes might become
  1349. // invalidated with the override.
  1350. collectModulesAffectedByOverrides(arr) {
  1351. const seenModules = new Set();
  1352. const affectedModules = new Set();
  1353. const calcAffectedModulesRecur = (arr, path) => {
  1354. for (const value of arr) {
  1355. if (Array.isArray(value)) {
  1356. // If the value is an array, just flatten it (by invoking this function recursively),
  1357. // keeping "path" the same.
  1358. calcAffectedModulesRecur(value, path);
  1359. }
  1360. else if (hasNgModuleDef(value)) {
  1361. if (seenModules.has(value)) {
  1362. // If we've seen this module before and it's included into "affected modules" list, mark
  1363. // the whole path that leads to that module as affected, but do not descend into its
  1364. // imports, since we already examined them before.
  1365. if (affectedModules.has(value)) {
  1366. path.forEach((item) => affectedModules.add(item));
  1367. }
  1368. continue;
  1369. }
  1370. seenModules.add(value);
  1371. if (this.overriddenModules.has(value)) {
  1372. path.forEach((item) => affectedModules.add(item));
  1373. }
  1374. // Examine module imports recursively to look for overridden modules.
  1375. const moduleDef = value[_NG_MOD_DEF];
  1376. calcAffectedModulesRecur(maybeUnwrapFn(moduleDef.imports), path.concat(value));
  1377. }
  1378. }
  1379. };
  1380. calcAffectedModulesRecur(arr, []);
  1381. return affectedModules;
  1382. }
  1383. /**
  1384. * Preserve an original def (such as ɵmod, ɵinj, etc) before applying an override.
  1385. * Note: one class may have multiple defs (for example: ɵmod and ɵinj in case of
  1386. * an NgModule). If there is a def in a set already, don't override it, since
  1387. * an original one should be restored at the end of a test.
  1388. */
  1389. maybeStoreNgDef(prop, type) {
  1390. if (!this.initialNgDefs.has(type)) {
  1391. this.initialNgDefs.set(type, new Map());
  1392. }
  1393. const currentDefs = this.initialNgDefs.get(type);
  1394. if (!currentDefs.has(prop)) {
  1395. const currentDef = Object.getOwnPropertyDescriptor(type, prop);
  1396. currentDefs.set(prop, currentDef);
  1397. }
  1398. }
  1399. storeFieldOfDefOnType(type, defField, fieldName) {
  1400. const def = type[defField];
  1401. const originalValue = def[fieldName];
  1402. this.defCleanupOps.push({ object: def, fieldName, originalValue });
  1403. }
  1404. /**
  1405. * Clears current components resolution queue, but stores the state of the queue, so we can
  1406. * restore it later. Clearing the queue is required before we try to compile components (via
  1407. * `TestBed.compileComponents`), so that component defs are in sync with the resolution queue.
  1408. */
  1409. clearComponentResolutionQueue() {
  1410. if (this.originalComponentResolutionQueue === null) {
  1411. this.originalComponentResolutionQueue = new Map();
  1412. }
  1413. _clearResolutionOfComponentResourcesQueue().forEach((value, key) => this.originalComponentResolutionQueue.set(key, value));
  1414. }
  1415. /*
  1416. * Restores component resolution queue to the previously saved state. This operation is performed
  1417. * as a part of restoring the state after completion of the current set of tests (that might
  1418. * potentially mutate the state).
  1419. */
  1420. restoreComponentResolutionQueue() {
  1421. if (this.originalComponentResolutionQueue !== null) {
  1422. _restoreComponentResolutionQueue(this.originalComponentResolutionQueue);
  1423. this.originalComponentResolutionQueue = null;
  1424. }
  1425. }
  1426. restoreOriginalState() {
  1427. // Process cleanup ops in reverse order so the field's original value is restored correctly (in
  1428. // case there were multiple overrides for the same field).
  1429. forEachRight(this.defCleanupOps, (op) => {
  1430. op.object[op.fieldName] = op.originalValue;
  1431. });
  1432. // Restore initial component/directive/pipe defs
  1433. this.initialNgDefs.forEach((defs, type) => {
  1434. if (_USE_RUNTIME_DEPS_TRACKER_FOR_JIT) {
  1435. _depsTracker.clearScopeCacheFor(type);
  1436. }
  1437. defs.forEach((descriptor, prop) => {
  1438. if (!descriptor) {
  1439. // Delete operations are generally undesirable since they have performance
  1440. // implications on objects they were applied to. In this particular case, situations
  1441. // where this code is invoked should be quite rare to cause any noticeable impact,
  1442. // since it's applied only to some test cases (for example when class with no
  1443. // annotations extends some @Component) when we need to clear 'ɵcmp' field on a given
  1444. // class to restore its original state (before applying overrides and running tests).
  1445. delete type[prop];
  1446. }
  1447. else {
  1448. Object.defineProperty(type, prop, descriptor);
  1449. }
  1450. });
  1451. });
  1452. this.initialNgDefs.clear();
  1453. this.scopesWithOverriddenProviders.clear();
  1454. this.restoreComponentResolutionQueue();
  1455. // Restore the locale ID to the default value, this shouldn't be necessary but we never know
  1456. _setLocaleId(_DEFAULT_LOCALE_ID);
  1457. }
  1458. compileTestModule() {
  1459. class RootScopeModule {
  1460. }
  1461. _compileNgModuleDefs(RootScopeModule, {
  1462. providers: [
  1463. ...this.rootProviderOverrides,
  1464. _internalProvideZoneChangeDetection({}),
  1465. TestBedApplicationErrorHandler,
  1466. { provide: _ChangeDetectionScheduler, useExisting: _ChangeDetectionSchedulerImpl },
  1467. ],
  1468. });
  1469. const providers = [
  1470. { provide: Compiler, useFactory: () => new R3TestCompiler(this) },
  1471. { provide: _DEFER_BLOCK_CONFIG, useValue: { behavior: this.deferBlockBehavior } },
  1472. {
  1473. provide: _INTERNAL_APPLICATION_ERROR_HANDLER,
  1474. useFactory: () => {
  1475. if (this.rethrowApplicationTickErrors) {
  1476. const handler = inject$1(TestBedApplicationErrorHandler);
  1477. return (e) => {
  1478. handler.handleError(e);
  1479. };
  1480. }
  1481. else {
  1482. const userErrorHandler = inject$1(ErrorHandler);
  1483. const ngZone = inject$1(NgZone);
  1484. return (e) => ngZone.runOutsideAngular(() => userErrorHandler.handleError(e));
  1485. }
  1486. },
  1487. },
  1488. ...this.providers,
  1489. ...this.providerOverrides,
  1490. ];
  1491. const imports = [RootScopeModule, this.additionalModuleTypes, this.imports || []];
  1492. _compileNgModuleDefs(this.testModuleType, {
  1493. declarations: this.declarations,
  1494. imports,
  1495. schemas: this.schemas,
  1496. providers,
  1497. },
  1498. /* allowDuplicateDeclarationsInRoot */ true);
  1499. this.applyProviderOverridesInScope(this.testModuleType);
  1500. }
  1501. get injector() {
  1502. if (this._injector !== null) {
  1503. return this._injector;
  1504. }
  1505. const providers = [];
  1506. const compilerOptions = this.platform.injector.get(COMPILER_OPTIONS, []);
  1507. compilerOptions.forEach((opts) => {
  1508. if (opts.providers) {
  1509. providers.push(opts.providers);
  1510. }
  1511. });
  1512. if (this.compilerProviders !== null) {
  1513. providers.push(...this.compilerProviders);
  1514. }
  1515. this._injector = Injector.create({ providers, parent: this.platform.injector });
  1516. return this._injector;
  1517. }
  1518. // get overrides for a specific provider (if any)
  1519. getSingleProviderOverrides(provider) {
  1520. const token = getProviderToken(provider);
  1521. return this.providerOverridesByToken.get(token) || null;
  1522. }
  1523. getProviderOverrides(providers) {
  1524. if (!providers || !providers.length || this.providerOverridesByToken.size === 0)
  1525. return [];
  1526. // There are two flattening operations here. The inner flattenProviders() operates on the
  1527. // metadata's providers and applies a mapping function which retrieves overrides for each
  1528. // incoming provider. The outer flatten() then flattens the produced overrides array. If this is
  1529. // not done, the array can contain other empty arrays (e.g. `[[], []]`) which leak into the
  1530. // providers array and contaminate any error messages that might be generated.
  1531. return flatten(flattenProviders(providers, (provider) => this.getSingleProviderOverrides(provider) || []));
  1532. }
  1533. getOverriddenProviders(providers) {
  1534. if (!providers || !providers.length || this.providerOverridesByToken.size === 0)
  1535. return [];
  1536. const flattenedProviders = flattenProviders(providers);
  1537. const overrides = this.getProviderOverrides(flattenedProviders);
  1538. const overriddenProviders = [...flattenedProviders, ...overrides];
  1539. const final = [];
  1540. const seenOverriddenProviders = new Set();
  1541. // We iterate through the list of providers in reverse order to make sure provider overrides
  1542. // take precedence over the values defined in provider list. We also filter out all providers
  1543. // that have overrides, keeping overridden values only. This is needed, since presence of a
  1544. // provider with `ngOnDestroy` hook will cause this hook to be registered and invoked later.
  1545. forEachRight(overriddenProviders, (provider) => {
  1546. const token = getProviderToken(provider);
  1547. if (this.providerOverridesByToken.has(token)) {
  1548. if (!seenOverriddenProviders.has(token)) {
  1549. seenOverriddenProviders.add(token);
  1550. // Treat all overridden providers as `{multi: false}` (even if it's a multi-provider) to
  1551. // make sure that provided override takes highest precedence and is not combined with
  1552. // other instances of the same multi provider.
  1553. final.unshift({ ...provider, multi: false });
  1554. }
  1555. }
  1556. else {
  1557. final.unshift(provider);
  1558. }
  1559. });
  1560. return final;
  1561. }
  1562. hasProviderOverrides(providers) {
  1563. return this.getProviderOverrides(providers).length > 0;
  1564. }
  1565. patchDefWithProviderOverrides(declaration, field) {
  1566. const def = declaration[field];
  1567. if (def && def.providersResolver) {
  1568. this.maybeStoreNgDef(field, declaration);
  1569. const resolver = def.providersResolver;
  1570. const processProvidersFn = (providers) => this.getOverriddenProviders(providers);
  1571. this.storeFieldOfDefOnType(declaration, field, 'providersResolver');
  1572. def.providersResolver = (ngDef) => resolver(ngDef, processProvidersFn);
  1573. }
  1574. }
  1575. }
  1576. function initResolvers() {
  1577. return {
  1578. module: new NgModuleResolver(),
  1579. component: new ComponentResolver(),
  1580. directive: new DirectiveResolver(),
  1581. pipe: new PipeResolver(),
  1582. };
  1583. }
  1584. function isStandaloneComponent(value) {
  1585. const def = getComponentDef(value);
  1586. return !!def?.standalone;
  1587. }
  1588. function getComponentDef(value) {
  1589. return value.ɵcmp ?? null;
  1590. }
  1591. function hasNgModuleDef(value) {
  1592. return value.hasOwnProperty('ɵmod');
  1593. }
  1594. function isNgModule(value) {
  1595. return hasNgModuleDef(value);
  1596. }
  1597. function maybeUnwrapFn(maybeFn) {
  1598. return maybeFn instanceof Function ? maybeFn() : maybeFn;
  1599. }
  1600. function flatten(values) {
  1601. const out = [];
  1602. values.forEach((value) => {
  1603. if (Array.isArray(value)) {
  1604. out.push(...flatten(value));
  1605. }
  1606. else {
  1607. out.push(value);
  1608. }
  1609. });
  1610. return out;
  1611. }
  1612. function identityFn(value) {
  1613. return value;
  1614. }
  1615. function flattenProviders(providers, mapFn = identityFn) {
  1616. const out = [];
  1617. for (let provider of providers) {
  1618. if (_isEnvironmentProviders(provider)) {
  1619. provider = provider.ɵproviders;
  1620. }
  1621. if (Array.isArray(provider)) {
  1622. out.push(...flattenProviders(provider, mapFn));
  1623. }
  1624. else {
  1625. out.push(mapFn(provider));
  1626. }
  1627. }
  1628. return out;
  1629. }
  1630. function getProviderField(provider, field) {
  1631. return provider && typeof provider === 'object' && provider[field];
  1632. }
  1633. function getProviderToken(provider) {
  1634. return getProviderField(provider, 'provide') || provider;
  1635. }
  1636. function isModuleWithProviders(value) {
  1637. return value.hasOwnProperty('ngModule');
  1638. }
  1639. function forEachRight(values, fn) {
  1640. for (let idx = values.length - 1; idx >= 0; idx--) {
  1641. fn(values[idx], idx);
  1642. }
  1643. }
  1644. function invalidTypeError(name, expectedType) {
  1645. return new Error(`${name} class doesn't have @${expectedType} decorator or is missing metadata.`);
  1646. }
  1647. class R3TestCompiler {
  1648. testBed;
  1649. constructor(testBed) {
  1650. this.testBed = testBed;
  1651. }
  1652. compileModuleSync(moduleType) {
  1653. this.testBed._compileNgModuleSync(moduleType);
  1654. return new _NgModuleFactory(moduleType);
  1655. }
  1656. async compileModuleAsync(moduleType) {
  1657. await this.testBed._compileNgModuleAsync(moduleType);
  1658. return new _NgModuleFactory(moduleType);
  1659. }
  1660. compileModuleAndAllComponentsSync(moduleType) {
  1661. const ngModuleFactory = this.compileModuleSync(moduleType);
  1662. const componentFactories = this.testBed._getComponentFactories(moduleType);
  1663. return new ModuleWithComponentFactories(ngModuleFactory, componentFactories);
  1664. }
  1665. async compileModuleAndAllComponentsAsync(moduleType) {
  1666. const ngModuleFactory = await this.compileModuleAsync(moduleType);
  1667. const componentFactories = this.testBed._getComponentFactories(moduleType);
  1668. return new ModuleWithComponentFactories(ngModuleFactory, componentFactories);
  1669. }
  1670. clearCache() { }
  1671. clearCacheFor(type) { }
  1672. getModuleId(moduleType) {
  1673. const meta = this.testBed._getModuleResolver().resolve(moduleType);
  1674. return (meta && meta.id) || undefined;
  1675. }
  1676. }
  1677. // The formatter and CI disagree on how this import statement should be formatted. Both try to keep
  1678. // it on one line, too, which has gotten very hard to read & manage. So disable the formatter for
  1679. // this statement only.
  1680. let _nextRootElementId = 0;
  1681. /**
  1682. * Returns a singleton of the `TestBed` class.
  1683. *
  1684. * @publicApi
  1685. */
  1686. function getTestBed() {
  1687. return TestBedImpl.INSTANCE;
  1688. }
  1689. /**
  1690. * @description
  1691. * Configures and initializes environment for unit testing and provides methods for
  1692. * creating components and services in unit tests.
  1693. *
  1694. * TestBed is the primary api for writing unit tests for Angular applications and libraries.
  1695. */
  1696. class TestBedImpl {
  1697. static _INSTANCE = null;
  1698. static get INSTANCE() {
  1699. return (TestBedImpl._INSTANCE = TestBedImpl._INSTANCE || new TestBedImpl());
  1700. }
  1701. /**
  1702. * Teardown options that have been configured at the environment level.
  1703. * Used as a fallback if no instance-level options have been provided.
  1704. */
  1705. static _environmentTeardownOptions;
  1706. /**
  1707. * "Error on unknown elements" option that has been configured at the environment level.
  1708. * Used as a fallback if no instance-level option has been provided.
  1709. */
  1710. static _environmentErrorOnUnknownElementsOption;
  1711. /**
  1712. * "Error on unknown properties" option that has been configured at the environment level.
  1713. * Used as a fallback if no instance-level option has been provided.
  1714. */
  1715. static _environmentErrorOnUnknownPropertiesOption;
  1716. /**
  1717. * Teardown options that have been configured at the `TestBed` instance level.
  1718. * These options take precedence over the environment-level ones.
  1719. */
  1720. _instanceTeardownOptions;
  1721. /**
  1722. * Defer block behavior option that specifies whether defer blocks will be triggered manually
  1723. * or set to play through.
  1724. */
  1725. _instanceDeferBlockBehavior = DEFER_BLOCK_DEFAULT_BEHAVIOR;
  1726. /**
  1727. * "Error on unknown elements" option that has been configured at the `TestBed` instance level.
  1728. * This option takes precedence over the environment-level one.
  1729. */
  1730. _instanceErrorOnUnknownElementsOption;
  1731. /**
  1732. * "Error on unknown properties" option that has been configured at the `TestBed` instance level.
  1733. * This option takes precedence over the environment-level one.
  1734. */
  1735. _instanceErrorOnUnknownPropertiesOption;
  1736. /**
  1737. * Stores the previous "Error on unknown elements" option value,
  1738. * allowing to restore it in the reset testing module logic.
  1739. */
  1740. _previousErrorOnUnknownElementsOption;
  1741. /**
  1742. * Stores the previous "Error on unknown properties" option value,
  1743. * allowing to restore it in the reset testing module logic.
  1744. */
  1745. _previousErrorOnUnknownPropertiesOption;
  1746. /**
  1747. * Initialize the environment for testing with a compiler factory, a PlatformRef, and an
  1748. * angular module. These are common to every test in the suite.
  1749. *
  1750. * This may only be called once, to set up the common providers for the current test
  1751. * suite on the current platform. If you absolutely need to change the providers,
  1752. * first use `resetTestEnvironment`.
  1753. *
  1754. * Test modules and platforms for individual platforms are available from
  1755. * '@angular/<platform_name>/testing'.
  1756. *
  1757. * @publicApi
  1758. */
  1759. static initTestEnvironment(ngModule, platform, options) {
  1760. const testBed = TestBedImpl.INSTANCE;
  1761. testBed.initTestEnvironment(ngModule, platform, options);
  1762. return testBed;
  1763. }
  1764. /**
  1765. * Reset the providers for the test injector.
  1766. *
  1767. * @publicApi
  1768. */
  1769. static resetTestEnvironment() {
  1770. TestBedImpl.INSTANCE.resetTestEnvironment();
  1771. }
  1772. static configureCompiler(config) {
  1773. return TestBedImpl.INSTANCE.configureCompiler(config);
  1774. }
  1775. /**
  1776. * Allows overriding default providers, directives, pipes, modules of the test injector,
  1777. * which are defined in test_injector.js
  1778. */
  1779. static configureTestingModule(moduleDef) {
  1780. return TestBedImpl.INSTANCE.configureTestingModule(moduleDef);
  1781. }
  1782. /**
  1783. * Compile components with a `templateUrl` for the test's NgModule.
  1784. * It is necessary to call this function
  1785. * as fetching urls is asynchronous.
  1786. */
  1787. static compileComponents() {
  1788. return TestBedImpl.INSTANCE.compileComponents();
  1789. }
  1790. static overrideModule(ngModule, override) {
  1791. return TestBedImpl.INSTANCE.overrideModule(ngModule, override);
  1792. }
  1793. static overrideComponent(component, override) {
  1794. return TestBedImpl.INSTANCE.overrideComponent(component, override);
  1795. }
  1796. static overrideDirective(directive, override) {
  1797. return TestBedImpl.INSTANCE.overrideDirective(directive, override);
  1798. }
  1799. static overridePipe(pipe, override) {
  1800. return TestBedImpl.INSTANCE.overridePipe(pipe, override);
  1801. }
  1802. static overrideTemplate(component, template) {
  1803. return TestBedImpl.INSTANCE.overrideTemplate(component, template);
  1804. }
  1805. /**
  1806. * Overrides the template of the given component, compiling the template
  1807. * in the context of the TestingModule.
  1808. *
  1809. * Note: This works for JIT and AOTed components as well.
  1810. */
  1811. static overrideTemplateUsingTestingModule(component, template) {
  1812. return TestBedImpl.INSTANCE.overrideTemplateUsingTestingModule(component, template);
  1813. }
  1814. static overrideProvider(token, provider) {
  1815. return TestBedImpl.INSTANCE.overrideProvider(token, provider);
  1816. }
  1817. static inject(token, notFoundValue, flags) {
  1818. return TestBedImpl.INSTANCE.inject(token, notFoundValue, _convertToBitFlags(flags));
  1819. }
  1820. /** @deprecated from v9.0.0 use TestBed.inject */
  1821. static get(token, notFoundValue = Injector.THROW_IF_NOT_FOUND, flags = InjectFlags.Default) {
  1822. return TestBedImpl.INSTANCE.inject(token, notFoundValue, flags);
  1823. }
  1824. /**
  1825. * Runs the given function in the `EnvironmentInjector` context of `TestBed`.
  1826. *
  1827. * @see {@link EnvironmentInjector#runInContext}
  1828. */
  1829. static runInInjectionContext(fn) {
  1830. return TestBedImpl.INSTANCE.runInInjectionContext(fn);
  1831. }
  1832. static createComponent(component) {
  1833. return TestBedImpl.INSTANCE.createComponent(component);
  1834. }
  1835. static resetTestingModule() {
  1836. return TestBedImpl.INSTANCE.resetTestingModule();
  1837. }
  1838. static execute(tokens, fn, context) {
  1839. return TestBedImpl.INSTANCE.execute(tokens, fn, context);
  1840. }
  1841. static get platform() {
  1842. return TestBedImpl.INSTANCE.platform;
  1843. }
  1844. static get ngModule() {
  1845. return TestBedImpl.INSTANCE.ngModule;
  1846. }
  1847. static flushEffects() {
  1848. return TestBedImpl.INSTANCE.flushEffects();
  1849. }
  1850. // Properties
  1851. platform = null;
  1852. ngModule = null;
  1853. _compiler = null;
  1854. _testModuleRef = null;
  1855. _activeFixtures = [];
  1856. /**
  1857. * Internal-only flag to indicate whether a module
  1858. * scoping queue has been checked and flushed already.
  1859. * @docs-private
  1860. */
  1861. globalCompilationChecked = false;
  1862. /**
  1863. * Initialize the environment for testing with a compiler factory, a PlatformRef, and an
  1864. * angular module. These are common to every test in the suite.
  1865. *
  1866. * This may only be called once, to set up the common providers for the current test
  1867. * suite on the current platform. If you absolutely need to change the providers,
  1868. * first use `resetTestEnvironment`.
  1869. *
  1870. * Test modules and platforms for individual platforms are available from
  1871. * '@angular/<platform_name>/testing'.
  1872. *
  1873. * @publicApi
  1874. */
  1875. initTestEnvironment(ngModule, platform, options) {
  1876. if (this.platform || this.ngModule) {
  1877. throw new Error('Cannot set base providers because it has already been called');
  1878. }
  1879. TestBedImpl._environmentTeardownOptions = options?.teardown;
  1880. TestBedImpl._environmentErrorOnUnknownElementsOption = options?.errorOnUnknownElements;
  1881. TestBedImpl._environmentErrorOnUnknownPropertiesOption = options?.errorOnUnknownProperties;
  1882. this.platform = platform;
  1883. this.ngModule = ngModule;
  1884. this._compiler = new TestBedCompiler(this.platform, this.ngModule);
  1885. // TestBed does not have an API which can reliably detect the start of a test, and thus could be
  1886. // used to track the state of the NgModule registry and reset it correctly. Instead, when we
  1887. // know we're in a testing scenario, we disable the check for duplicate NgModule registration
  1888. // completely.
  1889. _setAllowDuplicateNgModuleIdsForTest(true);
  1890. }
  1891. /**
  1892. * Reset the providers for the test injector.
  1893. *
  1894. * @publicApi
  1895. */
  1896. resetTestEnvironment() {
  1897. this.resetTestingModule();
  1898. this._compiler = null;
  1899. this.platform = null;
  1900. this.ngModule = null;
  1901. TestBedImpl._environmentTeardownOptions = undefined;
  1902. _setAllowDuplicateNgModuleIdsForTest(false);
  1903. }
  1904. resetTestingModule() {
  1905. this.checkGlobalCompilationFinished();
  1906. _resetCompiledComponents();
  1907. if (this._compiler !== null) {
  1908. this.compiler.restoreOriginalState();
  1909. }
  1910. this._compiler = new TestBedCompiler(this.platform, this.ngModule);
  1911. // Restore the previous value of the "error on unknown elements" option
  1912. _setUnknownElementStrictMode(this._previousErrorOnUnknownElementsOption ?? THROW_ON_UNKNOWN_ELEMENTS_DEFAULT);
  1913. // Restore the previous value of the "error on unknown properties" option
  1914. _setUnknownPropertyStrictMode(this._previousErrorOnUnknownPropertiesOption ?? THROW_ON_UNKNOWN_PROPERTIES_DEFAULT);
  1915. // We have to chain a couple of try/finally blocks, because each step can
  1916. // throw errors and we don't want it to interrupt the next step and we also
  1917. // want an error to be thrown at the end.
  1918. try {
  1919. this.destroyActiveFixtures();
  1920. }
  1921. finally {
  1922. try {
  1923. if (this.shouldTearDownTestingModule()) {
  1924. this.tearDownTestingModule();
  1925. }
  1926. }
  1927. finally {
  1928. this._testModuleRef = null;
  1929. this._instanceTeardownOptions = undefined;
  1930. this._instanceErrorOnUnknownElementsOption = undefined;
  1931. this._instanceErrorOnUnknownPropertiesOption = undefined;
  1932. this._instanceDeferBlockBehavior = DEFER_BLOCK_DEFAULT_BEHAVIOR;
  1933. }
  1934. }
  1935. return this;
  1936. }
  1937. configureCompiler(config) {
  1938. if (config.useJit != null) {
  1939. throw new Error('JIT compiler is not configurable via TestBed APIs.');
  1940. }
  1941. if (config.providers !== undefined) {
  1942. this.compiler.setCompilerProviders(config.providers);
  1943. }
  1944. return this;
  1945. }
  1946. configureTestingModule(moduleDef) {
  1947. this.assertNotInstantiated('TestBed.configureTestingModule', 'configure the test module');
  1948. // Trigger module scoping queue flush before executing other TestBed operations in a test.
  1949. // This is needed for the first test invocation to ensure that globally declared modules have
  1950. // their components scoped properly. See the `checkGlobalCompilationFinished` function
  1951. // description for additional info.
  1952. this.checkGlobalCompilationFinished();
  1953. // Always re-assign the options, even if they're undefined.
  1954. // This ensures that we don't carry them between tests.
  1955. this._instanceTeardownOptions = moduleDef.teardown;
  1956. this._instanceErrorOnUnknownElementsOption = moduleDef.errorOnUnknownElements;
  1957. this._instanceErrorOnUnknownPropertiesOption = moduleDef.errorOnUnknownProperties;
  1958. this._instanceDeferBlockBehavior = moduleDef.deferBlockBehavior ?? DEFER_BLOCK_DEFAULT_BEHAVIOR;
  1959. // Store the current value of the strict mode option,
  1960. // so we can restore it later
  1961. this._previousErrorOnUnknownElementsOption = _getUnknownElementStrictMode();
  1962. _setUnknownElementStrictMode(this.shouldThrowErrorOnUnknownElements());
  1963. this._previousErrorOnUnknownPropertiesOption = _getUnknownPropertyStrictMode();
  1964. _setUnknownPropertyStrictMode(this.shouldThrowErrorOnUnknownProperties());
  1965. this.compiler.configureTestingModule(moduleDef);
  1966. return this;
  1967. }
  1968. compileComponents() {
  1969. return this.compiler.compileComponents();
  1970. }
  1971. inject(token, notFoundValue, flags) {
  1972. if (token === TestBed) {
  1973. return this;
  1974. }
  1975. const UNDEFINED = {};
  1976. const result = this.testModuleRef.injector.get(token, UNDEFINED, _convertToBitFlags(flags));
  1977. return result === UNDEFINED
  1978. ? this.compiler.injector.get(token, notFoundValue, flags)
  1979. : result;
  1980. }
  1981. /** @deprecated from v9.0.0 use TestBed.inject */
  1982. get(token, notFoundValue = Injector.THROW_IF_NOT_FOUND, flags = InjectFlags.Default) {
  1983. return this.inject(token, notFoundValue, flags);
  1984. }
  1985. runInInjectionContext(fn) {
  1986. return runInInjectionContext(this.inject(EnvironmentInjector), fn);
  1987. }
  1988. execute(tokens, fn, context) {
  1989. const params = tokens.map((t) => this.inject(t));
  1990. return fn.apply(context, params);
  1991. }
  1992. overrideModule(ngModule, override) {
  1993. this.assertNotInstantiated('overrideModule', 'override module metadata');
  1994. this.compiler.overrideModule(ngModule, override);
  1995. return this;
  1996. }
  1997. overrideComponent(component, override) {
  1998. this.assertNotInstantiated('overrideComponent', 'override component metadata');
  1999. this.compiler.overrideComponent(component, override);
  2000. return this;
  2001. }
  2002. overrideTemplateUsingTestingModule(component, template) {
  2003. this.assertNotInstantiated('TestBed.overrideTemplateUsingTestingModule', 'Cannot override template when the test module has already been instantiated');
  2004. this.compiler.overrideTemplateUsingTestingModule(component, template);
  2005. return this;
  2006. }
  2007. overrideDirective(directive, override) {
  2008. this.assertNotInstantiated('overrideDirective', 'override directive metadata');
  2009. this.compiler.overrideDirective(directive, override);
  2010. return this;
  2011. }
  2012. overridePipe(pipe, override) {
  2013. this.assertNotInstantiated('overridePipe', 'override pipe metadata');
  2014. this.compiler.overridePipe(pipe, override);
  2015. return this;
  2016. }
  2017. /**
  2018. * Overwrites all providers for the given token with the given provider definition.
  2019. */
  2020. overrideProvider(token, provider) {
  2021. this.assertNotInstantiated('overrideProvider', 'override provider');
  2022. this.compiler.overrideProvider(token, provider);
  2023. return this;
  2024. }
  2025. overrideTemplate(component, template) {
  2026. return this.overrideComponent(component, { set: { template, templateUrl: null } });
  2027. }
  2028. createComponent(type) {
  2029. const testComponentRenderer = this.inject(TestComponentRenderer);
  2030. const rootElId = `root${_nextRootElementId++}`;
  2031. testComponentRenderer.insertRootElement(rootElId);
  2032. if (_getAsyncClassMetadataFn(type)) {
  2033. throw new Error(`Component '${type.name}' has unresolved metadata. ` +
  2034. `Please call \`await TestBed.compileComponents()\` before running this test.`);
  2035. }
  2036. const componentDef = type.ɵcmp;
  2037. if (!componentDef) {
  2038. throw new Error(`It looks like '${_stringify(type)}' has not been compiled.`);
  2039. }
  2040. const componentFactory = new _Render3ComponentFactory(componentDef);
  2041. const initComponent = () => {
  2042. const componentRef = componentFactory.create(Injector.NULL, [], `#${rootElId}`, this.testModuleRef);
  2043. return this.runInInjectionContext(() => new ComponentFixture(componentRef));
  2044. };
  2045. const noNgZone = this.inject(ComponentFixtureNoNgZone, false);
  2046. const ngZone = noNgZone ? null : this.inject(NgZone, null);
  2047. const fixture = ngZone ? ngZone.run(initComponent) : initComponent();
  2048. this._activeFixtures.push(fixture);
  2049. return fixture;
  2050. }
  2051. /**
  2052. * @internal strip this from published d.ts files due to
  2053. * https://github.com/microsoft/TypeScript/issues/36216
  2054. */
  2055. get compiler() {
  2056. if (this._compiler === null) {
  2057. throw new Error(`Need to call TestBed.initTestEnvironment() first`);
  2058. }
  2059. return this._compiler;
  2060. }
  2061. /**
  2062. * @internal strip this from published d.ts files due to
  2063. * https://github.com/microsoft/TypeScript/issues/36216
  2064. */
  2065. get testModuleRef() {
  2066. if (this._testModuleRef === null) {
  2067. this._testModuleRef = this.compiler.finalize();
  2068. }
  2069. return this._testModuleRef;
  2070. }
  2071. assertNotInstantiated(methodName, methodDescription) {
  2072. if (this._testModuleRef !== null) {
  2073. throw new Error(`Cannot ${methodDescription} when the test module has already been instantiated. ` +
  2074. `Make sure you are not using \`inject\` before \`${methodName}\`.`);
  2075. }
  2076. }
  2077. /**
  2078. * Check whether the module scoping queue should be flushed, and flush it if needed.
  2079. *
  2080. * When the TestBed is reset, it clears the JIT module compilation queue, cancelling any
  2081. * in-progress module compilation. This creates a potential hazard - the very first time the
  2082. * TestBed is initialized (or if it's reset without being initialized), there may be pending
  2083. * compilations of modules declared in global scope. These compilations should be finished.
  2084. *
  2085. * To ensure that globally declared modules have their components scoped properly, this function
  2086. * is called whenever TestBed is initialized or reset. The _first_ time that this happens, prior
  2087. * to any other operations, the scoping queue is flushed.
  2088. */
  2089. checkGlobalCompilationFinished() {
  2090. // Checking _testNgModuleRef is null should not be necessary, but is left in as an additional
  2091. // guard that compilations queued in tests (after instantiation) are never flushed accidentally.
  2092. if (!this.globalCompilationChecked && this._testModuleRef === null) {
  2093. _flushModuleScopingQueueAsMuchAsPossible();
  2094. }
  2095. this.globalCompilationChecked = true;
  2096. }
  2097. destroyActiveFixtures() {
  2098. let errorCount = 0;
  2099. this._activeFixtures.forEach((fixture) => {
  2100. try {
  2101. fixture.destroy();
  2102. }
  2103. catch (e) {
  2104. errorCount++;
  2105. console.error('Error during cleanup of component', {
  2106. component: fixture.componentInstance,
  2107. stacktrace: e,
  2108. });
  2109. }
  2110. });
  2111. this._activeFixtures = [];
  2112. if (errorCount > 0 && this.shouldRethrowTeardownErrors()) {
  2113. throw Error(`${errorCount} ${errorCount === 1 ? 'component' : 'components'} ` +
  2114. `threw errors during cleanup`);
  2115. }
  2116. }
  2117. shouldRethrowTeardownErrors() {
  2118. const instanceOptions = this._instanceTeardownOptions;
  2119. const environmentOptions = TestBedImpl._environmentTeardownOptions;
  2120. // If the new teardown behavior hasn't been configured, preserve the old behavior.
  2121. if (!instanceOptions && !environmentOptions) {
  2122. return TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT;
  2123. }
  2124. // Otherwise use the configured behavior or default to rethrowing.
  2125. return (instanceOptions?.rethrowErrors ??
  2126. environmentOptions?.rethrowErrors ??
  2127. this.shouldTearDownTestingModule());
  2128. }
  2129. shouldThrowErrorOnUnknownElements() {
  2130. // Check if a configuration has been provided to throw when an unknown element is found
  2131. return (this._instanceErrorOnUnknownElementsOption ??
  2132. TestBedImpl._environmentErrorOnUnknownElementsOption ??
  2133. THROW_ON_UNKNOWN_ELEMENTS_DEFAULT);
  2134. }
  2135. shouldThrowErrorOnUnknownProperties() {
  2136. // Check if a configuration has been provided to throw when an unknown property is found
  2137. return (this._instanceErrorOnUnknownPropertiesOption ??
  2138. TestBedImpl._environmentErrorOnUnknownPropertiesOption ??
  2139. THROW_ON_UNKNOWN_PROPERTIES_DEFAULT);
  2140. }
  2141. shouldTearDownTestingModule() {
  2142. return (this._instanceTeardownOptions?.destroyAfterEach ??
  2143. TestBedImpl._environmentTeardownOptions?.destroyAfterEach ??
  2144. TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT);
  2145. }
  2146. getDeferBlockBehavior() {
  2147. return this._instanceDeferBlockBehavior;
  2148. }
  2149. tearDownTestingModule() {
  2150. // If the module ref has already been destroyed, we won't be able to get a test renderer.
  2151. if (this._testModuleRef === null) {
  2152. return;
  2153. }
  2154. // Resolve the renderer ahead of time, because we want to remove the root elements as the very
  2155. // last step, but the injector will be destroyed as a part of the module ref destruction.
  2156. const testRenderer = this.inject(TestComponentRenderer);
  2157. try {
  2158. this._testModuleRef.destroy();
  2159. }
  2160. catch (e) {
  2161. if (this.shouldRethrowTeardownErrors()) {
  2162. throw e;
  2163. }
  2164. else {
  2165. console.error('Error during cleanup of a testing module', {
  2166. component: this._testModuleRef.instance,
  2167. stacktrace: e,
  2168. });
  2169. }
  2170. }
  2171. finally {
  2172. testRenderer.removeAllRootElements?.();
  2173. }
  2174. }
  2175. /**
  2176. * Execute any pending effects.
  2177. *
  2178. * @developerPreview
  2179. */
  2180. flushEffects() {
  2181. this.inject(_MicrotaskEffectScheduler).flush();
  2182. this.inject(_EffectScheduler).flush();
  2183. }
  2184. }
  2185. /**
  2186. * @description
  2187. * Configures and initializes environment for unit testing and provides methods for
  2188. * creating components and services in unit tests.
  2189. *
  2190. * `TestBed` is the primary api for writing unit tests for Angular applications and libraries.
  2191. *
  2192. * @publicApi
  2193. */
  2194. const TestBed = TestBedImpl;
  2195. /**
  2196. * Allows injecting dependencies in `beforeEach()` and `it()`. Note: this function
  2197. * (imported from the `@angular/core/testing` package) can **only** be used to inject dependencies
  2198. * in tests. To inject dependencies in your application code, use the [`inject`](api/core/inject)
  2199. * function from the `@angular/core` package instead.
  2200. *
  2201. * Example:
  2202. *
  2203. * ```ts
  2204. * beforeEach(inject([Dependency, AClass], (dep, object) => {
  2205. * // some code that uses `dep` and `object`
  2206. * // ...
  2207. * }));
  2208. *
  2209. * it('...', inject([AClass], (object) => {
  2210. * object.doSomething();
  2211. * expect(...);
  2212. * })
  2213. * ```
  2214. *
  2215. * @publicApi
  2216. */
  2217. function inject(tokens, fn) {
  2218. const testBed = TestBedImpl.INSTANCE;
  2219. // Not using an arrow function to preserve context passed from call site
  2220. return function () {
  2221. return testBed.execute(tokens, fn, this);
  2222. };
  2223. }
  2224. /**
  2225. * @publicApi
  2226. */
  2227. class InjectSetupWrapper {
  2228. _moduleDef;
  2229. constructor(_moduleDef) {
  2230. this._moduleDef = _moduleDef;
  2231. }
  2232. _addModule() {
  2233. const moduleDef = this._moduleDef();
  2234. if (moduleDef) {
  2235. TestBedImpl.configureTestingModule(moduleDef);
  2236. }
  2237. }
  2238. inject(tokens, fn) {
  2239. const self = this;
  2240. // Not using an arrow function to preserve context passed from call site
  2241. return function () {
  2242. self._addModule();
  2243. return inject(tokens, fn).call(this);
  2244. };
  2245. }
  2246. }
  2247. function withModule(moduleDef, fn) {
  2248. if (fn) {
  2249. // Not using an arrow function to preserve context passed from call site
  2250. return function () {
  2251. const testBed = TestBedImpl.INSTANCE;
  2252. if (moduleDef) {
  2253. testBed.configureTestingModule(moduleDef);
  2254. }
  2255. return fn.apply(this);
  2256. };
  2257. }
  2258. return new InjectSetupWrapper(() => moduleDef);
  2259. }
  2260. /**
  2261. * Public Test Library for unit testing Angular applications. Assumes that you are running
  2262. * with Jasmine, Mocha, or a similar framework which exports a beforeEach function and
  2263. * allows tests to be asynchronous by either returning a promise or using a 'done' parameter.
  2264. */
  2265. // Reset the test providers and the fake async zone before each test.
  2266. // We keep a guard because somehow this file can make it into a bundle and be executed
  2267. // beforeEach is only defined when executing the tests
  2268. globalThis.beforeEach?.(getCleanupHook(false));
  2269. // We provide both a `beforeEach` and `afterEach`, because the updated behavior for
  2270. // tearing down the module is supposed to run after the test so that we can associate
  2271. // teardown errors with the correct test.
  2272. // We keep a guard because somehow this file can make it into a bundle and be executed
  2273. // afterEach is only defined when executing the tests
  2274. globalThis.afterEach?.(getCleanupHook(true));
  2275. function getCleanupHook(expectedTeardownValue) {
  2276. return () => {
  2277. const testBed = TestBedImpl.INSTANCE;
  2278. if (testBed.shouldTearDownTestingModule() === expectedTeardownValue) {
  2279. testBed.resetTestingModule();
  2280. resetFakeAsyncZoneIfExists();
  2281. }
  2282. };
  2283. }
  2284. /**
  2285. * This API should be removed. But doing so seems to break `google3` and so it requires a bit of
  2286. * investigation.
  2287. *
  2288. * A work around is to mark it as `@codeGenApi` for now and investigate later.
  2289. *
  2290. * @codeGenApi
  2291. */
  2292. // TODO(iminar): Remove this code in a safe way.
  2293. const __core_private_testing_placeholder__ = '';
  2294. /**
  2295. * Fake implementation of user agent history and navigation behavior. This is a
  2296. * high-fidelity implementation of browser behavior that attempts to emulate
  2297. * things like traversal delay.
  2298. */
  2299. class FakeNavigation {
  2300. /**
  2301. * The fake implementation of an entries array. Only same-document entries
  2302. * allowed.
  2303. */
  2304. entriesArr = [];
  2305. /**
  2306. * The current active entry index into `entriesArr`.
  2307. */
  2308. currentEntryIndex = 0;
  2309. /**
  2310. * The current navigate event.
  2311. * @internal
  2312. */
  2313. navigateEvent = null;
  2314. /**
  2315. * A Map of pending traversals, so that traversals to the same entry can be
  2316. * re-used.
  2317. */
  2318. traversalQueue = new Map();
  2319. /**
  2320. * A Promise that resolves when the previous traversals have finished. Used to
  2321. * simulate the cross-process communication necessary for traversals.
  2322. */
  2323. nextTraversal = Promise.resolve();
  2324. /**
  2325. * A prospective current active entry index, which includes unresolved
  2326. * traversals. Used by `go` to determine where navigations are intended to go.
  2327. */
  2328. prospectiveEntryIndex = 0;
  2329. /**
  2330. * A test-only option to make traversals synchronous, rather than emulate
  2331. * cross-process communication.
  2332. */
  2333. synchronousTraversals = false;
  2334. /** Whether to allow a call to setInitialEntryForTesting. */
  2335. canSetInitialEntry = true;
  2336. /**
  2337. * `EventTarget` to dispatch events.
  2338. * @internal
  2339. */
  2340. eventTarget;
  2341. /** The next unique id for created entries. Replace recreates this id. */
  2342. nextId = 0;
  2343. /** The next unique key for created entries. Replace inherits this id. */
  2344. nextKey = 0;
  2345. /** Whether this fake is disposed. */
  2346. disposed = false;
  2347. /** Equivalent to `navigation.currentEntry`. */
  2348. get currentEntry() {
  2349. return this.entriesArr[this.currentEntryIndex];
  2350. }
  2351. get canGoBack() {
  2352. return this.currentEntryIndex > 0;
  2353. }
  2354. get canGoForward() {
  2355. return this.currentEntryIndex < this.entriesArr.length - 1;
  2356. }
  2357. createEventTarget;
  2358. _window;
  2359. get window() {
  2360. return this._window;
  2361. }
  2362. constructor(doc, startURL) {
  2363. this.createEventTarget = () => {
  2364. try {
  2365. // `document.createElement` because NodeJS `EventTarget` is
  2366. // incompatible with Domino's `Event`. That is, attempting to
  2367. // dispatch an event created by Domino's patched `Event` will
  2368. // throw an error since it is not an instance of a real Node
  2369. // `Event`.
  2370. return doc.createElement('div');
  2371. }
  2372. catch {
  2373. // Fallback to a basic EventTarget if `document.createElement`
  2374. // fails. This can happen with tests that pass in a value for document
  2375. // that is stubbed.
  2376. return new EventTarget();
  2377. }
  2378. };
  2379. this._window = document.defaultView ?? this.createEventTarget();
  2380. this.eventTarget = this.createEventTarget();
  2381. // First entry.
  2382. this.setInitialEntryForTesting(startURL);
  2383. }
  2384. /**
  2385. * Sets the initial entry.
  2386. */
  2387. setInitialEntryForTesting(url, options = { historyState: null }) {
  2388. if (!this.canSetInitialEntry) {
  2389. throw new Error('setInitialEntryForTesting can only be called before any ' + 'navigation has occurred');
  2390. }
  2391. const currentInitialEntry = this.entriesArr[0];
  2392. this.entriesArr[0] = new FakeNavigationHistoryEntry(this.eventTarget, new URL(url).toString(), {
  2393. index: 0,
  2394. key: currentInitialEntry?.key ?? String(this.nextKey++),
  2395. id: currentInitialEntry?.id ?? String(this.nextId++),
  2396. sameDocument: true,
  2397. historyState: options?.historyState,
  2398. state: options.state,
  2399. });
  2400. }
  2401. /** Returns whether the initial entry is still eligible to be set. */
  2402. canSetInitialEntryForTesting() {
  2403. return this.canSetInitialEntry;
  2404. }
  2405. /**
  2406. * Sets whether to emulate traversals as synchronous rather than
  2407. * asynchronous.
  2408. */
  2409. setSynchronousTraversalsForTesting(synchronousTraversals) {
  2410. this.synchronousTraversals = synchronousTraversals;
  2411. }
  2412. /** Equivalent to `navigation.entries()`. */
  2413. entries() {
  2414. return this.entriesArr.slice();
  2415. }
  2416. /** Equivalent to `navigation.navigate()`. */
  2417. navigate(url, options) {
  2418. const fromUrl = new URL(this.currentEntry.url);
  2419. const toUrl = new URL(url, this.currentEntry.url);
  2420. let navigationType;
  2421. if (!options?.history || options.history === 'auto') {
  2422. // Auto defaults to push, but if the URLs are the same, is a replace.
  2423. if (fromUrl.toString() === toUrl.toString()) {
  2424. navigationType = 'replace';
  2425. }
  2426. else {
  2427. navigationType = 'push';
  2428. }
  2429. }
  2430. else {
  2431. navigationType = options.history;
  2432. }
  2433. const hashChange = isHashChange(fromUrl, toUrl);
  2434. const destination = new FakeNavigationDestination({
  2435. url: toUrl.toString(),
  2436. state: options?.state,
  2437. sameDocument: hashChange,
  2438. historyState: null,
  2439. });
  2440. const result = new InternalNavigationResult(this);
  2441. const intercepted = this.userAgentNavigate(destination, result, {
  2442. navigationType,
  2443. cancelable: true,
  2444. canIntercept: true,
  2445. // Always false for navigate().
  2446. userInitiated: false,
  2447. hashChange,
  2448. info: options?.info,
  2449. });
  2450. if (!intercepted) {
  2451. this.updateNavigationEntriesForSameDocumentNavigation(this.navigateEvent);
  2452. }
  2453. return {
  2454. committed: result.committed,
  2455. finished: result.finished,
  2456. };
  2457. }
  2458. /** Equivalent to `history.pushState()`. */
  2459. pushState(data, title, url) {
  2460. this.pushOrReplaceState('push', data, title, url);
  2461. }
  2462. /** Equivalent to `history.replaceState()`. */
  2463. replaceState(data, title, url) {
  2464. this.pushOrReplaceState('replace', data, title, url);
  2465. }
  2466. pushOrReplaceState(navigationType, data, _title, url) {
  2467. const fromUrl = new URL(this.currentEntry.url);
  2468. const toUrl = url ? new URL(url, this.currentEntry.url) : fromUrl;
  2469. const hashChange = isHashChange(fromUrl, toUrl);
  2470. const destination = new FakeNavigationDestination({
  2471. url: toUrl.toString(),
  2472. sameDocument: true,
  2473. historyState: data,
  2474. });
  2475. const result = new InternalNavigationResult(this);
  2476. const intercepted = this.userAgentNavigate(destination, result, {
  2477. navigationType,
  2478. cancelable: true,
  2479. canIntercept: true,
  2480. // Always false for pushState() or replaceState().
  2481. userInitiated: false,
  2482. hashChange,
  2483. });
  2484. if (intercepted) {
  2485. return;
  2486. }
  2487. this.updateNavigationEntriesForSameDocumentNavigation(this.navigateEvent);
  2488. }
  2489. /** Equivalent to `navigation.traverseTo()`. */
  2490. traverseTo(key, options) {
  2491. const fromUrl = new URL(this.currentEntry.url);
  2492. const entry = this.findEntry(key);
  2493. if (!entry) {
  2494. const domException = new DOMException('Invalid key', 'InvalidStateError');
  2495. const committed = Promise.reject(domException);
  2496. const finished = Promise.reject(domException);
  2497. committed.catch(() => { });
  2498. finished.catch(() => { });
  2499. return {
  2500. committed,
  2501. finished,
  2502. };
  2503. }
  2504. if (entry === this.currentEntry) {
  2505. return {
  2506. committed: Promise.resolve(this.currentEntry),
  2507. finished: Promise.resolve(this.currentEntry),
  2508. };
  2509. }
  2510. if (this.traversalQueue.has(entry.key)) {
  2511. const existingResult = this.traversalQueue.get(entry.key);
  2512. return {
  2513. committed: existingResult.committed,
  2514. finished: existingResult.finished,
  2515. };
  2516. }
  2517. const hashChange = isHashChange(fromUrl, new URL(entry.url, this.currentEntry.url));
  2518. const destination = new FakeNavigationDestination({
  2519. url: entry.url,
  2520. state: entry.getState(),
  2521. historyState: entry.getHistoryState(),
  2522. key: entry.key,
  2523. id: entry.id,
  2524. index: entry.index,
  2525. sameDocument: entry.sameDocument,
  2526. });
  2527. this.prospectiveEntryIndex = entry.index;
  2528. const result = new InternalNavigationResult(this);
  2529. this.traversalQueue.set(entry.key, result);
  2530. this.runTraversal(() => {
  2531. this.traversalQueue.delete(entry.key);
  2532. const intercepted = this.userAgentNavigate(destination, result, {
  2533. navigationType: 'traverse',
  2534. cancelable: true,
  2535. canIntercept: true,
  2536. // Always false for traverseTo().
  2537. userInitiated: false,
  2538. hashChange,
  2539. info: options?.info,
  2540. });
  2541. if (!intercepted) {
  2542. this.userAgentTraverse(this.navigateEvent);
  2543. }
  2544. });
  2545. return {
  2546. committed: result.committed,
  2547. finished: result.finished,
  2548. };
  2549. }
  2550. /** Equivalent to `navigation.back()`. */
  2551. back(options) {
  2552. if (this.currentEntryIndex === 0) {
  2553. const domException = new DOMException('Cannot go back', 'InvalidStateError');
  2554. const committed = Promise.reject(domException);
  2555. const finished = Promise.reject(domException);
  2556. committed.catch(() => { });
  2557. finished.catch(() => { });
  2558. return {
  2559. committed,
  2560. finished,
  2561. };
  2562. }
  2563. const entry = this.entriesArr[this.currentEntryIndex - 1];
  2564. return this.traverseTo(entry.key, options);
  2565. }
  2566. /** Equivalent to `navigation.forward()`. */
  2567. forward(options) {
  2568. if (this.currentEntryIndex === this.entriesArr.length - 1) {
  2569. const domException = new DOMException('Cannot go forward', 'InvalidStateError');
  2570. const committed = Promise.reject(domException);
  2571. const finished = Promise.reject(domException);
  2572. committed.catch(() => { });
  2573. finished.catch(() => { });
  2574. return {
  2575. committed,
  2576. finished,
  2577. };
  2578. }
  2579. const entry = this.entriesArr[this.currentEntryIndex + 1];
  2580. return this.traverseTo(entry.key, options);
  2581. }
  2582. /**
  2583. * Equivalent to `history.go()`.
  2584. * Note that this method does not actually work precisely to how Chrome
  2585. * does, instead choosing a simpler model with less unexpected behavior.
  2586. * Chrome has a few edge case optimizations, for instance with repeated
  2587. * `back(); forward()` chains it collapses certain traversals.
  2588. */
  2589. go(direction) {
  2590. const targetIndex = this.prospectiveEntryIndex + direction;
  2591. if (targetIndex >= this.entriesArr.length || targetIndex < 0) {
  2592. return;
  2593. }
  2594. this.prospectiveEntryIndex = targetIndex;
  2595. this.runTraversal(() => {
  2596. // Check again that destination is in the entries array.
  2597. if (targetIndex >= this.entriesArr.length || targetIndex < 0) {
  2598. return;
  2599. }
  2600. const fromUrl = new URL(this.currentEntry.url);
  2601. const entry = this.entriesArr[targetIndex];
  2602. const hashChange = isHashChange(fromUrl, new URL(entry.url, this.currentEntry.url));
  2603. const destination = new FakeNavigationDestination({
  2604. url: entry.url,
  2605. state: entry.getState(),
  2606. historyState: entry.getHistoryState(),
  2607. key: entry.key,
  2608. id: entry.id,
  2609. index: entry.index,
  2610. sameDocument: entry.sameDocument,
  2611. });
  2612. const result = new InternalNavigationResult(this);
  2613. const intercepted = this.userAgentNavigate(destination, result, {
  2614. navigationType: 'traverse',
  2615. cancelable: true,
  2616. canIntercept: true,
  2617. // Always false for go().
  2618. userInitiated: false,
  2619. hashChange,
  2620. });
  2621. if (!intercepted) {
  2622. this.userAgentTraverse(this.navigateEvent);
  2623. }
  2624. });
  2625. }
  2626. /** Runs a traversal synchronously or asynchronously */
  2627. runTraversal(traversal) {
  2628. if (this.synchronousTraversals) {
  2629. traversal();
  2630. return;
  2631. }
  2632. // Each traversal occupies a single timeout resolution.
  2633. // This means that Promises added to commit and finish should resolve
  2634. // before the next traversal.
  2635. this.nextTraversal = this.nextTraversal.then(() => {
  2636. return new Promise((resolve) => {
  2637. setTimeout(() => {
  2638. resolve();
  2639. traversal();
  2640. });
  2641. });
  2642. });
  2643. }
  2644. /** Equivalent to `navigation.addEventListener()`. */
  2645. addEventListener(type, callback, options) {
  2646. this.eventTarget.addEventListener(type, callback, options);
  2647. }
  2648. /** Equivalent to `navigation.removeEventListener()`. */
  2649. removeEventListener(type, callback, options) {
  2650. this.eventTarget.removeEventListener(type, callback, options);
  2651. }
  2652. /** Equivalent to `navigation.dispatchEvent()` */
  2653. dispatchEvent(event) {
  2654. return this.eventTarget.dispatchEvent(event);
  2655. }
  2656. /** Cleans up resources. */
  2657. dispose() {
  2658. // Recreate eventTarget to release current listeners.
  2659. this.eventTarget = this.createEventTarget();
  2660. this.disposed = true;
  2661. }
  2662. /** Returns whether this fake is disposed. */
  2663. isDisposed() {
  2664. return this.disposed;
  2665. }
  2666. /**
  2667. * Implementation for all navigations and traversals.
  2668. * @returns true if the event was intercepted, otherwise false
  2669. */
  2670. userAgentNavigate(destination, result, options) {
  2671. // The first navigation should disallow any future calls to set the initial
  2672. // entry.
  2673. this.canSetInitialEntry = false;
  2674. if (this.navigateEvent) {
  2675. this.navigateEvent.cancel(new DOMException('Navigation was aborted', 'AbortError'));
  2676. this.navigateEvent = null;
  2677. }
  2678. return dispatchNavigateEvent({
  2679. navigationType: options.navigationType,
  2680. cancelable: options.cancelable,
  2681. canIntercept: options.canIntercept,
  2682. userInitiated: options.userInitiated,
  2683. hashChange: options.hashChange,
  2684. signal: result.signal,
  2685. destination,
  2686. info: options.info,
  2687. sameDocument: destination.sameDocument,
  2688. result,
  2689. });
  2690. }
  2691. /**
  2692. * Implementation for a push or replace navigation.
  2693. * https://whatpr.org/html/10919/browsing-the-web.html#url-and-history-update-steps
  2694. * https://whatpr.org/html/10919/nav-history-apis.html#update-the-navigation-api-entries-for-a-same-document-navigation
  2695. * @internal
  2696. */
  2697. urlAndHistoryUpdateSteps(navigateEvent) {
  2698. this.updateNavigationEntriesForSameDocumentNavigation(navigateEvent);
  2699. }
  2700. /**
  2701. * Implementation for a traverse navigation.
  2702. *
  2703. * https://whatpr.org/html/10919/browsing-the-web.html#apply-the-traverse-history-step
  2704. * ...
  2705. * > Let updateDocument be an algorithm step which performs update document for history step application given targetEntry's document, targetEntry, changingNavigableContinuation's update-only, scriptHistoryLength, scriptHistoryIndex, navigationType, entriesForNavigationAPI, and previousEntry.
  2706. * > If targetEntry's document is equal to displayedDocument, then perform updateDocument.
  2707. * https://whatpr.org/html/10919/browsing-the-web.html#update-document-for-history-step-application
  2708. * which then goes to https://whatpr.org/html/10919/nav-history-apis.html#update-the-navigation-api-entries-for-a-same-document-navigation
  2709. * @internal
  2710. */
  2711. userAgentTraverse(navigateEvent) {
  2712. const oldUrl = this.currentEntry.url;
  2713. this.updateNavigationEntriesForSameDocumentNavigation(navigateEvent);
  2714. // Happens as part of "updating the document" steps https://whatpr.org/html/10919/browsing-the-web.html#updating-the-document
  2715. const popStateEvent = createPopStateEvent({
  2716. state: navigateEvent.destination.getHistoryState(),
  2717. });
  2718. this._window.dispatchEvent(popStateEvent);
  2719. if (navigateEvent.hashChange) {
  2720. const hashchangeEvent = createHashChangeEvent(oldUrl, this.currentEntry.url);
  2721. this._window.dispatchEvent(hashchangeEvent);
  2722. }
  2723. }
  2724. /**
  2725. * https://whatpr.org/html/10919/nav-history-apis.html#update-the-navigation-api-entries-for-a-same-document-navigation
  2726. * @internal
  2727. */
  2728. updateNavigationEntriesForSameDocumentNavigation({ destination, navigationType, result, }) {
  2729. const oldCurrentNHE = this.currentEntry;
  2730. const disposedNHEs = [];
  2731. if (navigationType === 'traverse') {
  2732. this.currentEntryIndex = destination.index;
  2733. if (this.currentEntryIndex === -1) {
  2734. throw new Error('unexpected current entry index');
  2735. }
  2736. }
  2737. else if (navigationType === 'push') {
  2738. this.currentEntryIndex++;
  2739. this.prospectiveEntryIndex = this.currentEntryIndex; // prospectiveEntryIndex isn't in the spec but is an implementation detail
  2740. disposedNHEs.push(...this.entriesArr.splice(this.currentEntryIndex));
  2741. }
  2742. else if (navigationType === 'replace') {
  2743. disposedNHEs.push(oldCurrentNHE);
  2744. }
  2745. if (navigationType === 'push' || navigationType === 'replace') {
  2746. const index = this.currentEntryIndex;
  2747. const key = navigationType === 'push' ? String(this.nextKey++) : this.currentEntry.key;
  2748. const newNHE = new FakeNavigationHistoryEntry(this.eventTarget, destination.url, {
  2749. id: String(this.nextId++),
  2750. key,
  2751. index,
  2752. sameDocument: true,
  2753. state: destination.getState(),
  2754. historyState: destination.getHistoryState(),
  2755. });
  2756. this.entriesArr[this.currentEntryIndex] = newNHE;
  2757. }
  2758. result.committedResolve(this.currentEntry);
  2759. const currentEntryChangeEvent = createFakeNavigationCurrentEntryChangeEvent({
  2760. from: oldCurrentNHE,
  2761. navigationType: navigationType,
  2762. });
  2763. this.eventTarget.dispatchEvent(currentEntryChangeEvent);
  2764. for (const disposedNHE of disposedNHEs) {
  2765. disposedNHE.dispose();
  2766. }
  2767. }
  2768. /** Utility method for finding entries with the given `key`. */
  2769. findEntry(key) {
  2770. for (const entry of this.entriesArr) {
  2771. if (entry.key === key)
  2772. return entry;
  2773. }
  2774. return undefined;
  2775. }
  2776. set onnavigate(
  2777. // tslint:disable-next-line:no-any
  2778. _handler) {
  2779. throw new Error('unimplemented');
  2780. }
  2781. // tslint:disable-next-line:no-any
  2782. get onnavigate() {
  2783. throw new Error('unimplemented');
  2784. }
  2785. set oncurrententrychange(_handler) {
  2786. throw new Error('unimplemented');
  2787. }
  2788. get oncurrententrychange() {
  2789. throw new Error('unimplemented');
  2790. }
  2791. set onnavigatesuccess(
  2792. // tslint:disable-next-line:no-any
  2793. _handler) {
  2794. throw new Error('unimplemented');
  2795. }
  2796. // tslint:disable-next-line:no-any
  2797. get onnavigatesuccess() {
  2798. throw new Error('unimplemented');
  2799. }
  2800. set onnavigateerror(
  2801. // tslint:disable-next-line:no-any
  2802. _handler) {
  2803. throw new Error('unimplemented');
  2804. }
  2805. // tslint:disable-next-line:no-any
  2806. get onnavigateerror() {
  2807. throw new Error('unimplemented');
  2808. }
  2809. _transition = null;
  2810. /** @internal */
  2811. set transition(t) {
  2812. this._transition = t;
  2813. }
  2814. get transition() {
  2815. return this._transition;
  2816. }
  2817. updateCurrentEntry(_options) {
  2818. throw new Error('unimplemented');
  2819. }
  2820. reload(_options) {
  2821. throw new Error('unimplemented');
  2822. }
  2823. }
  2824. /**
  2825. * Fake equivalent of `NavigationHistoryEntry`.
  2826. */
  2827. class FakeNavigationHistoryEntry {
  2828. eventTarget;
  2829. url;
  2830. sameDocument;
  2831. id;
  2832. key;
  2833. index;
  2834. state;
  2835. historyState;
  2836. // tslint:disable-next-line:no-any
  2837. ondispose = null;
  2838. constructor(eventTarget, url, { id, key, index, sameDocument, state, historyState, }) {
  2839. this.eventTarget = eventTarget;
  2840. this.url = url;
  2841. this.id = id;
  2842. this.key = key;
  2843. this.index = index;
  2844. this.sameDocument = sameDocument;
  2845. this.state = state;
  2846. this.historyState = historyState;
  2847. }
  2848. getState() {
  2849. // Budget copy.
  2850. return this.state ? JSON.parse(JSON.stringify(this.state)) : this.state;
  2851. }
  2852. getHistoryState() {
  2853. // Budget copy.
  2854. return this.historyState
  2855. ? JSON.parse(JSON.stringify(this.historyState))
  2856. : this.historyState;
  2857. }
  2858. addEventListener(type, callback, options) {
  2859. this.eventTarget.addEventListener(type, callback, options);
  2860. }
  2861. removeEventListener(type, callback, options) {
  2862. this.eventTarget.removeEventListener(type, callback, options);
  2863. }
  2864. dispatchEvent(event) {
  2865. return this.eventTarget.dispatchEvent(event);
  2866. }
  2867. /** internal */
  2868. dispose() {
  2869. const disposeEvent = new Event('disposed');
  2870. this.dispatchEvent(disposeEvent);
  2871. // release current listeners
  2872. this.eventTarget = null;
  2873. }
  2874. }
  2875. /**
  2876. * Create a fake equivalent of `NavigateEvent`. This is not a class because ES5
  2877. * transpiled JavaScript cannot extend native Event.
  2878. *
  2879. * https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigate-event-firing
  2880. */
  2881. function dispatchNavigateEvent({ cancelable, canIntercept, userInitiated, hashChange, navigationType, signal, destination, info, sameDocument, result, }) {
  2882. const { navigation } = result;
  2883. const event = new Event('navigate', { bubbles: false, cancelable });
  2884. event.focusResetBehavior = null;
  2885. event.scrollBehavior = null;
  2886. event.interceptionState = 'none';
  2887. event.canIntercept = canIntercept;
  2888. event.userInitiated = userInitiated;
  2889. event.hashChange = hashChange;
  2890. event.navigationType = navigationType;
  2891. event.signal = signal;
  2892. event.destination = destination;
  2893. event.info = info;
  2894. event.downloadRequest = null;
  2895. event.formData = null;
  2896. event.result = result;
  2897. event.sameDocument = sameDocument;
  2898. let precommitHandlers = [];
  2899. let handlers = [];
  2900. // https://whatpr.org/html/10919/nav-history-apis.html#dom-navigateevent-intercept
  2901. event.intercept = function (options) {
  2902. if (!this.canIntercept) {
  2903. throw new DOMException(`Cannot intercept when canIntercept is 'false'`, 'SecurityError');
  2904. }
  2905. this.interceptionState = 'intercepted';
  2906. event.sameDocument = true;
  2907. const precommitHandler = options?.precommitHandler;
  2908. if (precommitHandler) {
  2909. if (!this.cancelable) {
  2910. throw new DOMException(`Cannot use precommitHandler when cancelable is 'false'`, 'InvalidStateError');
  2911. }
  2912. precommitHandlers.push(precommitHandler);
  2913. }
  2914. if (event.interceptionState !== 'none' && event.interceptionState !== 'intercepted') {
  2915. throw new Error('Event interceptionState should be "none" or "intercepted"');
  2916. }
  2917. event.interceptionState = 'intercepted';
  2918. const handler = options?.handler;
  2919. if (handler) {
  2920. handlers.push(handler);
  2921. }
  2922. // override old options with new ones. UA _may_ report a console warning if new options differ from previous
  2923. event.focusResetBehavior = options?.focusReset ?? event.focusResetBehavior;
  2924. event.scrollBehavior = options?.scroll ?? event.scrollBehavior;
  2925. };
  2926. // https://whatpr.org/html/10919/nav-history-apis.html#dom-navigateevent-scroll
  2927. event.scroll = function () {
  2928. if (event.interceptionState !== 'committed') {
  2929. throw new DOMException(`Failed to execute 'scroll' on 'NavigateEvent': scroll() must be ` +
  2930. `called after commit() and interception options must specify manual scroll.`, 'InvalidStateError');
  2931. }
  2932. processScrollBehavior(event);
  2933. };
  2934. // https://whatpr.org/html/10919/nav-history-apis.html#dom-navigationprecommitcontroller-redirect
  2935. function redirect(url) {
  2936. if (event.interceptionState === 'none') {
  2937. throw new Error('cannot redirect when event is not intercepted');
  2938. }
  2939. if (event.interceptionState !== 'intercepted') {
  2940. throw new DOMException(`cannot redirect when event is not in 'intercepted' state`, 'InvalidStateError');
  2941. }
  2942. if (event.navigationType !== 'push' && event.navigationType !== 'replace') {
  2943. throw new DOMException(`cannot redirect when navigationType is not 'push' or 'replace`, 'InvalidStateError');
  2944. }
  2945. const toUrl = new URL(url, navigation.currentEntry.url);
  2946. event.destination.url = toUrl.href;
  2947. }
  2948. // https://whatpr.org/html/10919/nav-history-apis.html#inner-navigate-event-firing-algorithm
  2949. // "Let commit be the following steps:"
  2950. function commit() {
  2951. if (result.signal.aborted) {
  2952. return;
  2953. }
  2954. if (event.interceptionState !== 'none') {
  2955. event.interceptionState = 'committed';
  2956. if (!navigation.currentEntry) {
  2957. throw new Error('from history entry should not be null');
  2958. }
  2959. navigation.transition = new InternalNavigationTransition(navigation.currentEntry, navigationType);
  2960. switch (event.navigationType) {
  2961. case 'push':
  2962. case 'replace': {
  2963. navigation.urlAndHistoryUpdateSteps(event);
  2964. break;
  2965. }
  2966. case 'reload': {
  2967. navigation.updateNavigationEntriesForSameDocumentNavigation(event);
  2968. break;
  2969. }
  2970. case 'traverse': {
  2971. navigation.userAgentTraverse(event);
  2972. break;
  2973. }
  2974. }
  2975. }
  2976. const promisesList = handlers.map((handler) => handler());
  2977. if (promisesList.length === 0) {
  2978. promisesList.push(Promise.resolve());
  2979. }
  2980. Promise.all(promisesList)
  2981. .then(() => {
  2982. // Follows steps outlined under "Wait for all of promisesList, with the following success steps:"
  2983. // in the spec https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigate-event-firing.
  2984. if (result.signal.aborted) {
  2985. return;
  2986. }
  2987. if (event !== navigation.navigateEvent) {
  2988. throw new Error("Navigation's ongoing event not equal to resolved event");
  2989. }
  2990. navigation.navigateEvent = null;
  2991. finishNavigationEvent(event, true);
  2992. const navigatesuccessEvent = new Event('navigatesuccess', { bubbles: false, cancelable });
  2993. navigation.eventTarget.dispatchEvent(navigatesuccessEvent);
  2994. result.finishedResolve();
  2995. if (navigation.transition !== null) {
  2996. navigation.transition.finishedResolve();
  2997. }
  2998. navigation.transition = null;
  2999. })
  3000. .catch((reason) => event.cancel(reason));
  3001. }
  3002. // Internal only.
  3003. // https://whatpr.org/html/10919/nav-history-apis.html#inner-navigate-event-firing-algorithm
  3004. // "Let cancel be the following steps given reason"
  3005. event.cancel = function (reason) {
  3006. if (result.signal.aborted) {
  3007. return;
  3008. }
  3009. if (event !== navigation.navigateEvent) {
  3010. throw new Error("Navigation's ongoing event not equal to resolved event");
  3011. }
  3012. navigation.navigateEvent = null;
  3013. if (event.interceptionState !== 'intercepted') {
  3014. finishNavigationEvent(event, false);
  3015. }
  3016. const navigateerrorEvent = new Event('navigateerror', { bubbles: false, cancelable });
  3017. navigation.eventTarget.dispatchEvent(navigateerrorEvent);
  3018. result.finishedReject(reason);
  3019. if (navigation.transition !== null) {
  3020. navigation.transition.finishedReject(reason);
  3021. }
  3022. navigation.transition = null;
  3023. };
  3024. function dispatch() {
  3025. navigation.navigateEvent = event;
  3026. navigation.eventTarget.dispatchEvent(event);
  3027. if (precommitHandlers.length === 0) {
  3028. commit();
  3029. }
  3030. else {
  3031. const precommitController = { redirect };
  3032. const precommitPromisesList = precommitHandlers.map((handler) => handler(precommitController));
  3033. Promise.all(precommitPromisesList)
  3034. .then(() => commit())
  3035. .catch((reason) => event.cancel(reason));
  3036. }
  3037. }
  3038. dispatch();
  3039. return event.interceptionState !== 'none';
  3040. }
  3041. /** https://whatpr.org/html/10919/nav-history-apis.html#navigateevent-finish */
  3042. function finishNavigationEvent(event, didFulfill) {
  3043. if (event.interceptionState === 'finished') {
  3044. throw new Error('Attempting to finish navigation event that was already finished');
  3045. }
  3046. if (event.interceptionState === 'intercepted') {
  3047. if (didFulfill === true) {
  3048. throw new Error('didFulfill should be false');
  3049. }
  3050. // assert precommit handlers is not empty
  3051. event.interceptionState = 'finished';
  3052. return;
  3053. }
  3054. if (event.interceptionState === 'none') {
  3055. return;
  3056. }
  3057. potentiallyResetFocus(event);
  3058. if (didFulfill) {
  3059. potentiallyResetScroll(event);
  3060. }
  3061. event.interceptionState = 'finished';
  3062. }
  3063. /** https://whatpr.org/html/10919/nav-history-apis.html#potentially-reset-the-focus */
  3064. function potentiallyResetFocus(event) {
  3065. if (event.interceptionState !== 'committed' && event.interceptionState !== 'scrolled') {
  3066. throw new Error('cannot reset focus if navigation event is not committed or scrolled');
  3067. }
  3068. // TODO(atscott): The rest of the steps
  3069. }
  3070. function potentiallyResetScroll(event) {
  3071. if (event.interceptionState !== 'committed' && event.interceptionState !== 'scrolled') {
  3072. throw new Error('cannot reset scroll if navigation event is not committed or scrolled');
  3073. }
  3074. if (event.interceptionState === 'scrolled' || event.scrollBehavior === 'manual') {
  3075. return;
  3076. }
  3077. processScrollBehavior(event);
  3078. }
  3079. /* https://whatpr.org/html/10919/nav-history-apis.html#process-scroll-behavior */
  3080. function processScrollBehavior(event) {
  3081. if (event.interceptionState !== 'committed') {
  3082. throw new Error('invalid event interception state when processing scroll behavior');
  3083. }
  3084. event.interceptionState = 'scrolled';
  3085. // TODO(atscott): the rest of the steps
  3086. }
  3087. /**
  3088. * Create a fake equivalent of `NavigationCurrentEntryChange`. This does not use
  3089. * a class because ES5 transpiled JavaScript cannot extend native Event.
  3090. */
  3091. function createFakeNavigationCurrentEntryChangeEvent({ from, navigationType, }) {
  3092. const event = new Event('currententrychange', {
  3093. bubbles: false,
  3094. cancelable: false,
  3095. });
  3096. event.from = from;
  3097. event.navigationType = navigationType;
  3098. return event;
  3099. }
  3100. /**
  3101. * Create a fake equivalent of `PopStateEvent`. This does not use a class
  3102. * because ES5 transpiled JavaScript cannot extend native Event.
  3103. */
  3104. function createPopStateEvent({ state }) {
  3105. const event = new Event('popstate', {
  3106. bubbles: false,
  3107. cancelable: false,
  3108. });
  3109. event.state = state;
  3110. return event;
  3111. }
  3112. function createHashChangeEvent(newURL, oldURL) {
  3113. const event = new Event('hashchange', {
  3114. bubbles: false,
  3115. cancelable: false,
  3116. });
  3117. event.newURL = newURL;
  3118. event.oldURL = oldURL;
  3119. return event;
  3120. }
  3121. /**
  3122. * Fake equivalent of `NavigationDestination`.
  3123. */
  3124. class FakeNavigationDestination {
  3125. url;
  3126. sameDocument;
  3127. key;
  3128. id;
  3129. index;
  3130. state;
  3131. historyState;
  3132. constructor({ url, sameDocument, historyState, state, key = null, id = null, index = -1, }) {
  3133. this.url = url;
  3134. this.sameDocument = sameDocument;
  3135. this.state = state;
  3136. this.historyState = historyState;
  3137. this.key = key;
  3138. this.id = id;
  3139. this.index = index;
  3140. }
  3141. getState() {
  3142. return this.state;
  3143. }
  3144. getHistoryState() {
  3145. return this.historyState;
  3146. }
  3147. }
  3148. /** Utility function to determine whether two UrlLike have the same hash. */
  3149. function isHashChange(from, to) {
  3150. return (to.hash !== from.hash &&
  3151. to.hostname === from.hostname &&
  3152. to.pathname === from.pathname &&
  3153. to.search === from.search);
  3154. }
  3155. class InternalNavigationTransition {
  3156. from;
  3157. navigationType;
  3158. finished;
  3159. finishedResolve;
  3160. finishedReject;
  3161. constructor(from, navigationType) {
  3162. this.from = from;
  3163. this.navigationType = navigationType;
  3164. this.finished = new Promise((resolve, reject) => {
  3165. this.finishedReject = reject;
  3166. this.finishedResolve = resolve;
  3167. });
  3168. // All rejections are handled.
  3169. this.finished.catch(() => { });
  3170. }
  3171. }
  3172. /**
  3173. * Internal utility class for representing the result of a navigation.
  3174. * Generally equivalent to the "apiMethodTracker" in the spec.
  3175. */
  3176. class InternalNavigationResult {
  3177. navigation;
  3178. committedTo = null;
  3179. committedResolve;
  3180. committedReject;
  3181. finishedResolve;
  3182. finishedReject;
  3183. committed;
  3184. finished;
  3185. get signal() {
  3186. return this.abortController.signal;
  3187. }
  3188. abortController = new AbortController();
  3189. constructor(navigation) {
  3190. this.navigation = navigation;
  3191. this.committed = new Promise((resolve, reject) => {
  3192. this.committedResolve = (entry) => {
  3193. this.committedTo = entry;
  3194. resolve(entry);
  3195. };
  3196. this.committedReject = reject;
  3197. });
  3198. this.finished = new Promise(async (resolve, reject) => {
  3199. this.finishedResolve = () => {
  3200. if (this.committedTo === null) {
  3201. throw new Error('NavigateEvent should have been committed before resolving finished promise.');
  3202. }
  3203. resolve(this.committedTo);
  3204. };
  3205. this.finishedReject = (reason) => {
  3206. reject(reason);
  3207. this.abortController.abort(reason);
  3208. };
  3209. });
  3210. // All rejections are handled.
  3211. this.committed.catch(() => { });
  3212. this.finished.catch(() => { });
  3213. }
  3214. }
  3215. class Log {
  3216. logItems;
  3217. constructor() {
  3218. this.logItems = [];
  3219. }
  3220. add(value) {
  3221. this.logItems.push(value);
  3222. }
  3223. fn(value) {
  3224. return () => {
  3225. this.logItems.push(value);
  3226. };
  3227. }
  3228. clear() {
  3229. this.logItems = [];
  3230. }
  3231. result() {
  3232. return this.logItems.join('; ');
  3233. }
  3234. static ɵfac = function Log_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || Log)(); };
  3235. static ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: Log, factory: Log.ɵfac });
  3236. }
  3237. (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(Log, [{
  3238. type: Injectable
  3239. }], () => [], null); })();
  3240. export { ComponentFixture, ComponentFixtureAutoDetect, ComponentFixtureNoNgZone, DeferBlockFixture, InjectSetupWrapper, TestBed, TestComponentRenderer, __core_private_testing_placeholder__, discardPeriodicTasks, fakeAsync, flush, flushMicrotasks, getTestBed, inject, resetFakeAsyncZone, tick, waitForAsync, withModule, FakeNavigation as ɵFakeNavigation, Log as ɵLog, MetadataOverrider as ɵMetadataOverrider };
  3241. //# sourceMappingURL=testing.mjs.map