testing.mjs 136 KB

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