UNPKG

21.2 kBTypeScriptView Raw
1/**
2 * @license Angular v13.0.2
3 * (c) 2010-2021 Google LLC. https://angular.io/
4 * License: MIT
5 */
6
7import { ChangeDetectorRef } from '@angular/core';
8import { Compiler } from '@angular/core';
9import { CompilerOptions } from '@angular/core';
10import { Component } from '@angular/core';
11import { ComponentFactory } from '@angular/core';
12import { ComponentRef } from '@angular/core';
13import { DebugElement } from '@angular/core';
14import { Directive } from '@angular/core';
15import { ElementRef } from '@angular/core';
16import * as i0 from '@angular/core';
17import { InjectFlags } from '@angular/core';
18import { InjectionToken } from '@angular/core';
19import { Injector } from '@angular/core';
20import { NgModule } from '@angular/core';
21import { NgZone } from '@angular/core';
22import { Pipe } from '@angular/core';
23import { PlatformRef } from '@angular/core';
24import { ProviderToken } from '@angular/core';
25import { SchemaMetadata } from '@angular/core';
26import { Type } from '@angular/core';
27
28/**
29 * This API should be removed. But doing so seems to break `google3` and so it requires a bit of
30 * investigation.
31 *
32 * A work around is to mark it as `@codeGenApi` for now and investigate later.
33 *
34 * @codeGenApi
35 */
36export declare const __core_private_testing_placeholder__ = "";
37
38/**
39 * @deprecated use `waitForAsync()`, (expected removal in v12)
40 * @see {@link waitForAsync}
41 * @publicApi
42 * */
43export declare function async(fn: Function): (done: any) => any;
44
45/**
46 * Fixture for debugging and testing a component.
47 *
48 * @publicApi
49 */
50export declare class ComponentFixture<T> {
51 componentRef: ComponentRef<T>;
52 ngZone: NgZone | null;
53 private _autoDetect;
54 /**
55 * The DebugElement associated with the root element of this component.
56 */
57 debugElement: DebugElement;
58 /**
59 * The instance of the root component class.
60 */
61 componentInstance: T;
62 /**
63 * The native element at the root of the component.
64 */
65 nativeElement: any;
66 /**
67 * The ElementRef for the element at the root of the component.
68 */
69 elementRef: ElementRef;
70 /**
71 * The ChangeDetectorRef for the component
72 */
73 changeDetectorRef: ChangeDetectorRef;
74 private _renderer;
75 private _isStable;
76 private _isDestroyed;
77 private _resolve;
78 private _promise;
79 private _onUnstableSubscription;
80 private _onStableSubscription;
81 private _onMicrotaskEmptySubscription;
82 private _onErrorSubscription;
83 constructor(componentRef: ComponentRef<T>, ngZone: NgZone | null, _autoDetect: boolean);
84 private _tick;
85 /**
86 * Trigger a change detection cycle for the component.
87 */
88 detectChanges(checkNoChanges?: boolean): void;
89 /**
90 * Do a change detection run to make sure there were no changes.
91 */
92 checkNoChanges(): void;
93 /**
94 * Set whether the fixture should autodetect changes.
95 *
96 * Also runs detectChanges once so that any existing change is detected.
97 */
98 autoDetectChanges(autoDetect?: boolean): void;
99 /**
100 * Return whether the fixture is currently stable or has async tasks that have not been completed
101 * yet.
102 */
103 isStable(): boolean;
104 /**
105 * Get a promise that resolves when the fixture is stable.
106 *
107 * This can be used to resume testing after events have triggered asynchronous activity or
108 * asynchronous change detection.
109 */
110 whenStable(): Promise<any>;
111 private _getRenderer;
112 /**
113 * Get a promise that resolves when the ui state is stable following animations.
114 */
115 whenRenderingDone(): Promise<any>;
116 /**
117 * Trigger component destruction.
118 */
119 destroy(): void;
120}
121
122/**
123 * @publicApi
124 */
125export declare const ComponentFixtureAutoDetect: InjectionToken<boolean[]>;
126
127/**
128 * @publicApi
129 */
130export declare const ComponentFixtureNoNgZone: InjectionToken<boolean[]>;
131
132/**
133 * Discard all remaining periodic tasks.
134 *
135 * @publicApi
136 */
137export declare function discardPeriodicTasks(): void;
138
139/**
140 * Wraps a function to be executed in the `fakeAsync` zone:
141 * - Microtasks are manually executed by calling `flushMicrotasks()`.
142 * - Timers are synchronous; `tick()` simulates the asynchronous passage of time.
143 *
144 * If there are any pending timers at the end of the function, an exception is thrown.
145 *
146 * Can be used to wrap `inject()` calls.
147 *
148 * @param fn The function that you want to wrap in the `fakeAysnc` zone.
149 *
150 * @usageNotes
151 * ### Example
152 *
153 * {@example core/testing/ts/fake_async.ts region='basic'}
154 *
155 *
156 * @returns The function wrapped to be executed in the `fakeAsync` zone.
157 * Any arguments passed when calling this returned function will be passed through to the `fn`
158 * function in the parameters when it is called.
159 *
160 * @publicApi
161 */
162export declare function fakeAsync(fn: Function): (...args: any[]) => any;
163
164/**
165 * Simulates the asynchronous passage of time for the timers in the `fakeAsync` zone by
166 * draining the macrotask queue until it is empty.
167 *
168 * @param maxTurns The maximum number of times the scheduler attempts to clear its queue before
169 * throwing an error.
170 * @returns The simulated time elapsed, in milliseconds.
171 *
172 * @publicApi
173 */
174export declare function flush(maxTurns?: number): number;
175
176/**
177 * Flush any pending microtasks.
178 *
179 * @publicApi
180 */
181export declare function flushMicrotasks(): void;
182
183/**
184 * Returns a singleton of the applicable `TestBed`.
185 *
186 * It will be either an instance of `TestBedViewEngine` or `TestBedRender3`.
187 *
188 * @publicApi
189 */
190export declare const getTestBed: () => TestBed;
191
192/**
193 * Allows injecting dependencies in `beforeEach()` and `it()`.
194 *
195 * Example:
196 *
197 * ```
198 * beforeEach(inject([Dependency, AClass], (dep, object) => {
199 * // some code that uses `dep` and `object`
200 * // ...
201 * }));
202 *
203 * it('...', inject([AClass], (object) => {
204 * object.doSomething();
205 * expect(...);
206 * })
207 * ```
208 *
209 * @publicApi
210 */
211export declare function inject(tokens: any[], fn: Function): () => any;
212
213/**
214 * @publicApi
215 */
216export declare class InjectSetupWrapper {
217 private _moduleDef;
218 constructor(_moduleDef: () => TestModuleMetadata);
219 private _addModule;
220 inject(tokens: any[], fn: Function): () => any;
221}
222
223
224/**
225 * Type used for modifications to metadata
226 *
227 * @publicApi
228 */
229export declare type MetadataOverride<T> = {
230 add?: Partial<T>;
231 remove?: Partial<T>;
232 set?: Partial<T>;
233};
234
235/**
236 * Configures the test module teardown behavior in `TestBed`.
237 * @publicApi
238 */
239export declare interface ModuleTeardownOptions {
240 /** Whether the test module should be destroyed after every test. */
241 destroyAfterEach: boolean;
242 /** Whether errors during test module destruction should be re-thrown. Defaults to `true`. */
243 rethrowErrors?: boolean;
244}
245
246/**
247 * Clears out the shared fake async zone for a test.
248 * To be called in a global `beforeEach`.
249 *
250 * @publicApi
251 */
252export declare function resetFakeAsyncZone(): void;
253
254/**
255 * @publicApi
256 */
257export declare interface TestBed {
258 platform: PlatformRef;
259 ngModule: Type<any> | Type<any>[];
260 /**
261 * Initialize the environment for testing with a compiler factory, a PlatformRef, and an
262 * angular module. These are common to every test in the suite.
263 *
264 * This may only be called once, to set up the common providers for the current test
265 * suite on the current platform. If you absolutely need to change the providers,
266 * first use `resetTestEnvironment`.
267 *
268 * Test modules and platforms for individual platforms are available from
269 * '@angular/<platform_name>/testing'.
270 */
271 initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, options?: TestEnvironmentOptions): void;
272 /**
273 * Initialize the environment for testing with a compiler factory, a PlatformRef, and an
274 * angular module. These are common to every test in the suite.
275 *
276 * This may only be called once, to set up the common providers for the current test
277 * suite on the current platform. If you absolutely need to change the providers,
278 * first use `resetTestEnvironment`.
279 *
280 * Test modules and platforms for individual platforms are available from
281 * '@angular/<platform_name>/testing'.
282 *
283 * @deprecated This API that allows providing AOT summaries is deprecated, since summary files are
284 * unused in Ivy.
285 */
286 initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]): void;
287 /**
288 * Reset the providers for the test injector.
289 */
290 resetTestEnvironment(): void;
291 resetTestingModule(): void;
292 configureCompiler(config: {
293 providers?: any[];
294 useJit?: boolean;
295 }): void;
296 configureTestingModule(moduleDef: TestModuleMetadata): void;
297 compileComponents(): Promise<any>;
298 inject<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T;
299 inject<T>(token: ProviderToken<T>, notFoundValue: null, flags?: InjectFlags): T | null;
300 /** @deprecated from v9.0.0 use TestBed.inject */
301 get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
302 /** @deprecated from v9.0.0 use TestBed.inject */
303 get(token: any, notFoundValue?: any): any;
304 execute(tokens: any[], fn: Function, context?: any): any;
305 overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): void;
306 overrideComponent(component: Type<any>, override: MetadataOverride<Component>): void;
307 overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): void;
308 overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): void;
309 /**
310 * Overwrites all providers for the given token with the given provider definition.
311 */
312 overrideProvider(token: any, provider: {
313 useFactory: Function;
314 deps: any[];
315 }): void;
316 overrideProvider(token: any, provider: {
317 useValue: any;
318 }): void;
319 overrideProvider(token: any, provider: {
320 useFactory?: Function;
321 useValue?: any;
322 deps?: any[];
323 }): void;
324 overrideTemplateUsingTestingModule(component: Type<any>, template: string): void;
325 createComponent<T>(component: Type<T>): ComponentFixture<T>;
326}
327
328/**
329 * @description
330 * Configures and initializes environment for unit testing and provides methods for
331 * creating components and services in unit tests.
332 *
333 * `TestBed` is the primary api for writing unit tests for Angular applications and libraries.
334 *
335 * Note: Use `TestBed` in tests. It will be set to either `TestBedViewEngine` or `TestBedRender3`
336 * according to the compiler used.
337 *
338 * @publicApi
339 */
340export declare const TestBed: TestBedStatic;
341
342/**
343 * Static methods implemented by the `TestBedViewEngine` and `TestBedRender3`
344 *
345 * @publicApi
346 */
347export declare interface TestBedStatic {
348 new (...args: any[]): TestBed;
349 /**
350 * Initialize the environment for testing with a compiler factory, a PlatformRef, and an
351 * angular module. These are common to every test in the suite.
352 *
353 * This may only be called once, to set up the common providers for the current test
354 * suite on the current platform. If you absolutely need to change the providers,
355 * first use `resetTestEnvironment`.
356 *
357 * Test modules and platforms for individual platforms are available from
358 * '@angular/<platform_name>/testing'.
359 */
360 initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, options?: TestEnvironmentOptions): TestBed;
361 /**
362 * Initialize the environment for testing with a compiler factory, a PlatformRef, and an
363 * angular module. These are common to every test in the suite.
364 *
365 * This may only be called once, to set up the common providers for the current test
366 * suite on the current platform. If you absolutely need to change the providers,
367 * first use `resetTestEnvironment`.
368 *
369 * Test modules and platforms for individual platforms are available from
370 * '@angular/<platform_name>/testing'.
371 *
372 * @deprecated This API that allows providing AOT summaries is deprecated, since summary files are
373 * unused in Ivy.
374 */
375 initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]): TestBed;
376 /**
377 * Reset the providers for the test injector.
378 */
379 resetTestEnvironment(): void;
380 resetTestingModule(): TestBedStatic;
381 /**
382 * Allows overriding default compiler providers and settings
383 * which are defined in test_injector.js
384 */
385 configureCompiler(config: {
386 providers?: any[];
387 useJit?: boolean;
388 }): TestBedStatic;
389 /**
390 * Allows overriding default providers, directives, pipes, modules of the test injector,
391 * which are defined in test_injector.js
392 */
393 configureTestingModule(moduleDef: TestModuleMetadata): TestBedStatic;
394 /**
395 * Compile components with a `templateUrl` for the test's NgModule.
396 * It is necessary to call this function
397 * as fetching urls is asynchronous.
398 */
399 compileComponents(): Promise<any>;
400 overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): TestBedStatic;
401 overrideComponent(component: Type<any>, override: MetadataOverride<Component>): TestBedStatic;
402 overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): TestBedStatic;
403 overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): TestBedStatic;
404 overrideTemplate(component: Type<any>, template: string): TestBedStatic;
405 /**
406 * Overrides the template of the given component, compiling the template
407 * in the context of the TestingModule.
408 *
409 * Note: This works for JIT and AOTed components as well.
410 */
411 overrideTemplateUsingTestingModule(component: Type<any>, template: string): TestBedStatic;
412 /**
413 * Overwrites all providers for the given token with the given provider definition.
414 *
415 * Note: This works for JIT and AOTed components as well.
416 */
417 overrideProvider(token: any, provider: {
418 useFactory: Function;
419 deps: any[];
420 }): TestBedStatic;
421 overrideProvider(token: any, provider: {
422 useValue: any;
423 }): TestBedStatic;
424 overrideProvider(token: any, provider: {
425 useFactory?: Function;
426 useValue?: any;
427 deps?: any[];
428 }): TestBedStatic;
429 inject<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T;
430 inject<T>(token: ProviderToken<T>, notFoundValue: null, flags?: InjectFlags): T | null;
431 /** @deprecated from v9.0.0 use TestBed.inject */
432 get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
433 /** @deprecated from v9.0.0 use TestBed.inject */
434 get(token: any, notFoundValue?: any): any;
435 createComponent<T>(component: Type<T>): ComponentFixture<T>;
436}
437
438/**
439 * An abstract class for inserting the root test component element in a platform independent way.
440 *
441 * @publicApi
442 */
443export declare class TestComponentRenderer {
444 insertRootElement(rootElementId: string): void;
445 removeAllRootElements?(): void;
446}
447
448/**
449 * @publicApi
450 */
451export declare interface TestEnvironmentOptions {
452 /**
453 * Provides a way to specify AOT summaries to use in TestBed.
454 * This parameter is unused and deprecated in Ivy.
455 *
456 * @deprecated With Ivy, AOT summary files are unused.
457 */
458 aotSummaries?: () => any[];
459 /**
460 * Configures the test module teardown behavior in `TestBed`.
461 */
462 teardown?: ModuleTeardownOptions;
463}
464
465/**
466 * @publicApi
467 */
468export declare type TestModuleMetadata = {
469 providers?: any[];
470 declarations?: any[];
471 imports?: any[];
472 schemas?: Array<SchemaMetadata | any[]>;
473 /**
474 * @deprecated With Ivy, AOT summary files are unused.
475 */
476 aotSummaries?: () => any[];
477 teardown?: ModuleTeardownOptions;
478};
479
480/**
481 * Simulates the asynchronous passage of time for the timers in the `fakeAsync` zone.
482 *
483 * The microtasks queue is drained at the very start of this function and after any timer callback
484 * has been executed.
485 *
486 * @param millis The number of milliseconds to advance the virtual timer.
487 * @param tickOptions The options to pass to the `tick()` function.
488 *
489 * @usageNotes
490 *
491 * The `tick()` option is a flag called `processNewMacroTasksSynchronously`,
492 * which determines whether or not to invoke new macroTasks.
493 *
494 * If you provide a `tickOptions` object, but do not specify a
495 * `processNewMacroTasksSynchronously` property (`tick(100, {})`),
496 * then `processNewMacroTasksSynchronously` defaults to true.
497 *
498 * If you omit the `tickOptions` parameter (`tick(100))`), then
499 * `tickOptions` defaults to `{processNewMacroTasksSynchronously: true}`.
500 *
501 * ### Example
502 *
503 * {@example core/testing/ts/fake_async.ts region='basic'}
504 *
505 * The following example includes a nested timeout (new macroTask), and
506 * the `tickOptions` parameter is allowed to default. In this case,
507 * `processNewMacroTasksSynchronously` defaults to true, and the nested
508 * function is executed on each tick.
509 *
510 * ```
511 * it ('test with nested setTimeout', fakeAsync(() => {
512 * let nestedTimeoutInvoked = false;
513 * function funcWithNestedTimeout() {
514 * setTimeout(() => {
515 * nestedTimeoutInvoked = true;
516 * });
517 * };
518 * setTimeout(funcWithNestedTimeout);
519 * tick();
520 * expect(nestedTimeoutInvoked).toBe(true);
521 * }));
522 * ```
523 *
524 * In the following case, `processNewMacroTasksSynchronously` is explicitly
525 * set to false, so the nested timeout function is not invoked.
526 *
527 * ```
528 * it ('test with nested setTimeout', fakeAsync(() => {
529 * let nestedTimeoutInvoked = false;
530 * function funcWithNestedTimeout() {
531 * setTimeout(() => {
532 * nestedTimeoutInvoked = true;
533 * });
534 * };
535 * setTimeout(funcWithNestedTimeout);
536 * tick(0, {processNewMacroTasksSynchronously: false});
537 * expect(nestedTimeoutInvoked).toBe(false);
538 * }));
539 * ```
540 *
541 *
542 * @publicApi
543 */
544export declare function tick(millis?: number, tickOptions?: {
545 processNewMacroTasksSynchronously: boolean;
546}): void;
547
548
549/**
550 * Wraps a test function in an asynchronous test zone. The test will automatically
551 * complete when all asynchronous calls within this zone are done. Can be used
552 * to wrap an {@link inject} call.
553 *
554 * Example:
555 *
556 * ```
557 * it('...', waitForAsync(inject([AClass], (object) => {
558 * object.doSomething.then(() => {
559 * expect(...);
560 * })
561 * });
562 * ```
563 *
564 * @publicApi
565 */
566export declare function waitForAsync(fn: Function): (done: any) => any;
567
568/**
569 * @publicApi
570 */
571export declare function withModule(moduleDef: TestModuleMetadata): InjectSetupWrapper;
572
573export declare function withModule(moduleDef: TestModuleMetadata, fn: Function): () => any;
574
575export declare class ɵMetadataOverrider {
576 private _references;
577 /**
578 * Creates a new instance for the given metadata class
579 * based on an old instance and overrides.
580 */
581 overrideMetadata<C extends T, T>(metadataClass: {
582 new (options: T): C;
583 }, oldMetadata: C, override: MetadataOverride<T>): C;
584}
585
586/**
587 * Special interface to the compiler only used by testing
588 *
589 * @publicApi
590 */
591export declare class ɵTestingCompiler extends Compiler {
592 get injector(): Injector;
593 overrideModule(module: Type<any>, overrides: MetadataOverride<NgModule>): void;
594 overrideDirective(directive: Type<any>, overrides: MetadataOverride<Directive>): void;
595 overrideComponent(component: Type<any>, overrides: MetadataOverride<Component>): void;
596 overridePipe(directive: Type<any>, overrides: MetadataOverride<Pipe>): void;
597 /**
598 * Allows to pass the compile summary from AOT compilation to the JIT compiler,
599 * so that it can use the code generated by AOT.
600 */
601 loadAotSummaries(summaries: () => any[]): void;
602 /**
603 * Gets the component factory for the given component.
604 * This assumes that the component has been compiled before calling this call using
605 * `compileModuleAndAllComponents*`.
606 */
607 getComponentFactory<T>(component: Type<T>): ComponentFactory<T>;
608 /**
609 * Returns the component type that is stored in the given error.
610 * This can be used for errors created by compileModule...
611 */
612 getComponentFromError(error: Error): Type<any> | null;
613 static ɵfac: i0.ɵɵFactoryDeclaration<ɵTestingCompiler, never>;
614 static ɵprov: i0.ɵɵInjectableDeclaration<ɵTestingCompiler>;
615}
616
617/**
618 * A factory for creating a Compiler
619 *
620 * @publicApi
621 */
622export declare abstract class ɵTestingCompilerFactory {
623 abstract createTestingCompiler(options?: CompilerOptions[]): ɵTestingCompiler;
624}
625
626export { }
627
\No newline at end of file