12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158 |
- /**
- * @license Angular v19.2.4
- * (c) 2010-2025 Google LLC. https://angular.io/
- * License: MIT
- */
- import * as i0 from '@angular/core';
- import { ɵDeferBlockState as _DeferBlockState, ɵtriggerResourceLoading as _triggerResourceLoading, ɵrenderDeferBlockState as _renderDeferBlockState, ɵCONTAINER_HEADER_OFFSET as _CONTAINER_HEADER_OFFSET, ɵgetDeferBlocks as _getDeferBlocks, InjectionToken, ɵDeferBlockBehavior as _DeferBlockBehavior, inject as inject$1, NgZone, ErrorHandler, Injectable, ɵNoopNgZone as _NoopNgZone, ApplicationRef, ɵPendingTasksInternal as _PendingTasksInternal, ɵZONELESS_ENABLED as _ZONELESS_ENABLED, ɵChangeDetectionScheduler as _ChangeDetectionScheduler, ɵEffectScheduler as _EffectScheduler, ɵMicrotaskEffectScheduler as _MicrotaskEffectScheduler, getDebugNode, RendererFactory2, ɵstringify as _stringify, Pipe, Directive, Component, NgModule, ɵReflectionCapabilities as _ReflectionCapabilities, ɵUSE_RUNTIME_DEPS_TRACKER_FOR_JIT as _USE_RUNTIME_DEPS_TRACKER_FOR_JIT, ɵdepsTracker as _depsTracker, ɵgetInjectableDef as _getInjectableDef, resolveForwardRef, ɵisComponentDefPendingResolution as _isComponentDefPendingResolution, ɵgetAsyncClassMetadataFn as _getAsyncClassMetadataFn, ɵresolveComponentResources as _resolveComponentResources, ɵRender3NgModuleRef as _Render3NgModuleRef, ApplicationInitStatus, LOCALE_ID, ɵDEFAULT_LOCALE_ID as _DEFAULT_LOCALE_ID, ɵsetLocaleId as _setLocaleId, ɵRender3ComponentFactory as _Render3ComponentFactory, ɵNG_COMP_DEF as _NG_COMP_DEF, ɵcompileComponent as _compileComponent, ɵNG_DIR_DEF as _NG_DIR_DEF, ɵcompileDirective as _compileDirective, ɵNG_PIPE_DEF as _NG_PIPE_DEF, ɵcompilePipe as _compilePipe, ɵNG_MOD_DEF as _NG_MOD_DEF, ɵpatchComponentDefWithScope as _patchComponentDefWithScope, ɵNG_INJ_DEF as _NG_INJ_DEF, ɵcompileNgModuleDefs as _compileNgModuleDefs, ɵclearResolutionOfComponentResourcesQueue as _clearResolutionOfComponentResourcesQueue, ɵrestoreComponentResolutionQueue as _restoreComponentResolutionQueue, ɵinternalProvideZoneChangeDetection as _internalProvideZoneChangeDetection, ɵChangeDetectionSchedulerImpl as _ChangeDetectionSchedulerImpl, Compiler, ɵDEFER_BLOCK_CONFIG as _DEFER_BLOCK_CONFIG, ɵINTERNAL_APPLICATION_ERROR_HANDLER as _INTERNAL_APPLICATION_ERROR_HANDLER, COMPILER_OPTIONS, Injector, ɵtransitiveScopesFor as _transitiveScopesFor, ɵgenerateStandaloneInDeclarationsError as _generateStandaloneInDeclarationsError, ɵNgModuleFactory as _NgModuleFactory, ModuleWithComponentFactories, ɵisEnvironmentProviders as _isEnvironmentProviders, ɵconvertToBitFlags as _convertToBitFlags, InjectFlags, ɵsetAllowDuplicateNgModuleIdsForTest as _setAllowDuplicateNgModuleIdsForTest, ɵresetCompiledComponents as _resetCompiledComponents, ɵsetUnknownElementStrictMode as _setUnknownElementStrictMode, ɵsetUnknownPropertyStrictMode as _setUnknownPropertyStrictMode, ɵgetUnknownElementStrictMode as _getUnknownElementStrictMode, ɵgetUnknownPropertyStrictMode as _getUnknownPropertyStrictMode, runInInjectionContext, EnvironmentInjector, ɵflushModuleScopingQueueAsMuchAsPossible as _flushModuleScopingQueueAsMuchAsPossible } from '@angular/core';
- export { ɵDeferBlockBehavior as DeferBlockBehavior, ɵDeferBlockState as DeferBlockState } from '@angular/core';
- import { Subscription } from 'rxjs';
- import { ResourceLoader } from '@angular/compiler';
- /**
- * Wraps a test function in an asynchronous test zone. The test will automatically
- * complete when all asynchronous calls within this zone are done. Can be used
- * to wrap an {@link inject} call.
- *
- * Example:
- *
- * ```ts
- * it('...', waitForAsync(inject([AClass], (object) => {
- * object.doSomething.then(() => {
- * expect(...);
- * })
- * })));
- * ```
- *
- * @publicApi
- */
- function waitForAsync(fn) {
- const _Zone = typeof Zone !== 'undefined' ? Zone : null;
- if (!_Zone) {
- return function () {
- return Promise.reject('Zone is needed for the waitForAsync() test helper but could not be found. ' +
- 'Please make sure that your environment includes zone.js');
- };
- }
- const asyncTest = _Zone && _Zone[_Zone.__symbol__('asyncTest')];
- if (typeof asyncTest === 'function') {
- return asyncTest(fn);
- }
- return function () {
- return Promise.reject('zone-testing.js is needed for the async() test helper but could not be found. ' +
- 'Please make sure that your environment includes zone.js/testing');
- };
- }
- /**
- * Represents an individual defer block for testing purposes.
- *
- * @publicApi
- */
- class DeferBlockFixture {
- block;
- componentFixture;
- /** @nodoc */
- constructor(block, componentFixture) {
- this.block = block;
- this.componentFixture = componentFixture;
- }
- /**
- * Renders the specified state of the defer fixture.
- * @param state the defer state to render
- */
- async render(state) {
- if (!hasStateTemplate(state, this.block)) {
- const stateAsString = getDeferBlockStateNameFromEnum(state);
- throw new Error(`Tried to render this defer block in the \`${stateAsString}\` state, ` +
- `but there was no @${stateAsString.toLowerCase()} block defined in a template.`);
- }
- if (state === _DeferBlockState.Complete) {
- await _triggerResourceLoading(this.block.tDetails, this.block.lView, this.block.tNode);
- }
- // If the `render` method is used explicitly - skip timer-based scheduling for
- // `@placeholder` and `@loading` blocks and render them immediately.
- const skipTimerScheduling = true;
- _renderDeferBlockState(state, this.block.tNode, this.block.lContainer, skipTimerScheduling);
- this.componentFixture.detectChanges();
- }
- /**
- * Retrieves all nested child defer block fixtures
- * in a given defer block.
- */
- getDeferBlocks() {
- const deferBlocks = [];
- // An LContainer that represents a defer block has at most 1 view, which is
- // located right after an LContainer header. Get a hold of that view and inspect
- // it for nested defer blocks.
- const deferBlockFixtures = [];
- if (this.block.lContainer.length >= _CONTAINER_HEADER_OFFSET) {
- const lView = this.block.lContainer[_CONTAINER_HEADER_OFFSET];
- _getDeferBlocks(lView, deferBlocks);
- for (const block of deferBlocks) {
- deferBlockFixtures.push(new DeferBlockFixture(block, this.componentFixture));
- }
- }
- return Promise.resolve(deferBlockFixtures);
- }
- }
- function hasStateTemplate(state, block) {
- switch (state) {
- case _DeferBlockState.Placeholder:
- return block.tDetails.placeholderTmplIndex !== null;
- case _DeferBlockState.Loading:
- return block.tDetails.loadingTmplIndex !== null;
- case _DeferBlockState.Error:
- return block.tDetails.errorTmplIndex !== null;
- case _DeferBlockState.Complete:
- return true;
- default:
- return false;
- }
- }
- function getDeferBlockStateNameFromEnum(state) {
- switch (state) {
- case _DeferBlockState.Placeholder:
- return 'Placeholder';
- case _DeferBlockState.Loading:
- return 'Loading';
- case _DeferBlockState.Error:
- return 'Error';
- default:
- return 'Main';
- }
- }
- /** Whether test modules should be torn down by default. */
- const TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT = true;
- /** Whether unknown elements in templates should throw by default. */
- const THROW_ON_UNKNOWN_ELEMENTS_DEFAULT = false;
- /** Whether unknown properties in templates should throw by default. */
- const THROW_ON_UNKNOWN_PROPERTIES_DEFAULT = false;
- /** Whether defer blocks should use manual triggering or play through normally. */
- const DEFER_BLOCK_DEFAULT_BEHAVIOR = _DeferBlockBehavior.Playthrough;
- /**
- * An abstract class for inserting the root test component element in a platform independent way.
- *
- * @publicApi
- */
- class TestComponentRenderer {
- insertRootElement(rootElementId) { }
- removeAllRootElements() { }
- }
- /**
- * @publicApi
- */
- const ComponentFixtureAutoDetect = new InjectionToken('ComponentFixtureAutoDetect');
- /**
- * @publicApi
- */
- const ComponentFixtureNoNgZone = new InjectionToken('ComponentFixtureNoNgZone');
- const RETHROW_APPLICATION_ERRORS_DEFAULT = true;
- class TestBedApplicationErrorHandler {
- zone = inject$1(NgZone);
- userErrorHandler = inject$1(ErrorHandler);
- whenStableRejectFunctions = new Set();
- handleError(e) {
- try {
- this.zone.runOutsideAngular(() => this.userErrorHandler.handleError(e));
- }
- catch (userError) {
- e = userError;
- }
- // Instead of throwing the error when there are outstanding `fixture.whenStable` promises,
- // reject those promises with the error. This allows developers to write
- // expectAsync(fix.whenStable()).toBeRejected();
- if (this.whenStableRejectFunctions.size > 0) {
- for (const fn of this.whenStableRejectFunctions.values()) {
- fn(e);
- }
- this.whenStableRejectFunctions.clear();
- }
- else {
- throw e;
- }
- }
- static ɵfac = function TestBedApplicationErrorHandler_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || TestBedApplicationErrorHandler)(); };
- static ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: TestBedApplicationErrorHandler, factory: TestBedApplicationErrorHandler.ɵfac });
- }
- (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(TestBedApplicationErrorHandler, [{
- type: Injectable
- }], null, null); })();
- /**
- * Fixture for debugging and testing a component.
- *
- * @publicApi
- */
- class ComponentFixture {
- componentRef;
- /**
- * The DebugElement associated with the root element of this component.
- */
- debugElement;
- /**
- * The instance of the root component class.
- */
- componentInstance;
- /**
- * The native element at the root of the component.
- */
- nativeElement;
- /**
- * The ElementRef for the element at the root of the component.
- */
- elementRef;
- /**
- * The ChangeDetectorRef for the component
- */
- changeDetectorRef;
- _renderer;
- _isDestroyed = false;
- /** @internal */
- _noZoneOptionIsSet = inject$1(ComponentFixtureNoNgZone, { optional: true });
- /** @internal */
- _ngZone = this._noZoneOptionIsSet ? new _NoopNgZone() : inject$1(NgZone);
- // Inject ApplicationRef to ensure NgZone stableness causes after render hooks to run
- // This will likely happen as a result of fixture.detectChanges because it calls ngZone.run
- // This is a crazy way of doing things but hey, it's the world we live in.
- // The zoneless scheduler should instead do this more imperatively by attaching
- // the `ComponentRef` to `ApplicationRef` and calling `appRef.tick` as the `detectChanges`
- // behavior.
- /** @internal */
- _appRef = inject$1(ApplicationRef);
- _testAppRef = this._appRef;
- pendingTasks = inject$1(_PendingTasksInternal);
- appErrorHandler = inject$1(TestBedApplicationErrorHandler);
- zonelessEnabled = inject$1(_ZONELESS_ENABLED);
- scheduler = inject$1(_ChangeDetectionScheduler);
- rootEffectScheduler = inject$1(_EffectScheduler);
- microtaskEffectScheduler = inject$1(_MicrotaskEffectScheduler);
- autoDetectDefault = this.zonelessEnabled ? true : false;
- autoDetect = inject$1(ComponentFixtureAutoDetect, { optional: true }) ?? this.autoDetectDefault;
- subscriptions = new Subscription();
- // TODO(atscott): Remove this from public API
- ngZone = this._noZoneOptionIsSet ? null : this._ngZone;
- /** @nodoc */
- constructor(componentRef) {
- this.componentRef = componentRef;
- this.changeDetectorRef = componentRef.changeDetectorRef;
- this.elementRef = componentRef.location;
- this.debugElement = getDebugNode(this.elementRef.nativeElement);
- this.componentInstance = componentRef.instance;
- this.nativeElement = this.elementRef.nativeElement;
- this.componentRef = componentRef;
- if (this.autoDetect) {
- this._testAppRef.externalTestViews.add(this.componentRef.hostView);
- this.scheduler?.notify(8 /* ɵNotificationSource.ViewAttached */);
- this.scheduler?.notify(0 /* ɵNotificationSource.MarkAncestorsForTraversal */);
- }
- this.componentRef.hostView.onDestroy(() => {
- this._testAppRef.externalTestViews.delete(this.componentRef.hostView);
- });
- // Create subscriptions outside the NgZone so that the callbacks run outside
- // of NgZone.
- this._ngZone.runOutsideAngular(() => {
- this.subscriptions.add(this._ngZone.onError.subscribe({
- next: (error) => {
- throw error;
- },
- }));
- });
- }
- /**
- * Trigger a change detection cycle for the component.
- */
- detectChanges(checkNoChanges = true) {
- this.microtaskEffectScheduler.flush();
- const originalCheckNoChanges = this.componentRef.changeDetectorRef.checkNoChanges;
- try {
- if (!checkNoChanges) {
- this.componentRef.changeDetectorRef.checkNoChanges = () => { };
- }
- if (this.zonelessEnabled) {
- try {
- this._testAppRef.externalTestViews.add(this.componentRef.hostView);
- this._appRef.tick();
- }
- finally {
- if (!this.autoDetect) {
- this._testAppRef.externalTestViews.delete(this.componentRef.hostView);
- }
- }
- }
- else {
- // Run the change detection inside the NgZone so that any async tasks as part of the change
- // detection are captured by the zone and can be waited for in isStable.
- this._ngZone.run(() => {
- // Flush root effects before `detectChanges()`, to emulate the sequencing of `tick()`.
- this.rootEffectScheduler.flush();
- this.changeDetectorRef.detectChanges();
- this.checkNoChanges();
- });
- }
- }
- finally {
- this.componentRef.changeDetectorRef.checkNoChanges = originalCheckNoChanges;
- }
- this.microtaskEffectScheduler.flush();
- }
- /**
- * Do a change detection run to make sure there were no changes.
- */
- checkNoChanges() {
- this.changeDetectorRef.checkNoChanges();
- }
- /**
- * Set whether the fixture should autodetect changes.
- *
- * Also runs detectChanges once so that any existing change is detected.
- *
- * @param autoDetect Whether to autodetect changes. By default, `true`.
- */
- autoDetectChanges(autoDetect = true) {
- if (this._noZoneOptionIsSet && !this.zonelessEnabled) {
- throw new Error('Cannot call autoDetectChanges when ComponentFixtureNoNgZone is set.');
- }
- if (autoDetect !== this.autoDetect) {
- if (autoDetect) {
- this._testAppRef.externalTestViews.add(this.componentRef.hostView);
- }
- else {
- this._testAppRef.externalTestViews.delete(this.componentRef.hostView);
- }
- }
- this.autoDetect = autoDetect;
- this.detectChanges();
- }
- /**
- * Return whether the fixture is currently stable or has async tasks that have not been completed
- * yet.
- */
- isStable() {
- return !this.pendingTasks.hasPendingTasks.value;
- }
- /**
- * Get a promise that resolves when the fixture is stable.
- *
- * This can be used to resume testing after events have triggered asynchronous activity or
- * asynchronous change detection.
- */
- whenStable() {
- if (this.isStable()) {
- return Promise.resolve(false);
- }
- return new Promise((resolve, reject) => {
- this.appErrorHandler.whenStableRejectFunctions.add(reject);
- this._appRef.whenStable().then(() => {
- this.appErrorHandler.whenStableRejectFunctions.delete(reject);
- resolve(true);
- });
- });
- }
- /**
- * Retrieves all defer block fixtures in the component fixture.
- */
- getDeferBlocks() {
- const deferBlocks = [];
- const lView = this.componentRef.hostView['_lView'];
- _getDeferBlocks(lView, deferBlocks);
- const deferBlockFixtures = [];
- for (const block of deferBlocks) {
- deferBlockFixtures.push(new DeferBlockFixture(block, this));
- }
- return Promise.resolve(deferBlockFixtures);
- }
- _getRenderer() {
- if (this._renderer === undefined) {
- this._renderer = this.componentRef.injector.get(RendererFactory2, null);
- }
- return this._renderer;
- }
- /**
- * Get a promise that resolves when the ui state is stable following animations.
- */
- whenRenderingDone() {
- const renderer = this._getRenderer();
- if (renderer && renderer.whenRenderingDone) {
- return renderer.whenRenderingDone();
- }
- return this.whenStable();
- }
- /**
- * Trigger component destruction.
- */
- destroy() {
- this.subscriptions.unsubscribe();
- this._testAppRef.externalTestViews.delete(this.componentRef.hostView);
- if (!this._isDestroyed) {
- this.componentRef.destroy();
- this._isDestroyed = true;
- }
- }
- }
- const _Zone = typeof Zone !== 'undefined' ? Zone : null;
- const fakeAsyncTestModule = _Zone && _Zone[_Zone.__symbol__('fakeAsyncTest')];
- const fakeAsyncTestModuleNotLoadedErrorMessage = `zone-testing.js is needed for the fakeAsync() test helper but could not be found.
- Please make sure that your environment includes zone.js/testing`;
- /**
- * Clears out the shared fake async zone for a test.
- * To be called in a global `beforeEach`.
- *
- * @publicApi
- */
- function resetFakeAsyncZone() {
- if (fakeAsyncTestModule) {
- return fakeAsyncTestModule.resetFakeAsyncZone();
- }
- throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
- }
- function resetFakeAsyncZoneIfExists() {
- if (fakeAsyncTestModule) {
- fakeAsyncTestModule.resetFakeAsyncZone();
- }
- }
- /**
- * Wraps a function to be executed in the `fakeAsync` zone:
- * - Microtasks are manually executed by calling `flushMicrotasks()`.
- * - Timers are synchronous; `tick()` simulates the asynchronous passage of time.
- *
- * Can be used to wrap `inject()` calls.
- *
- * @param fn The function that you want to wrap in the `fakeAsync` zone.
- * @param options
- * - flush: When true, will drain the macrotask queue after the test function completes.
- * When false, will throw an exception at the end of the function if there are pending timers.
- *
- * @usageNotes
- * ### Example
- *
- * {@example core/testing/ts/fake_async.ts region='basic'}
- *
- *
- * @returns The function wrapped to be executed in the `fakeAsync` zone.
- * Any arguments passed when calling this returned function will be passed through to the `fn`
- * function in the parameters when it is called.
- *
- * @publicApi
- */
- function fakeAsync(fn, options) {
- if (fakeAsyncTestModule) {
- return fakeAsyncTestModule.fakeAsync(fn, options);
- }
- throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
- }
- /**
- * Simulates the asynchronous passage of time for the timers in the `fakeAsync` zone.
- *
- * The microtasks queue is drained at the very start of this function and after any timer callback
- * has been executed.
- *
- * @param millis The number of milliseconds to advance the virtual timer.
- * @param tickOptions The options to pass to the `tick()` function.
- *
- * @usageNotes
- *
- * The `tick()` option is a flag called `processNewMacroTasksSynchronously`,
- * which determines whether or not to invoke new macroTasks.
- *
- * If you provide a `tickOptions` object, but do not specify a
- * `processNewMacroTasksSynchronously` property (`tick(100, {})`),
- * then `processNewMacroTasksSynchronously` defaults to true.
- *
- * If you omit the `tickOptions` parameter (`tick(100))`), then
- * `tickOptions` defaults to `{processNewMacroTasksSynchronously: true}`.
- *
- * ### Example
- *
- * {@example core/testing/ts/fake_async.ts region='basic'}
- *
- * The following example includes a nested timeout (new macroTask), and
- * the `tickOptions` parameter is allowed to default. In this case,
- * `processNewMacroTasksSynchronously` defaults to true, and the nested
- * function is executed on each tick.
- *
- * ```ts
- * it ('test with nested setTimeout', fakeAsync(() => {
- * let nestedTimeoutInvoked = false;
- * function funcWithNestedTimeout() {
- * setTimeout(() => {
- * nestedTimeoutInvoked = true;
- * });
- * };
- * setTimeout(funcWithNestedTimeout);
- * tick();
- * expect(nestedTimeoutInvoked).toBe(true);
- * }));
- * ```
- *
- * In the following case, `processNewMacroTasksSynchronously` is explicitly
- * set to false, so the nested timeout function is not invoked.
- *
- * ```ts
- * it ('test with nested setTimeout', fakeAsync(() => {
- * let nestedTimeoutInvoked = false;
- * function funcWithNestedTimeout() {
- * setTimeout(() => {
- * nestedTimeoutInvoked = true;
- * });
- * };
- * setTimeout(funcWithNestedTimeout);
- * tick(0, {processNewMacroTasksSynchronously: false});
- * expect(nestedTimeoutInvoked).toBe(false);
- * }));
- * ```
- *
- *
- * @publicApi
- */
- function tick(millis = 0, tickOptions = {
- processNewMacroTasksSynchronously: true,
- }) {
- if (fakeAsyncTestModule) {
- return fakeAsyncTestModule.tick(millis, tickOptions);
- }
- throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
- }
- /**
- * Flushes any pending microtasks and simulates the asynchronous passage of time for the timers in
- * the `fakeAsync` zone by
- * draining the macrotask queue until it is empty.
- *
- * @param maxTurns The maximum number of times the scheduler attempts to clear its queue before
- * throwing an error.
- * @returns The simulated time elapsed, in milliseconds.
- *
- * @publicApi
- */
- function flush(maxTurns) {
- if (fakeAsyncTestModule) {
- return fakeAsyncTestModule.flush(maxTurns);
- }
- throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
- }
- /**
- * Discard all remaining periodic tasks.
- *
- * @publicApi
- */
- function discardPeriodicTasks() {
- if (fakeAsyncTestModule) {
- return fakeAsyncTestModule.discardPeriodicTasks();
- }
- throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
- }
- /**
- * Flush any pending microtasks.
- *
- * @publicApi
- */
- function flushMicrotasks() {
- if (fakeAsyncTestModule) {
- return fakeAsyncTestModule.flushMicrotasks();
- }
- throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
- }
- let _nextReferenceId = 0;
- class MetadataOverrider {
- _references = new Map();
- /**
- * Creates a new instance for the given metadata class
- * based on an old instance and overrides.
- */
- overrideMetadata(metadataClass, oldMetadata, override) {
- const props = {};
- if (oldMetadata) {
- _valueProps(oldMetadata).forEach((prop) => (props[prop] = oldMetadata[prop]));
- }
- if (override.set) {
- if (override.remove || override.add) {
- throw new Error(`Cannot set and add/remove ${_stringify(metadataClass)} at the same time!`);
- }
- setMetadata(props, override.set);
- }
- if (override.remove) {
- removeMetadata(props, override.remove, this._references);
- }
- if (override.add) {
- addMetadata(props, override.add);
- }
- return new metadataClass(props);
- }
- }
- function removeMetadata(metadata, remove, references) {
- const removeObjects = new Set();
- for (const prop in remove) {
- const removeValue = remove[prop];
- if (Array.isArray(removeValue)) {
- removeValue.forEach((value) => {
- removeObjects.add(_propHashKey(prop, value, references));
- });
- }
- else {
- removeObjects.add(_propHashKey(prop, removeValue, references));
- }
- }
- for (const prop in metadata) {
- const propValue = metadata[prop];
- if (Array.isArray(propValue)) {
- metadata[prop] = propValue.filter((value) => !removeObjects.has(_propHashKey(prop, value, references)));
- }
- else {
- if (removeObjects.has(_propHashKey(prop, propValue, references))) {
- metadata[prop] = undefined;
- }
- }
- }
- }
- function addMetadata(metadata, add) {
- for (const prop in add) {
- const addValue = add[prop];
- const propValue = metadata[prop];
- if (propValue != null && Array.isArray(propValue)) {
- metadata[prop] = propValue.concat(addValue);
- }
- else {
- metadata[prop] = addValue;
- }
- }
- }
- function setMetadata(metadata, set) {
- for (const prop in set) {
- metadata[prop] = set[prop];
- }
- }
- function _propHashKey(propName, propValue, references) {
- let nextObjectId = 0;
- const objectIds = new Map();
- const replacer = (key, value) => {
- if (value !== null && typeof value === 'object') {
- if (objectIds.has(value)) {
- return objectIds.get(value);
- }
- // Record an id for this object such that any later references use the object's id instead
- // of the object itself, in order to break cyclic pointers in objects.
- objectIds.set(value, `ɵobj#${nextObjectId++}`);
- // The first time an object is seen the object itself is serialized.
- return value;
- }
- else if (typeof value === 'function') {
- value = _serializeReference(value, references);
- }
- return value;
- };
- return `${propName}:${JSON.stringify(propValue, replacer)}`;
- }
- function _serializeReference(ref, references) {
- let id = references.get(ref);
- if (!id) {
- id = `${_stringify(ref)}${_nextReferenceId++}`;
- references.set(ref, id);
- }
- return id;
- }
- function _valueProps(obj) {
- const props = [];
- // regular public props
- Object.keys(obj).forEach((prop) => {
- if (!prop.startsWith('_')) {
- props.push(prop);
- }
- });
- // getters
- let proto = obj;
- while ((proto = Object.getPrototypeOf(proto))) {
- Object.keys(proto).forEach((protoProp) => {
- const desc = Object.getOwnPropertyDescriptor(proto, protoProp);
- if (!protoProp.startsWith('_') && desc && 'get' in desc) {
- props.push(protoProp);
- }
- });
- }
- return props;
- }
- const reflection = new _ReflectionCapabilities();
- /**
- * Allows to override ivy metadata for tests (via the `TestBed`).
- */
- class OverrideResolver {
- overrides = new Map();
- resolved = new Map();
- addOverride(type, override) {
- const overrides = this.overrides.get(type) || [];
- overrides.push(override);
- this.overrides.set(type, overrides);
- this.resolved.delete(type);
- }
- setOverrides(overrides) {
- this.overrides.clear();
- overrides.forEach(([type, override]) => {
- this.addOverride(type, override);
- });
- }
- getAnnotation(type) {
- const annotations = reflection.annotations(type);
- // Try to find the nearest known Type annotation and make sure that this annotation is an
- // instance of the type we are looking for, so we can use it for resolution. Note: there might
- // be multiple known annotations found due to the fact that Components can extend Directives (so
- // both Directive and Component annotations would be present), so we always check if the known
- // annotation has the right type.
- for (let i = annotations.length - 1; i >= 0; i--) {
- const annotation = annotations[i];
- const isKnownType = annotation instanceof Directive ||
- annotation instanceof Component ||
- annotation instanceof Pipe ||
- annotation instanceof NgModule;
- if (isKnownType) {
- return annotation instanceof this.type ? annotation : null;
- }
- }
- return null;
- }
- resolve(type) {
- let resolved = this.resolved.get(type) || null;
- if (!resolved) {
- resolved = this.getAnnotation(type);
- if (resolved) {
- const overrides = this.overrides.get(type);
- if (overrides) {
- const overrider = new MetadataOverrider();
- overrides.forEach((override) => {
- resolved = overrider.overrideMetadata(this.type, resolved, override);
- });
- }
- }
- this.resolved.set(type, resolved);
- }
- return resolved;
- }
- }
- class DirectiveResolver extends OverrideResolver {
- get type() {
- return Directive;
- }
- }
- class ComponentResolver extends OverrideResolver {
- get type() {
- return Component;
- }
- }
- class PipeResolver extends OverrideResolver {
- get type() {
- return Pipe;
- }
- }
- class NgModuleResolver extends OverrideResolver {
- get type() {
- return NgModule;
- }
- }
- var TestingModuleOverride;
- (function (TestingModuleOverride) {
- TestingModuleOverride[TestingModuleOverride["DECLARATION"] = 0] = "DECLARATION";
- TestingModuleOverride[TestingModuleOverride["OVERRIDE_TEMPLATE"] = 1] = "OVERRIDE_TEMPLATE";
- })(TestingModuleOverride || (TestingModuleOverride = {}));
- function isTestingModuleOverride(value) {
- return (value === TestingModuleOverride.DECLARATION || value === TestingModuleOverride.OVERRIDE_TEMPLATE);
- }
- function assertNoStandaloneComponents(types, resolver, location) {
- types.forEach((type) => {
- if (!_getAsyncClassMetadataFn(type)) {
- const component = resolver.resolve(type);
- if (component && (component.standalone == null || component.standalone)) {
- throw new Error(_generateStandaloneInDeclarationsError(type, location));
- }
- }
- });
- }
- class TestBedCompiler {
- platform;
- additionalModuleTypes;
- originalComponentResolutionQueue = null;
- // Testing module configuration
- declarations = [];
- imports = [];
- providers = [];
- schemas = [];
- // Queues of components/directives/pipes that should be recompiled.
- pendingComponents = new Set();
- pendingDirectives = new Set();
- pendingPipes = new Set();
- // Set of components with async metadata, i.e. components with `@defer` blocks
- // in their templates.
- componentsWithAsyncMetadata = new Set();
- // Keep track of all components and directives, so we can patch Providers onto defs later.
- seenComponents = new Set();
- seenDirectives = new Set();
- // Keep track of overridden modules, so that we can collect all affected ones in the module tree.
- overriddenModules = new Set();
- // Store resolved styles for Components that have template overrides present and `styleUrls`
- // defined at the same time.
- existingComponentStyles = new Map();
- resolvers = initResolvers();
- // Map of component type to an NgModule that declares it.
- //
- // There are a couple special cases:
- // - for standalone components, the module scope value is `null`
- // - when a component is declared in `TestBed.configureTestingModule()` call or
- // a component's template is overridden via `TestBed.overrideTemplateUsingTestingModule()`.
- // we use a special value from the `TestingModuleOverride` enum.
- componentToModuleScope = new Map();
- // Map that keeps initial version of component/directive/pipe defs in case
- // we compile a Type again, thus overriding respective static fields. This is
- // required to make sure we restore defs to their initial states between test runs.
- // Note: one class may have multiple defs (for example: ɵmod and ɵinj in case of an
- // NgModule), store all of them in a map.
- initialNgDefs = new Map();
- // Array that keeps cleanup operations for initial versions of component/directive/pipe/module
- // defs in case TestBed makes changes to the originals.
- defCleanupOps = [];
- _injector = null;
- compilerProviders = null;
- providerOverrides = [];
- rootProviderOverrides = [];
- // Overrides for injectables with `{providedIn: SomeModule}` need to be tracked and added to that
- // module's provider list.
- providerOverridesByModule = new Map();
- providerOverridesByToken = new Map();
- scopesWithOverriddenProviders = new Set();
- testModuleType;
- testModuleRef = null;
- deferBlockBehavior = DEFER_BLOCK_DEFAULT_BEHAVIOR;
- rethrowApplicationTickErrors = RETHROW_APPLICATION_ERRORS_DEFAULT;
- constructor(platform, additionalModuleTypes) {
- this.platform = platform;
- this.additionalModuleTypes = additionalModuleTypes;
- class DynamicTestModule {
- }
- this.testModuleType = DynamicTestModule;
- }
- setCompilerProviders(providers) {
- this.compilerProviders = providers;
- this._injector = null;
- }
- configureTestingModule(moduleDef) {
- // Enqueue any compilation tasks for the directly declared component.
- if (moduleDef.declarations !== undefined) {
- // Verify that there are no standalone components
- assertNoStandaloneComponents(moduleDef.declarations, this.resolvers.component, '"TestBed.configureTestingModule" call');
- this.queueTypeArray(moduleDef.declarations, TestingModuleOverride.DECLARATION);
- this.declarations.push(...moduleDef.declarations);
- }
- // Enqueue any compilation tasks for imported modules.
- if (moduleDef.imports !== undefined) {
- this.queueTypesFromModulesArray(moduleDef.imports);
- this.imports.push(...moduleDef.imports);
- }
- if (moduleDef.providers !== undefined) {
- this.providers.push(...moduleDef.providers);
- }
- if (moduleDef.schemas !== undefined) {
- this.schemas.push(...moduleDef.schemas);
- }
- this.deferBlockBehavior = moduleDef.deferBlockBehavior ?? DEFER_BLOCK_DEFAULT_BEHAVIOR;
- this.rethrowApplicationTickErrors =
- moduleDef.rethrowApplicationErrors ?? RETHROW_APPLICATION_ERRORS_DEFAULT;
- }
- overrideModule(ngModule, override) {
- if (_USE_RUNTIME_DEPS_TRACKER_FOR_JIT) {
- _depsTracker.clearScopeCacheFor(ngModule);
- }
- this.overriddenModules.add(ngModule);
- // Compile the module right away.
- this.resolvers.module.addOverride(ngModule, override);
- const metadata = this.resolvers.module.resolve(ngModule);
- if (metadata === null) {
- throw invalidTypeError(ngModule.name, 'NgModule');
- }
- this.recompileNgModule(ngModule, metadata);
- // At this point, the module has a valid module def (ɵmod), but the override may have introduced
- // new declarations or imported modules. Ingest any possible new types and add them to the
- // current queue.
- this.queueTypesFromModulesArray([ngModule]);
- }
- overrideComponent(component, override) {
- this.verifyNoStandaloneFlagOverrides(component, override);
- this.resolvers.component.addOverride(component, override);
- this.pendingComponents.add(component);
- // If this is a component with async metadata (i.e. a component with a `@defer` block
- // in a template) - store it for future processing.
- this.maybeRegisterComponentWithAsyncMetadata(component);
- }
- overrideDirective(directive, override) {
- this.verifyNoStandaloneFlagOverrides(directive, override);
- this.resolvers.directive.addOverride(directive, override);
- this.pendingDirectives.add(directive);
- }
- overridePipe(pipe, override) {
- this.verifyNoStandaloneFlagOverrides(pipe, override);
- this.resolvers.pipe.addOverride(pipe, override);
- this.pendingPipes.add(pipe);
- }
- verifyNoStandaloneFlagOverrides(type, override) {
- if (override.add?.hasOwnProperty('standalone') ||
- override.set?.hasOwnProperty('standalone') ||
- override.remove?.hasOwnProperty('standalone')) {
- throw new Error(`An override for the ${type.name} class has the \`standalone\` flag. ` +
- `Changing the \`standalone\` flag via TestBed overrides is not supported.`);
- }
- }
- overrideProvider(token, provider) {
- let providerDef;
- if (provider.useFactory !== undefined) {
- providerDef = {
- provide: token,
- useFactory: provider.useFactory,
- deps: provider.deps || [],
- multi: provider.multi,
- };
- }
- else if (provider.useValue !== undefined) {
- providerDef = { provide: token, useValue: provider.useValue, multi: provider.multi };
- }
- else {
- providerDef = { provide: token };
- }
- const injectableDef = typeof token !== 'string' ? _getInjectableDef(token) : null;
- const providedIn = injectableDef === null ? null : resolveForwardRef(injectableDef.providedIn);
- const overridesBucket = providedIn === 'root' ? this.rootProviderOverrides : this.providerOverrides;
- overridesBucket.push(providerDef);
- // Keep overrides grouped by token as well for fast lookups using token
- this.providerOverridesByToken.set(token, providerDef);
- if (injectableDef !== null && providedIn !== null && typeof providedIn !== 'string') {
- const existingOverrides = this.providerOverridesByModule.get(providedIn);
- if (existingOverrides !== undefined) {
- existingOverrides.push(providerDef);
- }
- else {
- this.providerOverridesByModule.set(providedIn, [providerDef]);
- }
- }
- }
- overrideTemplateUsingTestingModule(type, template) {
- const def = type[_NG_COMP_DEF];
- const hasStyleUrls = () => {
- const metadata = this.resolvers.component.resolve(type);
- return !!metadata.styleUrl || !!metadata.styleUrls?.length;
- };
- const overrideStyleUrls = !!def && !_isComponentDefPendingResolution(type) && hasStyleUrls();
- // In Ivy, compiling a component does not require knowing the module providing the
- // component's scope, so overrideTemplateUsingTestingModule can be implemented purely via
- // overrideComponent. Important: overriding template requires full Component re-compilation,
- // which may fail in case styleUrls are also present (thus Component is considered as required
- // resolution). In order to avoid this, we preemptively set styleUrls to an empty array,
- // preserve current styles available on Component def and restore styles back once compilation
- // is complete.
- const override = overrideStyleUrls
- ? { template, styles: [], styleUrls: [], styleUrl: undefined }
- : { template };
- this.overrideComponent(type, { set: override });
- if (overrideStyleUrls && def.styles && def.styles.length > 0) {
- this.existingComponentStyles.set(type, def.styles);
- }
- // Set the component's scope to be the testing module.
- this.componentToModuleScope.set(type, TestingModuleOverride.OVERRIDE_TEMPLATE);
- }
- async resolvePendingComponentsWithAsyncMetadata() {
- if (this.componentsWithAsyncMetadata.size === 0)
- return;
- const promises = [];
- for (const component of this.componentsWithAsyncMetadata) {
- const asyncMetadataFn = _getAsyncClassMetadataFn(component);
- if (asyncMetadataFn) {
- promises.push(asyncMetadataFn());
- }
- }
- this.componentsWithAsyncMetadata.clear();
- const resolvedDeps = await Promise.all(promises);
- const flatResolvedDeps = resolvedDeps.flat(2);
- this.queueTypesFromModulesArray(flatResolvedDeps);
- // Loaded standalone components might contain imports of NgModules
- // with providers, make sure we override providers there too.
- for (const component of flatResolvedDeps) {
- this.applyProviderOverridesInScope(component);
- }
- }
- async compileComponents() {
- this.clearComponentResolutionQueue();
- // Wait for all async metadata for components that were
- // overridden, we need resolved metadata to perform an override
- // and re-compile a component.
- await this.resolvePendingComponentsWithAsyncMetadata();
- // Verify that there were no standalone components present in the `declarations` field
- // during the `TestBed.configureTestingModule` call. We perform this check here in addition
- // to the logic in the `configureTestingModule` function, since at this point we have
- // all async metadata resolved.
- assertNoStandaloneComponents(this.declarations, this.resolvers.component, '"TestBed.configureTestingModule" call');
- // Run compilers for all queued types.
- let needsAsyncResources = this.compileTypesSync();
- // compileComponents() should not be async unless it needs to be.
- if (needsAsyncResources) {
- let resourceLoader;
- let resolver = (url) => {
- if (!resourceLoader) {
- resourceLoader = this.injector.get(ResourceLoader);
- }
- return Promise.resolve(resourceLoader.get(url));
- };
- await _resolveComponentResources(resolver);
- }
- }
- finalize() {
- // One last compile
- this.compileTypesSync();
- // Create the testing module itself.
- this.compileTestModule();
- this.applyTransitiveScopes();
- this.applyProviderOverrides();
- // Patch previously stored `styles` Component values (taken from ɵcmp), in case these
- // Components have `styleUrls` fields defined and template override was requested.
- this.patchComponentsWithExistingStyles();
- // Clear the componentToModuleScope map, so that future compilations don't reset the scope of
- // every component.
- this.componentToModuleScope.clear();
- const parentInjector = this.platform.injector;
- this.testModuleRef = new _Render3NgModuleRef(this.testModuleType, parentInjector, []);
- // ApplicationInitStatus.runInitializers() is marked @internal to core.
- // Cast it to any before accessing it.
- this.testModuleRef.injector.get(ApplicationInitStatus).runInitializers();
- // Set locale ID after running app initializers, since locale information might be updated while
- // running initializers. This is also consistent with the execution order while bootstrapping an
- // app (see `packages/core/src/application_ref.ts` file).
- const localeId = this.testModuleRef.injector.get(LOCALE_ID, _DEFAULT_LOCALE_ID);
- _setLocaleId(localeId);
- return this.testModuleRef;
- }
- /**
- * @internal
- */
- _compileNgModuleSync(moduleType) {
- this.queueTypesFromModulesArray([moduleType]);
- this.compileTypesSync();
- this.applyProviderOverrides();
- this.applyProviderOverridesInScope(moduleType);
- this.applyTransitiveScopes();
- }
- /**
- * @internal
- */
- async _compileNgModuleAsync(moduleType) {
- this.queueTypesFromModulesArray([moduleType]);
- await this.compileComponents();
- this.applyProviderOverrides();
- this.applyProviderOverridesInScope(moduleType);
- this.applyTransitiveScopes();
- }
- /**
- * @internal
- */
- _getModuleResolver() {
- return this.resolvers.module;
- }
- /**
- * @internal
- */
- _getComponentFactories(moduleType) {
- return maybeUnwrapFn(moduleType.ɵmod.declarations).reduce((factories, declaration) => {
- const componentDef = declaration.ɵcmp;
- componentDef && factories.push(new _Render3ComponentFactory(componentDef, this.testModuleRef));
- return factories;
- }, []);
- }
- compileTypesSync() {
- // Compile all queued components, directives, pipes.
- let needsAsyncResources = false;
- this.pendingComponents.forEach((declaration) => {
- if (_getAsyncClassMetadataFn(declaration)) {
- throw new Error(`Component '${declaration.name}' has unresolved metadata. ` +
- `Please call \`await TestBed.compileComponents()\` before running this test.`);
- }
- needsAsyncResources = needsAsyncResources || _isComponentDefPendingResolution(declaration);
- const metadata = this.resolvers.component.resolve(declaration);
- if (metadata === null) {
- throw invalidTypeError(declaration.name, 'Component');
- }
- this.maybeStoreNgDef(_NG_COMP_DEF, declaration);
- if (_USE_RUNTIME_DEPS_TRACKER_FOR_JIT) {
- _depsTracker.clearScopeCacheFor(declaration);
- }
- _compileComponent(declaration, metadata);
- });
- this.pendingComponents.clear();
- this.pendingDirectives.forEach((declaration) => {
- const metadata = this.resolvers.directive.resolve(declaration);
- if (metadata === null) {
- throw invalidTypeError(declaration.name, 'Directive');
- }
- this.maybeStoreNgDef(_NG_DIR_DEF, declaration);
- _compileDirective(declaration, metadata);
- });
- this.pendingDirectives.clear();
- this.pendingPipes.forEach((declaration) => {
- const metadata = this.resolvers.pipe.resolve(declaration);
- if (metadata === null) {
- throw invalidTypeError(declaration.name, 'Pipe');
- }
- this.maybeStoreNgDef(_NG_PIPE_DEF, declaration);
- _compilePipe(declaration, metadata);
- });
- this.pendingPipes.clear();
- return needsAsyncResources;
- }
- applyTransitiveScopes() {
- if (this.overriddenModules.size > 0) {
- // Module overrides (via `TestBed.overrideModule`) might affect scopes that were previously
- // calculated and stored in `transitiveCompileScopes`. If module overrides are present,
- // collect all affected modules and reset scopes to force their re-calculation.
- const testingModuleDef = this.testModuleType[_NG_MOD_DEF];
- const affectedModules = this.collectModulesAffectedByOverrides(testingModuleDef.imports);
- if (affectedModules.size > 0) {
- affectedModules.forEach((moduleType) => {
- if (!_USE_RUNTIME_DEPS_TRACKER_FOR_JIT) {
- this.storeFieldOfDefOnType(moduleType, _NG_MOD_DEF, 'transitiveCompileScopes');
- moduleType[_NG_MOD_DEF].transitiveCompileScopes = null;
- }
- else {
- _depsTracker.clearScopeCacheFor(moduleType);
- }
- });
- }
- }
- const moduleToScope = new Map();
- const getScopeOfModule = (moduleType) => {
- if (!moduleToScope.has(moduleType)) {
- const isTestingModule = isTestingModuleOverride(moduleType);
- const realType = isTestingModule ? this.testModuleType : moduleType;
- moduleToScope.set(moduleType, _transitiveScopesFor(realType));
- }
- return moduleToScope.get(moduleType);
- };
- this.componentToModuleScope.forEach((moduleType, componentType) => {
- if (moduleType !== null) {
- const moduleScope = getScopeOfModule(moduleType);
- this.storeFieldOfDefOnType(componentType, _NG_COMP_DEF, 'directiveDefs');
- this.storeFieldOfDefOnType(componentType, _NG_COMP_DEF, 'pipeDefs');
- _patchComponentDefWithScope(getComponentDef(componentType), moduleScope);
- }
- // `tView` that is stored on component def contains information about directives and pipes
- // that are in the scope of this component. Patching component scope will cause `tView` to be
- // changed. Store original `tView` before patching scope, so the `tView` (including scope
- // information) is restored back to its previous/original state before running next test.
- // Resetting `tView` is also needed for cases when we apply provider overrides and those
- // providers are defined on component's level, in which case they may end up included into
- // `tView.blueprint`.
- this.storeFieldOfDefOnType(componentType, _NG_COMP_DEF, 'tView');
- });
- this.componentToModuleScope.clear();
- }
- applyProviderOverrides() {
- const maybeApplyOverrides = (field) => (type) => {
- const resolver = field === _NG_COMP_DEF ? this.resolvers.component : this.resolvers.directive;
- const metadata = resolver.resolve(type);
- if (this.hasProviderOverrides(metadata.providers)) {
- this.patchDefWithProviderOverrides(type, field);
- }
- };
- this.seenComponents.forEach(maybeApplyOverrides(_NG_COMP_DEF));
- this.seenDirectives.forEach(maybeApplyOverrides(_NG_DIR_DEF));
- this.seenComponents.clear();
- this.seenDirectives.clear();
- }
- /**
- * Applies provider overrides to a given type (either an NgModule or a standalone component)
- * and all imported NgModules and standalone components recursively.
- */
- applyProviderOverridesInScope(type) {
- const hasScope = isStandaloneComponent(type) || isNgModule(type);
- // The function can be re-entered recursively while inspecting dependencies
- // of an NgModule or a standalone component. Exit early if we come across a
- // type that can not have a scope (directive or pipe) or the type is already
- // processed earlier.
- if (!hasScope || this.scopesWithOverriddenProviders.has(type)) {
- return;
- }
- this.scopesWithOverriddenProviders.add(type);
- // NOTE: the line below triggers JIT compilation of the module injector,
- // which also invokes verification of the NgModule semantics, which produces
- // detailed error messages. The fact that the code relies on this line being
- // present here is suspicious and should be refactored in a way that the line
- // below can be moved (for ex. after an early exit check below).
- const injectorDef = type[_NG_INJ_DEF];
- // No provider overrides, exit early.
- if (this.providerOverridesByToken.size === 0)
- return;
- if (isStandaloneComponent(type)) {
- // Visit all component dependencies and override providers there.
- const def = getComponentDef(type);
- const dependencies = maybeUnwrapFn(def.dependencies ?? []);
- for (const dependency of dependencies) {
- this.applyProviderOverridesInScope(dependency);
- }
- }
- else {
- const providers = [
- ...injectorDef.providers,
- ...(this.providerOverridesByModule.get(type) || []),
- ];
- if (this.hasProviderOverrides(providers)) {
- this.maybeStoreNgDef(_NG_INJ_DEF, type);
- this.storeFieldOfDefOnType(type, _NG_INJ_DEF, 'providers');
- injectorDef.providers = this.getOverriddenProviders(providers);
- }
- // Apply provider overrides to imported modules recursively
- const moduleDef = type[_NG_MOD_DEF];
- const imports = maybeUnwrapFn(moduleDef.imports);
- for (const importedModule of imports) {
- this.applyProviderOverridesInScope(importedModule);
- }
- // Also override the providers on any ModuleWithProviders imports since those don't appear in
- // the moduleDef.
- for (const importedModule of flatten(injectorDef.imports)) {
- if (isModuleWithProviders(importedModule)) {
- this.defCleanupOps.push({
- object: importedModule,
- fieldName: 'providers',
- originalValue: importedModule.providers,
- });
- importedModule.providers = this.getOverriddenProviders(importedModule.providers);
- }
- }
- }
- }
- patchComponentsWithExistingStyles() {
- this.existingComponentStyles.forEach((styles, type) => (type[_NG_COMP_DEF].styles = styles));
- this.existingComponentStyles.clear();
- }
- queueTypeArray(arr, moduleType) {
- for (const value of arr) {
- if (Array.isArray(value)) {
- this.queueTypeArray(value, moduleType);
- }
- else {
- this.queueType(value, moduleType);
- }
- }
- }
- recompileNgModule(ngModule, metadata) {
- // Cache the initial ngModuleDef as it will be overwritten.
- this.maybeStoreNgDef(_NG_MOD_DEF, ngModule);
- this.maybeStoreNgDef(_NG_INJ_DEF, ngModule);
- _compileNgModuleDefs(ngModule, metadata);
- }
- maybeRegisterComponentWithAsyncMetadata(type) {
- const asyncMetadataFn = _getAsyncClassMetadataFn(type);
- if (asyncMetadataFn) {
- this.componentsWithAsyncMetadata.add(type);
- }
- }
- queueType(type, moduleType) {
- // If this is a component with async metadata (i.e. a component with a `@defer` block
- // in a template) - store it for future processing.
- this.maybeRegisterComponentWithAsyncMetadata(type);
- const component = this.resolvers.component.resolve(type);
- if (component) {
- // Check whether a give Type has respective NG def (ɵcmp) and compile if def is
- // missing. That might happen in case a class without any Angular decorators extends another
- // class where Component/Directive/Pipe decorator is defined.
- if (_isComponentDefPendingResolution(type) || !type.hasOwnProperty(_NG_COMP_DEF)) {
- this.pendingComponents.add(type);
- }
- this.seenComponents.add(type);
- // Keep track of the module which declares this component, so later the component's scope
- // can be set correctly. If the component has already been recorded here, then one of several
- // cases is true:
- // * the module containing the component was imported multiple times (common).
- // * the component is declared in multiple modules (which is an error).
- // * the component was in 'declarations' of the testing module, and also in an imported module
- // in which case the module scope will be TestingModuleOverride.DECLARATION.
- // * overrideTemplateUsingTestingModule was called for the component in which case the module
- // scope will be TestingModuleOverride.OVERRIDE_TEMPLATE.
- //
- // If the component was previously in the testing module's 'declarations' (meaning the
- // current value is TestingModuleOverride.DECLARATION), then `moduleType` is the component's
- // real module, which was imported. This pattern is understood to mean that the component
- // should use its original scope, but that the testing module should also contain the
- // component in its scope.
- if (!this.componentToModuleScope.has(type) ||
- this.componentToModuleScope.get(type) === TestingModuleOverride.DECLARATION) {
- this.componentToModuleScope.set(type, moduleType);
- }
- return;
- }
- const directive = this.resolvers.directive.resolve(type);
- if (directive) {
- if (!type.hasOwnProperty(_NG_DIR_DEF)) {
- this.pendingDirectives.add(type);
- }
- this.seenDirectives.add(type);
- return;
- }
- const pipe = this.resolvers.pipe.resolve(type);
- if (pipe && !type.hasOwnProperty(_NG_PIPE_DEF)) {
- this.pendingPipes.add(type);
- return;
- }
- }
- queueTypesFromModulesArray(arr) {
- // Because we may encounter the same NgModule or a standalone Component while processing
- // the dependencies of an NgModule or a standalone Component, we cache them in this set so we
- // can skip ones that have already been seen encountered. In some test setups, this caching
- // resulted in 10X runtime improvement.
- const processedDefs = new Set();
- const queueTypesFromModulesArrayRecur = (arr) => {
- for (const value of arr) {
- if (Array.isArray(value)) {
- queueTypesFromModulesArrayRecur(value);
- }
- else if (hasNgModuleDef(value)) {
- const def = value.ɵmod;
- if (processedDefs.has(def)) {
- continue;
- }
- processedDefs.add(def);
- // Look through declarations, imports, and exports, and queue
- // everything found there.
- this.queueTypeArray(maybeUnwrapFn(def.declarations), value);
- queueTypesFromModulesArrayRecur(maybeUnwrapFn(def.imports));
- queueTypesFromModulesArrayRecur(maybeUnwrapFn(def.exports));
- }
- else if (isModuleWithProviders(value)) {
- queueTypesFromModulesArrayRecur([value.ngModule]);
- }
- else if (isStandaloneComponent(value)) {
- this.queueType(value, null);
- const def = getComponentDef(value);
- if (processedDefs.has(def)) {
- continue;
- }
- processedDefs.add(def);
- const dependencies = maybeUnwrapFn(def.dependencies ?? []);
- dependencies.forEach((dependency) => {
- // Note: in AOT, the `dependencies` might also contain regular
- // (NgModule-based) Component, Directive and Pipes, so we handle
- // them separately and proceed with recursive process for standalone
- // Components and NgModules only.
- if (isStandaloneComponent(dependency) || hasNgModuleDef(dependency)) {
- queueTypesFromModulesArrayRecur([dependency]);
- }
- else {
- this.queueType(dependency, null);
- }
- });
- }
- }
- };
- queueTypesFromModulesArrayRecur(arr);
- }
- // When module overrides (via `TestBed.overrideModule`) are present, it might affect all modules
- // that import (even transitively) an overridden one. For all affected modules we need to
- // recalculate their scopes for a given test run and restore original scopes at the end. The goal
- // of this function is to collect all affected modules in a set for further processing. Example:
- // if we have the following module hierarchy: A -> B -> C (where `->` means `imports`) and module
- // `C` is overridden, we consider `A` and `B` as affected, since their scopes might become
- // invalidated with the override.
- collectModulesAffectedByOverrides(arr) {
- const seenModules = new Set();
- const affectedModules = new Set();
- const calcAffectedModulesRecur = (arr, path) => {
- for (const value of arr) {
- if (Array.isArray(value)) {
- // If the value is an array, just flatten it (by invoking this function recursively),
- // keeping "path" the same.
- calcAffectedModulesRecur(value, path);
- }
- else if (hasNgModuleDef(value)) {
- if (seenModules.has(value)) {
- // If we've seen this module before and it's included into "affected modules" list, mark
- // the whole path that leads to that module as affected, but do not descend into its
- // imports, since we already examined them before.
- if (affectedModules.has(value)) {
- path.forEach((item) => affectedModules.add(item));
- }
- continue;
- }
- seenModules.add(value);
- if (this.overriddenModules.has(value)) {
- path.forEach((item) => affectedModules.add(item));
- }
- // Examine module imports recursively to look for overridden modules.
- const moduleDef = value[_NG_MOD_DEF];
- calcAffectedModulesRecur(maybeUnwrapFn(moduleDef.imports), path.concat(value));
- }
- }
- };
- calcAffectedModulesRecur(arr, []);
- return affectedModules;
- }
- /**
- * Preserve an original def (such as ɵmod, ɵinj, etc) before applying an override.
- * Note: one class may have multiple defs (for example: ɵmod and ɵinj in case of
- * an NgModule). If there is a def in a set already, don't override it, since
- * an original one should be restored at the end of a test.
- */
- maybeStoreNgDef(prop, type) {
- if (!this.initialNgDefs.has(type)) {
- this.initialNgDefs.set(type, new Map());
- }
- const currentDefs = this.initialNgDefs.get(type);
- if (!currentDefs.has(prop)) {
- const currentDef = Object.getOwnPropertyDescriptor(type, prop);
- currentDefs.set(prop, currentDef);
- }
- }
- storeFieldOfDefOnType(type, defField, fieldName) {
- const def = type[defField];
- const originalValue = def[fieldName];
- this.defCleanupOps.push({ object: def, fieldName, originalValue });
- }
- /**
- * Clears current components resolution queue, but stores the state of the queue, so we can
- * restore it later. Clearing the queue is required before we try to compile components (via
- * `TestBed.compileComponents`), so that component defs are in sync with the resolution queue.
- */
- clearComponentResolutionQueue() {
- if (this.originalComponentResolutionQueue === null) {
- this.originalComponentResolutionQueue = new Map();
- }
- _clearResolutionOfComponentResourcesQueue().forEach((value, key) => this.originalComponentResolutionQueue.set(key, value));
- }
- /*
- * Restores component resolution queue to the previously saved state. This operation is performed
- * as a part of restoring the state after completion of the current set of tests (that might
- * potentially mutate the state).
- */
- restoreComponentResolutionQueue() {
- if (this.originalComponentResolutionQueue !== null) {
- _restoreComponentResolutionQueue(this.originalComponentResolutionQueue);
- this.originalComponentResolutionQueue = null;
- }
- }
- restoreOriginalState() {
- // Process cleanup ops in reverse order so the field's original value is restored correctly (in
- // case there were multiple overrides for the same field).
- forEachRight(this.defCleanupOps, (op) => {
- op.object[op.fieldName] = op.originalValue;
- });
- // Restore initial component/directive/pipe defs
- this.initialNgDefs.forEach((defs, type) => {
- if (_USE_RUNTIME_DEPS_TRACKER_FOR_JIT) {
- _depsTracker.clearScopeCacheFor(type);
- }
- defs.forEach((descriptor, prop) => {
- if (!descriptor) {
- // Delete operations are generally undesirable since they have performance
- // implications on objects they were applied to. In this particular case, situations
- // where this code is invoked should be quite rare to cause any noticeable impact,
- // since it's applied only to some test cases (for example when class with no
- // annotations extends some @Component) when we need to clear 'ɵcmp' field on a given
- // class to restore its original state (before applying overrides and running tests).
- delete type[prop];
- }
- else {
- Object.defineProperty(type, prop, descriptor);
- }
- });
- });
- this.initialNgDefs.clear();
- this.scopesWithOverriddenProviders.clear();
- this.restoreComponentResolutionQueue();
- // Restore the locale ID to the default value, this shouldn't be necessary but we never know
- _setLocaleId(_DEFAULT_LOCALE_ID);
- }
- compileTestModule() {
- class RootScopeModule {
- }
- _compileNgModuleDefs(RootScopeModule, {
- providers: [
- ...this.rootProviderOverrides,
- _internalProvideZoneChangeDetection({}),
- TestBedApplicationErrorHandler,
- { provide: _ChangeDetectionScheduler, useExisting: _ChangeDetectionSchedulerImpl },
- ],
- });
- const providers = [
- { provide: Compiler, useFactory: () => new R3TestCompiler(this) },
- { provide: _DEFER_BLOCK_CONFIG, useValue: { behavior: this.deferBlockBehavior } },
- {
- provide: _INTERNAL_APPLICATION_ERROR_HANDLER,
- useFactory: () => {
- if (this.rethrowApplicationTickErrors) {
- const handler = inject$1(TestBedApplicationErrorHandler);
- return (e) => {
- handler.handleError(e);
- };
- }
- else {
- const userErrorHandler = inject$1(ErrorHandler);
- const ngZone = inject$1(NgZone);
- return (e) => ngZone.runOutsideAngular(() => userErrorHandler.handleError(e));
- }
- },
- },
- ...this.providers,
- ...this.providerOverrides,
- ];
- const imports = [RootScopeModule, this.additionalModuleTypes, this.imports || []];
- _compileNgModuleDefs(this.testModuleType, {
- declarations: this.declarations,
- imports,
- schemas: this.schemas,
- providers,
- },
- /* allowDuplicateDeclarationsInRoot */ true);
- this.applyProviderOverridesInScope(this.testModuleType);
- }
- get injector() {
- if (this._injector !== null) {
- return this._injector;
- }
- const providers = [];
- const compilerOptions = this.platform.injector.get(COMPILER_OPTIONS, []);
- compilerOptions.forEach((opts) => {
- if (opts.providers) {
- providers.push(opts.providers);
- }
- });
- if (this.compilerProviders !== null) {
- providers.push(...this.compilerProviders);
- }
- this._injector = Injector.create({ providers, parent: this.platform.injector });
- return this._injector;
- }
- // get overrides for a specific provider (if any)
- getSingleProviderOverrides(provider) {
- const token = getProviderToken(provider);
- return this.providerOverridesByToken.get(token) || null;
- }
- getProviderOverrides(providers) {
- if (!providers || !providers.length || this.providerOverridesByToken.size === 0)
- return [];
- // There are two flattening operations here. The inner flattenProviders() operates on the
- // metadata's providers and applies a mapping function which retrieves overrides for each
- // incoming provider. The outer flatten() then flattens the produced overrides array. If this is
- // not done, the array can contain other empty arrays (e.g. `[[], []]`) which leak into the
- // providers array and contaminate any error messages that might be generated.
- return flatten(flattenProviders(providers, (provider) => this.getSingleProviderOverrides(provider) || []));
- }
- getOverriddenProviders(providers) {
- if (!providers || !providers.length || this.providerOverridesByToken.size === 0)
- return [];
- const flattenedProviders = flattenProviders(providers);
- const overrides = this.getProviderOverrides(flattenedProviders);
- const overriddenProviders = [...flattenedProviders, ...overrides];
- const final = [];
- const seenOverriddenProviders = new Set();
- // We iterate through the list of providers in reverse order to make sure provider overrides
- // take precedence over the values defined in provider list. We also filter out all providers
- // that have overrides, keeping overridden values only. This is needed, since presence of a
- // provider with `ngOnDestroy` hook will cause this hook to be registered and invoked later.
- forEachRight(overriddenProviders, (provider) => {
- const token = getProviderToken(provider);
- if (this.providerOverridesByToken.has(token)) {
- if (!seenOverriddenProviders.has(token)) {
- seenOverriddenProviders.add(token);
- // Treat all overridden providers as `{multi: false}` (even if it's a multi-provider) to
- // make sure that provided override takes highest precedence and is not combined with
- // other instances of the same multi provider.
- final.unshift({ ...provider, multi: false });
- }
- }
- else {
- final.unshift(provider);
- }
- });
- return final;
- }
- hasProviderOverrides(providers) {
- return this.getProviderOverrides(providers).length > 0;
- }
- patchDefWithProviderOverrides(declaration, field) {
- const def = declaration[field];
- if (def && def.providersResolver) {
- this.maybeStoreNgDef(field, declaration);
- const resolver = def.providersResolver;
- const processProvidersFn = (providers) => this.getOverriddenProviders(providers);
- this.storeFieldOfDefOnType(declaration, field, 'providersResolver');
- def.providersResolver = (ngDef) => resolver(ngDef, processProvidersFn);
- }
- }
- }
- function initResolvers() {
- return {
- module: new NgModuleResolver(),
- component: new ComponentResolver(),
- directive: new DirectiveResolver(),
- pipe: new PipeResolver(),
- };
- }
- function isStandaloneComponent(value) {
- const def = getComponentDef(value);
- return !!def?.standalone;
- }
- function getComponentDef(value) {
- return value.ɵcmp ?? null;
- }
- function hasNgModuleDef(value) {
- return value.hasOwnProperty('ɵmod');
- }
- function isNgModule(value) {
- return hasNgModuleDef(value);
- }
- function maybeUnwrapFn(maybeFn) {
- return maybeFn instanceof Function ? maybeFn() : maybeFn;
- }
- function flatten(values) {
- const out = [];
- values.forEach((value) => {
- if (Array.isArray(value)) {
- out.push(...flatten(value));
- }
- else {
- out.push(value);
- }
- });
- return out;
- }
- function identityFn(value) {
- return value;
- }
- function flattenProviders(providers, mapFn = identityFn) {
- const out = [];
- for (let provider of providers) {
- if (_isEnvironmentProviders(provider)) {
- provider = provider.ɵproviders;
- }
- if (Array.isArray(provider)) {
- out.push(...flattenProviders(provider, mapFn));
- }
- else {
- out.push(mapFn(provider));
- }
- }
- return out;
- }
- function getProviderField(provider, field) {
- return provider && typeof provider === 'object' && provider[field];
- }
- function getProviderToken(provider) {
- return getProviderField(provider, 'provide') || provider;
- }
- function isModuleWithProviders(value) {
- return value.hasOwnProperty('ngModule');
- }
- function forEachRight(values, fn) {
- for (let idx = values.length - 1; idx >= 0; idx--) {
- fn(values[idx], idx);
- }
- }
- function invalidTypeError(name, expectedType) {
- return new Error(`${name} class doesn't have @${expectedType} decorator or is missing metadata.`);
- }
- class R3TestCompiler {
- testBed;
- constructor(testBed) {
- this.testBed = testBed;
- }
- compileModuleSync(moduleType) {
- this.testBed._compileNgModuleSync(moduleType);
- return new _NgModuleFactory(moduleType);
- }
- async compileModuleAsync(moduleType) {
- await this.testBed._compileNgModuleAsync(moduleType);
- return new _NgModuleFactory(moduleType);
- }
- compileModuleAndAllComponentsSync(moduleType) {
- const ngModuleFactory = this.compileModuleSync(moduleType);
- const componentFactories = this.testBed._getComponentFactories(moduleType);
- return new ModuleWithComponentFactories(ngModuleFactory, componentFactories);
- }
- async compileModuleAndAllComponentsAsync(moduleType) {
- const ngModuleFactory = await this.compileModuleAsync(moduleType);
- const componentFactories = this.testBed._getComponentFactories(moduleType);
- return new ModuleWithComponentFactories(ngModuleFactory, componentFactories);
- }
- clearCache() { }
- clearCacheFor(type) { }
- getModuleId(moduleType) {
- const meta = this.testBed._getModuleResolver().resolve(moduleType);
- return (meta && meta.id) || undefined;
- }
- }
- // The formatter and CI disagree on how this import statement should be formatted. Both try to keep
- // it on one line, too, which has gotten very hard to read & manage. So disable the formatter for
- // this statement only.
- let _nextRootElementId = 0;
- /**
- * Returns a singleton of the `TestBed` class.
- *
- * @publicApi
- */
- function getTestBed() {
- return TestBedImpl.INSTANCE;
- }
- /**
- * @description
- * Configures and initializes environment for unit testing and provides methods for
- * creating components and services in unit tests.
- *
- * TestBed is the primary api for writing unit tests for Angular applications and libraries.
- */
- class TestBedImpl {
- static _INSTANCE = null;
- static get INSTANCE() {
- return (TestBedImpl._INSTANCE = TestBedImpl._INSTANCE || new TestBedImpl());
- }
- /**
- * Teardown options that have been configured at the environment level.
- * Used as a fallback if no instance-level options have been provided.
- */
- static _environmentTeardownOptions;
- /**
- * "Error on unknown elements" option that has been configured at the environment level.
- * Used as a fallback if no instance-level option has been provided.
- */
- static _environmentErrorOnUnknownElementsOption;
- /**
- * "Error on unknown properties" option that has been configured at the environment level.
- * Used as a fallback if no instance-level option has been provided.
- */
- static _environmentErrorOnUnknownPropertiesOption;
- /**
- * Teardown options that have been configured at the `TestBed` instance level.
- * These options take precedence over the environment-level ones.
- */
- _instanceTeardownOptions;
- /**
- * Defer block behavior option that specifies whether defer blocks will be triggered manually
- * or set to play through.
- */
- _instanceDeferBlockBehavior = DEFER_BLOCK_DEFAULT_BEHAVIOR;
- /**
- * "Error on unknown elements" option that has been configured at the `TestBed` instance level.
- * This option takes precedence over the environment-level one.
- */
- _instanceErrorOnUnknownElementsOption;
- /**
- * "Error on unknown properties" option that has been configured at the `TestBed` instance level.
- * This option takes precedence over the environment-level one.
- */
- _instanceErrorOnUnknownPropertiesOption;
- /**
- * Stores the previous "Error on unknown elements" option value,
- * allowing to restore it in the reset testing module logic.
- */
- _previousErrorOnUnknownElementsOption;
- /**
- * Stores the previous "Error on unknown properties" option value,
- * allowing to restore it in the reset testing module logic.
- */
- _previousErrorOnUnknownPropertiesOption;
- /**
- * Initialize the environment for testing with a compiler factory, a PlatformRef, and an
- * angular module. These are common to every test in the suite.
- *
- * This may only be called once, to set up the common providers for the current test
- * suite on the current platform. If you absolutely need to change the providers,
- * first use `resetTestEnvironment`.
- *
- * Test modules and platforms for individual platforms are available from
- * '@angular/<platform_name>/testing'.
- *
- * @publicApi
- */
- static initTestEnvironment(ngModule, platform, options) {
- const testBed = TestBedImpl.INSTANCE;
- testBed.initTestEnvironment(ngModule, platform, options);
- return testBed;
- }
- /**
- * Reset the providers for the test injector.
- *
- * @publicApi
- */
- static resetTestEnvironment() {
- TestBedImpl.INSTANCE.resetTestEnvironment();
- }
- static configureCompiler(config) {
- return TestBedImpl.INSTANCE.configureCompiler(config);
- }
- /**
- * Allows overriding default providers, directives, pipes, modules of the test injector,
- * which are defined in test_injector.js
- */
- static configureTestingModule(moduleDef) {
- return TestBedImpl.INSTANCE.configureTestingModule(moduleDef);
- }
- /**
- * Compile components with a `templateUrl` for the test's NgModule.
- * It is necessary to call this function
- * as fetching urls is asynchronous.
- */
- static compileComponents() {
- return TestBedImpl.INSTANCE.compileComponents();
- }
- static overrideModule(ngModule, override) {
- return TestBedImpl.INSTANCE.overrideModule(ngModule, override);
- }
- static overrideComponent(component, override) {
- return TestBedImpl.INSTANCE.overrideComponent(component, override);
- }
- static overrideDirective(directive, override) {
- return TestBedImpl.INSTANCE.overrideDirective(directive, override);
- }
- static overridePipe(pipe, override) {
- return TestBedImpl.INSTANCE.overridePipe(pipe, override);
- }
- static overrideTemplate(component, template) {
- return TestBedImpl.INSTANCE.overrideTemplate(component, template);
- }
- /**
- * Overrides the template of the given component, compiling the template
- * in the context of the TestingModule.
- *
- * Note: This works for JIT and AOTed components as well.
- */
- static overrideTemplateUsingTestingModule(component, template) {
- return TestBedImpl.INSTANCE.overrideTemplateUsingTestingModule(component, template);
- }
- static overrideProvider(token, provider) {
- return TestBedImpl.INSTANCE.overrideProvider(token, provider);
- }
- static inject(token, notFoundValue, flags) {
- return TestBedImpl.INSTANCE.inject(token, notFoundValue, _convertToBitFlags(flags));
- }
- /** @deprecated from v9.0.0 use TestBed.inject */
- static get(token, notFoundValue = Injector.THROW_IF_NOT_FOUND, flags = InjectFlags.Default) {
- return TestBedImpl.INSTANCE.inject(token, notFoundValue, flags);
- }
- /**
- * Runs the given function in the `EnvironmentInjector` context of `TestBed`.
- *
- * @see {@link EnvironmentInjector#runInContext}
- */
- static runInInjectionContext(fn) {
- return TestBedImpl.INSTANCE.runInInjectionContext(fn);
- }
- static createComponent(component) {
- return TestBedImpl.INSTANCE.createComponent(component);
- }
- static resetTestingModule() {
- return TestBedImpl.INSTANCE.resetTestingModule();
- }
- static execute(tokens, fn, context) {
- return TestBedImpl.INSTANCE.execute(tokens, fn, context);
- }
- static get platform() {
- return TestBedImpl.INSTANCE.platform;
- }
- static get ngModule() {
- return TestBedImpl.INSTANCE.ngModule;
- }
- static flushEffects() {
- return TestBedImpl.INSTANCE.flushEffects();
- }
- // Properties
- platform = null;
- ngModule = null;
- _compiler = null;
- _testModuleRef = null;
- _activeFixtures = [];
- /**
- * Internal-only flag to indicate whether a module
- * scoping queue has been checked and flushed already.
- * @nodoc
- */
- globalCompilationChecked = false;
- /**
- * Initialize the environment for testing with a compiler factory, a PlatformRef, and an
- * angular module. These are common to every test in the suite.
- *
- * This may only be called once, to set up the common providers for the current test
- * suite on the current platform. If you absolutely need to change the providers,
- * first use `resetTestEnvironment`.
- *
- * Test modules and platforms for individual platforms are available from
- * '@angular/<platform_name>/testing'.
- *
- * @publicApi
- */
- initTestEnvironment(ngModule, platform, options) {
- if (this.platform || this.ngModule) {
- throw new Error('Cannot set base providers because it has already been called');
- }
- TestBedImpl._environmentTeardownOptions = options?.teardown;
- TestBedImpl._environmentErrorOnUnknownElementsOption = options?.errorOnUnknownElements;
- TestBedImpl._environmentErrorOnUnknownPropertiesOption = options?.errorOnUnknownProperties;
- this.platform = platform;
- this.ngModule = ngModule;
- this._compiler = new TestBedCompiler(this.platform, this.ngModule);
- // TestBed does not have an API which can reliably detect the start of a test, and thus could be
- // used to track the state of the NgModule registry and reset it correctly. Instead, when we
- // know we're in a testing scenario, we disable the check for duplicate NgModule registration
- // completely.
- _setAllowDuplicateNgModuleIdsForTest(true);
- }
- /**
- * Reset the providers for the test injector.
- *
- * @publicApi
- */
- resetTestEnvironment() {
- this.resetTestingModule();
- this._compiler = null;
- this.platform = null;
- this.ngModule = null;
- TestBedImpl._environmentTeardownOptions = undefined;
- _setAllowDuplicateNgModuleIdsForTest(false);
- }
- resetTestingModule() {
- this.checkGlobalCompilationFinished();
- _resetCompiledComponents();
- if (this._compiler !== null) {
- this.compiler.restoreOriginalState();
- }
- this._compiler = new TestBedCompiler(this.platform, this.ngModule);
- // Restore the previous value of the "error on unknown elements" option
- _setUnknownElementStrictMode(this._previousErrorOnUnknownElementsOption ?? THROW_ON_UNKNOWN_ELEMENTS_DEFAULT);
- // Restore the previous value of the "error on unknown properties" option
- _setUnknownPropertyStrictMode(this._previousErrorOnUnknownPropertiesOption ?? THROW_ON_UNKNOWN_PROPERTIES_DEFAULT);
- // We have to chain a couple of try/finally blocks, because each step can
- // throw errors and we don't want it to interrupt the next step and we also
- // want an error to be thrown at the end.
- try {
- this.destroyActiveFixtures();
- }
- finally {
- try {
- if (this.shouldTearDownTestingModule()) {
- this.tearDownTestingModule();
- }
- }
- finally {
- this._testModuleRef = null;
- this._instanceTeardownOptions = undefined;
- this._instanceErrorOnUnknownElementsOption = undefined;
- this._instanceErrorOnUnknownPropertiesOption = undefined;
- this._instanceDeferBlockBehavior = DEFER_BLOCK_DEFAULT_BEHAVIOR;
- }
- }
- return this;
- }
- configureCompiler(config) {
- if (config.useJit != null) {
- throw new Error('JIT compiler is not configurable via TestBed APIs.');
- }
- if (config.providers !== undefined) {
- this.compiler.setCompilerProviders(config.providers);
- }
- return this;
- }
- configureTestingModule(moduleDef) {
- this.assertNotInstantiated('TestBed.configureTestingModule', 'configure the test module');
- // Trigger module scoping queue flush before executing other TestBed operations in a test.
- // This is needed for the first test invocation to ensure that globally declared modules have
- // their components scoped properly. See the `checkGlobalCompilationFinished` function
- // description for additional info.
- this.checkGlobalCompilationFinished();
- // Always re-assign the options, even if they're undefined.
- // This ensures that we don't carry them between tests.
- this._instanceTeardownOptions = moduleDef.teardown;
- this._instanceErrorOnUnknownElementsOption = moduleDef.errorOnUnknownElements;
- this._instanceErrorOnUnknownPropertiesOption = moduleDef.errorOnUnknownProperties;
- this._instanceDeferBlockBehavior = moduleDef.deferBlockBehavior ?? DEFER_BLOCK_DEFAULT_BEHAVIOR;
- // Store the current value of the strict mode option,
- // so we can restore it later
- this._previousErrorOnUnknownElementsOption = _getUnknownElementStrictMode();
- _setUnknownElementStrictMode(this.shouldThrowErrorOnUnknownElements());
- this._previousErrorOnUnknownPropertiesOption = _getUnknownPropertyStrictMode();
- _setUnknownPropertyStrictMode(this.shouldThrowErrorOnUnknownProperties());
- this.compiler.configureTestingModule(moduleDef);
- return this;
- }
- compileComponents() {
- return this.compiler.compileComponents();
- }
- inject(token, notFoundValue, flags) {
- if (token === TestBed) {
- return this;
- }
- const UNDEFINED = {};
- const result = this.testModuleRef.injector.get(token, UNDEFINED, _convertToBitFlags(flags));
- return result === UNDEFINED
- ? this.compiler.injector.get(token, notFoundValue, flags)
- : result;
- }
- /** @deprecated from v9.0.0 use TestBed.inject */
- get(token, notFoundValue = Injector.THROW_IF_NOT_FOUND, flags = InjectFlags.Default) {
- return this.inject(token, notFoundValue, flags);
- }
- runInInjectionContext(fn) {
- return runInInjectionContext(this.inject(EnvironmentInjector), fn);
- }
- execute(tokens, fn, context) {
- const params = tokens.map((t) => this.inject(t));
- return fn.apply(context, params);
- }
- overrideModule(ngModule, override) {
- this.assertNotInstantiated('overrideModule', 'override module metadata');
- this.compiler.overrideModule(ngModule, override);
- return this;
- }
- overrideComponent(component, override) {
- this.assertNotInstantiated('overrideComponent', 'override component metadata');
- this.compiler.overrideComponent(component, override);
- return this;
- }
- overrideTemplateUsingTestingModule(component, template) {
- this.assertNotInstantiated('TestBed.overrideTemplateUsingTestingModule', 'Cannot override template when the test module has already been instantiated');
- this.compiler.overrideTemplateUsingTestingModule(component, template);
- return this;
- }
- overrideDirective(directive, override) {
- this.assertNotInstantiated('overrideDirective', 'override directive metadata');
- this.compiler.overrideDirective(directive, override);
- return this;
- }
- overridePipe(pipe, override) {
- this.assertNotInstantiated('overridePipe', 'override pipe metadata');
- this.compiler.overridePipe(pipe, override);
- return this;
- }
- /**
- * Overwrites all providers for the given token with the given provider definition.
- */
- overrideProvider(token, provider) {
- this.assertNotInstantiated('overrideProvider', 'override provider');
- this.compiler.overrideProvider(token, provider);
- return this;
- }
- overrideTemplate(component, template) {
- return this.overrideComponent(component, { set: { template, templateUrl: null } });
- }
- createComponent(type) {
- const testComponentRenderer = this.inject(TestComponentRenderer);
- const rootElId = `root${_nextRootElementId++}`;
- testComponentRenderer.insertRootElement(rootElId);
- if (_getAsyncClassMetadataFn(type)) {
- throw new Error(`Component '${type.name}' has unresolved metadata. ` +
- `Please call \`await TestBed.compileComponents()\` before running this test.`);
- }
- const componentDef = type.ɵcmp;
- if (!componentDef) {
- throw new Error(`It looks like '${_stringify(type)}' has not been compiled.`);
- }
- const componentFactory = new _Render3ComponentFactory(componentDef);
- const initComponent = () => {
- const componentRef = componentFactory.create(Injector.NULL, [], `#${rootElId}`, this.testModuleRef);
- return this.runInInjectionContext(() => new ComponentFixture(componentRef));
- };
- const noNgZone = this.inject(ComponentFixtureNoNgZone, false);
- const ngZone = noNgZone ? null : this.inject(NgZone, null);
- const fixture = ngZone ? ngZone.run(initComponent) : initComponent();
- this._activeFixtures.push(fixture);
- return fixture;
- }
- /**
- * @internal strip this from published d.ts files due to
- * https://github.com/microsoft/TypeScript/issues/36216
- */
- get compiler() {
- if (this._compiler === null) {
- throw new Error(`Need to call TestBed.initTestEnvironment() first`);
- }
- return this._compiler;
- }
- /**
- * @internal strip this from published d.ts files due to
- * https://github.com/microsoft/TypeScript/issues/36216
- */
- get testModuleRef() {
- if (this._testModuleRef === null) {
- this._testModuleRef = this.compiler.finalize();
- }
- return this._testModuleRef;
- }
- assertNotInstantiated(methodName, methodDescription) {
- if (this._testModuleRef !== null) {
- throw new Error(`Cannot ${methodDescription} when the test module has already been instantiated. ` +
- `Make sure you are not using \`inject\` before \`${methodName}\`.`);
- }
- }
- /**
- * Check whether the module scoping queue should be flushed, and flush it if needed.
- *
- * When the TestBed is reset, it clears the JIT module compilation queue, cancelling any
- * in-progress module compilation. This creates a potential hazard - the very first time the
- * TestBed is initialized (or if it's reset without being initialized), there may be pending
- * compilations of modules declared in global scope. These compilations should be finished.
- *
- * To ensure that globally declared modules have their components scoped properly, this function
- * is called whenever TestBed is initialized or reset. The _first_ time that this happens, prior
- * to any other operations, the scoping queue is flushed.
- */
- checkGlobalCompilationFinished() {
- // Checking _testNgModuleRef is null should not be necessary, but is left in as an additional
- // guard that compilations queued in tests (after instantiation) are never flushed accidentally.
- if (!this.globalCompilationChecked && this._testModuleRef === null) {
- _flushModuleScopingQueueAsMuchAsPossible();
- }
- this.globalCompilationChecked = true;
- }
- destroyActiveFixtures() {
- let errorCount = 0;
- this._activeFixtures.forEach((fixture) => {
- try {
- fixture.destroy();
- }
- catch (e) {
- errorCount++;
- console.error('Error during cleanup of component', {
- component: fixture.componentInstance,
- stacktrace: e,
- });
- }
- });
- this._activeFixtures = [];
- if (errorCount > 0 && this.shouldRethrowTeardownErrors()) {
- throw Error(`${errorCount} ${errorCount === 1 ? 'component' : 'components'} ` +
- `threw errors during cleanup`);
- }
- }
- shouldRethrowTeardownErrors() {
- const instanceOptions = this._instanceTeardownOptions;
- const environmentOptions = TestBedImpl._environmentTeardownOptions;
- // If the new teardown behavior hasn't been configured, preserve the old behavior.
- if (!instanceOptions && !environmentOptions) {
- return TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT;
- }
- // Otherwise use the configured behavior or default to rethrowing.
- return (instanceOptions?.rethrowErrors ??
- environmentOptions?.rethrowErrors ??
- this.shouldTearDownTestingModule());
- }
- shouldThrowErrorOnUnknownElements() {
- // Check if a configuration has been provided to throw when an unknown element is found
- return (this._instanceErrorOnUnknownElementsOption ??
- TestBedImpl._environmentErrorOnUnknownElementsOption ??
- THROW_ON_UNKNOWN_ELEMENTS_DEFAULT);
- }
- shouldThrowErrorOnUnknownProperties() {
- // Check if a configuration has been provided to throw when an unknown property is found
- return (this._instanceErrorOnUnknownPropertiesOption ??
- TestBedImpl._environmentErrorOnUnknownPropertiesOption ??
- THROW_ON_UNKNOWN_PROPERTIES_DEFAULT);
- }
- shouldTearDownTestingModule() {
- return (this._instanceTeardownOptions?.destroyAfterEach ??
- TestBedImpl._environmentTeardownOptions?.destroyAfterEach ??
- TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT);
- }
- getDeferBlockBehavior() {
- return this._instanceDeferBlockBehavior;
- }
- tearDownTestingModule() {
- // If the module ref has already been destroyed, we won't be able to get a test renderer.
- if (this._testModuleRef === null) {
- return;
- }
- // Resolve the renderer ahead of time, because we want to remove the root elements as the very
- // last step, but the injector will be destroyed as a part of the module ref destruction.
- const testRenderer = this.inject(TestComponentRenderer);
- try {
- this._testModuleRef.destroy();
- }
- catch (e) {
- if (this.shouldRethrowTeardownErrors()) {
- throw e;
- }
- else {
- console.error('Error during cleanup of a testing module', {
- component: this._testModuleRef.instance,
- stacktrace: e,
- });
- }
- }
- finally {
- testRenderer.removeAllRootElements?.();
- }
- }
- /**
- * Execute any pending effects.
- *
- * @developerPreview
- */
- flushEffects() {
- this.inject(_MicrotaskEffectScheduler).flush();
- this.inject(_EffectScheduler).flush();
- }
- }
- /**
- * @description
- * Configures and initializes environment for unit testing and provides methods for
- * creating components and services in unit tests.
- *
- * `TestBed` is the primary api for writing unit tests for Angular applications and libraries.
- *
- * @publicApi
- */
- const TestBed = TestBedImpl;
- /**
- * Allows injecting dependencies in `beforeEach()` and `it()`. Note: this function
- * (imported from the `@angular/core/testing` package) can **only** be used to inject dependencies
- * in tests. To inject dependencies in your application code, use the [`inject`](api/core/inject)
- * function from the `@angular/core` package instead.
- *
- * Example:
- *
- * ```ts
- * beforeEach(inject([Dependency, AClass], (dep, object) => {
- * // some code that uses `dep` and `object`
- * // ...
- * }));
- *
- * it('...', inject([AClass], (object) => {
- * object.doSomething();
- * expect(...);
- * })
- * ```
- *
- * @publicApi
- */
- function inject(tokens, fn) {
- const testBed = TestBedImpl.INSTANCE;
- // Not using an arrow function to preserve context passed from call site
- return function () {
- return testBed.execute(tokens, fn, this);
- };
- }
- /**
- * @publicApi
- */
- class InjectSetupWrapper {
- _moduleDef;
- constructor(_moduleDef) {
- this._moduleDef = _moduleDef;
- }
- _addModule() {
- const moduleDef = this._moduleDef();
- if (moduleDef) {
- TestBedImpl.configureTestingModule(moduleDef);
- }
- }
- inject(tokens, fn) {
- const self = this;
- // Not using an arrow function to preserve context passed from call site
- return function () {
- self._addModule();
- return inject(tokens, fn).call(this);
- };
- }
- }
- function withModule(moduleDef, fn) {
- if (fn) {
- // Not using an arrow function to preserve context passed from call site
- return function () {
- const testBed = TestBedImpl.INSTANCE;
- if (moduleDef) {
- testBed.configureTestingModule(moduleDef);
- }
- return fn.apply(this);
- };
- }
- return new InjectSetupWrapper(() => moduleDef);
- }
- /**
- * Public Test Library for unit testing Angular applications. Assumes that you are running
- * with Jasmine, Mocha, or a similar framework which exports a beforeEach function and
- * allows tests to be asynchronous by either returning a promise or using a 'done' parameter.
- */
- // Reset the test providers and the fake async zone before each test.
- // We keep a guard because somehow this file can make it into a bundle and be executed
- // beforeEach is only defined when executing the tests
- globalThis.beforeEach?.(getCleanupHook(false));
- // We provide both a `beforeEach` and `afterEach`, because the updated behavior for
- // tearing down the module is supposed to run after the test so that we can associate
- // teardown errors with the correct test.
- // We keep a guard because somehow this file can make it into a bundle and be executed
- // afterEach is only defined when executing the tests
- globalThis.afterEach?.(getCleanupHook(true));
- function getCleanupHook(expectedTeardownValue) {
- return () => {
- const testBed = TestBedImpl.INSTANCE;
- if (testBed.shouldTearDownTestingModule() === expectedTeardownValue) {
- testBed.resetTestingModule();
- resetFakeAsyncZoneIfExists();
- }
- };
- }
- /**
- * This API should be removed. But doing so seems to break `google3` and so it requires a bit of
- * investigation.
- *
- * A work around is to mark it as `@codeGenApi` for now and investigate later.
- *
- * @codeGenApi
- */
- // TODO(iminar): Remove this code in a safe way.
- const __core_private_testing_placeholder__ = '';
- /**
- * Fake implementation of user agent history and navigation behavior. This is a
- * high-fidelity implementation of browser behavior that attempts to emulate
- * things like traversal delay.
- */
- class FakeNavigation {
- window;
- /**
- * The fake implementation of an entries array. Only same-document entries
- * allowed.
- */
- entriesArr = [];
- /**
- * The current active entry index into `entriesArr`.
- */
- currentEntryIndex = 0;
- /**
- * The current navigate event.
- * @internal
- */
- navigateEvent = null;
- /**
- * A Map of pending traversals, so that traversals to the same entry can be
- * re-used.
- */
- traversalQueue = new Map();
- /**
- * A Promise that resolves when the previous traversals have finished. Used to
- * simulate the cross-process communication necessary for traversals.
- */
- nextTraversal = Promise.resolve();
- /**
- * A prospective current active entry index, which includes unresolved
- * traversals. Used by `go` to determine where navigations are intended to go.
- */
- prospectiveEntryIndex = 0;
- /**
- * A test-only option to make traversals synchronous, rather than emulate
- * cross-process communication.
- */
- synchronousTraversals = false;
- /** Whether to allow a call to setInitialEntryForTesting. */
- canSetInitialEntry = true;
- /**
- * `EventTarget` to dispatch events.
- * @internal
- */
- eventTarget;
- /** The next unique id for created entries. Replace recreates this id. */
- nextId = 0;
- /** The next unique key for created entries. Replace inherits this id. */
- nextKey = 0;
- /** Whether this fake is disposed. */
- disposed = false;
- /** Equivalent to `navigation.currentEntry`. */
- get currentEntry() {
- return this.entriesArr[this.currentEntryIndex];
- }
- get canGoBack() {
- return this.currentEntryIndex > 0;
- }
- get canGoForward() {
- return this.currentEntryIndex < this.entriesArr.length - 1;
- }
- constructor(window, startURL) {
- this.window = window;
- this.eventTarget = this.window.document.createElement('div');
- // First entry.
- this.setInitialEntryForTesting(startURL);
- }
- /**
- * Sets the initial entry.
- */
- setInitialEntryForTesting(url, options = { historyState: null }) {
- if (!this.canSetInitialEntry) {
- throw new Error('setInitialEntryForTesting can only be called before any ' + 'navigation has occurred');
- }
- const currentInitialEntry = this.entriesArr[0];
- this.entriesArr[0] = new FakeNavigationHistoryEntry(this.window.document.createElement('div'), new URL(url).toString(), {
- index: 0,
- key: currentInitialEntry?.key ?? String(this.nextKey++),
- id: currentInitialEntry?.id ?? String(this.nextId++),
- sameDocument: true,
- historyState: options?.historyState,
- state: options.state,
- });
- }
- /** Returns whether the initial entry is still eligible to be set. */
- canSetInitialEntryForTesting() {
- return this.canSetInitialEntry;
- }
- /**
- * Sets whether to emulate traversals as synchronous rather than
- * asynchronous.
- */
- setSynchronousTraversalsForTesting(synchronousTraversals) {
- this.synchronousTraversals = synchronousTraversals;
- }
- /** Equivalent to `navigation.entries()`. */
- entries() {
- return this.entriesArr.slice();
- }
- /** Equivalent to `navigation.navigate()`. */
- navigate(url, options) {
- const fromUrl = new URL(this.currentEntry.url);
- const toUrl = new URL(url, this.currentEntry.url);
- let navigationType;
- if (!options?.history || options.history === 'auto') {
- // Auto defaults to push, but if the URLs are the same, is a replace.
- if (fromUrl.toString() === toUrl.toString()) {
- navigationType = 'replace';
- }
- else {
- navigationType = 'push';
- }
- }
- else {
- navigationType = options.history;
- }
- const hashChange = isHashChange(fromUrl, toUrl);
- const destination = new FakeNavigationDestination({
- url: toUrl.toString(),
- state: options?.state,
- sameDocument: hashChange,
- historyState: null,
- });
- const result = new InternalNavigationResult(this);
- this.userAgentNavigate(destination, result, {
- navigationType,
- cancelable: true,
- canIntercept: true,
- // Always false for navigate().
- userInitiated: false,
- hashChange,
- info: options?.info,
- });
- return {
- committed: result.committed,
- finished: result.finished,
- };
- }
- /** Equivalent to `history.pushState()`. */
- pushState(data, title, url) {
- this.pushOrReplaceState('push', data, title, url);
- }
- /** Equivalent to `history.replaceState()`. */
- replaceState(data, title, url) {
- this.pushOrReplaceState('replace', data, title, url);
- }
- pushOrReplaceState(navigationType, data, _title, url) {
- const fromUrl = new URL(this.currentEntry.url);
- const toUrl = url ? new URL(url, this.currentEntry.url) : fromUrl;
- const hashChange = isHashChange(fromUrl, toUrl);
- const destination = new FakeNavigationDestination({
- url: toUrl.toString(),
- sameDocument: true,
- historyState: data,
- });
- const result = new InternalNavigationResult(this);
- this.userAgentNavigate(destination, result, {
- navigationType,
- cancelable: true,
- canIntercept: true,
- // Always false for pushState() or replaceState().
- userInitiated: false,
- hashChange,
- });
- }
- /** Equivalent to `navigation.traverseTo()`. */
- traverseTo(key, options) {
- const fromUrl = new URL(this.currentEntry.url);
- const entry = this.findEntry(key);
- if (!entry) {
- const domException = new DOMException('Invalid key', 'InvalidStateError');
- const committed = Promise.reject(domException);
- const finished = Promise.reject(domException);
- committed.catch(() => { });
- finished.catch(() => { });
- return {
- committed,
- finished,
- };
- }
- if (entry === this.currentEntry) {
- return {
- committed: Promise.resolve(this.currentEntry),
- finished: Promise.resolve(this.currentEntry),
- };
- }
- if (this.traversalQueue.has(entry.key)) {
- const existingResult = this.traversalQueue.get(entry.key);
- return {
- committed: existingResult.committed,
- finished: existingResult.finished,
- };
- }
- const hashChange = isHashChange(fromUrl, new URL(entry.url, this.currentEntry.url));
- const destination = new FakeNavigationDestination({
- url: entry.url,
- state: entry.getState(),
- historyState: entry.getHistoryState(),
- key: entry.key,
- id: entry.id,
- index: entry.index,
- sameDocument: entry.sameDocument,
- });
- this.prospectiveEntryIndex = entry.index;
- const result = new InternalNavigationResult(this);
- this.traversalQueue.set(entry.key, result);
- this.runTraversal(() => {
- this.traversalQueue.delete(entry.key);
- const event = this.userAgentNavigate(destination, result, {
- navigationType: 'traverse',
- cancelable: true,
- canIntercept: true,
- // Always false for traverseTo().
- userInitiated: false,
- hashChange,
- info: options?.info,
- });
- // Note this does not pay attention at all to the commit status of the event (and thus, does not support deferred commit for traversals)
- this.userAgentTraverse(event);
- });
- return {
- committed: result.committed,
- finished: result.finished,
- };
- }
- /** Equivalent to `navigation.back()`. */
- back(options) {
- if (this.currentEntryIndex === 0) {
- const domException = new DOMException('Cannot go back', 'InvalidStateError');
- const committed = Promise.reject(domException);
- const finished = Promise.reject(domException);
- committed.catch(() => { });
- finished.catch(() => { });
- return {
- committed,
- finished,
- };
- }
- const entry = this.entriesArr[this.currentEntryIndex - 1];
- return this.traverseTo(entry.key, options);
- }
- /** Equivalent to `navigation.forward()`. */
- forward(options) {
- if (this.currentEntryIndex === this.entriesArr.length - 1) {
- const domException = new DOMException('Cannot go forward', 'InvalidStateError');
- const committed = Promise.reject(domException);
- const finished = Promise.reject(domException);
- committed.catch(() => { });
- finished.catch(() => { });
- return {
- committed,
- finished,
- };
- }
- const entry = this.entriesArr[this.currentEntryIndex + 1];
- return this.traverseTo(entry.key, options);
- }
- /**
- * Equivalent to `history.go()`.
- * Note that this method does not actually work precisely to how Chrome
- * does, instead choosing a simpler model with less unexpected behavior.
- * Chrome has a few edge case optimizations, for instance with repeated
- * `back(); forward()` chains it collapses certain traversals.
- */
- go(direction) {
- const targetIndex = this.prospectiveEntryIndex + direction;
- if (targetIndex >= this.entriesArr.length || targetIndex < 0) {
- return;
- }
- this.prospectiveEntryIndex = targetIndex;
- this.runTraversal(() => {
- // Check again that destination is in the entries array.
- if (targetIndex >= this.entriesArr.length || targetIndex < 0) {
- return;
- }
- const fromUrl = new URL(this.currentEntry.url);
- const entry = this.entriesArr[targetIndex];
- const hashChange = isHashChange(fromUrl, new URL(entry.url, this.currentEntry.url));
- const destination = new FakeNavigationDestination({
- url: entry.url,
- state: entry.getState(),
- historyState: entry.getHistoryState(),
- key: entry.key,
- id: entry.id,
- index: entry.index,
- sameDocument: entry.sameDocument,
- });
- const result = new InternalNavigationResult(this);
- const event = this.userAgentNavigate(destination, result, {
- navigationType: 'traverse',
- cancelable: true,
- canIntercept: true,
- // Always false for go().
- userInitiated: false,
- hashChange,
- });
- // Note this does not pay attention at all to the commit status of the event (and thus, does not support deferred commit for traversals)
- this.userAgentTraverse(event);
- });
- }
- /** Runs a traversal synchronously or asynchronously */
- runTraversal(traversal) {
- if (this.synchronousTraversals) {
- traversal();
- return;
- }
- // Each traversal occupies a single timeout resolution.
- // This means that Promises added to commit and finish should resolve
- // before the next traversal.
- this.nextTraversal = this.nextTraversal.then(() => {
- return new Promise((resolve) => {
- setTimeout(() => {
- resolve();
- traversal();
- });
- });
- });
- }
- /** Equivalent to `navigation.addEventListener()`. */
- addEventListener(type, callback, options) {
- this.eventTarget.addEventListener(type, callback, options);
- }
- /** Equivalent to `navigation.removeEventListener()`. */
- removeEventListener(type, callback, options) {
- this.eventTarget.removeEventListener(type, callback, options);
- }
- /** Equivalent to `navigation.dispatchEvent()` */
- dispatchEvent(event) {
- return this.eventTarget.dispatchEvent(event);
- }
- /** Cleans up resources. */
- dispose() {
- // Recreate eventTarget to release current listeners.
- // `document.createElement` because NodeJS `EventTarget` is incompatible with Domino's `Event`.
- this.eventTarget = this.window.document.createElement('div');
- this.disposed = true;
- }
- /** Returns whether this fake is disposed. */
- isDisposed() {
- return this.disposed;
- }
- /** Implementation for all navigations and traversals. */
- userAgentNavigate(destination, result, options) {
- // The first navigation should disallow any future calls to set the initial
- // entry.
- this.canSetInitialEntry = false;
- if (this.navigateEvent) {
- this.navigateEvent.cancel(new DOMException('Navigation was aborted', 'AbortError'));
- this.navigateEvent = null;
- }
- return dispatchNavigateEvent({
- navigationType: options.navigationType,
- cancelable: options.cancelable,
- canIntercept: options.canIntercept,
- userInitiated: options.userInitiated,
- hashChange: options.hashChange,
- signal: result.signal,
- destination,
- info: options.info,
- sameDocument: destination.sameDocument,
- result,
- });
- }
- /**
- * Implementation to commit a navigation.
- * https://whatpr.org/html/10919/nav-history-apis.html#navigateevent-commit
- * @internal
- */
- commitNavigateEvent(navigateEvent) {
- navigateEvent.interceptionState = 'committed';
- const from = this.currentEntry;
- if (!from) {
- throw new Error('cannot commit navigation when current entry is null');
- }
- if (!navigateEvent.sameDocument) {
- const error = new Error('Cannot navigate to a non-same-document URL.');
- navigateEvent.cancel(error);
- throw error;
- }
- // "If navigationType is "push" or "replace", then run the URL and history update steps given document and event's destination's URL, with serialiedData set to event's classic history API state and historyHandling set to navigationType."
- if (navigateEvent.navigationType === 'push' || navigateEvent.navigationType === 'replace') {
- this.urlAndHistoryUpdateSteps(navigateEvent);
- }
- else if (navigateEvent.navigationType === 'reload') {
- this.updateNavigationEntriesForSameDocumentNavigation(navigateEvent);
- }
- else ;
- }
- /**
- * Implementation for a push or replace navigation.
- * https://whatpr.org/html/10919/browsing-the-web.html#url-and-history-update-steps
- * https://whatpr.org/html/10919/nav-history-apis.html#update-the-navigation-api-entries-for-a-same-document-navigation
- */
- urlAndHistoryUpdateSteps(navigateEvent) {
- this.updateNavigationEntriesForSameDocumentNavigation(navigateEvent);
- }
- /**
- * Implementation for a traverse navigation.
- *
- * https://whatpr.org/html/10919/browsing-the-web.html#apply-the-traverse-history-step
- * ...
- * > 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.
- * > If targetEntry's document is equal to displayedDocument, then perform updateDocument.
- * https://whatpr.org/html/10919/browsing-the-web.html#update-document-for-history-step-application
- * which then goes to https://whatpr.org/html/10919/nav-history-apis.html#update-the-navigation-api-entries-for-a-same-document-navigation
- */
- userAgentTraverse(navigateEvent) {
- this.updateNavigationEntriesForSameDocumentNavigation(navigateEvent);
- // Happens as part of "updating the document" steps https://whatpr.org/html/10919/browsing-the-web.html#updating-the-document
- const popStateEvent = createPopStateEvent({
- state: navigateEvent.destination.getHistoryState(),
- });
- this.window.dispatchEvent(popStateEvent);
- // TODO(atscott): If oldURL's fragment is not equal to entry's URL's fragment, then queue a global task to fire an event named hashchange
- }
- /** https://whatpr.org/html/10919/nav-history-apis.html#update-the-navigation-api-entries-for-a-same-document-navigation */
- updateNavigationEntriesForSameDocumentNavigation({ destination, navigationType, result, }) {
- const oldCurrentNHE = this.currentEntry;
- const disposedNHEs = [];
- if (navigationType === 'traverse') {
- this.currentEntryIndex = destination.index;
- if (this.currentEntryIndex === -1) {
- throw new Error('unexpected current entry index');
- }
- }
- else if (navigationType === 'push') {
- this.currentEntryIndex++;
- this.prospectiveEntryIndex = this.currentEntryIndex; // prospectiveEntryIndex isn't in the spec but is an implementation detail
- disposedNHEs.push(...this.entriesArr.splice(this.currentEntryIndex));
- }
- else if (navigationType === 'replace') {
- disposedNHEs.push(oldCurrentNHE);
- }
- if (navigationType === 'push' || navigationType === 'replace') {
- const index = this.currentEntryIndex;
- const key = navigationType === 'push' ? String(this.nextKey++) : this.currentEntry.key;
- const newNHE = new FakeNavigationHistoryEntry(this.window.document.createElement('div'), destination.url, {
- id: String(this.nextId++),
- key,
- index,
- sameDocument: true,
- state: destination.getState(),
- historyState: destination.getHistoryState(),
- });
- this.entriesArr[this.currentEntryIndex] = newNHE;
- }
- result.committedResolve(this.currentEntry);
- const currentEntryChangeEvent = createFakeNavigationCurrentEntryChangeEvent({
- from: oldCurrentNHE,
- navigationType: navigationType,
- });
- this.eventTarget.dispatchEvent(currentEntryChangeEvent);
- for (const disposedNHE of disposedNHEs) {
- disposedNHE.dispose();
- }
- }
- /** Utility method for finding entries with the given `key`. */
- findEntry(key) {
- for (const entry of this.entriesArr) {
- if (entry.key === key)
- return entry;
- }
- return undefined;
- }
- set onnavigate(
- // tslint:disable-next-line:no-any
- _handler) {
- throw new Error('unimplemented');
- }
- // tslint:disable-next-line:no-any
- get onnavigate() {
- throw new Error('unimplemented');
- }
- set oncurrententrychange(_handler) {
- throw new Error('unimplemented');
- }
- get oncurrententrychange() {
- throw new Error('unimplemented');
- }
- set onnavigatesuccess(
- // tslint:disable-next-line:no-any
- _handler) {
- throw new Error('unimplemented');
- }
- // tslint:disable-next-line:no-any
- get onnavigatesuccess() {
- throw new Error('unimplemented');
- }
- set onnavigateerror(
- // tslint:disable-next-line:no-any
- _handler) {
- throw new Error('unimplemented');
- }
- // tslint:disable-next-line:no-any
- get onnavigateerror() {
- throw new Error('unimplemented');
- }
- _transition = null;
- /** @internal */
- set transition(t) {
- this._transition = t;
- }
- get transition() {
- return this._transition;
- }
- updateCurrentEntry(_options) {
- throw new Error('unimplemented');
- }
- reload(_options) {
- throw new Error('unimplemented');
- }
- }
- /**
- * Fake equivalent of `NavigationHistoryEntry`.
- */
- class FakeNavigationHistoryEntry {
- eventTarget;
- url;
- sameDocument;
- id;
- key;
- index;
- state;
- historyState;
- // tslint:disable-next-line:no-any
- ondispose = null;
- constructor(eventTarget, url, { id, key, index, sameDocument, state, historyState, }) {
- this.eventTarget = eventTarget;
- this.url = url;
- this.id = id;
- this.key = key;
- this.index = index;
- this.sameDocument = sameDocument;
- this.state = state;
- this.historyState = historyState;
- }
- getState() {
- // Budget copy.
- return this.state ? JSON.parse(JSON.stringify(this.state)) : this.state;
- }
- getHistoryState() {
- // Budget copy.
- return this.historyState
- ? JSON.parse(JSON.stringify(this.historyState))
- : this.historyState;
- }
- addEventListener(type, callback, options) {
- this.eventTarget.addEventListener(type, callback, options);
- }
- removeEventListener(type, callback, options) {
- this.eventTarget.removeEventListener(type, callback, options);
- }
- dispatchEvent(event) {
- return this.eventTarget.dispatchEvent(event);
- }
- /** internal */
- dispose() {
- const disposeEvent = new Event('disposed');
- this.dispatchEvent(disposeEvent);
- // release current listeners
- this.eventTarget = null;
- }
- }
- /**
- * Create a fake equivalent of `NavigateEvent`. This is not a class because ES5
- * transpiled JavaScript cannot extend native Event.
- *
- * https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigate-event-firing
- */
- function dispatchNavigateEvent({ cancelable, canIntercept, userInitiated, hashChange, navigationType, signal, destination, info, sameDocument, result, }) {
- const { navigation } = result;
- const event = new Event('navigate', { bubbles: false, cancelable });
- event.focusResetBehavior = null;
- event.scrollBehavior = null;
- event.interceptionState = 'none';
- event.canIntercept = canIntercept;
- event.userInitiated = userInitiated;
- event.hashChange = hashChange;
- event.navigationType = navigationType;
- event.signal = signal;
- event.destination = destination;
- event.info = info;
- event.downloadRequest = null;
- event.formData = null;
- event.result = result;
- event.sameDocument = sameDocument;
- event.commitOption = 'immediate';
- let handlersFinished = [Promise.resolve()];
- let dispatchedNavigateEvent = false;
- event.intercept = function (options) {
- if (!this.canIntercept) {
- throw new DOMException(`Cannot intercept when canIntercept is 'false'`, 'SecurityError');
- }
- this.interceptionState = 'intercepted';
- event.sameDocument = true;
- const handler = options?.handler;
- if (handler) {
- handlersFinished.push(handler());
- }
- // override old options with new ones. UA _may_ report a console warning if new options differ from previous
- event.commitOption = options?.commit ?? event.commitOption;
- event.scrollBehavior = options?.scroll ?? event.scrollBehavior;
- event.focusResetBehavior = options?.focusReset ?? event.focusResetBehavior;
- };
- event.scroll = function () {
- if (event.interceptionState !== 'committed') {
- throw new DOMException(`Failed to execute 'scroll' on 'NavigateEvent': scroll() must be ` +
- `called after commit() and interception options must specify manual scroll.`, 'InvalidStateError');
- }
- processScrollBehavior(event);
- };
- event.commit = function (internal = false) {
- if (!internal && this.interceptionState !== 'intercepted') {
- throw new DOMException(`Failed to execute 'commit' on 'NavigateEvent': intercept() must be ` +
- `called before commit() and commit() cannot be already called.`, 'InvalidStateError');
- }
- if (!internal && event.commitOption !== 'after-transition') {
- throw new DOMException(`Failed to execute 'commit' on 'NavigateEvent': commit() may not be ` +
- `called if commit behavior is not "after-transition",.`, 'InvalidStateError');
- }
- if (!dispatchedNavigateEvent) {
- throw new DOMException(`Failed to execute 'commit' on 'NavigateEvent': commit() may not be ` +
- `called during event dispatch.`, 'InvalidStateError');
- }
- this.interceptionState = 'committed';
- result.navigation.commitNavigateEvent(event);
- };
- // Internal only.
- event.cancel = function (reason) {
- result.committedReject(reason);
- result.finishedReject(reason);
- };
- function dispatch() {
- navigation.navigateEvent = event;
- navigation.eventTarget.dispatchEvent(event);
- dispatchedNavigateEvent = true;
- if (event.interceptionState !== 'none') {
- navigation.transition = new InternalNavigationTransition(navigation.currentEntry, navigationType);
- if (event.commitOption !== 'after-transition') {
- event.commit(/** internal */ true);
- }
- }
- else {
- // In the spec, this isn't really part of the navigate API. Instead, the navigate event firing returns "true" to indicate
- // navigation steps should "continue" (https://whatpr.org/html/10919/browsing-the-web.html#beginning-navigation)
- event.commit(/** internal */ true);
- }
- Promise.all(handlersFinished).then(() => {
- // Follows steps outlined under "Wait for all of promisesList, with the following success steps:"
- // in the spec https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigate-event-firing.
- if (result.signal.aborted) {
- return;
- }
- if (event !== navigation.navigateEvent) {
- throw new Error("Navigation's ongoing event not equal to resolved event");
- }
- navigation.navigateEvent = null;
- if (event.interceptionState === 'intercepted') {
- navigation.commitNavigateEvent(event);
- }
- finishNavigationEvent(event, true);
- const navigatesuccessEvent = new Event('navigatesuccess', { bubbles: false, cancelable });
- navigation.eventTarget.dispatchEvent(navigatesuccessEvent);
- result.finishedResolve();
- if (navigation.transition !== null) {
- navigation.transition.finishedResolve();
- }
- navigation.transition = null;
- }, (reason) => {
- if (result.signal.aborted) {
- return;
- }
- if (event !== navigation.navigateEvent) {
- throw new Error("Navigation's ongoing event not equal to resolved event");
- }
- navigation.navigateEvent = null;
- event.interceptionState = 'rejected'; // TODO(atscott): this is not in the spec https://github.com/whatwg/html/issues/11087
- finishNavigationEvent(event, false);
- const navigateerrorEvent = new Event('navigateerror', { bubbles: false, cancelable });
- navigation.eventTarget.dispatchEvent(navigateerrorEvent);
- result.finishedReject(reason);
- if (navigation.transition !== null) {
- navigation.transition.finishedResolve();
- }
- navigation.transition = null;
- });
- }
- dispatch();
- return event;
- }
- /** https://whatpr.org/html/10919/nav-history-apis.html#navigateevent-finish */
- function finishNavigationEvent(event, didFulfill) {
- if (event.interceptionState === 'intercepted' || event.interceptionState === 'finished') {
- throw new Error('Attempting to finish navigation event that was incomplete or already finished');
- }
- if (event.interceptionState === 'none') {
- return;
- }
- if (didFulfill) {
- // TODO(atscott): https://github.com/whatwg/html/issues/11087 focus reset is not guarded by didFulfill in the spec
- potentiallyResetFocus(event);
- potentiallyResetScroll(event);
- }
- event.interceptionState = 'finished';
- }
- /** https://whatpr.org/html/10919/nav-history-apis.html#potentially-reset-the-focus */
- function potentiallyResetFocus(event) {
- if (event.interceptionState !== 'committed' && event.interceptionState !== 'scrolled') {
- throw new Error('cannot reset focus if navigation event is not committed or scrolled');
- }
- // TODO(atscott): The rest of the steps
- }
- function potentiallyResetScroll(event) {
- if (event.interceptionState !== 'committed' && event.interceptionState !== 'scrolled') {
- throw new Error('cannot reset scroll if navigation event is not committed or scrolled');
- }
- if (event.interceptionState === 'scrolled' || event.scrollBehavior === 'manual') {
- return;
- }
- processScrollBehavior(event);
- }
- /* https://whatpr.org/html/10919/nav-history-apis.html#process-scroll-behavior */
- function processScrollBehavior(event) {
- if (event.interceptionState !== 'committed') {
- throw new Error('invalid event interception state when processing scroll behavior');
- }
- event.interceptionState = 'scrolled';
- // TODO(atscott): the rest of the steps
- }
- /**
- * Create a fake equivalent of `NavigationCurrentEntryChange`. This does not use
- * a class because ES5 transpiled JavaScript cannot extend native Event.
- */
- function createFakeNavigationCurrentEntryChangeEvent({ from, navigationType, }) {
- const event = new Event('currententrychange', {
- bubbles: false,
- cancelable: false,
- });
- event.from = from;
- event.navigationType = navigationType;
- return event;
- }
- /**
- * Create a fake equivalent of `PopStateEvent`. This does not use a class
- * because ES5 transpiled JavaScript cannot extend native Event.
- */
- function createPopStateEvent({ state }) {
- const event = new Event('popstate', {
- bubbles: false,
- cancelable: false,
- });
- event.state = state;
- return event;
- }
- /**
- * Fake equivalent of `NavigationDestination`.
- */
- class FakeNavigationDestination {
- url;
- sameDocument;
- key;
- id;
- index;
- state;
- historyState;
- constructor({ url, sameDocument, historyState, state, key = null, id = null, index = -1, }) {
- this.url = url;
- this.sameDocument = sameDocument;
- this.state = state;
- this.historyState = historyState;
- this.key = key;
- this.id = id;
- this.index = index;
- }
- getState() {
- return this.state;
- }
- getHistoryState() {
- return this.historyState;
- }
- }
- /** Utility function to determine whether two UrlLike have the same hash. */
- function isHashChange(from, to) {
- return (to.hash !== from.hash &&
- to.hostname === from.hostname &&
- to.pathname === from.pathname &&
- to.search === from.search);
- }
- class InternalNavigationTransition {
- from;
- navigationType;
- finished;
- finishedResolve;
- finishedReject;
- constructor(from, navigationType) {
- this.from = from;
- this.navigationType = navigationType;
- this.finished = new Promise((resolve, reject) => {
- this.finishedReject = reject;
- this.finishedResolve = resolve;
- });
- }
- }
- /**
- * Internal utility class for representing the result of a navigation.
- * Generally equivalent to the "apiMethodTracker" in the spec.
- */
- class InternalNavigationResult {
- navigation;
- committedTo = null;
- committedResolve;
- committedReject;
- finishedResolve;
- finishedReject;
- committed;
- finished;
- get signal() {
- return this.abortController.signal;
- }
- abortController = new AbortController();
- constructor(navigation) {
- this.navigation = navigation;
- this.committed = new Promise((resolve, reject) => {
- this.committedResolve = (entry) => {
- this.committedTo = entry;
- resolve(entry);
- };
- this.committedReject = reject;
- });
- this.finished = new Promise(async (resolve, reject) => {
- this.finishedResolve = () => {
- if (this.committedTo === null) {
- throw new Error('NavigateEvent should have been committed before resolving finished promise.');
- }
- resolve(this.committedTo);
- };
- this.finishedReject = (reason) => {
- reject(reason);
- this.abortController.abort(reason);
- };
- });
- // All rejections are handled.
- this.committed.catch(() => { });
- this.finished.catch(() => { });
- }
- }
- export { ComponentFixture, ComponentFixtureAutoDetect, ComponentFixtureNoNgZone, DeferBlockFixture, InjectSetupWrapper, TestBed, TestComponentRenderer, __core_private_testing_placeholder__, discardPeriodicTasks, fakeAsync, flush, flushMicrotasks, getTestBed, inject, resetFakeAsyncZone, tick, waitForAsync, withModule, FakeNavigation as ɵFakeNavigation, MetadataOverrider as ɵMetadataOverrider };
- //# sourceMappingURL=testing.mjs.map
|