src/lib/test-select.ts
A wrapped DOM HTML select element, providing additional methods and attributes helping with writing tests
Methods |
Accessors |
constructor(tester: ComponentTester<>, debugElement: DebugElement)
|
|||||||||
Defined in src/lib/test-select.ts:8
|
|||||||||
Parameters :
|
selectIndex | ||||||
selectIndex(index: number)
|
||||||
Defined in src/lib/test-select.ts:17
|
||||||
Selects the option at the given index, then dispatches an event of type change and triggers a change detection. If the index is out of bounds and is not -1, then throws an error.
Parameters :
Returns :
Promise<void>
|
selectLabel | ||||||
selectLabel(label: string)
|
||||||
Defined in src/lib/test-select.ts:42
|
||||||
Selects the first option with the given label (or text), then dispatches an event of type change and triggers a change detection. If there is no option with the given label, then throws an error.
Parameters :
Returns :
Promise<void>
|
selectValue | ||||||
selectValue(value: string)
|
||||||
Defined in src/lib/test-select.ts:29
|
||||||
Selects the first option with the given value, then dispatches an event of type change and triggers a change detection. If there is no option with the given value, then throws an error.
Parameters :
Returns :
Promise<void>
|
Async click |
click()
|
Inherited from
TestHtmlElement
|
Defined in
TestHtmlElement:16
|
Clicks on the wrapped element, then triggers a change detection
Returns :
Promise<void>
|
attr | ||||||||
attr(name: string)
|
||||||||
Inherited from
TestElement
|
||||||||
Defined in
TestElement:66
|
||||||||
Gets the attribute of the wrapped element with the given name
Parameters :
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 :
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 :
|
||||||||
Gets the first directive matching the given component directive selector and returns its component instance
Parameters :
Returns :
R
|
components | ||||||||
components(selector: Type
|
||||||||
Inherited from
TestElement
|
||||||||
Defined in
TestElement:343
|
||||||||
Type parameters :
|
||||||||
Gets the directives matching the given component directive selector and returns their component instance
Parameters :
Returns :
Array<R>
|
custom | ||||||||||||
custom(selector: string | Type
|
||||||||||||
Inherited from
TestElement
|
||||||||||||
Defined in
TestElement:372
|
||||||||||||
Type parameters :
|
||||||||||||
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 :
Returns :
E | null
|
customs | ||||||||||||
customs(selector: string | Type
|
||||||||||||
Inherited from
TestElement
|
||||||||||||
Defined in
TestElement:384
|
||||||||||||
Type parameters :
|
||||||||||||
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 :
Returns :
Array<E>
|
Async 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 :
Returns :
Promise<void>
|
Async 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 :
Returns :
Promise<void>
|
element | ||||||||
element(selector: K)
|
||||||||
Inherited from
TestElement
|
||||||||
Defined in
TestElement:81
|
||||||||
Type parameters :
|
||||||||
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 :
Returns :
TestHtmlElement | null
the wrapped element, or null if no element matches the selector. |
elements | ||||||||
elements(selector: K)
|
||||||||
Inherited from
TestElement
|
||||||||
Defined in
TestElement:193
|
||||||||
Type parameters :
|
||||||||
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 :
Returns :
Array<TestHtmlElement<>>
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 :
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 :
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 :
Returns :
TestTextArea | null
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
|
||||||||||||
Inherited from
TestElement
|
||||||||||||
Defined in
TestElement:352
|
||||||||||||
Type parameters :
|
||||||||||||
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 :
Returns :
R | null
|
tokens | ||||||||||||
tokens(selector: string | Type
|
||||||||||||
Inherited from
TestElement
|
||||||||||||
Defined in
TestElement:361
|
||||||||||||
Type parameters :
|
||||||||||||
Gets the elements matching the given selector, then gets their given token from their injector, or null if there is no such token
Parameters :
Returns :
Array<R | null>
|
selectedIndex |
getselectedIndex()
|
Defined in src/lib/test-select.ts:54
|
the selected index of the wrapped select
Returns :
number
|
selectedValue |
getselectedValue()
|
Defined in src/lib/test-select.ts:61
|
the value of the selected option of the wrapped select, or null if there is no selected option
Returns :
string | null
|
selectedLabel |
getselectedLabel()
|
Defined in src/lib/test-select.ts:71
|
the label (or text if no label) of the selected option of the wrapped select, or null if there is no selected option
Returns :
string | null
|
optionValues |
getoptionValues()
|
Defined in src/lib/test-select.ts:81
|
the values of the options, as an array
Returns :
Array<string>
|
optionLabels |
getoptionLabels()
|
Defined in src/lib/test-select.ts:88
|
the labels (or texts if no label) of the options, as an array
Returns :
Array<string>
|
size |
getsize()
|
Defined in src/lib/test-select.ts:95
|
the number of options in the select
Returns :
number
|
disabled |
getdisabled()
|
Defined in src/lib/test-select.ts:102
|
the disabled property of the wrapped select
Returns :
boolean
|
import { ComponentTester } from './component-tester';
import { TestHtmlElement } from './test-html-element';
import { DebugElement } from '@angular/core';
/**
* A wrapped DOM HTML select element, providing additional methods and attributes helping with writing tests
*/
export class TestSelect extends TestHtmlElement<HTMLSelectElement> {
constructor(tester: ComponentTester<unknown>, debugElement: DebugElement) {
super(tester, debugElement);
}
/**
* Selects the option at the given index, then dispatches an event of type change and triggers a change detection.
* If the index is out of bounds and is not -1, then throws an error.
*/
selectIndex(index: number): Promise<void> {
if (index < -1 || index >= this.nativeElement.options.length) {
throw new Error(`The index ${index} is out of bounds`);
}
this.nativeElement.selectedIndex = index;
return this.dispatchEventOfType('change');
}
/**
* Selects the first option with the given value, then dispatches an event of type change and triggers a change detection.
* If there is no option with the given value, then throws an error.
*/
selectValue(value: string): Promise<void> {
const index = this.optionValues.indexOf(value);
if (index >= 0) {
return this.selectIndex(index);
} else {
throw new Error(`The value ${value} is not part of the option values (${this.optionValues.join(', ')})`);
}
}
/**
* Selects the first option with the given label (or text), then dispatches an event of type change and triggers a change detection.
* If there is no option with the given label, then throws an error.
*/
selectLabel(label: string): Promise<void> {
const index = this.optionLabels.indexOf(label);
if (index >= 0) {
return this.selectIndex(index);
} else {
throw new Error(`The label ${label} is not part of the option labels (${this.optionLabels.join(', ')})`);
}
}
/**
* the selected index of the wrapped select
*/
get selectedIndex(): number {
return this.nativeElement.selectedIndex;
}
/**
* the value of the selected option of the wrapped select, or null if there is no selected option
*/
get selectedValue(): string | null {
if (this.selectedIndex < 0) {
return null;
}
return this.nativeElement.options[this.selectedIndex].value;
}
/**
* the label (or text if no label) of the selected option of the wrapped select, or null if there is no selected option
*/
get selectedLabel(): string | null {
if (this.selectedIndex < 0) {
return null;
}
return this.nativeElement.options[this.selectedIndex].label;
}
/**
* the values of the options, as an array
*/
get optionValues(): Array<string> {
return (Array.prototype.slice.call(this.nativeElement.options) as Array<HTMLOptionElement>).map(option => option.value);
}
/**
* the labels (or texts if no label) of the options, as an array
*/
get optionLabels(): Array<string> {
return (Array.prototype.slice.call(this.nativeElement.options) as Array<HTMLOptionElement>).map(option => option.label);
}
/**
* the number of options in the select
*/
get size(): number {
return this.nativeElement.options.length;
}
/**
* the disabled property of the wrapped select
*/
get disabled(): boolean {
return this.nativeElement.disabled;
}
}