index.d.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { H as HarnessEnvironment, d as HarnessLoader, T as TestElement, C as ComponentHarness, e as ComponentHarnessConstructor, M as ModifierKeys, a as TestKey, b as TextOptions, E as ElementDimensions, c as EventData } from '../../harness-environment.d-BbFzIFDE.js';
  2. import { ComponentFixture } from '@angular/core/testing';
  3. /** Options to configure the environment. */
  4. interface TestbedHarnessEnvironmentOptions {
  5. /** The query function used to find DOM elements. */
  6. queryFn: (selector: string, root: Element) => Iterable<Element> | ArrayLike<Element>;
  7. }
  8. /** A `HarnessEnvironment` implementation for Angular's Testbed. */
  9. declare class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
  10. private _fixture;
  11. /** Whether the environment has been destroyed. */
  12. private _destroyed;
  13. /** Observable that emits whenever the test task state changes. */
  14. private _taskState?;
  15. /** The options for this environment. */
  16. private _options;
  17. /** Environment stabilization callback passed to the created test elements. */
  18. private _stabilizeCallback;
  19. protected constructor(rawRootElement: Element, _fixture: ComponentFixture<unknown>, options?: TestbedHarnessEnvironmentOptions);
  20. /** Creates a `HarnessLoader` rooted at the given fixture's root element. */
  21. static loader(fixture: ComponentFixture<unknown>, options?: TestbedHarnessEnvironmentOptions): HarnessLoader;
  22. /**
  23. * Creates a `HarnessLoader` at the document root. This can be used if harnesses are
  24. * located outside of a fixture (e.g. overlays appended to the document body).
  25. */
  26. static documentRootLoader(fixture: ComponentFixture<unknown>, options?: TestbedHarnessEnvironmentOptions): HarnessLoader;
  27. /** Gets the native DOM element corresponding to the given TestElement. */
  28. static getNativeElement(el: TestElement): Element;
  29. /**
  30. * Creates an instance of the given harness type, using the fixture's root element as the
  31. * harness's host element. This method should be used when creating a harness for the root element
  32. * of a fixture, as components do not have the correct selector when they are created as the root
  33. * of the fixture.
  34. */
  35. static harnessForFixture<T extends ComponentHarness>(fixture: ComponentFixture<unknown>, harnessType: ComponentHarnessConstructor<T>, options?: TestbedHarnessEnvironmentOptions): Promise<T>;
  36. /**
  37. * Flushes change detection and async tasks captured in the Angular zone.
  38. * In most cases it should not be necessary to call this manually. However, there may be some edge
  39. * cases where it is needed to fully flush animation events.
  40. */
  41. forceStabilize(): Promise<void>;
  42. /**
  43. * Waits for all scheduled or running async tasks to complete. This allows harness
  44. * authors to wait for async tasks outside of the Angular zone.
  45. */
  46. waitForTasksOutsideAngular(): Promise<void>;
  47. /** Gets the root element for the document. */
  48. protected getDocumentRoot(): Element;
  49. /** Creates a `TestElement` from a raw element. */
  50. protected createTestElement(element: Element): TestElement;
  51. /** Creates a `HarnessLoader` rooted at the given raw element. */
  52. protected createEnvironment(element: Element): HarnessEnvironment<Element>;
  53. /**
  54. * Gets a list of all elements matching the given selector under this environment's root element.
  55. */
  56. protected getAllRawElements(selector: string): Promise<Element[]>;
  57. }
  58. /** A `TestElement` implementation for unit tests. */
  59. declare class UnitTestElement implements TestElement {
  60. readonly element: Element;
  61. private _stabilize;
  62. constructor(element: Element, _stabilize: () => Promise<void>);
  63. /** Blur the element. */
  64. blur(): Promise<void>;
  65. /** Clear the element's input (for input and textarea elements only). */
  66. clear(): Promise<void>;
  67. /**
  68. * Click the element at the default location for the current environment. If you need to guarantee
  69. * the element is clicked at a specific location, consider using `click('center')` or
  70. * `click(x, y)` instead.
  71. */
  72. click(modifiers?: ModifierKeys): Promise<void>;
  73. /** Click the element at the element's center. */
  74. click(location: 'center', modifiers?: ModifierKeys): Promise<void>;
  75. /**
  76. * Click the element at the specified coordinates relative to the top-left of the element.
  77. * @param relativeX Coordinate within the element, along the X-axis at which to click.
  78. * @param relativeY Coordinate within the element, along the Y-axis at which to click.
  79. * @param modifiers Modifier keys held while clicking
  80. */
  81. click(relativeX: number, relativeY: number, modifiers?: ModifierKeys): Promise<void>;
  82. /**
  83. * Right clicks on the element at the specified coordinates relative to the top-left of it.
  84. * @param relativeX Coordinate within the element, along the X-axis at which to click.
  85. * @param relativeY Coordinate within the element, along the Y-axis at which to click.
  86. * @param modifiers Modifier keys held while clicking
  87. */
  88. rightClick(relativeX: number, relativeY: number, modifiers?: ModifierKeys): Promise<void>;
  89. /** Focus the element. */
  90. focus(): Promise<void>;
  91. /** Get the computed value of the given CSS property for the element. */
  92. getCssValue(property: string): Promise<string>;
  93. /** Hovers the mouse over the element. */
  94. hover(): Promise<void>;
  95. /** Moves the mouse away from the element. */
  96. mouseAway(): Promise<void>;
  97. /**
  98. * Sends the given string to the input as a series of key presses. Also fires input events
  99. * and attempts to add the string to the Element's value. Note that this cannot
  100. * reproduce native browser behavior for keyboard shortcuts such as Tab, Ctrl + A, etc.
  101. */
  102. sendKeys(...keys: (string | TestKey)[]): Promise<void>;
  103. /**
  104. * Sends the given string to the input as a series of key presses. Also fires input events
  105. * and attempts to add the string to the Element's value.
  106. */
  107. sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;
  108. /**
  109. * Gets the text from the element.
  110. * @param options Options that affect what text is included.
  111. */
  112. text(options?: TextOptions): Promise<string>;
  113. /**
  114. * Sets the value of a `contenteditable` element.
  115. * @param value Value to be set on the element.
  116. */
  117. setContenteditableValue(value: string): Promise<void>;
  118. /** Gets the value for the given attribute from the element. */
  119. getAttribute(name: string): Promise<string | null>;
  120. /** Checks whether the element has the given class. */
  121. hasClass(name: string): Promise<boolean>;
  122. /** Gets the dimensions of the element. */
  123. getDimensions(): Promise<ElementDimensions>;
  124. /** Gets the value of a property of an element. */
  125. getProperty<T = any>(name: string): Promise<T>;
  126. /** Sets the value of a property of an input. */
  127. setInputValue(value: string): Promise<void>;
  128. /** Selects the options at the specified indexes inside of a native `select` element. */
  129. selectOptions(...optionIndexes: number[]): Promise<void>;
  130. /** Checks whether this element matches the given selector. */
  131. matchesSelector(selector: string): Promise<boolean>;
  132. /** Checks whether the element is focused. */
  133. isFocused(): Promise<boolean>;
  134. /**
  135. * Dispatches an event with a particular name.
  136. * @param name Name of the event to be dispatched.
  137. */
  138. dispatchEvent(name: string, data?: Record<string, EventData>): Promise<void>;
  139. /**
  140. * Dispatches a pointer event on the current element if the browser supports it.
  141. * @param name Name of the pointer event to be dispatched.
  142. * @param clientX Coordinate of the user's pointer along the X axis.
  143. * @param clientY Coordinate of the user's pointer along the Y axis.
  144. * @param button Mouse button that should be pressed when dispatching the event.
  145. */
  146. private _dispatchPointerEventIfSupported;
  147. /**
  148. * Dispatches all the events that are part of a mouse event sequence
  149. * and then emits a given primary event at the end, if speciifed.
  150. */
  151. private _dispatchMouseEventSequence;
  152. }
  153. export { TestbedHarnessEnvironment, UnitTestElement };
  154. export type { TestbedHarnessEnvironmentOptions };