qunit-dom
Version:
DOM element assertions for QUnit
133 lines (83 loc) • 3.96 kB
Markdown
# API
[htmlelement]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement
[nodelist]: https://developer.mozilla.org/en-US/docs/Web/API/NodeList
## assert.dom()
Once installed the DOM element assertions are available at `assert.dom(...).*`:
**Parameters**
- `target` **([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [HTMLElement](https://developer.mozilla.org/en-US/docs/Web/HTML/Element))** A CSS selector that can be used to find elements using [`querySelector()`](https://developer.mozilla.org/de/docs/Web/API/Document/querySelector), or an [HTMLElement][] (Not all assertions support both target types.)
- `rootElement` **[HTMLElement](https://developer.mozilla.org/en-US/docs/Web/HTML/Element)?** The root element of the DOM in which to search for the `target` (optional, default `document`)
**Examples**
```javascript
test('the title exists', function(assert) {
assert.dom('#title').exists();
});
```
## Assertions
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
### exists
Assert an [HTMLElement][] (or multiple) matching the `selector` exists.
**Parameters**
- `options` **[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?**
- `message` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?**
**Examples**
```javascript
assert.dom('#title').exists();
assert.dom('.choice').exists({ count: 4 });
```
### doesNotExist
Assert an [HTMLElement][] matching the `selector` does not exists.
**Parameters**
- `message` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?**
**Examples**
```javascript
assert.dom('.should-not-exist').doesNotExist();
```
### isFocused
Assert that the [HTMLElement][] or an [HTMLElement][] matching the
`selector` is currently focused.
**Parameters**
- `message` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?**
**Examples**
```javascript
assert.dom('input.email').isFocused();
```
### isNotFocused
Assert that the [HTMLElement][] or an [HTMLElement][] matching the
`selector` is not currently focused.
**Parameters**
- `message` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?**
**Examples**
```javascript
assert.dom('input[type="password"]').isNotFocused();
```
### hasText
Assert that the text of the [HTMLElement][] or an [HTMLElement][]
matching the `selector` matches the `expected` text, using the
[`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent)
attribute and stripping/collapsing whitespace.
`expected` can also be a regular expression.
**Parameters**
- `expected` **([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp))**
- `message` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?**
**Examples**
```javascript
// <h2 id="title">
// Welcome to <b>QUnit</b>
// </h2>
assert.dom('#title').hasText('Welcome to QUnit');
```
```javascript
assert.dom('.foo').hasText(/[12]\d{3}/);
```
### hasTextContaining
Assert that the text of the [HTMLElement][] or an [HTMLElement][]
matching the `selector` contains the given `text`, using the
[`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent)
attribute.
**Parameters**
- `text` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
- `message` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?**
**Examples**
```javascript
assert.dom('#title').hasTextContaining('Welcome');
```