File

src/lib/test-html-element.ts

Description

A wrapped DOM HTML element, providing additional methods and attributes helping with writing tests

Extends

TestElement

Index

Methods
Accessors

Constructor

constructor(tester: ComponentTester<>, debugElement: DebugElement)
Parameters :
Name Type Optional
tester ComponentTester<> No
debugElement DebugElement No

Methods

click
click()

Clicks on the wrapped element, then triggers a change detection

Returns : void
attr
attr(name: string)
Inherited from TestElement
Defined in TestElement:66

Gets the attribute of the wrapped element with the given name

Parameters :
Name Type Optional Description
name string No

the name of the attribute to get

Returns : string | null
button
button(selector: string | Type)
Inherited from TestElement
Defined in TestElement:327

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

component
component(selector: Type)
Inherited from TestElement
Defined in TestElement:335
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 TestElement
Defined in TestElement:343
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>
custom
custom(selector: string | Type, customTestElementType: Type)
Inherited from TestElement
Defined in TestElement:372
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 TestElement
Defined in TestElement:384
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>
dispatchEvent
dispatchEvent(event: Event)
Inherited from TestElement
Defined in TestElement:50

dispatches the given event from the wrapped element, then triggers a change detection

Parameters :
Name Type Optional
event Event No
Returns : void
dispatchEventOfType
dispatchEventOfType(type: string)
Inherited from TestElement
Defined in TestElement:42

dispatches an event of the given type from the wrapped element, then triggers a change detection

Parameters :
Name Type Optional
type string No
Returns : void
element
element(selector: K)
Inherited from TestElement
Defined in TestElement:81
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 TestElement
Defined in TestElement:193
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 TestElement
Defined in TestElement:299

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 TestElement
Defined in TestElement:308

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

textarea
textarea(selector: string | Type)
Inherited from TestElement
Defined in TestElement:318

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 TestElement
Defined in TestElement:352
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 TestElement
Defined in TestElement:361
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

visible
getvisible()

Tests if the element is visible, in the same meaning (and implementation) as in jQuery, i.e. present anywhere in the DOM, and visible. An element is not visible typically, if its display style or any of its ancestors display style is none.

Returns : boolean
import { ComponentTester } from './component-tester';
import { TestElement } from './test-element';
import { DebugElement } from '@angular/core';

/**
 * A wrapped DOM HTML element, providing additional methods and attributes helping with writing tests
 */
export class TestHtmlElement<E extends HTMLElement> extends TestElement<E> {
  constructor(tester: ComponentTester<unknown>, debugElement: DebugElement) {
    super(tester, debugElement);
  }

  /**
   * Clicks on the wrapped element, then triggers a change detection
   */
  click(): void {
    this.nativeElement.click();
    this.tester.detectChanges();
  }

  /**
   * Tests if the element is visible, in the same meaning (and implementation) as in jQuery, i.e.
   * present anywhere in the DOM, and visible.
   * An element is not visible typically, if its display style or any of its ancestors display style is none.
   */
  get visible(): boolean {
    return !!(this.nativeElement.offsetWidth || this.nativeElement.offsetHeight || this.nativeElement.getClientRects().length);
  }
}

results matching ""

    No results matching ""