index.d.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /**
  2. * @license Angular v16.2.9
  3. * (c) 2010-2022 Google LLC. https://angular.io/
  4. * License: MIT
  5. */
  6. import { ChangeDetectorRef } from '@angular/core';
  7. import { Component } from '@angular/core';
  8. import { ComponentRef } from '@angular/core';
  9. import { DebugElement } from '@angular/core';
  10. import { Directive } from '@angular/core';
  11. import { ElementRef } from '@angular/core';
  12. import { InjectFlags } from '@angular/core';
  13. import { InjectionToken } from '@angular/core';
  14. import { InjectOptions } from '@angular/core';
  15. import { NgModule } from '@angular/core';
  16. import { NgZone } from '@angular/core';
  17. import { Pipe } from '@angular/core';
  18. import { PlatformRef } from '@angular/core';
  19. import { ProviderToken } from '@angular/core';
  20. import { SchemaMetadata } from '@angular/core';
  21. import { Type } from '@angular/core';
  22. /**
  23. * This API should be removed. But doing so seems to break `google3` and so it requires a bit of
  24. * investigation.
  25. *
  26. * A work around is to mark it as `@codeGenApi` for now and investigate later.
  27. *
  28. * @codeGenApi
  29. */
  30. export declare const __core_private_testing_placeholder__ = "";
  31. /**
  32. * @deprecated use `waitForAsync()`, (expected removal in v12)
  33. * @see {@link waitForAsync}
  34. * @publicApi
  35. * */
  36. export declare function async(fn: Function): (done: any) => any;
  37. /**
  38. * Fixture for debugging and testing a component.
  39. *
  40. * @publicApi
  41. */
  42. export declare class ComponentFixture<T> {
  43. componentRef: ComponentRef<T>;
  44. ngZone: NgZone | null;
  45. private _autoDetect;
  46. /**
  47. * The DebugElement associated with the root element of this component.
  48. */
  49. debugElement: DebugElement;
  50. /**
  51. * The instance of the root component class.
  52. */
  53. componentInstance: T;
  54. /**
  55. * The native element at the root of the component.
  56. */
  57. nativeElement: any;
  58. /**
  59. * The ElementRef for the element at the root of the component.
  60. */
  61. elementRef: ElementRef;
  62. /**
  63. * The ChangeDetectorRef for the component
  64. */
  65. changeDetectorRef: ChangeDetectorRef;
  66. private _renderer;
  67. private _isStable;
  68. private _isDestroyed;
  69. private _resolve;
  70. private _promise;
  71. private _onUnstableSubscription;
  72. private _onStableSubscription;
  73. private _onMicrotaskEmptySubscription;
  74. private _onErrorSubscription;
  75. constructor(componentRef: ComponentRef<T>, ngZone: NgZone | null, _autoDetect: boolean);
  76. private _tick;
  77. /**
  78. * Trigger a change detection cycle for the component.
  79. */
  80. detectChanges(checkNoChanges?: boolean): void;
  81. /**
  82. * Do a change detection run to make sure there were no changes.
  83. */
  84. checkNoChanges(): void;
  85. /**
  86. * Set whether the fixture should autodetect changes.
  87. *
  88. * Also runs detectChanges once so that any existing change is detected.
  89. */
  90. autoDetectChanges(autoDetect?: boolean): void;
  91. /**
  92. * Return whether the fixture is currently stable or has async tasks that have not been completed
  93. * yet.
  94. */
  95. isStable(): boolean;
  96. /**
  97. * Get a promise that resolves when the fixture is stable.
  98. *
  99. * This can be used to resume testing after events have triggered asynchronous activity or
  100. * asynchronous change detection.
  101. */
  102. whenStable(): Promise<any>;
  103. private _getRenderer;
  104. /**
  105. * Get a promise that resolves when the ui state is stable following animations.
  106. */
  107. whenRenderingDone(): Promise<any>;
  108. /**
  109. * Trigger component destruction.
  110. */
  111. destroy(): void;
  112. }
  113. /**
  114. * @publicApi
  115. */
  116. export declare const ComponentFixtureAutoDetect: InjectionToken<boolean>;
  117. /**
  118. * @publicApi
  119. */
  120. export declare const ComponentFixtureNoNgZone: InjectionToken<boolean>;
  121. /**
  122. * Discard all remaining periodic tasks.
  123. *
  124. * @publicApi
  125. */
  126. export declare function discardPeriodicTasks(): void;
  127. /**
  128. * Wraps a function to be executed in the `fakeAsync` zone:
  129. * - Microtasks are manually executed by calling `flushMicrotasks()`.
  130. * - Timers are synchronous; `tick()` simulates the asynchronous passage of time.
  131. *
  132. * If there are any pending timers at the end of the function, an exception is thrown.
  133. *
  134. * Can be used to wrap `inject()` calls.
  135. *
  136. * @param fn The function that you want to wrap in the `fakeAsync` zone.
  137. *
  138. * @usageNotes
  139. * ### Example
  140. *
  141. * {@example core/testing/ts/fake_async.ts region='basic'}
  142. *
  143. *
  144. * @returns The function wrapped to be executed in the `fakeAsync` zone.
  145. * Any arguments passed when calling this returned function will be passed through to the `fn`
  146. * function in the parameters when it is called.
  147. *
  148. * @publicApi
  149. */
  150. export declare function fakeAsync(fn: Function): (...args: any[]) => any;
  151. /**
  152. * Flushes any pending microtasks and simulates the asynchronous passage of time for the timers in
  153. * the `fakeAsync` zone by
  154. * draining the macrotask queue until it is empty.
  155. *
  156. * @param maxTurns The maximum number of times the scheduler attempts to clear its queue before
  157. * throwing an error.
  158. * @returns The simulated time elapsed, in milliseconds.
  159. *
  160. * @publicApi
  161. */
  162. export declare function flush(maxTurns?: number): number;
  163. /**
  164. * Flush any pending microtasks.
  165. *
  166. * @publicApi
  167. */
  168. export declare function flushMicrotasks(): void;
  169. /**
  170. * Returns a singleton of the `TestBed` class.
  171. *
  172. * @publicApi
  173. */
  174. export declare function getTestBed(): TestBed;
  175. /**
  176. * Allows injecting dependencies in `beforeEach()` and `it()`. Note: this function
  177. * (imported from the `@angular/core/testing` package) can **only** be used to inject dependencies
  178. * in tests. To inject dependencies in your application code, use the [`inject`](api/core/inject)
  179. * function from the `@angular/core` package instead.
  180. *
  181. * Example:
  182. *
  183. * ```
  184. * beforeEach(inject([Dependency, AClass], (dep, object) => {
  185. * // some code that uses `dep` and `object`
  186. * // ...
  187. * }));
  188. *
  189. * it('...', inject([AClass], (object) => {
  190. * object.doSomething();
  191. * expect(...);
  192. * })
  193. * ```
  194. *
  195. * @publicApi
  196. */
  197. export declare function inject(tokens: any[], fn: Function): () => any;
  198. /**
  199. * @publicApi
  200. */
  201. export declare class InjectSetupWrapper {
  202. private _moduleDef;
  203. constructor(_moduleDef: () => TestModuleMetadata);
  204. private _addModule;
  205. inject(tokens: any[], fn: Function): () => any;
  206. }
  207. /**
  208. * Type used for modifications to metadata
  209. *
  210. * @publicApi
  211. */
  212. export declare type MetadataOverride<T> = {
  213. add?: Partial<T>;
  214. remove?: Partial<T>;
  215. set?: Partial<T>;
  216. };
  217. /**
  218. * Configures the test module teardown behavior in `TestBed`.
  219. * @publicApi
  220. */
  221. export declare interface ModuleTeardownOptions {
  222. /** Whether the test module should be destroyed after every test. Defaults to `true`. */
  223. destroyAfterEach: boolean;
  224. /** Whether errors during test module destruction should be re-thrown. Defaults to `true`. */
  225. rethrowErrors?: boolean;
  226. }
  227. /**
  228. * Clears out the shared fake async zone for a test.
  229. * To be called in a global `beforeEach`.
  230. *
  231. * @publicApi
  232. */
  233. export declare function resetFakeAsyncZone(): void;
  234. /**
  235. * @publicApi
  236. */
  237. export declare interface TestBed {
  238. get platform(): PlatformRef;
  239. get ngModule(): Type<any> | Type<any>[];
  240. /**
  241. * Initialize the environment for testing with a compiler factory, a PlatformRef, and an
  242. * angular module. These are common to every test in the suite.
  243. *
  244. * This may only be called once, to set up the common providers for the current test
  245. * suite on the current platform. If you absolutely need to change the providers,
  246. * first use `resetTestEnvironment`.
  247. *
  248. * Test modules and platforms for individual platforms are available from
  249. * '@angular/<platform_name>/testing'.
  250. */
  251. initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, options?: TestEnvironmentOptions): void;
  252. /**
  253. * Reset the providers for the test injector.
  254. */
  255. resetTestEnvironment(): void;
  256. resetTestingModule(): TestBed;
  257. configureCompiler(config: {
  258. providers?: any[];
  259. useJit?: boolean;
  260. }): void;
  261. configureTestingModule(moduleDef: TestModuleMetadata): TestBed;
  262. compileComponents(): Promise<any>;
  263. inject<T>(token: ProviderToken<T>, notFoundValue: undefined, options: InjectOptions & {
  264. optional?: false;
  265. }): T;
  266. inject<T>(token: ProviderToken<T>, notFoundValue: null | undefined, options: InjectOptions): T | null;
  267. inject<T>(token: ProviderToken<T>, notFoundValue?: T, options?: InjectOptions): T;
  268. /** @deprecated use object-based flags (`InjectOptions`) instead. */
  269. inject<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T;
  270. /** @deprecated use object-based flags (`InjectOptions`) instead. */
  271. inject<T>(token: ProviderToken<T>, notFoundValue: null, flags?: InjectFlags): T | null;
  272. /** @deprecated from v9.0.0 use TestBed.inject */
  273. get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
  274. /** @deprecated from v9.0.0 use TestBed.inject */
  275. get(token: any, notFoundValue?: any): any;
  276. /**
  277. * Runs the given function in the `EnvironmentInjector` context of `TestBed`.
  278. *
  279. * @see {@link EnvironmentInjector#runInContext}
  280. */
  281. runInInjectionContext<T>(fn: () => T): T;
  282. execute(tokens: any[], fn: Function, context?: any): any;
  283. overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): TestBed;
  284. overrideComponent(component: Type<any>, override: MetadataOverride<Component>): TestBed;
  285. overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): TestBed;
  286. overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): TestBed;
  287. overrideTemplate(component: Type<any>, template: string): TestBed;
  288. /**
  289. * Overwrites all providers for the given token with the given provider definition.
  290. */
  291. overrideProvider(token: any, provider: {
  292. useFactory: Function;
  293. deps: any[];
  294. multi?: boolean;
  295. }): TestBed;
  296. overrideProvider(token: any, provider: {
  297. useValue: any;
  298. multi?: boolean;
  299. }): TestBed;
  300. overrideProvider(token: any, provider: {
  301. useFactory?: Function;
  302. useValue?: any;
  303. deps?: any[];
  304. multi?: boolean;
  305. }): TestBed;
  306. overrideTemplateUsingTestingModule(component: Type<any>, template: string): TestBed;
  307. createComponent<T>(component: Type<T>): ComponentFixture<T>;
  308. }
  309. /**
  310. * @description
  311. * Configures and initializes environment for unit testing and provides methods for
  312. * creating components and services in unit tests.
  313. *
  314. * `TestBed` is the primary api for writing unit tests for Angular applications and libraries.
  315. *
  316. * @publicApi
  317. */
  318. export declare const TestBed: TestBedStatic;
  319. /**
  320. * Static methods implemented by the `TestBed`.
  321. *
  322. * @publicApi
  323. */
  324. export declare interface TestBedStatic extends TestBed {
  325. new (...args: any[]): TestBed;
  326. }
  327. /**
  328. * An abstract class for inserting the root test component element in a platform independent way.
  329. *
  330. * @publicApi
  331. */
  332. export declare class TestComponentRenderer {
  333. insertRootElement(rootElementId: string): void;
  334. removeAllRootElements?(): void;
  335. }
  336. /**
  337. * @publicApi
  338. */
  339. export declare interface TestEnvironmentOptions {
  340. /**
  341. * Configures the test module teardown behavior in `TestBed`.
  342. */
  343. teardown?: ModuleTeardownOptions;
  344. /**
  345. * Whether errors should be thrown when unknown elements are present in component's template.
  346. * Defaults to `false`, where the error is simply logged.
  347. * If set to `true`, the error is thrown.
  348. * @see [NG8001](/errors/NG8001) for the description of the error and how to fix it
  349. */
  350. errorOnUnknownElements?: boolean;
  351. /**
  352. * Whether errors should be thrown when unknown properties are present in component's template.
  353. * Defaults to `false`, where the error is simply logged.
  354. * If set to `true`, the error is thrown.
  355. * @see [NG8002](/errors/NG8002) for the description of the error and how to fix it
  356. */
  357. errorOnUnknownProperties?: boolean;
  358. }
  359. /**
  360. * @publicApi
  361. */
  362. export declare interface TestModuleMetadata {
  363. providers?: any[];
  364. declarations?: any[];
  365. imports?: any[];
  366. schemas?: Array<SchemaMetadata | any[]>;
  367. teardown?: ModuleTeardownOptions;
  368. /**
  369. * Whether NG0304 runtime errors should be thrown when unknown elements are present in component's
  370. * template. Defaults to `false`, where the error is simply logged. If set to `true`, the error is
  371. * thrown.
  372. * @see [NG8001](/errors/NG8001) for the description of the problem and how to fix it
  373. */
  374. errorOnUnknownElements?: boolean;
  375. /**
  376. * Whether errors should be thrown when unknown properties are present in component's template.
  377. * Defaults to `false`, where the error is simply logged.
  378. * If set to `true`, the error is thrown.
  379. * @see [NG8002](/errors/NG8002) for the description of the error and how to fix it
  380. */
  381. errorOnUnknownProperties?: boolean;
  382. }
  383. /**
  384. * Simulates the asynchronous passage of time for the timers in the `fakeAsync` zone.
  385. *
  386. * The microtasks queue is drained at the very start of this function and after any timer callback
  387. * has been executed.
  388. *
  389. * @param millis The number of milliseconds to advance the virtual timer.
  390. * @param tickOptions The options to pass to the `tick()` function.
  391. *
  392. * @usageNotes
  393. *
  394. * The `tick()` option is a flag called `processNewMacroTasksSynchronously`,
  395. * which determines whether or not to invoke new macroTasks.
  396. *
  397. * If you provide a `tickOptions` object, but do not specify a
  398. * `processNewMacroTasksSynchronously` property (`tick(100, {})`),
  399. * then `processNewMacroTasksSynchronously` defaults to true.
  400. *
  401. * If you omit the `tickOptions` parameter (`tick(100))`), then
  402. * `tickOptions` defaults to `{processNewMacroTasksSynchronously: true}`.
  403. *
  404. * ### Example
  405. *
  406. * {@example core/testing/ts/fake_async.ts region='basic'}
  407. *
  408. * The following example includes a nested timeout (new macroTask), and
  409. * the `tickOptions` parameter is allowed to default. In this case,
  410. * `processNewMacroTasksSynchronously` defaults to true, and the nested
  411. * function is executed on each tick.
  412. *
  413. * ```
  414. * it ('test with nested setTimeout', fakeAsync(() => {
  415. * let nestedTimeoutInvoked = false;
  416. * function funcWithNestedTimeout() {
  417. * setTimeout(() => {
  418. * nestedTimeoutInvoked = true;
  419. * });
  420. * };
  421. * setTimeout(funcWithNestedTimeout);
  422. * tick();
  423. * expect(nestedTimeoutInvoked).toBe(true);
  424. * }));
  425. * ```
  426. *
  427. * In the following case, `processNewMacroTasksSynchronously` is explicitly
  428. * set to false, so the nested timeout function is not invoked.
  429. *
  430. * ```
  431. * it ('test with nested setTimeout', fakeAsync(() => {
  432. * let nestedTimeoutInvoked = false;
  433. * function funcWithNestedTimeout() {
  434. * setTimeout(() => {
  435. * nestedTimeoutInvoked = true;
  436. * });
  437. * };
  438. * setTimeout(funcWithNestedTimeout);
  439. * tick(0, {processNewMacroTasksSynchronously: false});
  440. * expect(nestedTimeoutInvoked).toBe(false);
  441. * }));
  442. * ```
  443. *
  444. *
  445. * @publicApi
  446. */
  447. export declare function tick(millis?: number, tickOptions?: {
  448. processNewMacroTasksSynchronously: boolean;
  449. }): void;
  450. /**
  451. * Wraps a test function in an asynchronous test zone. The test will automatically
  452. * complete when all asynchronous calls within this zone are done. Can be used
  453. * to wrap an {@link inject} call.
  454. *
  455. * Example:
  456. *
  457. * ```
  458. * it('...', waitForAsync(inject([AClass], (object) => {
  459. * object.doSomething.then(() => {
  460. * expect(...);
  461. * })
  462. * });
  463. * ```
  464. *
  465. * @publicApi
  466. */
  467. export declare function waitForAsync(fn: Function): (done: any) => any;
  468. /**
  469. * @publicApi
  470. */
  471. export declare function withModule(moduleDef: TestModuleMetadata): InjectSetupWrapper;
  472. export declare function withModule(moduleDef: TestModuleMetadata, fn: Function): () => any;
  473. export declare class ɵMetadataOverrider {
  474. private _references;
  475. /**
  476. * Creates a new instance for the given metadata class
  477. * based on an old instance and overrides.
  478. */
  479. overrideMetadata<C extends T, T>(metadataClass: {
  480. new (options: T): C;
  481. }, oldMetadata: C, override: MetadataOverride<T>): C;
  482. }
  483. export { }