File

src/lib/routing-tester.ts

Description

A thin wrapper around Angular RouterTestingHarness which helps testing routed components. It allows, based on a configured testing module where the router is provided, to initially navigate to a given route, then test the routed component. It's then possible to either navigate again with the wrapped harness, or to use the component to trigger navigation, and test that the URL has changed for example.

Extends

ComponentTester

Index

Properties
Methods
Accessors

Constructor

constructor(harness: RouterTestingHarness)
Parameters :
Name Type Optional
harness RouterTestingHarness No

Properties

Readonly fixture
Type : ComponentFixture<C>
Inherited from ComponentTester
Defined in ComponentTester:27

The component fixture of the component

Readonly mode
Type : "imperative" | "automatic"
Inherited from ComponentTester
Defined in ComponentTester:32

The mode used by the ComponentTester

Readonly testElement
Type : TestElement<HTMLElement>
Inherited from ComponentTester
Defined in ComponentTester:22

The test element of the component

Methods

Static Async forUrl
forUrl(url: string)

Creates a RouterTestngHarness and uses it to navigate to the given URL and its fixture.

Parameters :
Name Type Optional Description
url string No

the URL to initially navigate to.

Returns : unknown

a promise which resolves to a RoutingTester which wraps the harness and its fixture.

button
button(selector: string | Type)
Inherited from ComponentTester

Gets the first button matched by the given selector. Throws an Error if the matched element isn't actually a button.

Parameters :
Name Type Optional Description
selector string | Type<any> No

a CSS or directive selector

Returns : TestButton | null

the wrapped button, or null if no element was matched

Async change
change()
Inherited from ComponentTester

In imperative mode, runs change detection. In automatic mode, awaits stability.

Returns : any
component
component(selector: Type)
Inherited from ComponentTester
Type parameters :
  • R

Gets the first directive matching the given component directive selector and returns its component instance

Parameters :
Name Type Optional Description
selector Type<R> No

the selector of a component directive

Returns : R
components
components(selector: Type)
Inherited from ComponentTester
Type parameters :
  • R

Gets the directives matching the given component directive selector and returns their component instance

Parameters :
Name Type Optional Description
selector Type<R> No

the selector of a component directive

Returns : Array<R>
Static create
create(componentType: Type)
Inherited from ComponentTester
Defined in ComponentTester:37
Type parameters :
  • C

Creates a component fixture of the given type with the TestBed and wraps it into a ComponentTester

Parameters :
Name Type Optional
componentType Type<C> No
Returns : ComponentTester<C>
custom
custom(selector: string | Type, customTestElementType: Type)
Inherited from ComponentTester
Type parameters :
  • E

Gets the element matching the given selector, and if found, creates and returns a custom TestElement of the provided type. This is useful to create custom higher-level abstractions similar to TestInput, TestSelect, etc. for custom elements or components.

Parameters :
Name Type Optional Description
selector string | Type<any> No

a CSS or directive selector

customTestElementType Type<E> No

the type of the TestElement subclass that will wrap the found element

Returns : E | null
customs
customs(selector: string | Type, customTestElementType: Type)
Inherited from ComponentTester
Type parameters :
  • E

Gets the elements matching the given selector, and creates and returns custom TestElements of the provided type. This is useful to create custom higher-level abstractions similar to TestInput, TestSelect, etc. for custom elements or components.

Parameters :
Name Type Optional Description
selector string | Type<any> No

a CSS or directive selector

customTestElementType Type<E> No

the type of the TestElement subclass that will wrap the found elements

Returns : Array<E>
detectChanges
detectChanges(checkNoChanges?: boolean)
Inherited from ComponentTester

Triggers a change detection using the wrapped fixture in imperative mode. Throws an error in autodetection mode. You should generally prever

Parameters :
Name Type Optional
checkNoChanges boolean Yes
Returns : void
element
element(selector: K)
Inherited from ComponentTester
Defined in ComponentTester:93
Type parameters :
  • K

Gets the first element matching the given CSS selector and wraps it into a TestElement. The actual type of the returned value is the TestElement subclass matching the type of the found element. So, if the matched element is an input for example, the method will return a TestInput.

Usage:

const testElement: TestHtmlElement<HTMLDivElement> | null = tester.element('div');
Parameters :
Name Type Optional Description
selector K No

a CSS selector

the wrapped element, or null if no element matches the selector.

elements
elements(selector: K)
Inherited from ComponentTester
Type parameters :
  • K

Gets all the elements matching the given CSS selector and wraps them into a TestElement. The actual type of the returned elements is the TestElement subclass matching the type of the found element. So, if the matched elements are inputs for example, the method will return an array of TestInput.

Usage:

const testElements: Array<TestHtmlElement<HTMLDivElement>> = tester.elements('div');
Parameters :
Name Type Optional Description
selector K No

a CSS selector

the array of matched elements, empty if no element was matched

input
input(selector: string | Type)
Inherited from ComponentTester

Gets the first input matched by the given selector. Throws an Error if the matched element isn't actually an input.

Parameters :
Name Type Optional Description
selector string | Type<any> No

a CSS or directive selector

Returns : TestInput | null

the wrapped input, or null if no element was matched

select
select(selector: string | Type)
Inherited from ComponentTester

Gets the first select matched by the given selector. Throws an Error if the matched element isn't actually a select.

Parameters :
Name Type Optional Description
selector string | Type<any> No

a CSS or directive selector

Returns : TestSelect | null

the wrapped select, or null if no element was matched

Async stable
stable()
Inherited from ComponentTester

Delegates to the wrapped fixture whenStable and, in imperative mode, detect changes

Returns : Promise<void>
textarea
textarea(selector: string | Type)
Inherited from ComponentTester

Gets the first textarea matched by the given selector

Parameters :
Name Type Optional Description
selector string | Type<any> No

a CSS or directive selector

the wrapped textarea, or null if no element was matched. Throws an Error if the matched element isn't actually a textarea.

token
token(selector: string | Type, token: ProviderToken)
Inherited from ComponentTester
Type parameters :
  • R

Gets the first element matching the given selector, then gets the given token from its injector, or null if there is no such token

Parameters :
Name Type Optional Description
selector string | Type<any> No

a CSS or directive selector

token ProviderToken<R> No

the token to get from the matched element injector

Returns : R | null
tokens
tokens(selector: string | Type, token: ProviderToken)
Inherited from ComponentTester
Type parameters :
  • R

Gets the elements matching the given selector, then gets their given token from their injector, or null if there is no such token

Parameters :
Name Type Optional Description
selector string | Type<any> No

a CSS or directive selector

token ProviderToken<R> No

the token to get from the matched element injector

Returns : Array<R | null>

Accessors

url
geturl()

Gets the current URL of the Router service as a string

Returns : string
urlTree
geturlTree()

Gets the current URL of the Router service as an UrlTree, to be able to test its elements (query params, etc.)

Returns : UrlTree
import { ComponentTester } from './component-tester';
import { RouterTestingHarness } from '@angular/router/testing';
import { Router, UrlTree } from '@angular/router';
import { TestBed } from '@angular/core/testing';

/**
 * A thin wrapper around Angular RouterTestingHarness which helps testing routed components.
 * It allows, based on a configured testing module where the router is provided, to initially navigate
 * to a given route, then test the routed component. It's then possible to either navigate again with
 * the wrapped harness, or to use the component to trigger navigation, and test that the URL has changed
 * for example.
 */
export class RoutingTester extends ComponentTester<unknown> {
  constructor(readonly harness: RouterTestingHarness) {
    super(harness.fixture);
  }

  /**
   * Creates a RouterTestngHarness and uses it to navigate to the given URL
   * @param url the URL to initially navigate to.
   * @return a promise which resolves to a RoutingTester which wraps the harness
   * and its fixture.
   */
  static async forUrl(url: string) {
    const harness = await RouterTestingHarness.create(url);
    return new RoutingTester(harness);
  }

  /**
   * Gets the current URL of the Router service as a string
   */
  get url(): string {
    const router = TestBed.inject(Router);
    return router.url;
  }

  /**
   * Gets the current URL of the Router service as an UrlTree, to be able to test its
   * elements (query params, etc.)
   */
  get urlTree(): UrlTree {
    const router = TestBed.inject(Router);
    return router.parseUrl(router.url);
  }
}

results matching ""

    No results matching ""