Queries
The Spectator API includes convenient methods for querying the DOM as part of a test: query
, queryAll
, queryLast
, queryHost
and queryHostAll
. All query methods are polymorphic and allow you to query using any of the following techniques.
String Selector
Pass a string selector (in the same style as you would when using jQuery or document.querySelector) to query for elements that match that path in the DOM. This method for querying is equivalent to Angular's By.css predicate. Note that native HTML elements will be returned. For example:
Type Selector
Pass a type (such as a component, directive or provider class) to query for instances of that type in the DOM. This is equivalent to Angular's By.directive
predicate. You can optionally pass in a second parameter to read a specific injection token from the matching elements' injectors. For example:
DOM Selector
Spectator allows you to query for elements using selectors inspired by dom-testing-library. The available selectors are:
The difference between byText
and byTextContent
is that the former doesn't match text inside a nested elements.
For example, in this following HTML byText('foobar', {selector: 'div'})
won't match the following div
, but byTextContent
will:
Testing Select Elements
Spectator allows you to test <select></select>
elements easily, and supports multi select.
Example:
It also allows you to check if your change
event handler is acting correctly for each item selected. You can disable this if you need to pre set choices without dispatching the change event.
API:
Example:
You can also pass HTMLOptionElement
(s) as arguments to selectOption
and the toHaveSelectedOptions
matcher. This is particularly useful when you are using [ngValue]
binding on the <option>
: