• Overview
@angular/cdk/testing/testbed

TestbedHarnessEnvironment

Class

A HarnessEnvironment implementation for Angular's Testbed.

API

    
      class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {}
    
    

constructor

TestbedHarnessEnvironment
@paramrawRootElementElement
@param_fixtureFixture<unknown>
@paramoptionsTestbedHarnessEnvironmentOptions | undefined

forceStabilize

Promise<void>

Flushes change detection and async tasks captured in the Angular zone. In most cases it should not be necessary to call this manually. However, there may be some edge cases where it is needed to fully flush animation events.

@returnsPromise<void>

waitForTasksOutsideAngular

Promise<void>

Waits for all scheduled or running async tasks to complete. This allows harness authors to wait for async tasks outside of the Angular zone.

This only works when Zone.js is present and patches are applied to the test framework by zone.js/testing (Jasmine and Jest only) or another script.

@returnsPromise<void>

getDocumentRoot

Element

Gets the root element for the document.

@returnsElement

createTestElement

TestElement

Creates a TestElement from a raw element.

@paramelementElement
@returnsTestElement

createEnvironment

HarnessEnvironment<Element>

Creates a HarnessLoader rooted at the given raw element.

@paramelementElement
@returnsHarnessEnvironment<Element>

getAllRawElements

Promise<Element[]>

Gets a list of all elements matching the given selector under this environment's root element.

@paramselectorstring
@returnsPromise<Element[]>

rawRootElement

E

The native root element of this HarnessEnvironment.

rootElement

TestElement

The root element of this HarnessEnvironment as a TestElement.

rootElement

TestElement

documentRootLocatorFactory

LocatorFactory

Gets a locator factory rooted at the document root.

@returnsLocatorFactory

locatorFor

() => Promise<LocatorFnResult<T>>

Creates an asynchronous locator function that can be used to find a ComponentHarness instance or element under the root element of this HarnessEnvironment.

For example, given the following DOM and assuming DivHarness.hostSelector is 'div'

<div id="d1"></div><div id="d2"></div>

then we expect:

await lf.locatorFor(DivHarness, 'div')() // Gets a `DivHarness` instance for #d1
await lf.locatorFor('div', DivHarness)() // Gets a `TestElement` instance for #d1
await lf.locatorFor('span')()            // Throws because the `Promise` rejects
@paramqueriesT

A list of queries specifying which harnesses and elements to search for:

  • A string searches for elements matching the CSS selector specified by the string.
  • A ComponentHarness constructor searches for ComponentHarness instances matching the given class.
  • A HarnessPredicate searches for ComponentHarness instances matching the given predicate.
@returns() => Promise<LocatorFnResult<T>>

locatorForOptional

() => Promise<LocatorFnResult<T> | null>

Creates an asynchronous locator function that can be used to find a ComponentHarness instance or element under the root element of this HarnessEnvironmnet.

For example, given the following DOM and assuming DivHarness.hostSelector is 'div'

<div id="d1"></div><div id="d2"></div>

then we expect:

await lf.locatorForOptional(DivHarness, 'div')() // Gets a `DivHarness` instance for #d1
await lf.locatorForOptional('div', DivHarness)() // Gets a `TestElement` instance for #d1
await lf.locatorForOptional('span')()            // Gets `null`
@paramqueriesT

A list of queries specifying which harnesses and elements to search for:

  • A string searches for elements matching the CSS selector specified by the string.
  • A ComponentHarness constructor searches for ComponentHarness instances matching the given class.
  • A HarnessPredicate searches for ComponentHarness instances matching the given predicate.
@returns() => Promise<LocatorFnResult<T> | null>

locatorForAll

() => Promise<LocatorFnResult<T>[]>

Creates an asynchronous locator function that can be used to find ComponentHarness instances or elements under the root element of this HarnessEnvironment.

For example, given the following DOM and assuming DivHarness.hostSelector is 'div' and IdIsD1Harness.hostSelector is '#d1'

<div id="d1"></div><div id="d2"></div>

then we expect:

// Gets [DivHarness for #d1, TestElement for #d1, DivHarness for #d2, TestElement for #d2]
await lf.locatorForAll(DivHarness, 'div')()
// Gets [TestElement for #d1, TestElement for #d2]
await lf.locatorForAll('div', '#d1')()
// Gets [DivHarness for #d1, IdIsD1Harness for #d1, DivHarness for #d2]
await lf.locatorForAll(DivHarness, IdIsD1Harness)()
// Gets []
await lf.locatorForAll('span')()
@paramqueriesT

A list of queries specifying which harnesses and elements to search for:

  • A string searches for elements matching the CSS selector specified by the string.
  • A ComponentHarness constructor searches for ComponentHarness instances matching the given class.
  • A HarnessPredicate searches for ComponentHarness instances matching the given predicate.
@returns() => Promise<LocatorFnResult<T>[]>

rootHarnessLoader

Promise<HarnessLoader>
@returnsPromise<HarnessLoader>

harnessLoaderFor

Promise<HarnessLoader>

Gets a HarnessLoader instance for an element under the root of this HarnessEnvironment.

@paramselectorstring

The selector for the root element.

@returnsPromise<HarnessLoader>

harnessLoaderForOptional

Promise<HarnessLoader | null>

Gets a HarnessLoader instance for an element under the root of this HarnessEnvironment.

@paramselectorstring

The selector for the root element.

@returnsPromise<HarnessLoader | null>

harnessLoaderForAll

Promise<HarnessLoader[]>

Gets a list of HarnessLoader instances, one for each matching element.

@paramselectorstring

The selector for the root element.

@returnsPromise<HarnessLoader[]>

getHarness

Promise<T>

Searches for an instance of the component corresponding to the given harness type under the HarnessEnvironment's root element, and returns a ComponentHarness for that instance. If multiple matching components are found, a harness for the first one is returned. If no matching component is found, an error is thrown.

@paramqueryHarnessQuery<T>

A query for a harness to create

@returnsPromise<T>

getHarnessOrNull

Promise<T | null>

Searches for an instance of the component corresponding to the given harness type under the HarnessEnvironment's root element, and returns a ComponentHarness for that instance. If multiple matching components are found, a harness for the first one is returned. If no matching component is found, null is returned.

@paramqueryHarnessQuery<T>

A query for a harness to create

@returnsPromise<T | null>

getHarnessAtIndex

Promise<T>

Searches for an instance of the component corresponding to the given harness type and index under the HarnessEnvironment's root element, and returns a ComponentHarness for that instance. The index specifies the offset of the component to find. If no matching component is found at that index, an error is thrown.

@paramqueryHarnessQuery<T>

A query for a harness to create

@paramoffsetnumber
@returnsPromise<T>

getAllHarnesses

Promise<T[]>

Searches for all instances of the component corresponding to the given harness type under the HarnessEnvironment's root element, and returns a list ComponentHarness for each instance.

@paramqueryHarnessQuery<T>

A query for a harness to create

@returnsPromise<T[]>

countHarnesses

Promise<number>

Searches for all instance of the component corresponding to the given harness type under the HarnessEnvironment's root element, and returns the number that were found.

@paramqueryHarnessQuery<T>

A query for a harness to create

@returnsPromise<number>

hasHarness

Promise<boolean>

Searches for an instance of the component corresponding to the given harness type under the HarnessEnvironment's root element, and returns a boolean indicating if any were found.

@paramqueryHarnessQuery<T>

A query for a harness to create

@returnsPromise<boolean>

getChildLoader

Promise<HarnessLoader>

Searches for an element with the given selector under the evironment's root element, and returns a HarnessLoader rooted at the matching element. If multiple elements match the selector, the first is used. If no elements match, an error is thrown.

@paramselectorstring

The selector for the root element of the new HarnessLoader

@returnsPromise<HarnessLoader>

getAllChildLoaders

Promise<HarnessLoader[]>

Searches for all elements with the given selector under the environment's root element, and returns an array of HarnessLoaders, one for each matching element, rooted at that element.

@paramselectorstring

The selector for the root element of the new HarnessLoader

@returnsPromise<HarnessLoader[]>

createComponentHarness

T

Creates a ComponentHarness for the given harness type with the given raw host element.

@paramharnessTypeComponentHarnessConstructor<T>
@paramelementE
@returnsT

loader

HarnessLoader

Creates a HarnessLoader rooted at the given fixture's root element.

@paramfixtureFixture<unknown>
@paramoptionsTestbedHarnessEnvironmentOptions | undefined
@returnsHarnessLoader

documentRootLoader

HarnessLoader

Creates a HarnessLoader at the document root. This can be used if harnesses are located outside of a fixture (e.g. overlays appended to the document body).

@paramfixtureFixture<unknown>
@paramoptionsTestbedHarnessEnvironmentOptions | undefined
@returnsHarnessLoader

getNativeElement

Element

Gets the native DOM element corresponding to the given TestElement.

@paramelTestElement
@returnsElement

harnessForFixture

Promise<T>

Creates an instance of the given harness type, using the fixture's root element as the harness's host element. This method should be used when creating a harness for the root element of a fixture, as components do not have the correct selector when they are created as the root of the fixture.

@paramfixtureFixture<unknown>
@paramharnessTypeComponentHarnessConstructor<T>
@paramoptionsTestbedHarnessEnvironmentOptions | undefined
@returnsPromise<T>
Jump to details